how to keep caching up to date

2019-03-31 12:47发布

when memecached or Redis is used for data-storage caching. How is the cache being updated when the value changed?

For, example. If I read key1 from cache the first time and it missed, then I pull value1 and put key1=value1 into cache.

After that if the value of key1 changed to value2. How is value in cache updated or invalidated?

Does that mean whenever there is a change on key1's value. Either the application or database need to check if this key1 is in cache and update it?

1条回答
叛逆
2楼-- · 2019-03-31 13:16

Since you are using a cache, you have to tolerate the data inconsistency problem, i.e. at some time point, data in cache is different from data in database.

You don't need to update the value in cache, whenever the value has been changed. Otherwise, the whole cache system will be very complicated (e.g. you have to maintain a list of keys that have been cached), and it also might be unnecessary to do that (e.g. the key-value might be used only once, and no need to update it any more).

How can we update the data in cache and keep the cache system simple?

Normally, besides setting or updating a key-value pair in cache, we also set a TIMEOUT for each key. After that, client can get the key-value pair from the cache. However, if a key reaches the timeout, the cache system removes the key-value pair from the cache. This is called THE KEY HAS BEEN EXPIRED. The next time, the client trying to get that key from cache, will get nothing. This is called CACHE MISS. In this case, client has to get the key-value pair from database, and update it to cache with a new timeout.

If the data has been updated in database, while the key has NOT been expired in cache, client will get inconsistent data. However, when the key has been expired, its value will be retrieved from database and inserted into cache by some client. After that, other clients will get updated data until the data has been changed again.

How to set the timeout?

Normally, there're two kinds of expiration policy:

  1. Expire in N seconds/minutes/hours...
  2. Expire at some future timepoint, e.g. expire at 2017/7/30 00:00:00

A large timeout can largely reduce the load of database, while the data might be out-of-date for a long time. A small timeout can keep the data up-to-date as much as possible, while the database will have a heavy load. So you have to balance the trade-off when designing the timeout.

How does Redis expire keys?

Redis has two ways to expire keys:

  1. When client tries to operate on a key, Redis checks if the key has reached the timeout. If it does, Redis removes the key, and acts as if the key doesn't exist. In this way, Redis ensures that client doesn't get expired data.
  2. Redis also has an expiration thread that samples keys at a configured frequency. If the keys reach the timeout, Redis removes these keys. In this way, Redis can accelerate the key expiration process.
查看更多
登录 后发表回答