Skip to content
Ameen Abozaid
MID-037

Caching fundamentals

  • 5 Lessons
  • 1.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Local vs distributed
  • hit ratio
  • eviction
  • TTL

Learn first

1.5 h of practice (estimate)

Learning outcomes

  1. Explain where caches sit (client, CDN, gateway, application, database) and what each saves.
  2. Compare local and distributed caches.
  3. Reason about hit ratio, eviction policies (LRU, LFU), and TTL.
  4. Decide whether a given endpoint should be cached at all.

Lessons

  1. 01Why and where to cache
  2. 02Local vs distributed caches
  3. 03Hit ratio and what it hides
  4. 04Eviction and TTL
  5. 05Demo: a local Caffeine cache with metrics

Lab and exercises

Lab

MID-037-LAB1 · Cache candidates

Outcomes: 1–4 · Time: ~1.5 h Collect access data for Shop's endpoints (request counts, latency, data change rate) from logs or metrics. Rank them and pick the three worth caching, with an expected hit ratio, TTL, and layer for each. Also list two endpoints you reject and why. Acceptance: decision backed by access data (a table of numbers, not opinions).

Exercises (assessment: quiz)

IDOutcomeTask
MID-037-EX011For GET /products/{id}, name each layer where it could be cached and what a hit there saves.
MID-037-EX022Shop runs 4 instances. Compare Caffeine and Redis for the category list; pick one and justify.
MID-037-EX0331000 requests: 700 hits, 300 misses. Compute the hit ratio and the database calls saved. A miss costs 50 ms, a hit 1 ms: compute average latency.
MID-037-EX043A 3-slot LRU cache receives A B C A D B. Show the contents after each step.
MID-037-EX054Decide for each: /cart, /products/{id}, /stock/{id} during a flash sale, /categories. Cache or not, and with what TTL.
Show solutions
  • EX01 Client (Cache-Control), CDN (static and public product JSON), gateway (short TTL responses), application (Caffeine/Redis: avoids DB), database (buffer pool).
  • EX02 Redis: shared, one invalidation point, survives restarts. Caffeine would be fine for a rarely changing list if a few minutes of drift is acceptable. Either with a stated tolerance is a valid answer.
  • EX03 Ratio 70%; 700 database calls saved. Average = 0.7 × 1 + 0.3 × 50 = 15.7 ms.
  • EX04 After A B C: [A B C]. A hit. D evicts B: [C A D]. B miss, evicts C: [A D B].
  • EX05 /cart per-user and changing: no shared cache. /products/{id}: yes, minutes. /stock/{id} during a flash sale: no, or a very short TTL with the database as authority. /categories: yes, long TTL.

Outcomes 1-4 are also covered by the quiz and the lab criterion "decision backed by access data".

Quiz

Outcomes: O1 where caches sit · O2 local vs distributed · O3 hit ratio, eviction, TTL · O4 should we cache.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Which cache layer avoids sending any request to the server?O1
  2. 2A CDN mainly saves:O1
  3. 3A downside of a local cache with several instances:O2
  4. 4A benefit of a distributed cache:O2
  5. 5800 hits and 200 misses give a hit ratio of:O3
  6. 6Two caches have a 90% hit ratio. What decides which is more valuable?O3
  7. 7LRU evicts:O3
  8. 8TTL provides:O3
  9. 9Which data is a poor candidate for a global shared cache?O4
  10. 10Before adding a cache you should first:O4

Practical part

Lab MID-037-LAB1 (three cache candidates backed by access data). Pass: quiz at least 8/10 and a lab where every decision cites measured numbers.

Common mistakes

Caching before measuring; caching personalized or rapidly changing data globally.

Assessment

Quiz.

Career relevance

Every high-traffic service.

Type to search levels, phases, modules and lessons.