LearnThatStack Ace your next interview
API Design · question
Question 29 of 92

What caching strategies can you apply to APIs?

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

HTTP caching: infrastructure you do not own, working for you

Declare freshness with Cache-Control and identity with ETag, and caches serve your traffic. The hard part is invalidation.

For this question · all 3 chapters

1/3 Freshness keeps the request home
cache-controlFreshness keeps the request homethe fastest request never leavesBrowser Astored again/products60s/me45spage loads6requests sent3Browser B200 from CDNCDNstored again/products20sOriginpublic, max-age=60, s-maxage=20
cache-controlFreshness keeps the request homeBrowser Astored again/products60s/me45sloads6sent3Browser B200 from CDNCDNstored again/products20sOriginpublic, max-age=60, s-maxage=20

Freshness keeps the request home

  1. Browser A sends GET /products request, and the server responds 200 with Cache-Control: max-age=60. That header is a promise with a time limit: this answer remains true for 60 seconds. The server owns this header field as only the server knows how long its own data remains fresh. And only safe methods get the header in the response, so methods like POST don't get it.
  2. The browser stores the reply in its cache, and a countdown starts at 60 seconds. The client now holds the copy and its countdown, and nothing on the server side records that this copy exists. The max-age value is seconds, counted from the moment the server send the response.
  3. The next three page loads never reach the server, because the browser's own cache can answer these. Those three page loads sent zero requests. So four page loads have cost one request in total. A cache hit is the fastest response an API can give.
  4. A CDN is now placed between browsers and the server, and browser B sends its first request. The CDN fetches from the server once, and because the answer is public, the CDN can keep a copy in a shared cache. The s-maxage=20 directive gives that shared cache its own freshness limit of 20 seconds, this is separate from the browser's 60 seconds. One request to the server can now serve a city, and the server only handles one request instead of thousands.
  5. Browser A asks for /me, its own account page, and the server marks the response as private. The CDN passes the answer through and don't cache anything, because only that user's browser is allowed store a private answer. Anything personal has to be private, or a CDN hands one user's page to the next. The stricter option is no-store, which means never store the answer at all. This is different from no-cache rule which means always check first.
  6. Both timers, the browser's and the CDN's, reach 0, so both cached copies are now stale. The next GET /products request leaves the browser again, misses at the CDN because the CDN's copy is stale too, and reaches the server.
  7. The server reply again. The CDN and browser A both store the new reply, and restart their clocks: 20 seconds at the CDN, 60 seconds in the browser. And the new reply has full body for data that may not have changed.
Chapter 1 · step 1 of 7

Browser A sends GET /products request, and the server responds 200 with Cache-Control: max-age=60. That header is a promise with a time limit: this answer remains true for 60 seconds. The server owns this header field as only the server knows how long its own data remains fresh. And only safe methods get the header in the response, so methods like POST don't get it.

1 of 7 Use ← → or swipe See the concept

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime