Home›Learn›Redis
Course · ScaleLesson 16 of 330 read

Redis

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

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

The architecture: one thread, memory, and a disk on the side

Every command runs on one main thread, in memory. Disk is only for getting the data back after a restart; nothing is read from disk while serving.

The same layers as a still diagram, with every part named:

Redis: architecture

Request path

Processredis-server · one main thread runs every command in order · I/O threads can help with the network (6.0 and later) · a forked child writes snapshots
→
Memoryevery key and value, in a hash table · small values in compact encodings (listpack) · large sorted sets as a skiplist plus a hash
→
Disk, on the sidedump.rdb snapshots · appendonlydir/ AOF files (7.0 and later) · read only at startup
Who does what: The main thread runs commands one at a time, so every single command is atomic, and one slow command delays all the others. Saving happens beside it: a forked child writes RDB snapshots and rewrites the AOF, and a background thread flushes the AOF to disk.
Where the data lives

Memory, two kinds of file, and a buffer for replicas

PlaceWhat it holdsWhat to watch
Memory, the datasetevery key and valueused_memory against maxmemory. When it is full, maxmemory-policy decides: refuse writes (noeviction, the default) or evict keys.
dump.rdba point-in-time snapshot, written by a forked child (BGSAVE)While the child writes, every page the parent changes is copied, so a busy server can need much more memory during a save. latest_fork_usec shows how long the fork took.
appendonlydir/ (the AOF)every write command, appendedappendfsync: always, everysec (the default) or no. The AOF is rewritten in the background when it grows.
Replication backloga ring buffer of recent writes, for replicas that reconnectToo small, and a replica that drops for a moment needs a full resync: a fork and a copy of everything.
Follow it

Follow one write, and one power cut

The usual setup: the AOF on, flushed once a second. Step through it, then tick the box.

A SET got OK from Redis with appendfsync everysec. Is it on disk yet?

Not necessarily. Redis replies before the AOF is flushed, and the flush happens about once a second in the background. Tick the box in the widget to see what a power failure in that second does.

The settings that matter

Six settings decide what Redis is safe for

SettingDefaultWhat it trades
appendonlynoOff: only snapshots, so a crash loses everything since the last one. On: every write is logged.
appendfsynceverysecalways: a disk flush per write, safest and slowest. everysec: up to about a second of loss. no: the operating system decides, often about 30 seconds.
save3600 1 300 100 60 10000When RDB snapshots run: after an hour if 1 key changed, 5 minutes if 100, a minute if 10,000.
maxmemory0, no limitAlways set it, below the machine's memory, leaving room for the fork during a save.
maxmemory-policynoevictionFor a cache: allkeys-lru or allkeys-lfu. For data you must keep: noeviction, and an alert on memory.
WAIT and min-replicas-to-writenot usedWAIT makes one client wait until replicas have a write; min-replicas-to-write makes a primary refuse writes without replicas. Both narrow the loss window; neither makes Redis strongly consistent.
What breaks first

The failures I would plan for

What happensWhat you seeWhat I do
A big keyOne DEL or HGETALL on a hash with millions of fields blocks every client for hundreds of millisecondsFind it with redis-cli --bigkeys and MEMORY USAGE. Split it into smaller keys. Delete big keys with UNLINK, which frees memory in the background.
A hot keyOne shard at 100% CPU while the others idleA key lives on one shard and one thread. Spread it over several keys, or count in the application and write once a second.
KEYS * or a long Lua script in productionEverything stalls while it runs; SLOWLOG fills upSCAN instead of KEYS. Keep scripts short.
Memory fullWith noeviction, writes fail with an OOM error. With an eviction policy, keys quietly disappearSet maxmemory and a policy that matches the job. Alert well before the limit.
A fork on a big, busy instanceLatency spikes during snapshots and AOF rewrites; memory jumps; sometimes the out-of-memory killerLeave memory headroom, turn off transparent huge pages, and take snapshots on a replica.
A replica that keeps falling behindFull resync loops: fork, copy everything, fall behind againA bigger repl-backlog-size and client-output-buffer-limit for replicas.
Failover with asynchronous replicationThe newest writes, already acknowledged, are missing after a replica is promotedKeep only data you can rebuild in Redis, or accept and document the window.
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.

Check it yourself

The commands I run first

redis-cli INFO memory           # used_memory, maxmemory, mem_fragmentation_ratio
redis-cli INFO persistence      # rdb_last_bgsave_status, aof_enabled, aof_delayed_fsync, latest_fork_usec
redis-cli INFO replication      # role, connected replicas, offsets
redis-cli CONFIG GET appendfsync
redis-cli --bigkeys             # the biggest key of each type, by sampling
redis-cli MEMORY USAGE cart:42  # bytes for one key
redis-cli SLOWLOG GET 10        # the slowest recent commands
redis-cli LATENCY DOCTOR        # what caused recent latency spikes
Measured on this site

Notebooks 7 and 9, Redis 7.4.12, one client on the same machine

Versions and licences. Redis 7.4 moved to source-available licences (RSALv2 or SSPLv1). Valkey forked from 7.2.4 under the BSD licence in 2024, at the Linux Foundation, and AWS and Google Cloud offer managed Valkey. Redis 8 (2025) added AGPLv3 as an option, bundles JSON, search, time series and probabilistic types, and adds a vector set type. The current line is Redis 8.12 (November 2026). Everything on this page about memory, persistence and replication works the same in Valkey.

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.

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?

Part 1 of 4 · Learn · 23 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.