How Does Cache Work?
Typically, web application stores data in a database. When a client requests some data, it is fetched from the database and then it is returned to the user. Reading data from the database needs network calls and I/O operation which is a time-consuming process. Cache reduces the network call to the database and speeds up the performance of the system.
Lets understand how cache work with the help of an example:
Twitter: when a tweet becomes viral, a huge number of clients request the same tweet. Twitter is a gigantic website that has millions of users. It is inefficient to read data from the disks for this large volume of user requests.
Here is how using cache helps to resolve this problem:
- To reduce the number of calls to the database, we can use cache and the tweets can be provided much faster.
- In a typical web application, we can add an application server cache, and an in-memory store like Redis alongside our application server.
- When the first time a request is made a call will have to be made to the database to process the query. This is known as a cache miss.
- Before giving back the result to the user, the result will be saved in the cache.
- When the second time a user makes the same request, the application will check your cache first to see if the result for that request is cached or not.
- If it is then the result will be returned from the in-memory store. This is known as a cache hit.
- The response time for the second time request will be a lot less than the first time.
Caching – System Design Concept
Caching is a system design concept that involves storing frequently accessed data in a location that is easily and quickly accessible. The purpose of caching is to improve the performance and efficiency of a system by reducing the amount of time it takes to access frequently accessed data.
Important Topics for Caching in System Design
- What is Caching
- How Does Cache Work?
- Where Cache can be added?
- key points to understand Caching
- Types of Cache
- Applications of Caching
- What are the Advantages of using Caching?
- What are the Disadvantages of using Caching?
- Cache Invalidation Strategies
- Eviction Policies of Caching