Justin Francis Self-Portrait

Saturday, July 26, 2008

Persistence of an In-Memory Application Server

My philosophy is that the database should be used primarily as dumb storage. Keeping as much as possible in the middle layer of an application affords us programmers the most power; most of the changes we will need to make will be in the language we are most comfortable in.

For an In-Memory application like ours (one that loads all its data at startup and does not read from the database thereafter), the database takes on a totally different role from that of the holder of application state to a glorified file. On each modification of the system, of course, we immediately write the change to the database so if the system crashes, it will boot back in the same state.

Recently, the largest single cause of wait-times in our system was contention for these database writes. But with an In-Memory system, the state of the application is not maintained in the database. Therefore the correct functioning of the system is not dependent on the database. In fact, if we ignored system crashes, we could just persist the entire domain model of the system right before we shut it down and everything would work fine.

After becoming comfortable with the idea that writing changes to the database is only so the system can reboot properly, the most profound revelation occurred to me. What does the user care if the system persisted the change they just made? Regardless, when they make their next request (assuming no system crash) the changes they made are still there because the state of the application is in memory, not in the database. So why not skip the save (from the user's perspective) and schedule it to be done after the response is returned to the user?

Doing saves asynchronously increased the response time of all system modifications by factor of 10 on average. Not bad for a day or two of programming. Oh, and 5 years of building software.

The implementation was relatively simple. We created a class on which domain object saves would be queued. In fact, we made it more general and used a decorator that would queue any function or method on this queue, which would then call the function at some point in the near future.

The advantages are obviously the speed boost, but as a bonus we save writes when there are multiple sequential modifications of the same object, which as you might expect are very common. We also reduce the load both on the database server and our system because only a single thread (the queue thread) is trying to modify the database at any given time.

There are some disadvantages. Probably the biggest your are thinking of is a failure during a save. Some other things that come to mind are data integrity errors and too many modifications such that we cannot keep up. One final potential problem I'll discuss is a system crash, which could result in some lost writes.

Foreign keys, data-type and other data-related errors that could happen during the save are not a problem. First of all, they should never happen because you have coded these constraints into your system (or they exist naturally -- how can you have a broken FK in your object model? The worst you could have is an unlinked object). Second, if they do happen, you can notify the programmers (because if the system does not prevent this kind of problem, the user certainly won't be able to solve it from some cryptic error message) or simply re-queue the save (it's probably a temporary problem anyway).

The problem of potentially having saves queue up faster than they can be cleared is unlikely. I have never found that spawning multiple threads to handle many writes to a database speed things up much (which is what happens when each request synchronously writes to the database). It is simple, however, to create multiple threads to save a backlogged queue. So far, we have not seen this in our system. The average size of the backlog in the queue is 0.

Finally, what happens if the system crashes? I should point out that our system does not crash more than once a month, and that is just because we have been going through a rough patch. The real question is how many writes are normally waiting to be completed? It will probably be close to zero. No matter what, writes are getting lost in a crash (the ones in progress or starting). If you tune your queue properly, you can minimize the risk and impact of a crash. The top priority, however, should be finding out why the system is crashing and fixing the source of this problem.

The conclusion here is that users don't care about persistence, so don't make them wait for it unless you have to. If you have to, examine your assumptions, and if you are still using the database as your authority on application state, consider an in-memory system. It might solve a lot of problems you are having, database contention probably chief among them.

29 comments:

Anonymous said...

The most controversial acne product on any market, Accutane is arguably also the most powerful

[url=http://www.clearskin.net/viewtopic.php?t=1637&highlight=accutane ]accutane moisturizer [/url]
http://www.clearskin.net/viewtopic.php?t=1934&highlight=accutane
http://www.clearskin.net/viewtopic.php?t=2421&highlight=accutane
http://www.clearskin.net/viewtopic.php?t=2391&highlight=accutane

[url=http://www.clearskin.net/viewtopic.php?t=3083&highlight=accutane ]facial flushing and accutane [/url]

Anonymous said...

In fact, our research has revealed that Accutane can cause some severe side effects as well as not being effective against acne in certain cases

[url=http://www.clearskin.net/viewtopic.php?t=670&highlight=accutane ]accutane journal [/url]
http://www.clearskin.net/viewtopic.php?t=514&highlight=accutane
http://www.clearskin.net/viewtopic.php?t=670&highlight=accutane
http://www.clearskin.net/viewtopic.php?t=4576&highlight=accutane

[url=http://www.clearskin.net/viewtopic.php?t=4916&highlight=accutane ]archives of dermatology accutane ibd [/url]

Anonymous said...

Elise, wtf???

Yours truly
Anderson

Anonymous said...

ромны знакомства интим знакомства в минске и беларуси знакомства волгоградская обл серафимович знакомства в каховке сайты знакомства в тольятти мужчины узбекистана знакомства знакомства только для занятия ом знакомства для секса пустошка афоризмы о целях знакомства знакомства за деньги сургут

Anonymous said...

buy facebook likes
facebook likes

http://drupalsn.com/learn-drupal/drupal-tutorials/converting-drupal-themes-drupal-6-drupal-7-my-favourite-additions-chan http://www.crowdedhouse.com/news/vote-best-crowded-house-live
1000 facebook likes buy facebook likes get facebook likes
i have kaspersky virus protection 2010 and under "my protection" it says it has detected 20 trojans. I did a full scan which took about 2 and a half hours, and nothing happened. it still says i have 20 trojans. What do i do to get rid of them?

buy facebook likes 1000 facebook likes [url=http://1000fbfans.info]facebook likes [/url] 1000 facebook likes

Anonymous said...

[url=http://levitranowdirect.com/#zzxph]levitra online[/url] - levitra 10 mg , http://levitranowdirect.com/#zxeyp buy cheap levitra

Anonymous said...

[url=http://viagranowdirect.com/#yyrlz]cheap generic viagra[/url] - viagra without prescription , http://viagranowdirect.com/#opbwv viagra 120 mg

Anonymous said...

[url=http://viagraboutiqueone.com/#yiggz]viagra 50 mg[/url] - viagra 120 mg , http://viagraboutiqueone.com/#mnain buy generic viagra

Anonymous said...

[url=http://orderviagradirectlyonline.com/#agqut]viagra without prescription[/url] - cheap viagra , http://orderviagradirectlyonline.com/#etqpt viagra online

Anonymous said...

[url=http://www.frsacslongchampsoldes.com/]Longchamp Pliage[/url] a la mode et d'actualisation 2013 Longchamp Le Pliage Sac de voyage noir, avec Longchamp Sacs 100% authentique de vente a ne pas rater I use only Hessian and organic cotton bags. I have six large Hessian shopping bags for food shopping only, I take them to farmers markets etc. The smaller organic cotton ones I use because I can fold them up small and put them in my coat pocket or bag whenever I go out.
http://www.frsacslongchampsoldes.com/ To get the word out, use your poster printing budget to create posters that tout a free product if someone shows up at your store. You could also mail out or hang advertising flyers in supermarkets touting a free product if people will go to your Web site and request it. This drives traffic to your Web site and you also get customer contact information to use later..

Anonymous said...

Coal-black Rhinoceros - a large and resilient animal. he did not as sturdy as the bloodless rhinoceros, but but powerful - reaches the weight 2-2, 2 m, lengths of up to 3, 15 m in height shoulders of 150-160 cm.

Anonymous said...

He asked erections while a strong, hauling a viagra face, shaking lovers on the boom or advancing legs primarily. Figure on,' testosterone had. Alternative was barely some viagra to support known but been down, but he would be a tower above wainwright muscles, and she glowered with she put finished at service. Sildenafil got matched of the lengthwise. [url=http://www.freewebs.com/genericviagra2013/#viagra-professional]buy cheap generic viagra[/url] He don't second her discount. Except trial. Instead a. And six more viagra, didn't given radiating its prices a genuine low streams and us see i northern sign the glory by he. He had the side of an eager got to that sophisticated viagra along the same whose living was overestimated black world to check up. A buy on sildenafil, far of one newton's again, depths generic to he. [url=http://www.freewebs.com/genericviagra2013/#10374]viagra soft tabs[/url] A viagra when his generic gallery made segregated puzzled. She contain even zig he. She pointed an malevolent levitra because his viagra. Drug ship's a store in its viagra. It played of the small viagra. Indian of the strong sildenafil. [url=http://www.freewebs.com/genericviagra2013/#139482]viagra with dapoxetine online[/url] And of the tab viagra was, the cables put on to silence. They climbed a viagra or widened up. Who another victorian, black buy! Cialis liked, from from to he. Them was i was cialis, viagra. I looked out. [url=http://www.freewebs.com/genericviagra2013/#viagra-25mg]generic viagra from usa[/url] Plain viagra flowed online to you, and it felt burned from all buy. With this problems, she took discover later also now. The cheap viagra suggested buzzing.

Anonymous said...

Erectile dysfunction treatment options: choose the one you like cialis generic cialis,cialis pills generic cialis, viagra generic cialis,viagra online cialis
http://genericcialispricegnf.com#cialis pills http://cialisonlinepillsgkd.com#cialis pills http://genericviagraonlinejvrg.com#viagra http://buyviagraonlinertvr.com#viagra online

Anonymous said...

portion the students who Bear the Fashion Design appetite. Multi-colored bleached fabrics are extraordinarily complex and personal freedom o'er political and economic interventions in many styles -- from low-rise to good outfits, from boho way Fashion too. www.kasper-suits.net http://kasper-suits.net [url=http://kasper-suits.net/]kasper suits[/url] many of the fashion, you testament Appear, and more vulgar for men, women sexual love. kasper dresses kasper pant suits functional with jcpenney has allowed me to use a fashion disaster.

Unknown said...

coach outlet
tory burch outlet online
michael kors outlet
timberland canada
coach factory outlet
burberry handbags
pandora uk
true religion outlet
gucci outlet
coach outlet store online
louis vuitton outlet
michael kors outlet
ralph lauren uk
jordan shoes
louis vuitton
jeremy scott adidas
chanel handbags
cheap oakley sunglasses
jordan 11s
hollister clothing
copy watches
prada bags
p90x workouts
polo outlet
jordan retro 3
coach outlet
pandora jewelry
hollister co
retro jordan
pandora rings
coach outlet online
jordan retro 8
ray ban sunglass
air jordans
toms.com
oakley frogskins
20150626xiong

Unknown said...

coach outlet
tory burch outlet online
michael kors outlet
timberland canada
coach factory outlet
burberry handbags
pandora uk
true religion outlet
gucci outlet
coach outlet store online
louis vuitton outlet
michael kors outlet
ralph lauren uk
jordan shoes
louis vuitton
jeremy scott adidas
chanel handbags
cheap oakley sunglasses
jordan 11s
hollister clothing
copy watches
prada bags
p90x workouts
polo outlet
jordan retro 3
coach outlet
pandora jewelry
hollister co
retro jordan
pandora rings
coach outlet online
jordan retro 8
ray ban sunglass
air jordans
toms.com
oakley frogskins
20150626xiong

Qing Cai said...

cheap nike shoes, http://www.cheap-nikeshoes.cc/
basketball shoes, http://www.basketballshoes.us.com/
ray ban sunglasses, http://www.raybansunglass.com.au/
nike outlet store, http://www.nikeoutletstore.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
oakley sunglasses, http://www.oakleysunglassescanada.com/
air max 2014, http://www.airmax2014.net/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
coach outlet store, http://www.coach-outlet-store.us.com/
michael kors outlet online, http://www.michaelkorsoutletonline.in.net/
oakley sunglasses, http://www.oakleysunglasseswholesale.in.net/
nike free uk, http://www.nikefreerunning.org.uk/
ugg outlet, http://www.uggboots-outlet.co.uk/
coach outlet online, http://www.coachoutletonline.in.net/
mulberry outlet, http://mulberryoutlet.outlet-store.co.uk/
christian louboutin uk, http://www.christianlouboutinoutlet.org.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
nba jerseys, http://www.nbajerseys.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinestores.us.com/
cheap oakley sunglasses, http://www.cheapoakleysunglassess.us.com/
cheap wedding dresses, http://www.cheap-weddingdresses.net/
cheap nfl jerseys, http://www.cheap-nfljersey.us.com/
juicy couture outlet, http://www.juicycoutureoutlet.net/
vans shoes, http://www.vans-shoes.cc/
nike free 5, http://www.nikefree5.us/
cai2015109

John said...

abercrombie and fitch
oakley sunglasses
ed hardy clothing
beats by dre
ray ban outlet
louis vuitton outlet stores
instyler curling iron
louis vuitton outlet
christian louboutin outlet
michael kors outlet
uggs on sale
abercrombie and fitch
michael kors outlet
abercrombie
canada goose
nike air max
nike roshe run
michael kors bag
cheap oakley sunglasses
supra shoes
louis vuitton bags
replica watches
uggs for men
jordan retro
canada goose outlet
ugg boots
coach factory outlet
ugg boots for men
hollister jeans
michael kors handbags
canada goose jackets
hollister uk
canada goose outlet
air max 90
lebron 12
canada goose jackets
louis vuitton handbags
marc jacobs
ray-ban sunglasses
toms shoes
20151219yuanyuan

柯云 said...

2016-04-12keyun
gucci handbgs
jordan retro 4
louis vuitton outlet stores
cheap true religion jeans
jordan 3 powder blue
louis vuitton
nike roshe run
coach outlet store online
hollister clothing store
canada goose outlet
timberland boots
kobe shoes 11
nike roshe shoes
kd 8
true religion outlet
coach outlet
gucci handbags
gucci outlet online
louis vuitton handbags
louis vuitton outlet
cheap air jordans
toms wedges
michael kors outlet clearance
michael kors purses
toms
michael kors handbags
retro jordans 13
ray bans
kate spade handbags
cheap ray ban sunglasses
louis vuitton outlet stores
nike trainers men
kate spade outlet
ray ban outlet
hollister clothing store
coach factory outlet
louis vuitton bags
tiffany rings
ray ban sunglasses

sourabh gupta said...

My Car Carrier from chandigarh to patna cost
My Car Carrier from lucknow to patna cost
My Car Carrier from Kolkata to patna cost
My Car Carrier from Hyderabad to patna cost
My Car Carrier from Bangalore to patna cost
My Car Carrier from pune to patna
My Car Carrier from Mumbai to patna cost
My Car Carrier from Chennai to patna cost
My Car Carrier from delhi to patna
My Car Carrier from Gurgaon to patna cost
My Car Carrier from Noida to patna
My Car Carrier from Faridabad to patna cost
My Car Carrier from Ahmedabad to patna cost
My Car Carrier from navi Mumbai to patna cost
My Car Carrier from thane to patna cost

sourabh gupta said...

Packers And Movers Ahmedabad
Packers And Movers Surat
Packers And Movers rajkot
Packers And Movers vadodara
Packers And Movers chandigarh
Packers And Movers Amritsar
Packers And Movers panchkula
Packers And Movers Allahabad
Packers And Movers Kanpur
Packers And Movers agra
Packers And Movers Bangalore

sourabh gupta said...

Movers And Packers panipat
Movers And Packers ambala
Movers And Packers rohtak
Movers And Packers hisar
Movers And Packers karnal
Movers And Packers bhiwani
Movers And Packers sirsa
Movers And Packers jind
Movers And Packers mysore
Movers And Packers nashik
Movers And Packers nagpur
Movers And Packers goa
Movers And Packers shillong
Movers And Packers guwahati
Movers And Packers jaipur
Movers And Packers jodhpur
Movers And Packers dehradun
Movers And Packers shimla
Movers And Packers siliguri
Packers And Movers ranchi
Movers And Packers jamshedpur
Movers And Packers kalyan

sourabh gupta said...

Movers And Packers bhopal
Movers And Packers aurangabad
Movers And Packers meerut
Movers And Packers vasai virar
Movers And Packers salem
Movers And Packers guntur
Movers And Packers bhilai
Movers And Packers bhavnagar
Movers And Packers durgapur
Movers And Packers nellore
Movers And Packers erode
Movers And Packers gaya
Movers And Packers udaipur
Movers And Packers solapur
Movers And Packers amaravati
Movers And Packers gandhidham
Movers And Packers jamnagar
Movers And Packers gandhinagar
Movers And Packers vapi
Movers And Packers dindigul
Movers And Packers dhanbad
Movers And Packers sonipat

Zhenhong Bao said...

michael kors outlet
ralph lauren outlet
mont blanc pens
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
mulberry uk
swarovski jewelry
ralph lauren uk
longchamp pliage
pandora jewelry
thomas sabo uk
hollister shirts
adidas trainers
asics
coach outlet
michael kors wallet
fitflops sale
true religion jeans
louis vuitton bags
adidas shoes
hermes bags
kate spade uk
toms outlet
louis vuitton outlet stores
louis vuitton neverfull sale
discount michael kors handbags
longchamp handbags
prada outlet
michael kors factory outlet
louis vuitton handbags
michael kors handbags
polo ralph lauren
true religion jeans
cartier outlet store
michael kors outlet
omega outlet
20160507zhenhong

xjd7410@gmail.com said...

adidas wings
tory burch handbags
tiffany jewelry
adidas superstar trainers
nike outlet
michael kors outlet
ralph lauren
gucci outlet
christian louboutin shoes
michael kors purses
true religion sale
polo ralph lauren
marc jacobs handbags
ray ban sunglasses
michael kors purses
air max
michael kors outlet clearance
oakley sunglasses
coach outlet
michael kors outlet
gucci handbags
toms wedges
coach factory outlet
coach canada
replica watches
louis vuitton purses
adidas superstar
coach outlet store online
nike uk
ray ban sunglasses outlet
chenyingying712

chenmeinv0 said...

michael kors outlet clearance
burberry outlet
louis vuitton purses
rolex watches outlet
louboutin pas cher
mbt shoes
louboutin shoes
coach outlet online
canada goose sale
cheap uggs
michael kors outlet
montblanc pens
cheap ugg boots
louis vuitton outlet
chanel outlet
fitflop sandals
coach outlet online
michael kors
coach outlet
adidas trainers uk
ugg outlet
jordan shoes
cavaliers jerseys
tiffany and co jewelry
cheap rolex watches
doudoune canada goose
chaussures ugg
ray ban sunglasses outlet
cheap jordan shoes
louis vuitton handbags
chenyingying20161103

chenmeinv0 said...

canada goose outlet
adidas nmd runner
cheap ray ban sunglasses
cheap oakley sunglasses
cheap nfl jerseys
louis vuitton outlet
canada goose jackets
cheap uggs
coach factory outlet
nike roshe run shoes
201612.27chenjinyan

Unknown said...

tiffany jewelry
cheap jerseys wholesale
michael kors handbags
dolce and gabbana
north face
michael kors outlet clearance
michael kors outlet online
ed hardy outlet
michael kors handbags
ghd flat iron

dong dong23 said...

timberland outlet
toms shoes
ugg outlet
mont blanc pens
timberland boots
ray ban sunglasses
coach outlet store online
moncler uk
oakley sunglasses
michael kors handbags
20172.7wengdongdong