Cache Conundrums: Identifying Problems and Crafting Solutions

Fatma Erkan
SDTR
Published in
2 min readAug 3, 2024

Let’s delve into the common challenges faced in caching mechanisms by exploring scenarios and solutions.

Cache Penetration

This problem happens when a searched key does not exist in the cache and primary database. When the client requests a key, the application first looks at the cache and then hits the database due to cache miss. If the database does not include the key, respond to the empty result and the key is never cached. Every request for the not exist key will result these steps.

This operation seems insignificant, an attacker can attempt to crash the cache and database with many requests with these kinds of keys.

To Avoid

  • The empty result from the primary database will be cached with a short expiration time.

Cache Breakdown

When clients looking for the same key at the same time and the cache key expires, cache breakdown occurs. Because clients request to the primary database concurrently. Many keys expire at the same time, load on the primary database increases.

To Avoid

  • Obtain a lock on the searched key. When a request is updated to cache for the key, others need to wait.
  • Regularly update cache keys asynchronously, so the keys never expire.

Cache Avalanche

Many of the cached data expired at the same time and the cache service goes down. Then the number of requests increases suddenly to the primary database, cache avalanche occurs. Too many requests to the primary database might have a problem.

To Avoid

  • Different expiration times for different keys to avoid cache avalanche problems.
  • Regularly update cache keys asynchronously, so the keys never expire.
  • The usage of cluster architecture can solve this problem. If the master breaks down, one of the slaves is promoted as the master.

--

--