Understanding Caching From a Backend Engineer’s Perspective
As backend systems scale, repeatedly fetching the same data from databases or recomputing expensive operations becomes inefficient…
Understanding Caching From a Backend Engineer’s Perspective
As backend systems scale, repeatedly fetching the same data from databases or recomputing expensive operations becomes inefficient. Databases are comparatively slower because disk access, joins, network calls, and computation all add latency. If every request directly hits the database, system performance degrades quickly under high traffic. Caching exists to reduce this unnecessary repeated work.
In most simple terms, caching is storing a subset of data that is frequently accessed or is expensive to compute in a faster storage layer so that the future requests are faster.
Without caching, every request directly hits the database to fetch data, which increases latency and database load under high traffic. With caching in place, the application first checks whether the requested data already exists in cache.
If the data is found, it is called a cache hit, and the response is returned quickly without querying the database.
If the data is not found, it is called a cache miss. The application then fetches the data from the database, stores it in cache, and returns the response.
*Cache Hit Ratio = Cache Hits / Total Requests 100**
A higher cache hit ratio generally indicates that the cache is effectively serving repeated requests and reducing load on downstream systems like databases.
However, a very high cache hit ratio is not always sufficient by itself, cache freshness, memory usage, and invalidation strategy also matter.
What Should Be Cached?
Not all data should be cached. Good candidates for caching are:
- Frequently accessed data
- Expensive database queries
- Computed analytics
- Static or semi-static content
- Repeated API responses
Data that changes very frequently or requires strict consistency is usually harder to cache effectively.
Types of Caching
Caching can exist at multiple layers in a system, with each layer solving different performance problems.
- In Memory Application Cache — Application stores frequently used data directly in RAM(inside the app process memory). This provides extremely fast access because no network call is involved. However, the cache is lost if the application restarts, and each backend instance maintains its own local cache, meaning the data is not shared across servers.
- Distributed Cache — A distributed cache is shared across multiple backend instances using a separate cache server. Tools like Redis and Memcached are commonly used here. Unlike in-memory application cache, distributed cache survives application restarts because the cache exists outside the application process itself.
- Content Delievery Network / CDN — A CDN caches static assets like images, CSS Files, JS bundles, fonts, videos, PDFs, icons closer to users geographically. Servers are strategetically placed (at edge locations) so latency of users connection in that region is minimal.
- Browser Cache —Browser caching relies on HTTP caching headers like Cache-Control, ETag, Expires, and Last-Modified to determine whether resources should be reused or fetched again from the server.
Caching Strategies
Now that we understand where caching can exist in a system, the next important question is, How does data actually get populated into cache?Different caching strategies solve this problem differently.
- Cache Aside — It uses lazy loading behaviour for cache population. When a request is made, the application first checks the cache. If the data is found, the response is returned directly from cache. If the data is not found, the application fetches the data from the database, stores it in cache, and then returns the response.
- Write Through — Write operations go to cache as well as DB simultaneously. This ensures cache remains in sync with DB.
- Write Back/ Write Behind — Write only to Cache first, DB is updated asynchronously later.
- Read Through — Application talks only to cache. If data is missing, the cache itself fetches the data from the database automatically.
Cache Invalidation
Caching introduces an important challenge: stale data. Whenever data changes in the database, cached data may become outdated. Because of this, cache invalidation becomes a critical part of caching system design.
- TTL (Time To Live) — Each cache entry is assigned an expiration time after which it is automatically removed.
- Event Based Invalidation — Cached data is invalidated when specific events occur, such as database updates.
- Versioned keys — Instead of updating existing cache keys, newer versions of keys are generated. Older keys automatically become obsolete over time. This approach is commonly used in large distributed systems.
Cache Eviction Policies
Cache memory is limited, so when the cache becomes full, the system must decide which data should be removed. This is handled using cache eviction policies.
- Least Recently Used (LRU) — We remove data which was least recently used.
- Least Frequently Used(LFU)- Removes the least frequently accessed items. This works well for workloads with stable traffic patterns and frequently accessed hot data.
- First In, First Out (FIFO)— first inserted items are removed first.
- Random Eviction — randomly remove keys. it surprisingly works for some workloads.
Tradeoffs of Caching
Caching improves performance and reduces latency, but it also introduces tradeoffs:
- stale data
- synchronization complexity
- additional infrastructure
- invalidation challenges
- memory overhead
Designing an effective caching strategy is often a balance between performance and consistency.
Caching has become even more important in AI systems because LLM calls, embeddings, and vector searches are computationally expensive and often repeated across requests.
Understanding caching is not just about learning tools like Redis or Memcached, it is about understanding how backend systems reduce unnecessary work and scale efficiently under heavy traffic.
메타데이터
- post_id
- f268dfd9fb46
- slug
- understanding-caching-from-a-backend-engineers-perspective-f268dfd9fb46
- url
- https://medium.com/@shreyashukla680/understanding-caching-from-a-backend-engineers-perspective-f268dfd9fb46
- canonical_url
- https://medium.com/@shreyashukla680/understanding-caching-from-a-backend-engineers-perspective-f268dfd9fb46
- author_url
- https://medium.com/@shreyashukla680
- status
- ok
- fetched_at
- 2026-08-08 07:34:52