DEV Community

Andrew Lee
Andrew Lee

Posted on

Beginner's Guide to Caching

We want our apps to run fast. One way to achieve this is through caching.

Client cache

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.

Server 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.

Maintenance

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.

Invalidation

When data is modified in persistent storage, how do we update the cache? We have some options:

  1. Update the cache first (Write-back cache)

  2. Update the cache second (Write-around cache)

  3. 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.

Eviction

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:

  1. Least Recently Used (LRU)
  2. Least Frequently Used (LFU)
  3. First In First Out (FIFO)
  4. Last In First Out (LIFO)

Conclusion

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)