Module C4.10
Practical recipes in F#
The spine built the structures. This module is how they turn into the problems a queue actually solves.
Chapter C4 built lists, trees, sorting, maps, the HAMT and CHAMP, branded ids, persistence, and the
actor-guarded store. A data structure is a means, not an end — what a system needs are the recurring jobs those
structures do well. Three recur, and each is a deep dive: deduplication, refusing work already
seen; priority, serving many groups fairly rather than draining one; and windowing,
spreading work over time so a burst does not become a storm. None is exotic. Each is a few lines of idiomatic F#,
and each is exactly how the echo_fs bus port composes one of its verbs.
Three recurring jobs, three idioms
This module is the F# reflection of the Elixir course's E4.10 — practical
recipes. Where the Elixir module threads a request through a with chain, streams a
report, and reads complexity, the F# module turns the same instinct — let the idiom and the structure carry the
work — on a queue's three recurring needs. Each recipe pairs a one-line F# idiom with the real
echo_fs surface that uses it.
The first recipe is deduplication: List.distinct for a one-shot pass, a
Set for a stream, and a branded-id Map for the bus. The second is
priority: List.sortBy for ordering, and a rotating ring for the harder problem of
fairness across groups. The third is windowing: List.chunkBySize and
Seq.windowed over a collection, and a backoff curve over time.
The three recipes · select one
Removing repeats is a one-liner — and which line you reach for is a choice about cost: List.distinct for a one-shot pass, a Set for a stream, a branded-id Map for the bus.
Open this recipe · or pick another above
A structure is a means. These three recipes are the ends the chapter has been building toward — and the bus composes its verbs from exactly them.
EchoMQ in F# — the verbs, composed
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.
The three recipes are not invented examples. They are how the echo_fs bus — the F#
port of echo_mq at the repo root — composes its verbs. The in-memory core is built and
tested; the Valkey transport (EchoMQ.Connector) lands next. Each dive grounds in a real
surface, quoted verbatim from echo_fs/EchoMQ/.
Store.enqueue |
refuses a duplicate id — st.Rows.ContainsKey id returns Admission.Duplicate. Idempotent enqueue is dedup by branded id. |
Lanes.rotate |
rotates the fair-lane ring one step before serving, so fairness across groups is constructed, never hashed. |
Backoff · Admin.pause |
Backoff spreads retries over a growing window; Admin.pause holds a queue without losing its backlog. |
C4.01–C4.09 · the structures
Lists, trees, sorting, maps, the HAMT and CHAMP, branded ids, persistence, the actor-guarded store.
C4.10 · the recipes
The idioms that turn those structures into the bus's everyday verbs — admission, fair service, and pacing.
The three recipes
Read them in order. Each stands on valid F#: every // val it : echo below was produced
by dotnet fsi (9.0.303), and each closes on the real echo_fs
surface that uses the idiom.
Deduplication
List.distinct, a Set for a stream, and the branded-id Map the bus uses to refuse a duplicate.
- echo_fs
Store.enqueue→Admission.Duplicate
Priority
List.sortBy orders; a rotating ring builds the fairness a sort alone cannot.
- echo_fs
Lanes.rotate/next— the fair-lane rota
Windowing
List.chunkBySize and Seq.windowed over a collection; a backoff curve over time.
- echo_fs
Backoff+Admin.pause— pacing the work
References
Sources
- Microsoft — F# documentation — lists, sequences, sets and the collection functions these recipes use.
- FSharp.Core API reference —
List.distinct,List.sortBy,List.chunkBySize,Seq.windowed. - The F# Software Foundation — F# on the .NET runtime.
- Microsoft — .NET documentation — the runtime and cost model behind the structures.
Related in this course
- Elixir — Practical recipes (E4.10) — the parity module this one reflects.
- C4 — Algorithms & Data Structures — the chapter this module closes the spine of.
- C1.07 — Collections —
list,seq,MapandSet, in depth. - C4.07 — Identifiers, Snowflake & branded ids — the id that makes dedup free.