jargon

Backend & systems·topic 1 of 13

Caching and invalidation

Caching is the cheapest performance win available and the easiest way to serve confidently wrong data. Almost every term here is really about the second half of the problem: knowing when the copy you kept has stopped being true.

Read in order · tick what you already know

  1. 01

    you cache the return value of a pure function inside the process so calling it twice with the same arguments only computes once.

    Memoisation

  2. 02

    you keep the values in the application's own memory, so lookups are free and every instance ends up with a slightly different copy.

    In-process cache

  3. 03

    you move the cache into a shared service so every instance sees the same entries and one invalidation covers the whole fleet.

    Distributed cache

  4. 04

    you check the cache, miss, go and read the database yourself, and write the value back before you return it.

    Cache-aside

  5. 05

    you ask the cache for the key and the cache itself is the thing that goes and loads it when it is not there.

    Read-through cache

  6. 06

    every write goes into the cache and the database in the same call, so the cached copy is never behind the row.

    Write-through cache

  7. 07

    you write to the cache, return success immediately, and let a background flush push the batch down to the database later.

    Write-behind cache

  8. 08

    you let the cached value expire after a fixed number of seconds instead of trying to work out exactly when it stopped being true.

    Time to live

  9. 09

    the cache filled up and something had to decide which entries get thrown out to make room for the new one.

    Eviction policy

  10. 10

    you measure what fraction of lookups the cache actually answered and find out it is thirty percent and the cache is buying you almost nothing.

    Cache hit rate

  11. 11

    you changed the row and now you have to hunt down every cached copy of it before someone reads the old one.

    Cache invalidation

  12. 12

    you cache the fact that the thing does not exist, so a bad id cannot hit the database on every single retry.

    Negative caching

  13. 13

    the popular key expired and ten thousand requests all missed at the same instant and went straight through to the database.

    Cache stampede

  14. 14

    you populate the cache before you send real traffic at it, so the first users after a deploy do not eat every miss.

    Cache warming

  15. 15

    you hand back the expired value immediately and refresh it in the background, so nobody actually waits for the recompute.

    Stale-while-revalidate

  16. 16

    one key gets so much traffic that the single node holding it is saturated while the rest of the cluster sits idle.

    Hot key

  17. 17

    the client sends back the version tag it already has and you answer 304 with no body instead of resending the whole thing.

    ETag

  18. 18

    you push the asset out to servers near the user so the request is answered without ever reaching your origin.

    Content delivery network