Storage

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:

TypePrefix bytesPhysical keys per logical key
StringKVTypethe value
HashHashType, HSizeTypeone entry per field + a size counter
ListListType, LMetaTypeone entry per index + a meta record (head/tail/size)
SetSetType, SSizeTypeone entry per member + a size counter
Sorted setZSetType, ZSizeType, ZScoreTypeone 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).
  • SCAN has to walk five keyspaces; its cursor carries five sub-cursors.
  • Deleting a hash (HCLEAR) removes many physical keys; DEL removes 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 #

LimitValueError
Key size1024 bytes, non-emptyinvalid key size
Hash field size1024 bytes, non-emptyinvalid hash field size
Set member size1024 bytesinvalid set member size
Sorted-set member size1024 bytesinvalid zset member size
Value size1 GiBinvalid 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 <= now is 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/STTL subtract the local clock from the stored timestamp and return -1 both 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; --nosync disables the per-write disk sync in the Raft log layer, not in the driver.
  • Compaction is delegated to the driver (Compact()); for goleveldb/badger that is normal LSM compaction, for crdt/ipfs-log it is a no-op.
  • Snapshots iterate the driver and stream key/value pairs in sds format 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.