C

Cache

A cache is a temporary storage layer that stores frequently accessed data to improve performance and reduce latency.

What is a cache?

A cache is a high-speed data storage layer that keeps copies of frequently requested data so it can be retrieved faster than from the original source. By serving data closer to the requester, caching reduces response times, lowers backend load, and improves scalability.

Caches can exist at multiple layers: application, database, operating system, network, or client-side.

Why caching matters

Caching is essential because it:

  • Improves application and website performance
  • Reduces latency and bandwidth usage
  • Lowers load on databases and origin servers
  • Enhances user experience
  • Enables systems to scale efficiently

Most high-performance systems rely heavily on caching.

Common cache types

Caching can be implemented in several forms:

  • Memory cache -- stores data in RAM for very fast access
  • Disk cache -- stores data on local storage
  • Application cache -- managed by the app logic
  • Database cache -- speeds up repeated queries
  • Web cache -- caches HTTP responses
  • CDN cache -- distributes cached content globally

Each type addresses different performance bottlenecks.

Cache in web and applications

In web environments, caches are used to store:

  • HTML pages and fragments
  • API responses
  • Images, CSS, and JavaScript
  • Authentication tokens and sessions
  • Computed results

Proper cache configuration is critical for correctness and freshness.

Cache lifecycle and invalidation

Cached data is typically controlled by:

  • TTL (Time To Live) -- how long data remains valid
  • Expiration policies -- when data is removed
  • Eviction strategies -- which entries are discarded when full

Cache invalidation is one of the hardest problems in system design.

Cache hit vs cache miss

  • Cache hit -- requested data is found in cache (fast)
  • Cache miss -- data must be fetched from the source (slower)

A high cache hit ratio indicates effective caching.

Cache consistency challenges

Caching introduces trade-offs:

  • Stale data if invalidation is incorrect
  • Complexity in distributed systems
  • Synchronization across multiple cache layers
  • Balancing freshness vs performance

Design decisions depend on application requirements.

Cache in distributed and cloud systems

In modern architectures, caching:

  • Reduces database pressure
  • Improves microservices performance
  • Enables horizontal scaling
  • Is often shared across services
  • Works alongside load balancing and CDNs

Distributed caches are common in cloud-native systems.

Security considerations

From a security perspective:

  • Sensitive data should be cached carefully
  • Access control must apply to cached responses
  • Cache poisoning attacks are possible
  • Authentication data must respect user isolation

Improper caching can lead to data leaks.

Common misconceptions

  • "Caching always improves performance"
  • "Cache data is always up to date"
  • "More caching is always better"
  • "Caching is only for web applications"