Chapter C4
Algorithms & Data Structures
The structures a message queue is built from — re-derived in F#.
This chapter walks the same twelve rungs as the Elixir course's
E4 · Algorithms & Data Structures — lists, trees, sorting, maps, HAMT,
CHAMP, branded ids, persistence, a CHAMP behind an actor, recipes, dynamic programming, a lab — re-derived
in F# with cross-links to each E4 sibling and notes on the F# implementation and its efficiency. The running
example is EchoMQ, the Valkey-native job bus, now taking shape in F# as the echo_fs
port: every dive shows how that port — an in-memory model — uses the structure. The
EchoMQ protocol itself, the Lua transitions executing on a Valkey wire, is not implemented yet;
that is the coming-soon transport. Twelve modules, three dives each.
The queue is made of these
A message queue is not a special structure. It is a few ordinary ones, assembled: a sorted set of time-ordered ids, a map of rows, a recurrence for the retry delay, and one writer to keep mints from racing. That is the whole chapter — each rung builds one of those pieces, and each dive shows the piece at work in EchoMQ.
EchoMQ keeps its pending jobs in a sorted set keyed by the branded id. Because the id is a time-ordered
Snowflake and the Base62 encoding is fixed-width, the id order is the mint order — so the oldest
job is always the smallest id, and a claim is a single pop-minimum with no second
index to maintain. Claim the next job below and watch it leave oldest-first. The numbers come from the real
echo_fs port.
Mint-ordered claim · EchoMQ in F#
Pending (sorted set · head = oldest)
- +5msinvoice.send
- +12msreport.nightly
- +23mswebhook.retry
- +40msemail.welcome
- +58mscache.warm
- +71msimage.resize
Served (claim order)
- none yet
// echo_fs — the F# EchoMQ (in-memory core built; Valkey wire coming soon) let st, job = EchoMQ.Jobs.claim now leaseMs queue // pops the smallest branded id = the oldest mint
A queue is a sorted set of time-ordered ids, a map of rows, and one writer — the same algorithms this chapter builds, assembled into a bus.
The twelve modules
Each module re-derives one E4 rung in F#, three dives at a time, and carries an EchoMQ in F#
block in every dive — a short scenario showing how the bus uses that structure, grounded in the real
echo_fs port. The last module is a hands-on lab: assemble the pieces into a
working in-memory queue.
Lists, recursion & complexity
Cons cells; big-O on .NET.
Trees & traversals
Binary and n-ary trees; DFS/BFS, functionally.
Sorting & searching
Merge/quick sort and binary search, immutably.
Maps, sets & hashing
Hash maps, collisions, the cost model.
Hash Array Mapped Tries (HAMT)
Persistent maps via prefix trees.
CHAMP maps
Compressed Hash-Array Mapped Prefix-trees; layout & iteration.
Identifiers, Snowflake & branded ids
From naive ids to a Snowflake bigint and a branded, base62 id.
Branded ids & persistence
Branded ids as keys in SQLite, PostgreSQL, and Redis.
Branded CHAMP maps & MailboxProcessor
A CHAMP keyed by branded ids, behind F#’s actor — the MailboxProcessor.
Practical recipes in F#
Turning algorithmic problems into idiomatic F#.
Dynamic programming & advanced problems
Overlapping subproblems, memoized and tabulated.
Lab: build a branded CHAMP store
Assemble the pieces into a working in-memory queue.
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 — the language guide and reference.
- F# Core Library reference —
List,Map,Set,MailboxProcessor. - The F# Software Foundation — the language's home and community.
- Microsoft — .NET documentation — the CLR and the runtime that hosts F#.
Related in this course
- F# In Depth — course home — the full C0–C5 map.
- Elixir E4 — Algorithms & Data Structures — the arc this chapter re-derives.
- C1 — F# Language — the language these structures are built in.
- EchoMQ — the protocol, in depth — the bus the F# port mirrors.