Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best strategy to sync data between DB and redis cache

We are using Oracle db, we would like to use Redis Cache mechanism, We add some subset of DB data to cache, does it sync with DB automatically when there is a change in the data in DB or we will have to implement the sync strategy, if yes, what is the best way to do it.

like image 518
Chinnadurai_V Avatar asked Oct 27 '25 04:10

Chinnadurai_V


2 Answers

does it sync with DB automatically when there is a change in the data in DB

No, it doesn't.

we will have to implement the sync strategy, if yes, what is the best way to do it.

This will depend on your particular case. Usually caches are sync'd in two common ways:

  1. Data cached with expiration. Once cached data has expired, a background process adds fresh data to cache, and so on. Usually there's data that will be refreshed in different intervals: 10 minutes, 1 hour, every day...

  2. Data cached on demand. When an user requests some data, that request goes through the non-cached road, and that request stores the result in cache, and a limited number of subsequent requests will read cached data directly if cache is available. This approach can fall into #1 one too in terms of cache invalidation interval.

Now I believe that you've enough details to think about what could be your best strategy in your particular case!

like image 125
Matías Fidemraizer Avatar answered Oct 29 '25 17:10

Matías Fidemraizer


Additionally to what mathias wrote, you can look ath the problem from dynamic/static perspective:

  • Real/Time approach: each time a process changes the DB data, you dispatch an event or a message to a queue where a worker handles corresponding indexing of the cache. Some might event implement it as a DB Trigger (I don't like)
  • Static/delayed approach: Once a day/hour/minute.. depending on your needs there is a process that does a batch/whole indexing of the DB data to the cache.
like image 25
medunes Avatar answered Oct 29 '25 16:10

medunes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!