liveBook · Manning

Caching

As explained in The Art of Code

A cache is a storage layer that retains recently or frequently used results for faster retrieval and reduced dependency load.

Caching keeps recently or frequently used results available in a faster storage layer. Its central purpose is to avoid unnecessary work: serve a result locally when it is still suitable instead of repeatedly calling a remote endpoint, database, or other dependency. Used well, it lowers request overhead, improves responsiveness, and reduces dependency load; during a partial outage, it can preserve performance by supplying a recent result. section 8.4.4 section 9.2

Choose the cache tier to match how quickly the data changes. HTTP caching with Cache-Control headers can let a browser or intermediary reuse a response without reaching the server. The same principle can extend across in-memory browser, CDN, server-side, and database caches. Each tier needs clear expiration rules: cached data is useful only while its age and update rate remain acceptable for the feature. section 8.4.4

Treat caching as a tradeoff between fewer remote calls and controlled retention:

  • Cache data that is read repeatedly or frequently, especially when a remote call has fixed network overhead beyond its payload. section 8.4.4
  • Match expiration to change frequency rather than treating all data alike. section 8.4.4
  • Avoid allowing caches to become long-lived, oversized structures that retain more memory than the application actively needs. section 8.4.6
  • Combine local retention with offline-first behavior when appropriate: keep recently used data and record actions locally, then synchronize in the background. section 8.4.4
  • Use a cache as one resilience measure, not as a replacement for failure isolation, time limits, or graceful degradation. section 9.2

Profile lookup before database access

A simple cache lookup tells a readable story: first check whether a profile is already retained; if so, return it. Otherwise load the profile from the database, store it, and return it. This removes repeated database work for repeated requests, but the static map also makes retention a design concern: a cache needs a policy that prevents it from growing beyond useful data. section 8.4.6

  private static final Map<String, UserProfile> CACHE = new HashMap<>();

  public static UserProfile getUserProfile(String userId) {
    if (CACHE.containsKey(userId)) {
      return CACHE.get(userId);
    }
    UserProfile profile = loadProfileFromDatabase(userId);
    CACHE.put(userId, profile);
    return profile;
  }

The listing makes the cache-as-fast-path pattern concrete: a map lookup returns an existing profile before the database loader runs, and a miss stores the loaded result for later reuse. section 8.4.6

Caching is most valuable when it is intentional. Reduce calls first by designing endpoints around the information a page needs; then cache at the layer that can safely reuse that information. Prefer a bounded, expiring cache over retaining data indefinitely, and weigh freshness requirements against the cost of another dependency call. section 8.4.4 section 8.4.6

How The Art of Code covers it

How deep it goes
Explains cache tiers, expiration choices, and memory-retention tradeoffs without detailing eviction algorithms or implementation internals.
Examples
Illustrates browser or intermediary HTTP reuse and a Java map that loads a user profile only on a cache miss.
What you can do after
Apply it by selecting an appropriate cache tier, setting expiration rules, and limiting unnecessary retained data.
How it connects
Connects caching with HTTP caching, Cache-Control, CDNs, offline-first design, databases, and resilience patterns.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.