17 9 / 2011
Timeout is a very typical operation that you usually come across all the time, In my case there were few options I had to get around this issue (I was using tornado for handling requests, and Redis for storage):
- On every request before returning use add_timeout, keeping a Dictionary of ‘user_id’ -> add_timeout_returned_id; each time user comes back we remove the timeout event by looking up the user id in dictionary. If user doesn’t come back within time limit the callback fired will clean the things up.
- Keep time stamps against user id in Redis, run a background process cleaning those who didn’t updated their time stamp and have their entry left in redis. The background worker will be a separate configurable instance running after intervals and cleaning up the dead users.
- Use the lovely EXPIRE feature of Redis to remove the entries from DB and the worker process which looks for missing (expired) entries in channel and notifies other users with PUBLISH.
Last night I sat with this idea in my mind to have a PUBLISH event on a configured channel when a key expires (Cure for the itch). Walking around Redis community I found some really complicated discussions going on; with ideas of meta publish channels and some really brainy ideas stuff. I wanted to keep it simple, clean, configurable and that’s where opensource really helps; you have a different opinion go create your own flavor. I created my own fork and implemented what was really simple (and yummy chocolate flavored :P). So with new fork, Redis configuration can now contain an additional parameter named keys-expire-notify (so keys-expire-notify !KEYS will make Redis do a “PUBLISH !KEYS <expired_key>” every-time a key expires).
This feature will open up a whole new world of possibilities for the Redis users, including DB updates on cache invalidation, cascaded updates in Redis, time out events from Redis (Yes you can use keys to make it happen), Imagination is your limit! I’ve been playing around with the thing for a bit longer now and I am pretty much satisfied. It should be pretty much stable, since its built on top of existing infrastructure. You can grab your copy here and enjoy the new feature! I am really looking forward to see this idea as official part of Redis.