ZOMDir > Blog

Monday, 2 March 2015

Memcache to the rescue

ZOMDir was built on Google's App Engine. One of the reasons to choose for this platform was the excellent performance. Google's servers are really fast.

However, a fast platform is not enough. The software architecture should also be optimised for speed.

Google's recommendations 

Luckily Google has some suggestions for a good performance like:
I have taken all these recommendations where possible. Although I over optimised the last recommendation a bit. Let me explain.

Why use sharding counters

Writing to Google's datastore takes much longer than reading from the datastore and an update of any single entity or entity group is only possible about five times a second.

Therefor the solution Google suggests relies on the fact that reads from the App Engine datastore are extremely fast and cheap. The way to reduce the contention is to build a sharded counter – break the counter up into N different counters. When you want to increment the counter, you pick one of the shards at random and increment it. When you want to know the total count, you read all of the counter shards and sum up their individual counts. The more shards you have, the higher the throughput you will have for increments on your counter.

200 new links per second?

How much shards you should use depends on the number of updates you expect per second. I have chosen for 40 shards for the statistics, so ZOMDir should be able handle circa 200 new links per second without contention. Nice, isn't it.

However there is a disadvantage. To collect all these 40 shards takes time. More time than I expected. The reason is probably that I didn't code smart enough to save resources :-( 

Memcache to the rescue

Reducing the number of shards will ruin the statistics so to overcome this I fall back to memcache.

To improve the performance I decided to save the total number of links in memcache too. This way I was able to improve the performance with a factor 2. That is roughly from 1500ms to 750ms. 

That's what I call a nice improvement :-)
Hans

--
ZOMDir.com is a dynamic directory and a wiki
Everyone is able to add a link in 10 seconds

To learn more view this Slideshare presentation

N.B. Here are some pictures from the testresults based on the English page of hotels in Amsterdam

Before the I kept the total number of links in memcache: An endless list of shards I have to collect
The results after keeping the total number of links in memcache

The results according to tools.pingdom.com

The results according to GTMetrix