Hard java backend

When do you add a cache, and what are the risks?

The 30 second answer

I add a cache when data is read often, changes rarely, and the database is the measured bottleneck. I use cache-aside with a time to live and delete entries on updates. The risks are stale data, a stampede of requests when a hot key expires, memory growth without eviction, and one more system to run, so I measure the hit rate.

When data is read often and changes rarely, and the database is the bottleneck. I use cache-aside in Redis with a TTL, and I delete the key on update. The risks are stale data, a cold cache after a restart, and many misses at once on a hot key. Under high load I also watch Redis memory and hot keys.

Sources (2)
  1. Microsoft Azure Architecture Center: Cache-Aside pattern
  2. Redis docs: EXPIRE