The Wayback Machine - https://web.archive.org/web/20261129131237/https://www.geeksforgeeks.org/system-design/caching-system-design-concept-for-beginners/

Caching - System Design Concept

Last Updated : 23 Nov, 2026

Caching is a technique used to store frequently accessed or expensive-to-compute data in a faster storage layer. It improves application performance by reducing repeated requests to slower data sources such as databases, APIs, and storage systems.

  • Cached data can be stored locally within an application or in an external caching system such as Redis.
  • Caching reduces response time and database load, helping applications handle more requests.

Example: When a tweet goes viral, millions of users may request the same content. Instead of querying the database for every request, the application can serve the tweet from a cache.

Working of Cache

A cache stores frequently requested data so applications do not need to retrieve it from the database every time.

frame_1000001914

The caching process follows these steps:

  • The application receives a request and checks the cache.
  • If the data exists, it is returned directly. This is called a cache hit.
  • If the data is unavailable, a cache miss occurs.
  • The application retrieves the data from the database or another source.
  • The retrieved data is stored in the cache, and the response is returned to the user.

Future requests can use cached data until it expires or is invalidated.

Types of Cache

Caching can be implemented at different levels based on where data is stored and how applications access it. The following categories describe different caching architectures and may overlap.

1. Application Server Cache

An application server cache stores frequently accessed data locally within or alongside an application server. It provides fast retrieval without requiring a network request to an external cache.

Example: An e-commerce application stores product configuration data in each application server's local memory.

frame_1000002562

Limitations:

  • Each application server maintains an independent cache, which can lead to duplicate data and different cache hit rates.
  • Maintaining consistency across multiple application caches becomes difficult when the underlying data changes.

A shared caching layer can help applications maintain common cached data across multiple servers.

2. Distributed Cache

A distributed cache stores data across multiple cache nodes. Cached entries are partitioned among the nodes to distribute storage and request traffic.

  • Cache nodes store different subsets of cached data using partitioning mechanisms such as hash slots or consistent hashing.
  • Adding cache nodes can increase storage capacity and throughput.
  • Applications use a routing mechanism or cache client to locate the node responsible for a requested key.
frame_1000002560

When a cache miss occurs, the application typically retrieves the missing data from the database and stores it in the appropriate cache node. Some caching systems also support loading data through a dedicated cache service.

3. Global or Shared Cache

A global or shared cache is a caching layer accessible by multiple application servers. Instead of maintaining independent cached copies, application servers access a common caching service.

Example: Multiple application servers use the same Redis cache to retrieve product details.

frame_1000002561

When a cache miss occurs, the application retrieves the missing data from the database, stores it in the shared cache, and returns the response to the client.

Note: Distributed and shared caches are not mutually exclusive types.

  • A distributed cache can also be shared across multiple application servers.
  • Distributed describes how cached data is stored across nodes, while shared describes how applications access it.

4. CDN (Content Delivery Network) / Edge Cache

A CDN caches eligible content on geographically distributed edge servers closer to users, reducing latency and requests to the origin server.

  • Static content includes images, CSS, JavaScript, and downloadable files.
  • Media content includes videos and other large files.
  • Dynamic responses can also be cached when appropriate caching rules are configured.
frame_1000001921

When the requested content is available at the edge, the CDN returns it directly to the client. Otherwise, it retrieves the content from the origin server and may cache the response for future requests.

Cache Invalidation Strategies

Cache invalidation removes or updates cached entries when the underlying data changes or cached values become outdated.

Common strategies include:

  • Time-based expiration automatically expires cached entries after a configured Time-to-Live (TTL).
  • Event-driven invalidation uses events or messages about data changes to trigger cache updates or removal.
  • Direct invalidation allows application code to explicitly delete or update cached entries, typically after database write operations.

Event-driven invalidation can use explicit cache deletion internally. The distinction is whether invalidation is triggered by an event or directly by application logic.

Cache Eviction Policies

A cache has limited capacity, so an eviction policy determines which entries should be removed when more space is needed.

Common policies include:

  • Least Recently Used (LRU) removes the entry that has not been accessed for the longest time.
  • Least Frequently Used (LFU) removes entries with the lowest access frequency.
  • First-In-First-Out (FIFO) removes the oldest inserted entry first.

Cache eviction normally removes only cached copies. The original data remains in the primary database or another authoritative data source.

Limitations of Caching

Although caching improves performance, it introduces several challenges:

  • Fast cache storage, particularly RAM, is expensive and cannot always hold the entire dataset.
  • Cached data may become outdated when the original data changes.
  • Expiration, invalidation, and eviction policies require careful management.
  • An empty cache after a restart can temporarily increase database load.
  • External caching systems require additional monitoring, maintenance, and failure handling.
  • Frequently accessed keys can overload individual cache nodes in distributed systems.
Comment

Explore