Skip to the workspace
Pebellslearn by building
What is Redis?  ›  Lesson

What Redis Is Used For

Caching 12 minRedis is most often hired as a cache, a session store, a counter and leaderboard engine, and a lightweight message broker, because all of these need tiny, very fast reads and writes.

redis / What Redis Is Used For

Explore each scene at your own pace.

01 · The rule

The rule

What Redis Is Used For

Wordcraft's profile page runs three joins in PostgreSQL and takes 80 milliseconds. Thousands of players open it every minute, and almost nobody's profile changes in that time. Doing the same slow work again and again is the kind of waste Redis is usually hired to remove, and caching is only the first of its jobs.

The rule

Small, hot, fast-changing data goes to Redis.

Every common use of Redis has the same shape: many tiny reads or writes that must be quick. The usual jobs:

  • Cache. Keep the result of slow work (a query, an API call, a rendered page) under a key with an expiry. Read Redis first; only on a miss do the slow work and store the result.
  • Session store. A logged-in player's session is a key such as session:9f2c with a lifetime. Every request checks it, and Redis deletes it on its own when it expires.
  • Counters and rate limits. INCR adds one safely even when many servers count at once: page views, games played, login attempts per minute.
  • Leaderboards. A sorted set keeps members ordered by score, so "top ten" or "my rank" is one command.
  • Message broker. With publish/subscribe (pub/sub), one client publishes a message to a channel and every client listening on it receives it at once, which suits live chat and notifications. Lists and streams can also act as job queues between services.
  • Real-time analytics. Counters, sets of unique visitors and time-bucketed keys give numbers that update as events happen, not in tomorrow's report.

Here is the cache pattern for the profile page:

GET cache:profile:asha            # (nil) on the first visit: a miss
SET cache:profile:asha "{...}" EX 60
GET cache:profile:asha            # "{...}" for the next 60 seconds: a hit

EX 60 gives the key a 60-second lifetime, so a stale copy cleans itself up.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.