Swapnil Kotwal
Swapnil Kotwal

Reputation: 5728

How would Redis get to know if it has to return cached data or fresh data from DB

Say, I'm Fechting thousands or record using some long runing task from DB and caching it using Redis. Next day somebody have changed few records in DB.

Next time how redis would know that it has to return cached data or again have to revisit that all thousands of records in DB?

How this synchronisation achived?

Upvotes: 20

Views: 16806

Answers (4)

jeff
jeff

Reputation: 1

you may use @CacheEvict on any update/delete applied on DB. that would clear up responding values from cache, so next query would get from DB

Upvotes: 0

Adham Muzaffarov
Adham Muzaffarov

Reputation: 66

My solution is:

When you are updating, deleting or adding new data in database, you should delete all data in redis. In your get route, you should check if data exists. If not, you should store all data to redis from db.

Upvotes: 1

Averias
Averias

Reputation: 991

Since the source of truth resides on your Database and you push data from this DB to Redis, you always have to update from DB to Redis, at least you create another process to sync data.

My suggestion is just to run a first full update from DB to Redis and then use a synch process which every time you notice update/creation/deletion operation in your database you pull it to Redis.

I don't know which Redis structure are you using to store database records in Redis but I guess it could be a Hash, probably indexed by your table index so the sync operation will be immediate: if a record is created in your database you set a HSET, if deletion HDEL and so on.

You even could omit the first full sync from DB to Redis, and just clean Redis and start the sync process.

If you cannot do the above for some reason you can create a syncher daemon which constantly read data from the database and compare them with the data store in Redis if they are different in somehow you update or if they don't exist in some of both sides you can delete or create the entry in Redis.

Upvotes: 5

for_stack
for_stack

Reputation: 23031

Redis has no idea whether the data in DB has been updated.

Normally, we use Redis to cache data as follows:

  1. Client checks if the data, e.g. key-value pair, exists in Redis.
  2. If the key exists, client gets the corresponding value from Redis.
  3. Otherwise, it gets data from DB, and sets it to Redis. Also client sets an expiration, say 5 minutes, for the key-value pair in Redis.
  4. Then any subsequent requests for the same key will be served by Redis. Although the data in Redis might be out-of-date.
  5. However, after 5 minutes, this key will be removed from Redis automatically.
  6. Go to step 1.

So in order to keep your data in Redis update-to-date, you can set a short expiration time. However, your DB has to serve lots of requests.

If you want to largely decrease requests to DB, you can set a large expiration time. So that, most of time, Redis can serve the requests with possible staled data.

You should consider carefully about the trade-off between performance and staled data.

Upvotes: 25

Related Questions