I need to have a counter which allows a few increments within a second. Also the number will be stored in datastore. However, it doesn’t have to be very accurate. The error can be from the eviction on memcache or cron doesn’t work and could get the number in memcache being added to datastore in time.

I came up with a pingpong mechanism. The number will be increased by memcache.incr on two slots: ping slot and pong slot. When increment is on one slot, then a cron job will add the number from another slot to datastore and empty that slot if it successfully adds the number.

The time interval of slot change is 10 minutes, therefore, the number could be missing if memcache suddenly out of work. Every ten minutes, a cron job kicks in. So, normally, if the memcache doesn’t work, the lost number could be from between 0 seconds and 10 minutes ago, if it’s just short glitches from Google App Engine. If memcache is out of work longer than that, Google App Engine is probably down, that it doesn’t matter.

When datastore is down or under the scheduled maintenance, as long as memcache still works, the number still counts and won’t be lost as long as the number isn’t evicted from memcache. Once datastore is back with write capability, cron job can add the number to datastore.

There be no race condition when add the number to datastore because of using pingpong and cron job. I believe there is also no race condition with memcache.incr, if it’s processed on memcache server.

Here is my code, it’s from the source of my project. Unmodified, it’s not generalized for you to use it out-of-the-box. Read it and modify it before you use it.