62

I've have a Django app that's currently hosted up on Amazon's EC2 service. I have two machines, one with the Django app and the other with my PostgreSQL database. So far it has been rock solid.

Many sources claim I should implement Redis into my stack, but what would be the purpose of implementing Redis with Django and Postgresql? How can I implement Redis in my Django code for example?

How can I use it with PostgreSQL?

These are all the questions I've been trying to find answers to so I came here hoping to get answers from the biggest and the best. I really appreciate any answers.

Thank You

Daniel
  • 2,744
  • 1
  • 31
  • 41
deadlock
  • 7,048
  • 14
  • 67
  • 115

1 Answers1

89

Redis is a key-value storage system that operates in RAM memory, it's like a "light database" and since it works at RAM memory level it's orders of magnitude faster compared to reading/writing to PostgreSQL or any other traditional Relational Database. Redis is a so-called NoSQL database, like Mongo and many others. It can't directly replace PostgreSQL, you still want permanent storage, but it works along with Relational Databases as an alternate storage system. You can use Redis if your IO operations start getting expensive and it's great for quick calculations and key-based queries.

You can include it in your Django/Python project with a wrapper, for example redis-py.

Redis is very simple to install and use, you can check the examples at redis-py. Redis is independent from any Relational Database, that way you can use it for caching, calculating or storing values permanently and/or temporarily. It can help reduce querying to PostgreSQL, in the end you can use it the way you want and take advantage from it to improve your app/architecture.

This similar question can help you Redis with Django

PepperoniPizza
  • 8,842
  • 9
  • 58
  • 100
  • thanks for the quick reply! How would I use it with PostgreSQL? See, my confusion in all of this is how the app will work. Will Django pull information from Redis which pulls information from Postgresql? If so how does it pull that information? – deadlock Feb 20 '13 at 20:44
  • 3
    I've updated my answer, you need to take a couple of minutes to read the links I've posted and I'm pretty sure you will get the picture. – PepperoniPizza Feb 20 '13 at 20:53
  • 1
    I am not sure that Redis is really "orders of magnitude faster". Tested it (write-read 10E5 pairs of key-value). I've seen that Redis is x2 faster. In many cases it doesn't really matter if your cache provides a result in 1ms or in 2ms. I've found similar results here http://code.jjb.cc/benchmarking-postgres-vs-redis – Shimon S Jun 21 '21 at 07:13
  • almost 10 years after the original answer -- do we still need to reduce load on the Sourth of Truth (Postgres) on the expense of maybe faster response time, increased complexity of extra system, cache invalidation problem, cold start problem? – mishka Aug 22 '22 at 08:08