Web apps & software

What is a caching layer?

A caching layer is fast, temporary storage, usually in memory, that sits between an application and slower sources such as a database or external API. It keeps recent results so that repeat requests are answered in microseconds instead of being recalculated.

Where caches live

  • Application cache: Redis or Memcached holding query results, sessions or computed prices.
  • HTTP and CDN caches: whole pages or images stored near users.
  • Database cache: the database's own buffer of hot data in memory.
  • Client cache: the browser or mobile app keeping data it already fetched.

Common strategies

Cache-aside is the usual pattern: the code checks the cache, falls back to the database on a miss, then stores the result with an expiry time (TTL). Write-through updates the cache whenever data is written. Short TTLs suit data that changes often, such as available drivers; long TTLs suit reference data, such as a list of countries.

The hard part: invalidation

A cache that serves stale data can be worse than no cache, for instance showing a price that changed an hour ago. Decide for each item how stale it may be, delete or refresh cache entries when the underlying data changes, and never cache per-user data under a shared key. Also plan for a cold cache after a restart, when every request hits the database at once; staggered expiry and warming scripts help.

Questions, answered

Often it helps a lot, but slow queries and missing indexes should be fixed first.

No. It is also used for queues, rate-limit counters, pub/sub and sessions.

By the Stegnor team · Updated