4

I'm new to Redis and wondering how to use Redis and PostgreSql together — specifically using Redis just for LRU caching in Postgres.

Is there any special configuration for connecting Redis to Postgres?

Then if I want to store data, should I store it in Postgres db? If so, what does Redis do?

Thanks.

1
  • Welcome to stackoverflow. Please read the rules before posting. Commented Aug 27, 2014 at 14:01

2 Answers 2

6

I think you misunderstand what memory stores like Redis or memcached can do for you.

They are not connected to PostgreSQL or any other RDBMS. It is the job of your application to write the data in both stores: the permanent store (PostgreSQL in your case) and the transient one (Redis).

Redis and memcached do not offer many connectivity features. For caching, they have a rather simple and unobtrusive behavior. The smart part to manage the cache is on application side, and it is your job to define it.

You can imagine several strategies:

  • each time you write in the RDBMS, you write in the cache. Each time you need to read the data, read first in the cache. If nothing is found, read from the RDBMS and write it back to the cache.

  • each time you write in the RDBMS, you just invalidate the data in the cache (delete it). Each time you need to read the data, read first in the cache. If nothing is found, read from the RDBMS and write it back to the cache.

The tricky part is to clearly define a policy regarding the consistency of the data (between the RDBMS and the cache).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for reply, i need writing files on my database, is it a good idea to store them on PostreSql and save the indexes as value on redis?
It is a wrong idea to store files in a RDBMS database. If you need to store files, why not using a filesystem? They are designed for that. For instance, have a look at Hadoop HDFS, MapR, GlusterFS, Ceph, etc ...
Postgres supports foreign data wrapper. Including redis. I haven't use it but i have interest to it. wiki.postgresql.org/wiki/Foreign_data_wrappers
3

You can use Redis as LRU cache - I am assuming that you are using Redis for in-memory operations only and PostgreSQL as your persistent datastore. The following URL sheds more light on using Redis as LRU cache:

http://redis.io/topics/lru-cache

You will need to write to both Redis and PostgreSQL (Redis will not write to PostgreSQL). It would be a good idea to briefly test and compare results against other options such as Memcached to figure out the best approach for your use case.

Also, the LRU cache implementation in Redis is different between v2.6 and v2.8 (not sure if the 2.8 changes were backported to v2.6 or not).

In case you are not familiar, you can persist data onto the disk vai Redis as well though you should be aware of memory limitations of using Redis as your sole data store (and impact on Redis for disk persistence).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.