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

An In-Memory Data Structure Store

In-memory Data Structure Store 12 minRedis keeps every key and its value in RAM, and each value is a ready-made data structure such as a string, list, hash, set or sorted set.

redis / An In-Memory Data Structure Store

Explore each scene at your own pace.

01 · The rule

The rule

An In-Memory Data Structure Store

Wordcraft is a small online word game. Its player profiles live in a normal SQL database, but every screen also shows a live score, a visit counter and a top-ten board. Asking the main database for those numbers hundreds of times a second is slow and wasteful. Redis is the tool built for exactly that kind of tiny, constant question.

The rule

Every key lives in RAM, and every value is a ready-made structure.

The rule has two halves.

In memory. A classic database keeps its data on disk and pulls pages into memory when needed. Redis turns that around: the whole dataset sits in RAM all the time. Reading from memory takes well under a microsecond, while even a fast SSD needs tens of microseconds, so most Redis commands finish in a tiny fraction of a millisecond. The cost is size: everything must fit in the server's memory.

Data structures. Redis is a key-value database: every piece of data has a name, the key, and you always reach it through that name. What makes Redis different from a plain key-value box is the value. It is not just a blob of bytes but one of several structures Redis understands:

SET wordcraft:visits 41                 # string
HSET player:asha name Asha level 6      # hash: named fields
LPUSH feed:asha won                     # list: ordered items
SADD badges:asha first-win              # set: unique members
ZADD leaderboard 320 asha               # sorted set: members with scores

Because Redis knows the shape, it can work inside the value: add one to a counter, append to a list, or return the top three scores, without sending the whole value back to your app first.

Not only a cache. Memory is wiped when a server restarts, so Redis can also write its data to disk: a periodic snapshot (RDB) or a log of every write (AOF, the append-only file). Persistence is optional, and it is a safety net rather than the place Redis reads from.

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