Caching is the mechanism of storing data in a temporary storage location, either in memory or on disc, so that request to the data can be served faster.
Caching improves performance by decreasing page load times, and reduces the load on servers and databases.
In a typical caching model, when a new client request for a resource comes in, a lookup is done on the temporary storage to see if a similar request came in earlier. If a similar request is found then the previous result is returned with out hitting the server or database. If a similar request is not found, then the client request is send to the server or database to fetch the result, the result is updated in the temporary storage, and the result is returned back to the client.
Caching can be implemented in the following layers.
Client caching - Caching on the client side (browser)
CDN caching - A Content Delivery Network (CDN) which is a globally distributed network of proxy servers, typically serving static content, is considered a type of cache.
Web server caching - Caching on the web server or proxy server side, which enables returning client response without contacting the application servers or databases.
Application server caching - Caching on the application server side, which enables returning client response without contacting the database.
Database caching - Caching on the database side, which enables returning client response without executing the queries.
When data is updated in the database, then that data has to be refreshed in the cache as well. This is called cache invalidation.
There are three main methods of cache invalidation
Write-through cache - Data is written to the cache and database at the same time.
Write-around cache - Data is written to the database writing to cache. Data is written to cache when a request results in a 'cache miss', at which point data in retrieved from database, written to cache, and send back to client.
Write-back (Write-behind) cache - Data is written to the cache without writing to database. Data is written to database asynchronously.
When data is updated in the database, then that data has to be refreshed in the cache as well. This is called cache invalidation.
There are three main methods of cache invalidation
Write-through cache - Data is written to the cache and database at the same time.
Write-around cache - Data is written to the database writing to cache. Data is written to cache when a request results in a 'cache miss', at which point data in retrieved from database, written to cache, and send back to client.
Write-back (Write-behind) cache - Data is written to the cache without writing to database. Data is written to database asynchronously.
Following are some common cache eviction algorithms
First In First Out (FIFO)
Last In First Out (LIFO)
Least Recently Used (RLU)
Most Recently Used (MRO)
Least Frequently Used
Least Frequent Recently Used (LFRU)
Random Replacement (RR)