System Design - Caching

Author: Al-mamun Sarkar Date: 2020-09-17 08:53:44

In this post, I will write about what is caching, where it can be used, different types of cache, Cache invalidation, and eviction policies. 

What is caching:

The cache is a high-speed data storage layer mainly which can serve data faster in the future. RAM is being used to store data for caching. When we request to get the data it sends data to us form RAM so it doesn't need to find from the database or hard disk. That's why it's server data faster. Recently access data is being stored on cache. 

Where it can be used:

Caching is used in almost every layer. Hardware layer, OS Layer, Web Brawer, Web Application. 

Types of cache:

  • Application Server Cache: Caching on local storage. Using local storage for web frontend or mobile application.
  • Distribute Caching: Every node will have it's own caching. We may face a cache miss(duplicate cache) problem for the load balancers on this architecture. To solve this consistent hashing is being used. 
  • Global Caching: A single cache server for multiple nodes
  • CDN - Content Distribution Network: Caching for static data like images, files

Cache invalidation:

When a data is updated on the database it's cache should be invalidated. There are mainly 3 types of cache invalidation. 

  • Write Through Cache: Data is written into the database as well as to cache at the same time. 
  • Write Around Cache: Data is written to the database and remove that cache. For the next first request query data from the database and update the cache.
  • Write Back Cache: Data is written to the cache and send completion response to the user immediately. And it will be written to the database after a specific time.

Cache eviction policies:

There are several cache eviction policies. These are as follows. Amon them LRU is better

  • First in First out
  • LRU (Least Recently Used)
  • Most Recently Used
  • Least Frequently Used
  • Random Replacement