Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Redis write out to a database like PostgreSQL?

Tags:

I've been using PostgreSQL for the longest time. All of my data lives inside Postgres. I've recently looked into redis and it has a lot of powerful features that would otherwise take a couple of lines in Django (python) to do. Redis data is persistent as long the machine it's running on doesn't go down and you can configure it to write out the data it's storing to disk every 1000 keys or every 5 minutes or so depending on your choice.

Redis would make a great cache and it would certainly replace a lot of functions I have written in python (up voting a user's post, viewing their friends list etc...). But my concern is, all of this data would some how need to be translated over to postgres. I don't trust storing this data in redis. I see redis as a temporary storage solution for quick retrieval of information. It's extremely fast and this far outweighs doing repetitive queries against postgres.

I'm assuming the only way I could technically write the redis data to the database is to save() whatever I get from the 'get' query from redis to the postgres database through Django.

That's the only solution I could think of. Do you know of any other solutions to this problem?

like image 875
deadlock Avatar asked Jun 10 '13 21:06

deadlock


People also ask

Can Redis Replace Postgres?

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.

Is Redis a Postgres?

A Redis in-memory database cache allows you to access frequently used data from your server's RAM. This minimizes unnecessarily load and roundtrips to your PostgreSQL database server.

Should I use Redis or Postgres?

"Relational database", "High availability " and "Enterprise class database" are the key factors why developers consider PostgreSQL; whereas "Performance", "Super fast" and "Ease of use " are the primary reasons why Redis is favored.

Can I use Redis as database?

Redis is a database for a range of data sizes, from a few megabytes to hundreds of terabytes. With Redis Enterprise, you can use Redis as both an in-memory cache and a primary database in a single system, thus eliminating the complexity and latency of two separate systems.


1 Answers

Redis is increasingly used as a caching layer, much like a more sophisticated memcached, and is very useful in this role. You usually use Redis as a write-through cache for data you want to be durable, and write-back for data you might want to accumulate then batch write (where you can afford to lose recent data).

PostgreSQL's LISTEN and NOTIFY system is very useful for doing selective cache invalidation, letting you purge records from Redis when they're updated in PostgreSQL.

For combining it with PostgreSQL, you will find the Redis foreign data wrapper provider that Andrew Dunstain and Dave Page are working on very interesting.

I'm not aware of any tool that makes Redis into a transparent write-back cache for PostgreSQL. Their data models are probably too different for this to work well. Usually you write changes to PostgreSQL and invalidate their Redis cache entries using listen/notify to a cache manager worker, or you queue changes in Redis then have your app read them out and write them into Pg in chunks.

like image 72
Craig Ringer Avatar answered Sep 20 '22 16:09

Craig Ringer