Home›Learn›Redis 2: how it stores your data
Redis · Chapter 2

How Redis stores your data

Where does a key live, and what survives a restart?

Memory first, one thread running commands, and two ways to put it on disk. Then one request, end to end.

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.

Next · chapter 3High availabilitySentinel and Redis Cluster← Chapter 1: Why Redis
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