logoTan Chia Chun

Distributed Caching

An introduction to distributed caching, cache write strategies, cache eviction policies, and how caching improves system performance at scale.

Introduction

As applications scale, databases often become one of the largest performance bottlenecks.

Every request hitting the database introduces:

  • Network overhead
  • Disk access
  • CPU usage
  • Query execution cost

When traffic grows, repeatedly fetching the same data becomes inefficient.

This is where caching becomes important.


What is Caching?

Caching stores frequently accessed data in faster storage layers such as memory, allowing applications to retrieve data significantly faster than repeatedly querying a database.

In modern distributed systems, caching is one of the most important techniques used to improve:

  • Performance
  • Scalability
  • Latency
  • Throughput

What Can We Cache?

Many types of data can benefit from caching.

Common Examples

  • Database query results
  • API responses
  • User sessions
  • Authentication tokens
  • Frequently accessed configuration
  • Computed values
  • Web pages
  • Search results

The best cache candidates are:

  • Frequently read
  • Expensive to compute
  • Infrequently updated

Benefits of Caching

Caching provides several major advantages for distributed systems.

Faster Response Time

Memory access is significantly faster than database or disk access.

This reduces application latency and improves user experience.

Reduced Database Load

Frequently requested data can be served directly from cache instead of repeatedly querying the database.

This reduces:

  • Database CPU usage
  • Disk IO
  • Expensive query execution

Improved Scalability

By reducing direct database traffic, systems can handle more concurrent users without scaling the database as aggressively.

Better Throughput

Applications can process more requests per second when cache hits occur.

This is especially important for:

  • High traffic APIs
  • Ecommerce systems
  • Social media feeds
  • Real-time dashboards

Disadvantages of Caching

Although caching improves performance, it also introduces complexity.

Cache Invalidation

Keeping cache data synchronized with the source of truth (usually the database) is difficult.

One of the hardest problems in distributed systems is knowing:

  • When to update cache
  • When to remove stale data
  • How to maintain consistency

Stale Data

Cached data may become outdated if the database changes before the cache is refreshed.

This can cause users to see old or inconsistent information.

Increased System Complexity

Caching introduces additional infrastructure and coordination logic.

Developers must now manage:

  • Cache synchronization
  • Expiration policies
  • Eviction policies
  • Cache failures

Memory Cost

In-memory caching systems can become expensive at large scale because RAM is significantly more costly than disk storage.


Types of Caching

Caching systems can be implemented in different ways depending on scale and consistency requirements.

Server Local Caching

Server local caching stores cached data directly inside the application server.

Example

Application Server
   └── In-Memory Cache

Examples include:

  • In-memory JavaScript objects
  • LRU memory stores
  • Application-level memory caches

Advantages

  • Extremely fast access
  • No network calls required
  • Simple implementation

Disadvantages

  • Cache is isolated per server
  • Poor consistency across multiple servers
  • Cache disappears when server restarts

This approach works well for small systems but becomes problematic in distributed environments.

Global Caching Layer

A global caching layer centralizes cache storage across multiple application servers.

Example

App Servers
     |
Distributed Cache
     |
Database

Common technologies include:

  • Redis
  • Memcached

Advantages

  • Shared cache across all servers
  • Better consistency
  • Easier centralized cache management

Disadvantages

  • Additional network latency
  • Requires separate infrastructure
  • Cache cluster itself becomes a distributed system

Global caches are commonly used in large-scale applications.


Conclusion for Caching

Caching is fundamentally a trade-off.

Systems trade:

  • Complexity
  • Memory usage
  • Potential consistency issues

in exchange for:

  • Better performance
  • Lower latency
  • Higher scalability

At scale, effective caching is often essential for system performance.


Distributed Cache Writes

When cached data changes, systems must decide how writes interact with both:

  • The cache
  • The database

Different write strategies provide different trade-offs between:

  • Consistency
  • Performance
  • Reliability

Strategies of Distributed Cache Write

When cached data changes, systems must decide how writes interact with:

  • the cache
  • the database

Different strategies optimize for:

  • consistency
  • latency
  • throughput
  • fault tolerance

Write-Around Cache

In a write-around cache strategy:

  • Writes go directly to the database
  • Cache is bypassed during writes
  • Cache updates only happen during future reads

Flow

Write Request
      |
   Database

Advantages

  • Simpler cache management
  • Prevents caching unnecessary data
  • Reduces cache pollution

Disadvantages

  • First read after write becomes slower
  • Higher chance of cache misses

This approach is useful when write frequency is high but repeated reads are less common.

Write-Through Cache

In write-through caching:

  • Writes go to cache first
  • Cache immediately updates the database

Flow

Write Request
     |
   Cache
     |
  Database

Advantages

  • Cache always stays synchronized
  • Lower chance of stale reads
  • Simpler consistency model

Disadvantages

  • Higher write latency
  • Every write updates two systems

This strategy prioritizes consistency.

Write-Back Cache

In write-back caching:

  • Writes update cache first
  • Database updates happen asynchronously later

Flow

Write Request
      |
     Cache
      |
 Async Database Write

Advantages

  • Extremely fast writes
  • Reduced database load
  • High throughput

Disadvantages

  • Risk of data loss if cache fails before database sync
  • More complex recovery logic
  • Temporary inconsistency possible

This strategy is commonly used in high-performance systems.


Conclusion for Cache Write Strategies

Each caching write strategy optimizes for different goals.

StrategyPrioritizes
Write-AroundSimplicity
Write-ThroughConsistency
Write-BackPerformance

There is no universally correct approach.

The best strategy depends on:

  • Read/write patterns
  • Consistency requirements
  • Failure tolerance
  • Performance goals

Cache Eviction Policies

Caches have limited memory.

When cache capacity is full, systems must decide which items should be removed.

This process is called cache eviction.

Different eviction policies optimize for different access patterns and workloads.

FIFO (First In First Out)

FIFO removes the oldest cached item first.

Example

[ A ][ B ][ C ]
Remove A first

Advantages

  • Simple implementation
  • Low overhead

Disadvantages

  • Frequently used data may still be removed
  • Poor optimization for real-world access patterns

LRU (Least Recently Used)

LRU removes the item that has not been accessed for the longest time.

Example

If:

  • A was accessed recently
  • B was accessed recently
  • C has not been used for a long time

Then:

  • C gets removed first

Advantages

  • Works well for many workloads
  • Keeps frequently accessed data in cache

Disadvantages

  • Requires tracking access history
  • Slightly higher memory overhead

LRU is one of the most commonly used cache eviction strategies.

LFU (Least Frequently Used)

LFU removes items with the lowest access frequency.

Example

A accessed 100 times
B accessed 50 times
C accessed 2 times

C gets evicted first.

Advantages

  • Preserves highly popular items
  • Effective for stable access patterns

Disadvantages

  • More complex bookkeeping
  • Older frequently used items may stay cached too long

LFU is useful when long-term popularity matters.


Conclusion for Cache Eviction Policies

Different eviction policies optimize for different workloads.

PolicyBest For
FIFOSimplicity
LRURecent access patterns
LFULong-term popularity

Choosing the correct eviction policy can significantly impact cache efficiency.


Final Conclusion

Distributed caching is one of the most important performance optimization techniques in modern backend systems.

By storing frequently accessed data closer to applications, caching helps reduce:

  • Latency
  • Database load
  • Infrastructure cost

However, caching also introduces new challenges:

  • Cache invalidation
  • Consistency management
  • Distributed coordination
  • Eviction decisions

As systems scale, effective caching strategies become essential for building reliable, high-performance distributed applications.

Understanding:

  • Cache architectures
  • Write strategies
  • Eviction policies

is fundamental for designing scalable backend systems.


References

Redis

Memcached

Cache (computing) on Wikipedia

Caching Overview on Amazon

On this page