The Protocol · module 01
The owned keyspace
One module builds every key, to one braced grammar. The braces are the cluster hashtag.
The keyspace is the part of the protocol that says where. Every key EchoMQ writes
has one shape, emq:{q}:<type>: on the host EchoMQ.Keyspace
composes it, and on the server the native module checks it, byte for byte, so both executors address the same
keys. The braces are the cluster hashtag that pins one queue to one slot, and the reserved base
{emq}: holds the wire identity, first-byte-disjoint from any queue. Three dives read it in
depth: the grammar, the slot the brace computes, and the reserve.
The keyspace in three parts
The keyspace is three things, and the three dives of this module are each one of them. Pick a part to read what
it is and the real EchoMQ.Keyspace function that carries it — then take the dive.
The keyspace · pick a part
host: EchoMQ.Keyspace.queue_key/2 → emq:{q}:<type>
native: Queue::from_key — the server-side layout check
one literal shape: the host builds it (queue_key/2, job_key/2); the native module rebuilds a call's keys from one queue name and refuses any mismatch before it writes
Three parts, one grammar: the grammar says where a key lives, the hashtag says which slot it lands on, and the reserve says where the wire's own state goes — beside every queue, owned by none.
One keyspace, two executors
The protocol runs its transitions on two executors, and both address the same keys. The scripted
executor runs the inline Lua, EVALSHA-first, against the keys the host built with
EchoMQ.Keyspace. The native module, trafikey, is
a Valkey module where each core job transition is one EMQ.* command: it receives the
same keys as declared arguments, recovers one queue name from them, rebuilds the call's other keys, and refuses
the call when a key differs by a single byte.
Cluster-legal holds for the keyspace and the scripts: one hashtag per queue puts a script's keys, and the row
it derives from the declared base, on one slot. The scripted connector itself holds one connection to one server
and does not route by slot. The native module serves standalone servers
only, a limit the canon records: every native write also reads {emq}:version,
a key on another slot. A cluster-enabled server refuses every EMQ.* write with
EMQWIRE expected a standalone server but cluster mode is enabled.
- the host
EchoMQ.Keyspacebuilds every declared key;valid_queue!/1refuses a brace or NUL in a queue name at the admission verbs (Jobs.enqueue/4,Jobs.enqueue_many/4,Lanes.enqueue/5), before any wire.- the scripts
- run against those keys;
@claim2derives the row from the declared baseKEYS[3], so it shares the queue's slot on a cluster. - the native module
- checks the layout byte for byte, an empty queue name included, before it reads the wire identity; serves standalone servers.
One grammar on both sides of the wire: the host composes the keys, the native module refuses any other shape, and the hashtag keeps every script single-slot.
The dives
Three dives, each grounded in the real keyspace code — the host's EchoMQ.Keyspace and
the native module's mirror of it: the grammar and its server-side check, the slot the brace computes, and the
reserved base.
The braced grammar
emq:{q}:<type> from queue_key/2 and job_key/2 on the host; the native layout check refuses any other shape, byte for byte.
The hashtag & the slot
slot/1 = CRC16-XMODEM % 16384, vector slot("123456789")==12739; the scripts rely on one slot per queue, the native module serves standalone.
The reserve
{emq}: is first-byte-disjoint from every queue; {emq}:version holds the wire identity — claimed by a wire 4 connector; on a native node, installed administratively and only read by the write verbs and a wire 6 connector.
Pattern → implementation
The keyspace is one applied pattern made total. Hash-tag co-location is the move; the braced grammar is the wire that fixes it as the only way a key is built.
Hash-tag co-location: wrap the shared part of a key in {...} so every member of a
logical group routes to one cluster slot, where one atomic operation can touch them together.
Redis Patterns Applied R2.05 teaches the move.
EchoMQ.Keyspace.queue_key/2 makes the brace the protocol —
emq:{q}:<type> pins every key of one queue to one slot, so the scripts are
single-slot by construction. The native module checks the same shape on the server and refuses a call whose
keys do not share one {q}.
The pattern is the door; the keyspace is the wire. A naming convention becomes a protocol when one module builds every key to it and the server refuses every other shape — and the brace keeps the scripts cluster-correct by construction.
References
Sources
- Valkey — Documentation — the store every key is written to; the substrate of record.
- Redis — Keyspace & cluster hash tags — cluster key routing by the hashtag inside
{...}, the mechanism the braces use. - Valkey — Cluster specification — the
{hashtag}→hash-slot routing the per-queue hashtag is built for: every key of a queue on one slot, where a multi-key script is legal. - Valkey — Modules introduction — how a module such as the native one loads into the server and registers its own commands.
- llmstxt.org — The llms.txt convention — the machine-readable map format the course follows.
Related in this course
- The Protocol — the chapter this module belongs to.
- The braced grammar — dive 1: the grammar the host builds and the server checks.
- The hashtag & the slot — dive 2: the slot the brace computes.
- The reserve — dive 3: the cross-queue reserve and its two claim paths.
- The native module — the native commands that check these keys on the server.
- redis-patterns R2.05 · Hash-tag co-location — the near side of the door.