Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

cache
caching-patterns
communitycreator

Caching patterns

abhilash

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
communitycreator
RELATED COURSES

View all Courses

Keep Exploring