Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    Caching

    Published Jun 21, 2022 [  SystemDesign  ]

    Caching will enable you to make vastly better use of the resources you already have as well as making otherwise unattainable product requirements feasible. Caches take advantage of the locality of reference principle: recently requested data is likely to be requested again.

    Application Server Cache

    Placing a cache directly on a request layer node enables the local storage of response data. If the request layer is expended to multiple nodes, it is still quite possible to have each node host its own cache. However, if your load balancer randomly distributes requests across the nodes, the same request will go to different nodes, thus increasing cache misses. Two choices for overcoming this hurdle are global caches and distributed caches.

    Content Delivery (or Distribution) Network (CDN)

    CNDs are a kind of cache that comes into play for sites serving large amounts of static media. In a typical CDN setup, a request will first ask the CDN for a piece of static media; the CDN will serve the content if it has it locally available. It if isn’t available, the CDN will query the back-end servers for the file, cache it locally, and serve it to the requesting user.

    If the system we are building is not large enough to have its own CDN, we can ease a future transition by serving the static media off a separate subdomain using a lightweight HTTP server like NGINX, and cut-over the DNS from you servers to a CND later.

    Cache Invalidation

    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.

    Cache Eviction Policies

    1. FIFO: The cache evicts the fist block accessed without any regard to how often or how many times it was accessed before.
    2. LIFO: The cache evicts the block accessed mostly recently without any regard to how often or how many times it was accessed before.
    3. LRU: Discards the least recently used items first.
    4. MRU: Discards the most recently used items first.
    5. LFU: Counts how often an item is needed. Those that are used least often are discarded first.
    6. Random Replacement: Randomly selects a candidate item and discards it to make space when necessary.

    Reference