Storage layer #
Under the command handlers sits ledis: a data model that turns five Redis types into key/value
operations over a single driver.IDB. It is the reason the same commands work on a local LevelDB file
and on IPFS.
Per-type keyspaces #
ledis does not store “a key with a type”. It stores separate physical keys per type, each prefixed with a type byte:
| Type | Prefix bytes | Physical keys per logical key |
|---|---|---|
| String | KVType | the value |
| Hash | HashType, HSizeType | one entry per field + a size counter |
| List | ListType, LMetaType | one entry per index + a meta record (head/tail/size) |
| Set | SetType, SSizeType | one entry per member + a size counter |
| Sorted set | ZSetType, ZSizeType, ZScoreType | one entry per member + size counter + one index entry per (score, member) |
Expirations are separate records too (ExpMetaType, ExpTimeType), which is why a TTL belongs to the
type it was set on rather than to the key name.
Consequences you can observe from the client side:
- Two types can coexist at the same name (see Compatibility).
SCANhas to walk five keyspaces; its cursor carries five sub-cursors.- Deleting a hash (
HCLEAR) removes many physical keys;DELremoves only the string’s.
Physical key encoding #
Composite keys are length-prefixed and separator-delimited rather than hashed:
hash: [index][HashType][uint16 keyLen][key][':'][field]
list: [index][ListType][uint16 keyLen][key][uint32 index]
zset: [index][ZScoreType][uint16 keyLen][key][sep][score][member]
sep for sorted sets is one of two bytes depending on the score’s sign, so the byte order of the index
matches numeric score order — that is what makes ZRANGEBYSCORE a range scan instead of a full sort.
Member entries are sorted by encoded key, so SMEMBERS/HKEYS order is storage order, not insertion
order.
Limits enforced here, not in the handlers #
| Limit | Value | Error |
|---|---|---|
| Key size | 1024 bytes, non-empty | invalid key size |
| Hash field size | 1024 bytes, non-empty | invalid hash field size |
| Set member size | 1024 bytes | invalid set member size |
| Sorted-set member size | 1024 bytes | invalid zset member size |
| Value size | 1 GiB | invalid value size |
| Expiry timestamp in the past | — | invalid expire value |
These come from vendor/github.com/ledisdb/ledisdb/ledis/const.go; changing them means changing ledis.
Note that they are much tighter than Redis’ 512 MB values and effectively unlimited keys — a 1 KiB key
budget is the constraint most applications hit first.
Expiry #
- Expirations are stored in whole seconds and keyed per type.
- Timestamps are computed from the Raft clock by the handler and validated by ledis against the local
clock; a timestamp
<= nowis rejected. - Expired keys are reaped lazily/asynchronously, so a logically expired key can still be observed by an existence check on another replica. See Compatibility.
TTL/HTTL/LTTL/STTLsubtract the local clock from the stored timestamp and return-1both for “no expiry” and “already expired”.
Backends and durability #
The driver contract is deliberately small (see Storage backends). Above it:
- Writes go through ledis and then the driver’s
Put/WriteBatch;--nosyncdisables the per-write disk sync in the Raft log layer, not in the driver. - Compaction is delegated to the driver (
Compact()); forgoleveldb/badgerthat is normal LSM compaction, forcrdt/ipfs-logit is a no-op. - Snapshots iterate the driver and stream key/value pairs in
sdsformat inside a gzip container, so they are backend-independent — see Operations.
Why the design holds together #
The command layer never assumes a specific store. It asks ledis for operations (HSet, LPush,
ZRangeByScore, …), and ledis maps them onto prefixed key operations. Everything that makes the engine
a database — structures, ordering, expiry — lives in that mapping, so a new backend only has to be a
correct ordered KV store.
The cost is at the edges: no cross-type atomicity, O(n) list rebuilds, name-based scan cursors, and the 1024-byte key budget. Those are documented as limitations rather than papered over.