Trusted answers to developer questions

Caching patterns

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

What is a cache?

A cache is a component that stores portions of data that would otherwise either take a long time to calculate/process. Caches may also originate from another underlying backend system, where caching is used to prevent additional requests for round trips for frequently used data. Caching can be used to improve performance or reduce application latencies in both these cases.

A cache hit means the data required for the request is in the cache and is served from the cache.

A cache miss means the data required for the request is not in the cache and would need to be computed by the backend systems.

Caching strategies

Cache aside strategy

  1. When the request is received by the server/app, the app checks for the data in the cache. If the data is there in the cache, it’s a cache hit and served from the cache.
  2. If the data is not available in the cache, then it’s a cache miss.
  3. The app fetches the data from the database and updates the cache with the fetched data.

Write through cache

In this strategy, the cache has a writer component that can write to database.

  1. The application receives request to write data to database.
  2. The application writes the data to the cache.
  3. The cache invokes the writer to write to database as well as update the cache.

Read through cache

The cache is configured with a loader component that loads data from the database.

  1. When the application receives the read request, it asks the cache for data associated with the key.
  2. If there is a cache hit, then the data is served from the cache.
  3. If there is a cache miss, then the cache invokes the loader that fetches the data from the database, updates the cache, and serves the fetched data.
  4. The next time there is a read request for the same data, it’s served from the cache.

Write back cache

  1. The app writes data to the cache and receives an immediate acknowledgement that the write is successful.
  2. The cache internally maintains a buffer to save the writes.
  3. The cache asynchronously writes the data from the buffer to the database at a later point in time.

RELATED TAGS

cache
caching-patterns
Did you find this helpful?