We want our apps to run fast. One way to achieve this is through caching.
We can cache data on the client. For example, we can cache responses from API calls on the browser. Then, we can leverage this cached data to provide a smoother experience for the user by displaying cached information while we make network requests to update the cache.
We can cache data on the server as well. When a request is made, we can first check if the data exists in the cache. If it does we can return the data immediately, and if it's not, we query the database and cache the data for next time.
Content Distribution Network
CDNs cache static content such as images, videos, or websites. When a request is made for a specific asset, CDN will first check if it is available locally before querying the server.
There are two types of cache maintenance we need to do. First, we have to make sure that the cache stays in sync with its persistent storage. Second, we have to decide what to do when the cache gets full.
When data is modified in persistent storage, how do we update the cache? We have some options:
Update the cache first (Write-back cache)
Update the cache second (Write-around cache)
Update the cache and persistent storage at the same time (Write-through cache)
There are pros and cons to each option making tradeoffs between speed and data consistency.
For write-back cache, we risk data integrity for speed. For write-around cache, we risk cache miss and speed for data integrity. For write-through cache, we sacrifice speed (have to write twice) for data integrity.
Since we don't have unlimited amounts of cache, we have to make a decision on what to do once our cache is full. Some of the cache eviction policies we can implement are the following:
- Least Recently Used (LRU)
- Least Frequently Used (LFU)
- First In First Out (FIFO)
- Last In First Out (LIFO)
Caching can happen both on the client side and the server side and also for application data and for static assets. We have to make sure that the cache is up to date with data and also make sure we are using our limited space efficiently.
Top comments (0)