Posts

Caching

Tech

  • Take advantage of the locality of reference principle: recently requested data is likely to be requested again.
  • Exist at all levels in architecture, but often found at the level nearest to the front end.

Application server cache

  • Cache placed on a request layer node.
  • When a request layer node is expanded to many nodes
    • Load balancer randomly distributes requests across the nodes.
    • The same request can go to different nodes.
    • Increase cache misses.
    • Solutions:
      • Global caches
      • Distributed caches

Distributed cache

  • Each request layer node owns part of the cached data.
  • Entire cache is divided up using a consistent hashing function.
  • Pro
    • Cache space can be increased easily by adding more nodes to the request pool.
  • Con
    • A missing node leads to cache lost.

Global cache

  • A server or file store that is faster than original store, and accessible by all request layer nodes.
  • Two common forms
    • Cache server handles cache miss.
      • Used by most applications.
    • Request nodes handle cache miss.
      • Have a large percentage of the hot data set in the cache.
      • An architecture where the files stored in the cache are static and shouldn’t be evicted.
      • The application logic understands the eviction strategy or hot spots better than the cache

Content distributed network (CDN)

  • For sites serving large amounts of static media.
  • Process
    • A request first asks the CDN for a piece of static media.
    • CDN serves that content if it has it locally available.
    • If content isn’t available, CDN will query back-end servers for the file, cache it locally and serve it to the requesting user.
  • If the system is not large enough for CDN, it can be built like this:
    • Serving static media off a separate subdomain using lightweight HTTP server (e.g. Nginx).
    • Cutover the DNS from this subdomain to a CDN later.

Cache invalidation

  • Keep cache coherent with the source of truth. Invalidate cache when source of truth has changed.
  • Write-through cache
    • Data is written into the cache and permanent storage at the same time.
    • Pro
      • Fast retrieval, complete data consistency, robust to system disruptions.
    • Con
      • Higher latency for write operations.
  • Write-around cache
    • Data is written to permanent storage, not cache.
    • Pro
      • Reduce the cache that is no used.
    • Con
      • Query for recently written data creates a cache miss and higher latency.
  • Write-back cache
    • Data is only written to cache.
    • Write to the permanent storage is done later on.
    • Pro
      • Low latency, high throughput for write-intensive applications.
    • Con
      • Risk of data loss in case of system disruptions.

Cache eviction policies

  • FIFO: first in first out
  • LIFO: last in first out
  • LRU: least recently used
  • MRU: most recently used
  • LFU: least frequently used
  • RR: random replacement

About the Author

Software Engineer

1 comment

  1. Caching Overview
    RAM and In-Memory Engines: Due to the high request rates or IOPS (Input/Output operations per second) supported by RAM and In-Memory engines, caching results in improved data retrieval performance and reduces cost at scale. To support the same scale with traditional databases and disk-based hardware, additional resources would be required. These additional resources drive up cost and still fail to achieve the low latency performance provided by an In-Memory cache.

    Applications: Caches can be applied and leveraged throughout various layers of technology including Operating Systems, Networking layers including Content Delivery Networks (CDN) and DNS, web applications, and Databases. You can use caching to significantly reduce latency and improve IOPS for many read-heavy application workloads, such as Q&A portals, gaming, media sharing, and social networking. Cached information can include the results of database queries, computationally intensive calculations, API req…
Enter your comments here...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.