Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update redis after updating database?

Tags:

I cache some data in redis, and reading data from redis if it's exists, otherwise reading data from database and write the data in redis.

I find that there are several ways to update redis after updating database.For example:

  1. set keys in redis to expired
  2. update redis immediately after updating datebase.
  3. put data in MQ and use consumer to update redis.

I'm a little confused and don't know how to choose.

Could you tell me the advantage and disadvantage of each way and it's better to tell me other ways to update redis or recommend some blog about this problem.

like image 864
mengying.ye Avatar asked Mar 30 '16 08:03

mengying.ye


People also ask

How do I refresh Redis cache?

How do I flush my Redis cache using the Linux/Unix command line option? What is the command to delete all keys from my redis cluster? FLUSHDB command – Delete all the keys of the currently selected DB. FLUSHALL command – Remove all the keys of all the existing databases, not just the currently selected one.

Can we update data in Redis?

set keys in redis to expired. update redis immediately after updating datebase. put data in MQ and use consumer to update redis.

Can Redis be used with SQL database?

Spark-Redis library allows you to use the DataFrame APIs to store and access Redis data. In other words, you can insert, update and query data using SQL commands, but the data is internally mapped to Redis data structures.

Is Redis a cache or 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

Actual data store and cache should be synchronized using the third approach you've already described in your question.

As you add data to your definitive store (i.e. your SQL database), you need to enqueue this data to some service bus or message queue, and let some asynchronous service do the whole synchronization using some kind of background process.

You don't want get into this cases (when not using a service bus and asynchronous service):

  • Make your requests or processes slower because the user needs to wait until the data is both stored in your database and cache.
  • Have the risk of a fail during the caching process and not being able to have a retry policy (which is usually a built-in feature in a service bus or some message queues). Also, this failure can end up in a partial or complete cache corruption and you won't be able to automatically and easily schedule some task to fix this situation.

About using Redis key expiration, it's a good idea. Since Redis can expire keys using its built-in mechanism, you shouldn't implement key expiration from the whole background process. If a key exists is because it's still valid.

BTW, you won't be always on this case (if a key isn't expired it means that it shouldn't be overwritten). It might depend on your actual domain.

like image 190
Matías Fidemraizer Avatar answered Sep 20 '22 04:09

Matías Fidemraizer