Home›Learn›Redis 1: why Redis
Course · ScaleLesson 16 of 330 read

Redis

Why is Redis so fast, and what do you give up for it?

Explained clearly

What Redis is, and why it exists

1

The problem

Some data is read thousands of times a second: a session, a cart, a counter, a rate limit. Asking the main database every time costs a disk read, a query plan and a connection for something that never needed any of them.

2

What it is

Redis is a key-value store that keeps everything in memory. Values are data structures, not just strings: lists, hashes, sets, sorted sets, streams. One main thread runs every command, one at a time, so each command is atomic. Disk is only for getting the data back after a restart: snapshots (RDB) and an append-only file (AOF).

3

Follow one request

  1. The app sends GET session:42.
  2. The main thread looks the key up in memory. No disk, no query plan.
  3. The answer comes back in well under a millisecond on a local network.
  4. On a write, the change is also appended to the AOF buffer, flushed to disk once a second with the usual setting.

For your app: very fast reads and writes for data you can afford to rebuild or lose a second of.

4

What to watch

  • Memory is the dataset. When it is full, the eviction policy decides what goes, or writes fail.
  • One thread means one slow command stalls everyone. KEYS * on a big instance is the classic outage.
  • Persistence is a choice. With appendfsync everysec, a crash can lose about the last second of writes.

This page is about an in-memory data structure server. I show you where Redis keeps your data (memory first, disk second), the two ways it saves to disk, and why one busy key or one big key can slow everything down. After it you will be able to pick a persistence setting, size its memory, and find the key that is hurting you.

Redis: in one line

A data structure server that keeps everything in memory: strings, hashes, lists, sets, sorted sets and streams, each with its own commands.

Good at: Answers in a fraction of a millisecond. Counters, sessions, rate limits, leaderboards, small queues and caches.

Runs as: one redis-server process per node; Sentinel or Redis Cluster for failover and sharding
Where it fits in a design

Fast, small and rebuildable: that is where Redis belongs

Use it for

Not for

  • The only copy of money or orders: Payments and ledgers
  • Datasets bigger than memory
  • Queries across many keys, joins or reports
  • Anything where every acknowledged write must survive a failover
Try it

A Redis console in your browser

Type commands as you would in redis-cli. Pick an example to see a session store, a leaderboard and a rate limit built from a handful of commands, then change them.

You run KEYS * on a Redis with 50 million keys. What happens to other clients while it runs?

They wait. KEYS walks the whole keyspace on the one main thread, so every other command queues behind it, often for seconds. SCAN returns a few keys per call and lets other commands run in between.

Your turn

Your sessions live in Redis with appendfsync everysec and one replica. The primary's machine loses power and the replica is promoted. What do users notice? Is that acceptable for sessions? For shopping carts?

Write your answer down first. Then open mine.

Show my answer

What they notice: writes from about the last second can be gone, and more if the replica was lagging, because replication is asynchronous. Some people who just logged in are logged out; some last cart changes are missing.

Sessions: acceptable. The cost is a re-login.

Carts: annoying but survivable if the cart is rebuilt or the order is written to the relational database at checkout. Never keep the only copy of an order in Redis.

If it is not acceptable: use WAIT for the writes that matter, which narrows the window but does not close it, or keep that data in a database that waits for the disk and a standby.

Next · chapter 2How it stores your dataMemory, one thread, and two ways to disk
Connected

Where this shows up

The same mechanism, caches and memory, what a commit promises, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. With appendfsync everysec, what can a power failure lose?

2. Why can one slow command slow down every client?

3. Memory reaches maxmemory and the policy is noeviction, the default. What happens to a SET?

4. Why can a snapshot on a busy 40 GB instance need much more than 40 GB?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect