Module C4.07
Identifiers, Snowflake & branded ids
Every job needs a name. The good name carries its own timestamp, sorts by time, and never asks two machines to agree on a counter.
This module re-derives Elixir's E4.07 in F#: from a naive id, to a
time-ordered Snowflake int64, to a branded, Base62-encoded string
like JOB0456qLH4via. The grounding is direct — EchoMQ's job identity is a branded Snowflake.
The real F# bus (echo_fs/EchoMQ) mints and validates one today: Snowflake
composes a 64-bit id whose high bits are the timestamp, Base62 encodes it to eleven fixed-width
characters whose string order matches the integer order, and BrandedId tags it with the
three-letter JOB namespace. Three dives take each apart, every snippet verified in
dotnet fsi.
One string, a type and a timestamped integer
A database counter (1, 2, 3…) names rows in one place, but the moment two machines mint at once it breaks. A random GUID is unique without coordination, yet it is 16 bytes and has no order — a list of them cannot be paginated by time. A Snowflake answers both: a 64-bit integer that packs a millisecond timestamp in its high bits, a node id, and a per-millisecond sequence. It is unique without a central counter, and because time is the high field, a later mint is always a larger integer.
The first dive lays out the 64 bits and reads them back with shifts and masks. The second encodes the integer to Base62 — eleven fixed-width characters over an ascending alphabet, so a lexicographic string sort agrees with a numeric sort. The third brands it: a three-letter namespace plus the Base62 payload, with a total validity check that a key builder leans on before it touches the keyspace.
The three dives · select one
A 64-bit integer split into a timestamp, a node id, and a within-millisecond sequence, read with shifts and masks. Time is the high field, so a later mint is a larger integer.
Open this dive · or pick another above
The same id is the key the registry stores, the value a trie hashes, and the stamp printed at the foot of this page. C4.07 is the id itself, after five modules treated the key as opaque.
Compose a branded id
Pick a stage of the pipeline. The diagram marks the field in focus; the readout shows the actual value at that
stage for a fixed example — minted at 2024-06-01T12:00:00Z, node 7,
sequence 0. Every value below was produced by the real echo_fs/EchoMQ
functions and confirmed in dotnet fsi: the integer is
55264149504028672, the Base62 payload is 0456qLH4via, and the branded
id is JOB0456qLH4via.
From clock to brand · select a stage
EchoMQ in F#
EchoMQ's job identity is exactly this branded Snowflake. Snowflake.compose gives each job a
time-ordered id, Base62.encode puts it on the wire, and BrandedId.mint
BrandedId.Job tags it JOB — so the pending set sorts by mint with no second index, and
a service routes on the first three characters without decoding the rest.
The F# EchoMQ mints and validates ids today — Snowflake,
Base62, and BrandedId are built and tested
(SnowflakeTests, Base62Tests, BrandedIdTests).
The Valkey transport EchoMQ.Connector lands next.
The same arc, in two languages
The Elixir course mints a Snowflake with EchoData.Snowflake and brands it with
EchoData.Base62. F# does the same with EchoMQ.Snowflake and
EchoMQ.Base62 — the layout (ts << 22 | node << 12 | seq),
the epoch (1704067200000), and the alphabet are identical, so an id minted on one side
decodes on the other. The shift is the static type: F#'s id is an int64 the compiler
tracks, and isValid is a total predicate.
Elixir — mint and brand (the E4.07 idiom)
# Elixir — EchoData mints, the caller brands it snowflake = EchoData.Snowflake.generate(node_id: 7) # => a 64-bit integer; time-ordered branded = "JOB" <> EchoData.Base62.encode(snowflake) # => "JOB" <> an 11-char base62 payload
F# — mint and brand (verified in FSI)
// F# — echo_fs/EchoMQ (mint instant 2024-06-01T12:00:00Z, node 7, seq 0) let id = Snowflake.compose 1717243200000L 7L 0L // val id : int64 = 55264149504028672L let branded = BrandedId.mint BrandedId.Job id // val branded : string = "JOB0456qLH4via"
Elixir / BEAM
A Snowflake is a tagged integer; the branded form is a binary string. The id is dynamically typed at the boundary.
F# / CLR
A Snowflake is an int64 value type — 8 bytes, no boxing — and the branded form is one System.String, checked by a total isValid.
The three dives
Read them in order. Each stands on the real echo_fs/EchoMQ source, quoted verbatim, with
every // val it : echo produced by dotnet fsi, and each cross-links
its Elixir E4.07 sibling. Together they take an id from a wall-clock millisecond to the 14-byte string a key builder
will accept.
The Snowflake
A 64-bit integer split into a timestamp, a node id, and a sequence, composed and read with shifts and masks.
- int64time in the high bits, so byte order is mint order
Base62
A width-11 transport encoding over an ascending alphabet, so a string sort agrees with a numeric sort.
- wirefixed width + ascending alphabet = order preserved
Branded ids
A three-letter namespace plus the Base62 payload, with a total isValid the key builder enforces.
- brandthe
JOBnamespace, gated before a key is built
Status — echo_fs is an in-memory model. The EchoMQ protocol — the Lua transitions executing on a Valkey wire — is not implemented (there is no Lua and no Valkey client in the code); that is the coming-soon transport (EchoMQ.Connector). This block shows how the model uses the structure, not a running bus.
References
Sources
- Microsoft — F# documentation — integers, bitwise operators, and modules.
- FSharp.Core API reference — the operators and
Seqfunctions these modules use. - Microsoft — .NET documentation —
System.Int64,DateTimeOffset, andStringBuilder. - The F# Software Foundation — F# on the .NET runtime.
Related in this course
- Elixir — F4.07 Identifiers, Snowflake & branded ids — the module this one reflects.
- C4 — Algorithms & Data Structures — the chapter this module belongs to.
- C1.01 — Values, types & FSI —
int64literals and the FSI echoes used here. - F# In Depth — the course home.