# Request-Caching Layer: A New Engineer's Guide A request-caching layer sits between clients and a slower backend, such as a database, an internal service, or a third-party API. When a request arrives, the layer checks whether it already has a stored answer for that request. If it does (a **hit**), it returns the stored answer without doing the expensive work. If it does not (a **miss**), it forwards the request, returns the result, and usually stores a copy for next time. **Core concepts** - **Cache key:** A value derived from the request, such as a resource ID plus relevant parameters. Two requests with the same key should produce the same answer. - **TTL (time to live):** How long an entry stays valid. Every entry should have one. Entries without a TTL can accumulate indefinitely. - **Eviction:** When the cache is full, the least useful entries are removed, typically by LRU (least recently used) or a similar policy. Capacity is a budget, and it costs money. - **Invalidation:** Removing or refreshing entries when the underlying data changes. This is the hardest part to get right. **Cost-aware habits** 1. **Bound everything.** Set a maximum size, a TTL, and a limit on how many entries a single client or tenant can create. 2. **Keep key and metric cardinality low.** Putting user IDs or raw query strings into cache keys or metric labels creates a new time series for each value, which drives up monitoring costs. Use bucketed or normalized values instead. 3. **Log sparingly.** Log cache misses at a sampled rate or as aggregated counters. Logging every miss at debug level in production can generate large volumes of data. 4. **Size test environments to match.** Local and CI cache instances should be small and torn down after use. Do not leave bootcamp exercise clusters running. 5. **Measure the hit ratio.** A low hit ratio means the cache costs more than it saves. Track hit ratio, eviction rate, and memory use together. **Checklist before you ship a cache** - Does every entry have a TTL? - Is the cache size capped? - Are the key and metric labels bounded? - Do you know how invalidation works for this data? - Is there a dashboard showing hit ratio and memory use?