system-designnotesCaching
  • A cache is a temporary storage area that stores the result of expensive responses or frequently accessed data in memory so that it can be retrieved more quickly.
  • The separate cache tier offers several benefits such as better system performance, reducing database workloads, and the ability to reduce database workloads.

Considerations for using a cahce

  • Consider using a cache when data is read frequently but modified infrequently.
  • Since cached data is stored in volatile memory, a cache server is not ideal for persisting data.
  • Making the cache date too short makes the system reload the data from the database too frequently, while making the expiration date too long makes the data become stale.
  • Inconsistencies can happen because data-modifying operations on the data store and cache are not in a single transaction.
  • Multiple cache servers across different data centers are recommended to avoid the single point of failure issue. Another strategy is to overprovision the required memory by a certain percentage. This provides a buffer as the memory usage increaases.
  • Once the cache is full, further write requests to the cache might cause existing items to be removed. Cache eviciton policies include Least Recently Used (LRU), Least Frequently Used (LFU), or First In First Out (FIFO).

Content Delivery Network (CDN)

A CDN is a network of geographically dispersed servers used to deliver static content. CDN servers cache static content like images, videos, CSS, JavaScript, etc.

Considerations for using a CDN

  • CDNs are run by third-party providers, so there’s a cost for data transfers in and out of the CDN.
  • Cache expiry time should neither be too long nor too short. If it is too long, content might no longer be fresh. If it is too short, it can cause repeat reloading of content from origin servers to the CDN.
  • If the CDN goes offline, clients should be able to detect the problem and request resources from the origin.
  • Files can be removed from the CDN before it expires by performing one of the following operations:
    • Invalidate the CDN object using APIs provided by CDN vendors
    • Use object versioning to serve a different version of the object, by using URL parameters.