jonnifyechomq

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

grammaremq:{q}:<type> hashtag & slot{q} → 0..16383 reserve{emq}:version one grammar · built by EchoMQ.Keyspace · checked natively
The grammar — says where
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.Keyspace builds every declared key; valid_queue!/1 refuses 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; @claim2 derives the row from the declared base KEYS[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.

built page ready to read

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.

the pattern (Redis Patterns Applied)

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.

the implementation (echo_mq)

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

Related in this course