Throttling on Appengine

Throttling, or rate limiting, is really easy on Appengine using a combination of memcache and the taskqueue, particularly if you use the built-in deferred library. The code snippet below shows a pretty basic function you can use to do things like limit the number of comments a user is allowed to post over a 10 minute time span, or limit the rate of Twitter updates, etc. A call to memcache_trottle("foo", countdown=60*10) will increase the foo counter in memcache by 1 and decrease it by the same amount 10 minutes later. So, if you’re tracking blog comments, the value of foo in memcache will be the total number of comments in the last 10 minutes. If it exceeds some value, you could reject a comment, alert an admin, or whatever.