110

I've heard of redis-cache but how exactly does it work? Is it used as a layer between django and my rdbms, by caching the rdbms queries somehow?

Or is it supposed to be used directly as the database? Which I doubt, since that github page doesn't cover any login details, no setup.. just tells you to set some config property.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434

5 Answers5

79

This Python module for Redis has a clear usage example in the readme: http://github.com/andymccurdy/redis-py

Redis is designed to be a RAM cache. It supports basic GET and SET of keys plus the storing of collections such as dictionaries. You can cache RDBMS queries by storing their output in Redis. The goal would be to speed up your Django site. Don't start using Redis or any other cache until you need the speed - don't prematurely optimize.

Community
  • 1
  • 1
Spike Gronim
  • 6,154
  • 22
  • 21
  • But how do you check that the cache on redis needs updating? Do you compare record count or is there better way? – Marconi Mar 04 '11 at 11:29
  • 9
    This is usually done by setting a TTL for the key: http://redis.io/commands/ttl . If the key expires, you must go to the DB. So if the key is in redis, then you use it. Note that the simple implementation of this causes some problems: when a popular key expires you have http://en.wikipedia.org/wiki/Thundering_herd_problem, you want to use http://en.wikipedia.org/wiki/Negative_cache, and your DB needs http://en.wikipedia.org/wiki/Admission_control – Spike Gronim Mar 04 '11 at 16:50
  • Could you define "API compatible"? The protocol are certainly different. To quote Redis' author « Basically in the memcached protocol the length of the payload was one of the space separated arguments, while in the Redis protocol is at fixed offset (there is just a "$" character before) » (Source: http://antirez.com/post/redis-memcached-benchmark.html) However, it's true that since both are key/value store, it should be pretty straightforward to adapt code using Memcached to using Redis instead. My best guess is, that you meant "you can substitute one module/class for another", right? – jpetazzo Jul 15 '11 at 15:36
  • "you can substitute one module/class for another" - yes, that's the case. What I meant was that they were wire compatible (I was wrong about that). – Spike Gronim Jul 15 '11 at 16:25
  • 4
    Umm, well, a) Redis is designed to be more than RAM cache - it's persistent (but includes features for cache), b) it's more than simple key-value storage - there's lists, hashsets etc. with built-in manipulations. – Olli Apr 10 '12 at 12:42
  • 9
    Honestly caching model/response objects doesn't fall under premature optimization. Unless you're being charged to do so (like on heroku) it doesn't hurt to cache even for small apps. – vikki May 15 '13 at 21:11
  • 1
    It's not about speed, it's about resources. A table in cache will be less expensive. – dman Mar 14 '14 at 14:37
  • @SpikeGronim Could you elaborate on why would I have the Thundering herd problem? I suppose the first request after a popular key in Redis expires has to issue a query to my DBMS, whose result is then cached by Redis, so successive requests can simply read the query result from Redis. – nalzok Aug 29 '17 at 08:30
  • Can I use it in Django app? – Zhong Ri Nov 23 '18 at 23:42
62

Just because Redis stores things in-memory does not mean that it is meant to be a cache. I have seen people using it as a persistent store for data.

That it can be used as a cache is a hint that it is useful as a high-performance storage. If your Redis system goes down though you might loose data that was not been written back onto the disk again. There are some ways to mitigate such dangers, e.g. a hot-standby replica. If your data is 'mission-critical', like if you run a bank or a shop, Redis might not be the best pick for you. But if you write a high-traffic game with persistent live data or some social-interaction stuff and manage the probability of data-loss to be quite acceptable, then Redis might be worth a look.

Anyway, the point remains, yes, Redis can be used as a database.

Stefan Schubert-Peters
  • 5,419
  • 2
  • 20
  • 21
  • 3
    For my company, we use Redis to store summarization and products generated from Cassandra & Postgres. The quantity of these products ( several billion ), how often they need to be changed, and that we never know what is needed at any moment makes them a perfect fit for redis. Also, once you go above a million rows of anything in a SQL db, redis can be a great complimentary resource for membership testing ( eg Are these entities in this domain? ) – David Jun 24 '13 at 16:00
24

Redis is basically an 'in memory' KV store with loads of bells and whistles. It is extremely flexible. You can use it as a temporary store, like a cache, or a permanent store, like a database (with caveats as mentioned in other answers).

When combined with Django the best/most common use case for Redis is probably to cache 'responses' and sessions.

There's a backend here https://github.com/sebleier/django-redis-cache/ and excellent documentation in the Django docs here: https://docs.djangoproject.com/en/1.3/topics/cache/ .

I've recently started using https://github.com/erussell/django-redis-status to monitor my cache - works a charm. (Configure maxmemory on redis or the results aren't so very useful).

ostergaard
  • 3,377
  • 2
  • 30
  • 40
5

You can also use Redis as a queue for distributed tasks in your Django app. You can use it as a message broker for Celery or Python RQ.

Dmitrii Mikhailov
  • 5,053
  • 7
  • 43
  • 69
5

Redis as a Primary database

Yes you can use Redis key-value store as a primary database. Redis not only store key-value pairs it also support different data structures like

  1. List
  2. Set
  3. Sorted set
  4. Hashes
  5. Bitmaps
  6. Hyperloglogs

Redis Data types Official doc

Redis is in memory key-value store so you must aware of it if Redis server failure occurred your data will be lost.

Redis can also persist data check official doc.

Redis Persistence Official doc


Redis as a Cache

Yes Redis reside between in Django and RDBMS.

How it works

given a URL, try finding that page in the cache if the page is in the cache: return the cached page else: generate the page save the generated page in the cache (for next time) return the generated page

Django’s cache framework Official Doc


How can use Redis with Django

We can use redis python client redis-py for Django application.

Redis python client redis-py Github

We can use Django-redis for django cache backend.

Django-redis build on redis-py and added extra features related to django application.

Django-redis doc Github

Other libraries also exists.


Redis use cases and data types

Some use cases

  • Session cache
  • Real time analytics
  • Web caching
  • Leaderboards

Top Redis Use Cases by Core Data structure types


Big Tech companies using Redis

 Twitter GitHub Weibo Pinterest Snapchat Craigslist Digg StackOverflow Flickr 

spyker77
  • 143
  • 1
  • 8