Issue
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?
Solution
Redis has no idea whether the data in DB has been updated.
Normally, we use Redis to cache data as follows:
- Client checks if the data, e.g. key-value pair, exists in Redis.
- If the key exists, client gets the corresponding value from Redis.
- 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. - Then any subsequent requests for the same key will be served by Redis. Although the data in Redis might be out-of-date.
- However, after
5
minutes, this key will be removed from Redis automatically. - 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.
Answered By - for_stack Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.