BE4: Caching with Redis

Lesson 1 of 5 · reading 15–25 minutes; practice and project time is on the module page

What to cache, and what not to

🎯You'll be able to

Decide whether a piece of data is worth caching, from how often it's read, how often it changes, and how much a stale copy would hurt.

🏠The everyday picture

A tea stall keeps a big kettle of chai ready because hundreds of people want the same thing every hour. It doesn't pre-make a customer's special order "no sugar, extra ginger": nobody else will want that one.

📘What it really is

📝A worked example

TiffinBox data, judged for caching:

Data Reads Changes Stale for 5 minutes? Cache?
A kitchen's menu with ratings and 30-day counts thousands an hour a few times a day fine for counts; prices must update at once (Lesson 3) yes, cleared on change
A customer's own order list a few per customer each order confusing ("where's my order?") no
Kitchen capacity left today many at lunch every order could oversell no; read from the database inside the order transaction (BE2)
List of 20 kitchens and areas many rarely fine yes, long expiry

🌍Where it's used

📝Case scenario

A team caches each customer's wallet balance for 60 seconds to lighten the database. A customer pays twice within a minute from two devices; both requests see the cached balance, and both payments go through on money that wasn't there. Balances must be read, and changed, inside a transaction in the database; the cache was the wrong tool.

🖱️Try it

List ten TiffinBox reads (from the kit's routes and your own ideas). For each, decide cache or don't cache, and write the reason. Success: every "don't cache" names the harm a stale copy would cause.

⚠️Common mistakes

🛠️For your project

Project 2 caches the menu endpoint only; the order and wallet-style data in Project 1 stay uncached.

✅Quick check

Name two signs that data is a good fit for caching.

Show the answer

It's read much more often than it changes, and it's expensive to compute.

Why shouldn't checkout read remaining capacity from a cache?

Show the answer

A stale copy could let two customers book the last place; capacity must be checked in the database transaction.

← BackBE4
ProLearners Assist