Distributed FizzBuzz: When Does Distribution Actually Pay Off?
Summary:
I wrote a small Erlang program that spreads the 'interview challenge' of FizzBuzz across multiple nodes, mostly to explore distribution mechanics rather than to solve anything real. What I ended up doing was benchmarking whether distributing FizzBuzz is actually worth it.
The Short answer is : only at huge scales then, local cores beat the sending anything over the network.
For those who don't know, FizzBuzz is a test of programmer capability used in the interview process, it is an algorithm which counts from 1 upward, it prints:
- "fizz" for multiples of 3,
- "buzz" for multiples of 5,
- "fizzbuzz" for multiples of both
- Otherise prints number itself otherwise.
So the output would be something like this:
1 2 fizz 4 buzz fizz 7 8 fizz buzz. etc etc..
It is not useful, as far as I can see it has no practical application, it is a test. It is trivial to compute, which is exactly why it's a convenient stand-in here — the point isn't the algorithm. With AI now doing the bulk of the coding I expect to see it used even less often.
Introduction
In this example we plan to 'distribute' the work across multiple processes, these can be run on the same computer, or across the network.
This distributed_fizzbuzz is deliberately a small toy: a single module, fizzbuzz, that chunks a range of
numbers into equalish parts, hands one chunk to each node, and has each node compute FizzBuzz
over its slice and hand the result back.
distribute() ->
Nodes = ['node1@localhost', 'node2@localhost', 'node3@localhost'],
Total = 10,
Count = length(Nodes),
ChunkSize = Total div Count,
Rem = Total rem Count,
Tasks = [{lists:nth(I, Nodes), I,
(I-1) * ChunkSize + min(I-1, Rem) + 1,
I * ChunkSize + min(I, Rem)}
|| I <- lists:seq(1, Count)],
%% dispatch Tasks to each node, collect, print
Each 'erlang node' runs fizzbuzz/4 over its assigned range and reports back which node produced
which lines, so the merged output can be traced back to the node that actually did the work:
fizzbuzz(Parent, Index, Start, End) ->
NumberList = lists:seq(Start, End),
Solution = [solve(N) || N <- NumberList],
Flattened = [lists:flatten(Sub) || Sub <- Solution],
Parent ! {done, Index, node(), Flattened}.
Benchmark: when is distributing actually worth it?
Before knowing what to solve for, I benchmarked my local system:
- Single-core throughput for
solve/1(turns out to be about: ~1.8us/number). - To compute 100,000,000 numbers on one core: ~181 seconds.
- Same 100M numbers split across 8 local CPU schedulers (no network at all): ~23 seconds.
- RPC dispatch overhead per chunk (localhost or network) is a few ms, one-time, not per-item — negligible against minutes of compute at this scale.
For a task to be worth distributing, the length of the task must be at LEAST as long as it would take to send the task to another node and send it back. This doesn't consider the time that the actual 'work' task takes to run. If its any shorter forget it.
Here is the breakdown of time from 'packet send()' to the other end recv() using my basic measurements and typical published figures for network stack latency.
| Stage | Operation / Component | Standard Desktop OS | Tuned Server OS |
|---|---|---|---|
| 1. TX OS Processing | Syscalls, segmentation, & buffering for 76 pkts | ~1,500 to 3,000 µs | ~200 to 500 µs |
| 2. Bulk Transmission | 110 KB data + headers pushed onto wire | ~935 µs | ~935 µs |
| 3. Propagation Delay | Physical electrical travel time (60m total cable) | ~0.3 µs | ~0.3 µs |
| 4. RX OS Processing | 76 HW interrupts, stack traversal, recv() copies | ~2,000 to 5,000 µs | ~400 to 900 µs |
| Total Batch Time | Application send() start to final recv() | ~4.5 to 9.0+ ms | ~1.5 to 2.4 ms |
So we're looking at ~2500 µs before the first packet is received on the remote end and 11 ms in transmit time in addition to the time it takes to do the fizzbuzz processing and send the return the first packet (with no data in this measurement)
|---------|----------------------------|--------------| | SEND | PROCESSING | SEND | |---------|----------------------------|--------------| 0 ----------------------------------------------------> 11ms + processing time.
We can process on my machine about 1.8 µs per fizzbuzz number, so that means:
11.5 ms = 11,500 µs ÷ 1.8 µs/number ≈ 6,390 numbers
This is a rough first estimate: it treats the whole 11.5 ms as a fixed cost and ignores how much data has to come back. The next two sections refine it.
How big is the chunk actually being sent back?
The numbers above cover the request its a tuple of a Pid and three integers, effectively free to send.
What I didnt think about measuring was the response: the node has to end the computed strings back, and that's the part that actually scales with chunk size.
Using erlangs built in term_to_binary/1 on the {done, Index, Node, Data} tuple that
comes back from each node, a chunk of 6,390 numbers (the rough estimate from above)
serializes to about 60 KB — roughly 9.4 bytes per number in Erlang's external term format.
That's not free, and it's the same order of magnitude as the 110 KB / 76 packet example already used in the latency table above, so the "~9.0ms transmit" figure in that table isn't an arbitrary example, it is approximately the cost of shipping a chunk of that size.
| Link | Realistic throughput | Transmit time |
|---|---|---|
| 1 Gbps ethernet | ~110 MB/s | ~0.5 ms |
| 802.11ac wifi | ~30 MB/s | ~2.0 ms |
| 802.11n wifi | ~10 MB/s | ~6.0 ms |
| 100 Mbps ethernet | ~9 MB/s | ~6.7 ms |
So for a chunk sized at that first estimate, the return trip alone can eat most of the budget that made the chunk "worth it" in the first place on a slower link - the 6.7ms return trip over 100 Mbps is more than half of the 11.5ms total this whole calculation was built on. The breakeven chunk size isn't fixed; it grows on slower links, because the return payload gets relatively more expensive as available bandwidth shrinks.
This also means the earlier "negligible" claim about RPC overhead only holds for small chunks on fast links — scale the chunk up to, say, the 100M-number example above and the response payload swells to roughly a gigabyte, at which point transmit time stops being a rounding error and starts being the dominant cost.
So when is it actually worth it?
Putting the two costs together into one inequality: the fixed dispatch overhead (T_fixed),
and the cost of shipping the results home (N × b/BW). Distributing a chunk of N numbers to
a remote node beats computing it locally when:
N * t_c > T_fixed + N * (b / BW)
where N is the number of FizzBuzz items in the chunk, and:
t_c- local compute time per item (~1.8 µs, measured)
T_fixed- fixed dispatch overhead, independent of chunk size: the ~2,500 µs from above for a request to reach the remote node
b- size of the response per item (~9.4 bytes, from
term_to_binary/1) BW- realistic link throughput, in bytes/µs (conveniently, 1 MB/s = 1 byte/µs)
This assumes the local node keeps working while the remote node computes, so the
remote compute time runs in parallel and isn't on the critical path. T_fixed only counts
the request's trip out, not the reply's own fixed overhead coming back, so treat the
breakeven numbers below as a lower bound.
The throughputs below are realistic guesses for each link, not measurements. Link speeds are quoted in Mbps (megabits), throughput in MB/s (megabytes): Mbps = MB/s × 8.
Solving for N gives the breakeven chunk size:
N > T_fixed / (t_c - b/BW)
This only has a solution if t_c > b/BW — if the network can't move a number's worth of
response data in less time than it takes to compute that number, no chunk size ever makes
distributing worth it, because each additional number costs more to ship home than it saves
in compute. That happens below a bandwidth floor of b/t_c, which works out to roughly
5.2 MB/s (~42 Mbps) when each number's response is ~9.4 bytes. Below that, distributing is
a net loss at any N.
Above the floor, plugging in real link speeds gives the breakeven chunk size:
| Link | Realistic throughput | Breakeven N |
|---|---|---|
| 100 Mbps ethernet | ~9 MB/s | ~3,300 |
| 802.11n wifi | ~10 MB/s | ~2,900 |
| 802.11ac wifi | ~30 MB/s | ~1,700 |
| 1 Gbps ethernet | ~110 MB/s | ~1,500 |
The counterintuitive part: breakeven N gets smaller on faster links, not larger. It's tempting to think a slow link needs a bigger chunk to "amortize" the trip, but the fixed dispatch overhead is the same regardless of link speed — what changes is how much of your per-number compute savings the return trip eats into. A faster link erodes less of that savings per number, so it takes fewer numbers to clear the fixed overhead. A slow link erodes more of it, so you need more numbers - right up until the bandwidth floor, past which no amount of numbers ever clears it.
On the local machine, On the same erlang scheduler (no network involved) the transmit times are even lower, i'm down to 30 µs for the 'thread start' to work.
As my machine is an 8 core system, it would make sense to have this algorithm spread out across all local CPU cores before sending to another node on the network.
I did find that distributing mandelbrot was a worthwhile activity, The original run time being 35.5s, cutting it down to 0.81s across multiple devices.
Thats a wrap
None of this was ever about making FizzBuzz fast, it's a toy exercise for getting a feel for Erlang's distribution primitives: how nodes actually find each other, what synchronous vs async RPC really costs, and how easy it is to end up with something that looks distributed without being concurrent.
To be clear, nobody needs distributed FizzBuzz. It is a small, familiar problem you can use to explore Erlang distribution without the domain logic getting in the way. As a learning exercise it has real benefits.
In an effort to understand performance issues, I wrote both fib and mandel which allowed me me to better understand distribution problems.
The code lives at distributed_fizzbuzz if you want to take a look at it yourself.