Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Redis as intermediary cache for REST API

Tags:

rest

redis

django

We have an iOS app that talks to a django server via REST API. Most of the data consists of rather large Item objects that involve a few related models that render into single flat dictionary, and this data changes rarely.

We've found, that querying this is not a problem for Postgres, but generating JSON responses takes a noticeable amount of time. On the other hand, item collections vary per-user.

I thought about a rendering system, where we just build a dictionary for Item object and save it into redis as JSON string, this way we can serve API directly from redis (e.g. HMGET(id of items in user library), which is fast, and makes it relatively easy to regenerate "rendered instances", basically just a couple of post_save signals.

I wonder how good this design is, are there any major flaws in it? Maybe there's a better way for the task?

like image 240
Dmitry Shevchenko Avatar asked Apr 24 '13 18:04

Dmitry Shevchenko


1 Answers

Sure, we do the same at our firm, using Redis to store not JSON but large XML strings which are generated from backend databases for RESTful requests, and it saves lots of network hops and overhead.

A few things to keep in mind if this is the first time you're using Redis...

Dedicated Redis Server
Redis is single-threaded and should be deployed on a dedicated server with sufficient CPU power. Don't make the mistake of deploying it on your app or database server.

High Availability
Set up Redis with Master/Slave replication for high availability. I know there's been lots of progress with Redis cluster, so you may want to check on that too for HA.

Cache Hit/Miss
When checking Redis for a cache "hit", if the connection is dead or any exception occurs, don't fail the request, just default to the database; caching should always be 'best effort' since the database can always be used as a last resort.

like image 113
raffian Avatar answered Nov 11 '22 11:11

raffian