Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical (Django) Caching Strategy & Implementation? Cache long, Invalidate cache upon data change

I have a Django app that gets near-realtime data (tweets and votes), although updates occur only every minute or two on average. However we want to show the data by updating the site and api results right when it comes in.

We might see a whole ton of load on this site, so my initial thought is of course caching!

Is it practical to have some sort of Memcached cache that gets invalidated manually by another process or event? In other words, I would cache views for a long time, and then have new tweets and votes invalidate the entire view.

  • Do the perhaps modest performance enhancements justify the added complexity?
  • Is there a practical implementation I could create (I work with other developers, so hacking tons of stuff around each response call isn't a good option)?

I'm not concerned about invalidating only some of the objects, and I considered subclassing the MemcachedCache backend to add some functionality following this strategy. But of course, Django's sessions also use Memcached as a write through cache, and I don't want to invalidate that.

like image 884
Dave Avatar asked Sep 22 '11 22:09

Dave


1 Answers

Cache invalidation is probably the best way to handle the stuff you're trying to do. Based on your question's wording, I'm going to assume the following about your app:

  • You have some sort of API in place that is receiving new information updates and NOT doing polling. EG: Every minute or two you get an API request, and you store some information in your database.
  • You are already using Memcached to cache stuff for reading. Probably via a cronjob or similar process that periodically scans your database and updates your cache.

Assuming the above two things are true, cache invalidation is definitely the way to go. Here's the best way to do it in Django:

  1. A new API request comes into your server that contains new data to be stored. You save it in the database, and use a post save signal on your model class (EG: Tweet, Poll, etc.) to update your memcached data.
  2. A user visits your site and requests to read their most recent tweets, polls, or whatever.
  3. You pull the tweet, poll, etc., data out of memcached, and display it to them.

This is essentially what Django signals are meant for. They'll run automatically after your object is saved / updated, which is a great time to update your cache stores with the freshest information.

Doing it this way means that you'll never need to run a background job that periodically scans your database and updates your cache--your cache will always be up-to-date instantly with the latest data.

like image 73
rdegges Avatar answered Nov 10 '22 17:11

rdegges