Tuesday, June 29, 2010
Synchronizing Requests in App Engine
Sometimes, it's useful to have synchronized sections of code, where only one thread of execution can run at a time.
Can we synchronize web requests in a distributed computing environment like Google App Engine, where potentially different machines are serving different requests? It turns out this is possible, thanks to the memcache service. The app engine memcache is a shared resource (it always looks the same, no matter which machine in the network accesses it) and it provides atomic operations.
In our application, we cache JSON objects to the datastore to improve performance. When the cache has expired (say, after an hour), any request that needs that information triggers an operation that rebuilds the cache. However, we're not interested in multiple simultaneous requests triggering simultaneous cache rebuilds (these operations are expensive!). Instead, we'd rather have the first request trigger a rebuild, while subsequent requests wait for the results of that computation.
I've posted some code on github that implements this distributed mutex strategy. You're free to use/fork it however you wish.
Note that synchronization is potentially devastating to your application's scalability, so please use with caution!
Photo by Maldita la hora.
Subscribe to:
Post Comments (Atom)

Some other pages discuss this topic:
ReplyDeleteThread on stack overflow
App Engine mutex recipe (I don't recommend using this code, though)