Revoking the cache during the upgrade phase is a viable approach and has been used extremely rarely in the past.
You have two options when an UPDATE occurs:
- You can try to set a new value during the update operation or
- Just delete the old one and update during the read operation .
If you need an LRU cache , then UPDATE can simply delete the old value, and when you first get the object, you will create it again after reading from the real database. However, if you know that your cache is very small and you use a different main database for tasks other than the size of the data, you can update directly during the UPDATE.
However, all this is not enough to be completely consistent.
When you write to your database, the Redis cache may not be available, for example, for several seconds, so the data between them is not synchronized.
What are you doing in this case?
There are several options that you can use at the same time.
- In any case, set TTL so that eventually the corrupted data is updated.
- Use lazy reader repair. When you read from a database, from time to time check at the main, whether value matches. If not, update the cached item (or delete it).
- Use eras or similar ways to access your data. Not always possible, however sometimes you get access to cached data about this object. Whenever possible, you can change the object identifier / descriptor every time you change it, so it is impossible to access obsolete data in the cache: each key name refers to a specific version of your object.
Thus, del-cache-on-update and write-cache-on-read is a basic strategy, but you can use other additional systems to resolve inconsistencies.
In fact, there is another option instead of using the above options, which is a background process that uses Redis SCAN to check key by key, if there are inconsistencies. This process can be slow and work with a copy of your database.
As you can see, the basic idea is always the same: if the cache update fails, do not make it a permanent problem that may remain there forever, give it the opportunity to fix itself later.
source share