Redis, on disk

Redis keeps its data in memory and writes it to disk in two ways: an RDB snapshot of every key at one moment, and an append-only file (AOF) of the commands since. Both are read back only on startup. Every example here is read from the sample, files written by Redis 8.12.0: a music catalogue of 559 keys of every type.

The RDB file

A snapshot is a single stream, written from start to end by SAVE or a forked BGSAVE: REDIS and a four-digit version, then records, then a checksum. A record starts with one byte. Values 250 and up are opcodes: an aux field (the Redis version, the time, memory used), the database that follows, a hint of its size, a function library, a module's own data; or, just before a key, its expiry and how often it is used. Anything smaller is a value type, followed by the key and the value. There is no index and no page structure: to find one key, read the file to it.

OffsetPartBytes
0magic REDIS and version 17, as ASCII52 45 44 49 53 30 30 31 37
9aux field (opcode 250): redis-ver = 8.12.0fa 09 72 65 64 69 73 2d 76 65 72 06 38 2e 31 32 2e 30
27aux field (opcode 250): redis-bits = 64fa 0a 72 65 64 69 73 2d 62 69 74 73 c0 40
41aux field (opcode 250): ctime = 1796261633fa 05 63 74 69 6d 65 c2 01 c7 10 6b

Lengths and strings

Every length is in as few bytes as fit: the top two bits of the first byte say how many. 00: the length is the other 6 bits; 01: 14 bits across two bytes; 10: 32 or 64 bits that follow, big-endian. 11 marks a string stored specially: an integer in 1, 2 or 4 bytes (the counter counter:plays is c1 40 06, 1600), or LZF-compressed, which Redis tries for strings over 20 bytes: the 2,960-byte album:50:liner takes 112.

Small collections: listpacks

A small hash, list, set or sorted set is one listpack: a block of entries, each its encoding byte, its data and its own length again (so it can be walked from the end), stored as one string. The file holds the block exactly as it sits in memory, so loading it is a copy. Integers take as little as one byte. A hash's listpack alternates fields and values. artist:7, 72 bytes in all:

OffsetPartBytes
70311value type 16: hash listpack10
70312key: length 8, "artist:7"08 61 72 74 69 73 74 3a 37
70321the value: a string of 59 bytes holding the listpack3b 3b 00 00 00 08 00 84 6e 61 6d 65 05 88 41 72 74 69 73 74 20 37 09 87 …
OffsetPartBytes
0total bytes: 593b 00 00 00
4entries: 808 00
66-bit length string: "name", then its length84 6e 61 6d 65 05
126-bit length string: "Artist 7", then its length88 41 72 74 69 73 74 20 37 09
226-bit length string: "country", then its length87 63 6f 75 6e 74 72 79 08
316-bit length string: "NG", then its length82 4e 47 03
356-bit length string: "genre", then its length85 67 65 6e 72 65 06
426-bit length string: "pop", then its length83 70 6f 70 04
476-bit length string: "formed", then its length86 66 6f 72 6d 65 64 07
5513-bit integer: 1967, then its lengthc7 af 02
58end of listpackff

When a collection grows past a limit (by default 512 fields for a hash, 128 members for a set or sorted set, or any value over 64 bytes), Redis converts it to a hash table or a skiplist in memory, and the file stores its elements one by one instead: prices:all has 600 fields, stored as type 4, hash.

Integers: intsets

A set of integers only is an intset: a width (2, 4 or 8 bytes), a count, and the integers sorted. years holds 50 years in 108 bytes, 2 bytes each.

Lists: quicklists

A long list is a chain of listpacks, a quicklist; the file stores each node as its own string, and nodes Redis compressed in memory (list-compress-depth, which leaves the ends uncompressed) are written still LZF-compressed. queue:plays holds 1,604 items in 14 nodes, 13 of them compressed.

Hash templates

New in Redis 8.12, and off by default: many hashes with the same fields can share their field names. The file then writes each template once (opcode 242), before the keys, and each hash as its template's number and its values. The sample turns it on for the albums: hash template 0: year, price, title, artist_id; album:7 is 42 bytes against 72 for an artist, which carries its field names.

Expiry

A key's expiry comes before it: opcode 252 and the time in Unix milliseconds, 8 bytes, little-endian (11 keys in the sample). Since Redis 7.4 a hash's fields can expire on their own, and in Redis 8.12 a set's members can too: such a value starts with the earliest expiry, then stores each field's own. In a listpack each field gets a third entry, its expiry or 0 (artist:1's on_tour field expires in a month); in a hash table each field is preceded by its expiry minus the earliest, plus one, or 0 for none (promo:codes, type 24). With an LFU eviction policy each key also carries a one-byte use counter (opcode 249; 559 keys here).

Streams

A stream is a radix tree of listpacks in memory. The file stores each listpack with the ID it starts from, then the stream's counters, then its consumer groups. Each listpack starts with a "master entry", the field names of its first entry; entries with the same fields store only their values, and every ID is stored as its difference from the listpack's. Deleting an entry only flags it. plays has 397 entries in 4 listpacks (3 deleted but still there) and 2 consumer groups, whose unacknowledged entries are stored with each one's delivery time and count: archiver, 0 pending; scrobbler, 17 pending.

Bitmaps, arrays and modules

A bitmap is normally a string. With bitmap-default-roaring (Redis 8.12) it becomes a roaring bitmap, stored in CRoaring's portable form: listened:2026-12-01 has 6 bits set, the highest at 100,000,000: a 11.9 MB string, stored in 80 bytes. A run of 5,001 bits becomes a single run container. Arrays, also new, store only the indexes that hold something, each value tagged as an integer, a double or a string (ratings: 7 elements up to index 100,000).

A module's value (the sample has vectorset, TSDB-TYPE, ReJSON-RL, MBbloom--) is written by the module itself, but through Redis's API, which tags each item: a signed or unsigned integer, a float, a double or a string, then an end marker. So a reader that does not know the module can still split it into values. album:1:json is 1 value from module type ReJSON-RL, encoding version 4. The type is named by a 64-bit ID: nine characters of six bits each, and ten bits of version.

The end

After the last key comes opcode 255 and a CRC-64 of everything before it, 8 bytes; 0 means the check was turned off. Function libraries (Lua code loaded with FUNCTION LOAD) are saved before the data, as their source.

The append-only file

The AOF records every write command, in RESP, the protocol clients use. Since Redis 7 it is a directory: a base file, written when the AOF is rewritten (an RDB snapshot by default), one or more incremental files of commands since, and a manifest naming them in order. The sample's:

file appendonly.aof.2.base.rdb seq 2 type b
file appendonly.aof.2.incr.aof seq 2 type i startoffset 11043

Each command is * and its argument count, then each argument as $, its length and its bytes, lines ending in CR LF. The first command after the SELECT:

*6\r\n
$4\r\n
HSET\r\n
$8\r\n
artist:2\r\n
$7\r\n
country\r\n
$2\r\n
NO\r\n
$6\r\n
formed\r\n
$4\r\n
1999\r\n

Commands are not logged as typed but as their effect, so that replaying them gives the same data at any later time: relative expiries become absolute (SET session:xyz user:9 PXAT 1797125633696, PEXPIREAT album:198 1797125633696, SET cache:album:11 Album 11 PXAT 1798261633699, HPEXPIREAT artist:6 1799261633700 FIELDS 1 genre), INCRBYFLOAT becomes a SET of the result with KEEPTTL, a random SPOP becomes an SREM of what it took, and a script's writes are logged as the commands it ran, wrapped in MULTI/EXEC. On startup Redis loads the base, then runs each incremental file's commands. The sample's incremental file has 33 commands, written after the snapshot; the viewer replays them over the base and arrives at what Redis held, except for 1 module command it does not run, whose key it marks.

Reading it back

The viewer reads RDB files of any version since Redis 2.6 and every type Redis 8.12 writes, checks the CRC-64, and replays an AOF over its base. On the sample, every key it reads matches what Redis returned, from the snapshot and from the AOF.