AlgoViz

API caching

easy 25 min

API caching keeps a copy of a response nearer to the caller, so the same question does not travel to the database again.

Where it shows up

The same pattern appears across everyday software.

  • GitHub's REST API

    Most endpoints return an ETag. A correctly authorized conditional request that gets 304 does not count against the primary rate limit, so polite clients can check often.

  • Facebook's memcache

    A 2013 paper describes leases: on a miss one client gets permission to refill the key, which limits stampedes and stops an old value from being stored after a delete.

  • NGINX in front of an API

    proxy_cache_lock lets one request fill a missing entry, and proxy_cache_use_stale with background update serves the old one meanwhile.

  • Fastly surrogate keys

    A response can carry tags in a header. One purge call by tag removes every page that mentions a product.

  • Versioned static files

    Build tools put a content hash in file names, so browsers may keep them for a year and a new release simply uses new names.

  • Amazon's advice on two lifetimes

    The Builders' Library describes a soft lifetime, after which a refresh is tried, and a hard one, after which the copy is dropped. If the dependency is down, the soft copy still serves.