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.
| Offset | Part | Bytes |
|---|---|---|
| 0 | magic REDIS and version 17, as ASCII | 52 45 44 49 53 30 30 31 37 |
| 9 | aux field (opcode 250): redis-ver = 8.12.0 | fa 09 72 65 64 69 73 2d 76 65 72 06 38 2e 31 32 2e 30 |
| 27 | aux field (opcode 250): redis-bits = 64 | fa 0a 72 65 64 69 73 2d 62 69 74 73 c0 40 |
| 41 | aux field (opcode 250): ctime = 1796261633 | fa 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:
| Offset | Part | Bytes |
|---|---|---|
| 70311 | value type 16: hash listpack | 10 |
| 70312 | key: length 8, "artist:7" | 08 61 72 74 69 73 74 3a 37 |
| 70321 | the value: a string of 59 bytes holding the listpack | 3b 3b 00 00 00 08 00 84 6e 61 6d 65 05 88 41 72 74 69 73 74 20 37 09 87 … |
| Offset | Part | Bytes |
|---|---|---|
| 0 | total bytes: 59 | 3b 00 00 00 |
| 4 | entries: 8 | 08 00 |
| 6 | 6-bit length string: "name", then its length | 84 6e 61 6d 65 05 |
| 12 | 6-bit length string: "Artist 7", then its length | 88 41 72 74 69 73 74 20 37 09 |
| 22 | 6-bit length string: "country", then its length | 87 63 6f 75 6e 74 72 79 08 |
| 31 | 6-bit length string: "NG", then its length | 82 4e 47 03 |
| 35 | 6-bit length string: "genre", then its length | 85 67 65 6e 72 65 06 |
| 42 | 6-bit length string: "pop", then its length | 83 70 6f 70 04 |
| 47 | 6-bit length string: "formed", then its length | 86 66 6f 72 6d 65 64 07 |
| 55 | 13-bit integer: 1967, then its length | c7 af 02 |
| 58 | end of listpack | ff |
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.