Why is Redis so fast, and what do you give up for it?
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.
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).
GET session:42.For your app: very fast reads and writes for data you can afford to rebuild or lose a second of.
KEYS * on a big instance is the classic outage.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.
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 shardingType 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.
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 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.
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.
The same mechanism, caches and memory, what a commit promises, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. With appendfsync everysec, what can a power failure lose?
Redis flushes the AOF about once a second in the background, so writes acknowledged in that second can be lost.
2. Why can one slow command slow down every client?
Commands run one after another on the main thread, which is also why each command is atomic.
3. Memory reaches maxmemory and the policy is noeviction, the default. What happens to a SET?
noeviction refuses writes that need memory. For a cache, an LRU or LFU policy evicts instead.
4. Why can a snapshot on a busy 40 GB instance need much more than 40 GB?
Copy-on-write: the more the parent writes during the save, the more pages are duplicated.
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.