Chapter 15: Performance and cost

Now the tools for fixing it. The first one anyone reaches for, and the first one an agent reaches for, is a cache.

Caching

Also called LRU, Hit rate

Keeping a copy of an answer somewhere quick, so you can skip the slow work next time. A hit is when the copy is there. A miss means doing the work anyway. A cache is small, so something has to decide what to throw out, often the least recently used.

A cache is an easy diff that makes a benchmark look great, so it is a favourite agent fix, and it swaps a speed problem for a correctness problem. Check the hit rate on real traffic first: a cache that rarely hits costs memory and complexity, and gives nothing back.

Say it to your agent

“Before adding a cache to getProduct, log 10,000 real lookups and work out the hit rate for a cache of 100, 1,000 and 10,000 entries. Show me those numbers and how stale the data could get. Only then write it.”

The workshop

Name a word, or describe what you mean in everyday language.