liveBook · Manning

Caching

As explained in Advanced Algorithms and Data Structures

Caching is the technique of storing retrieved data or computed results in faster storage for possible reuse.

Suppose a web service repeatedly fetches social-network posts and computes a daily sentiment summary. Fetching and analysis take time, but a later request may need a result already computed. Caching stores retrieved data or computed results in faster storage so they can be reused instead of fetched or computed again. section 7.1 section 4.7.1

A basic cache grows out of three decisions:

  • Choose reusable results and a key for each one. If the possible inputs are few and requests recur, remembering results can substantially reduce repeated work. section 7.2
  • Check the cache before doing the expensive work. A hit returns stored data; a miss requires fetching or computing it. Expect an initial warm-up period before many results are available. section 4.7.1 section 7.2
  • Set a capacity and decide what to evict when it is full. Fast storage is limited; LRU, MRU, and LFU are replacement choices, and each can struggle with items requested only once. section 4.7.1 section 7.9
Figure 7.2 With the introduction of cache, before issuing a call to the app server, the web server checks if the results have already been computed, and in that case skips steps 3–6 in this figure. Note that cache can be added also for the single loaders, in case we allow partial results (for instance, if one of the external services is down). In theory, it can also be added to the “data cruncher” instead of the web server, delegating the interface with the data gatherer to the data cruncher, so that the web server directly calls only the data cruncher. Having cache on the web server (physically on the same machine), however, has several advantages.

The diagram places a cache check before the web server requests data and computation. On a hit, it skips those downstream steps; it also shows other possible locations for the cache. section 7.2

What fits depends on what is stored. Sentiment values for at most 100 companies may need little memory, while raw posts for those companies could take gigabytes. At larger scale, caches can run inside an application, in a separate process, or across distributed nodes. Web applications may also cache at CDN, web, application, and database layers. section 7.3 section 7.8

A cache can begin as an associative array, but capacity limits, replacement, and shared access make it a more involved component. Sharing one across threads calls for care: race conditions, inconsistent data, and deadlocks are possible. The useful trade-off is faster repeated access at the cost of storage and cache management. section 7.0 section 7.9

How Advanced Algorithms and Data Structures covers it

How deep it goes
Develops caching from remembered results through capacity and replacement choices, then considers deployment and concurrency rather than treating it as one container.
Examples
Uses a social-network sentiment service, cached database work, and application-scale memory demands to show where reuse helps and capacity matters.
What it assumes
Assumes familiarity with lists, queues, and hash tables while abstracting HTTP details to focus on caching.
What you can do after
Evaluate/understand when to cache reusable results and how capacity, eviction, placement, and shared access affect the design.
How it connects
Connects caching to associative arrays, LRU and other eviction policies, distributed caches, and concurrency.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.