AlgoViz

Caching Strategies and Eviction

medium 25 min

Place fast memory between applications and persistent storage to slash read latency and protect databases.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

Near, few, and a little old: keep the popular copies close and know their age.

A pencil case on the desk instead of the cupboard down the hall: quick to reach, room for a few, and worn pencils have to go.

Caching Strategies and Eviction in one picture

Cheat sheet

Average read
hits x cache time + misses x (cache + database time)
90% to 99% hits
10 times fewer database reads
Memory
hot items x size x 1.5 (assumed room)
On a change
write the database, delete the key
Three ways out
eviction, expiry, invalidation

Spot it when you see

  • The same data is read again and again
  • The database is busy with repeated reads
  • A slow result that may be a few seconds old

Tricks that stick

  • Copy, near, few, full, old: five words that cover the whole topic.
  • Full means eviction, time means expiry, change means invalidation.

Do not forget

  • No expiry on a key
  • A key without the person in it
  • A database sized only for the misses
  • One average hit rate for everything