Distributed computing in Jennifer

Jennifer

A master, workers that find it on the local network, and jobs split into tasks: https://github.com/jennifer-language/demo-distributed

November 2026, by @mplx.
Based on Jennifer 0.28.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 23

The problem

The master has to know at all times which tasks are done, which are out, and which are lost.

2 / 23

Roles

a client submits jobs to the master; workers pull tasks from it

3 / 23

Terms

TermMeaning
kindthe code of one type of computation, and its queue
jobone request to run a kind with options (a spec)
taskone piece of a job, run by one worker slot
node / slota registered worker / one of its tasks-at-a-time
leasea task handed to a slot, until its result comes back
4 / 23

A kind: six functions

FunctionRuns onDoes
begin(spec)mastercheck options; empty accumulator, first cursor
plan(spec, cursor, units)masterthe next few task payloads
work(payload)workerone task: a result and its size in units
merge(acc, index, result)masterfold one result in, in any order
finish(acc) / progress(acc)masterthe result / one line for the screen

All values are json.Value: they cross the network and are saved.

5 / 23

plan

func plan(spec as json.Value, at as json.Value, units as int) {
    def next as int init json.asInt($at, "/next");
    def below as int init json.asInt($at, "/below");
    def tasks as list of json.Value;
    while (len($tasks) < BATCH and $next < $below) {
        def hi as int init math.min($next + size($units), $below);
        $tasks[] = json.map({"lo": $next, "hi": $hi});
        $next = $hi;
    }
    $at = json.map({"next": $next, "below": $below});
    return kind.Batch{payloads: $tasks, cursor: $at,
        done: $next >= $below};
}
6 / 23

work

func work(payload as json.Value) {
    def lo as int init json.asInt($payload, "/lo");
    def hi as int init json.asInt($payload, "/hi");
    return kind.Output{
        result: json.map({"count": countPrimes($lo, $hi)}),
        units: $hi - $lo
    };
}
7 / 23

merge

func merge(acc as json.Value, index as int, result as json.Value) {
    def total as int init json.asInt($acc, "/count") + json.asInt($result, "/count");
    return kind.Merged{acc: json.set($acc, "/count", $total), emit: ""};
}
8 / 23

Workers pull

9 / 23

Discovery

worker -> 255.255.255.255:7401  {"distributed": "query", "protocol": 3, "nonce": "..."}
master -> worker                {"distributed": "offer", "protocol": 3, "port": 7400,
                                 "proof": "..."}
10 / 23

Scheduling

  1. Highest priority level: priority plus one level per ten minutes of waiting
  2. Fewest tasks out
  3. Served longest ago
  4. Submitted first

Jobs of equal priority take turns. Running tasks are not preempted.

11 / 23

Task size

Smaller tasks: more overhead. Larger tasks: more work lost per failure, longer waits for higher priorities.

12 / 23

Planning window

Master memory depends on the tasks in flight, not on job size.

13 / 23

Failures

EventMaster's response
work throwsthe task is queued again
a slot's connection closesits lease returns at once
no heartbeat for 10 sthe node is gone; all its leases return
3 leases without a resultthe job fails, naming the task and error
a late or second resultthe first result counts, later ones are ignored
14 / 23

Exactly-once results

15 / 23

Restart

submit --wait reconnects and continues.

16 / 23

One owner of the state

spawn deep-copies its scope; tasks share no memory.

connection tasks send events with a reply channel to the actor that owns the state

17 / 23

A cluster in one process: the worker

func worker(id as int, inbox as channel of Msg) {
    def reply as channel of int init channel.make(1);
    while (true) { # lint-disable: L105
        channel.send($inbox, message("lease", $id, 0, 0, $reply));
        def index as int init channel.recv($reply);
        if ($index == DONE) {
            return $id;
        }
        channel.send($inbox, message("result", $id, $index, work($index), $reply));
    }
}

code/cluster.j: three workers, one failing, and a check against a local run.

18 / 23

A cluster in one process: the master

def m as Msg init channel.recv($inbox);
if ($m.what == "lease") {
    def index as int init $queue[0];
    $queue = $queue[1..len($queue)];
    $leases[$index] = $leases[$index] + 1;
    channel.send($m.reply, $index);
} elseif ($m.what == "failed") {
    $queue[] = $m.index;
} elseif (not $finished[$m.index]) {
    $finished[$m.index] = true;
    $total = $total + $m.value;
}
19 / 23

Cost of moving data

KindPayloadOverhead
pia position and a countunder 1 %
sleepa durationone round trip
wordcountslices of whole linestext out, word maps back
execa script name and argumentsa process start

The World English Bibleā„¢: 925,038 words, "the" 67,866 times; identical to distributed local.

20 / 23

Security

21 / 23

Running it

./bin/distributed master                       # terminal 1: the live screen
./bin/distributed worker --slots 2             # terminal 2
./bin/distributed worker --slots 2             # terminal 3
./bin/distributed submit pi                    # endless: keeps every slot busy
./bin/distributed submit sleep -o seconds=3 -o tasks=20
./bin/distributed submit wordcount -f input=data/web.txt.gz -o top=5 --wait

Source: https://github.com/jennifer-language/demo-distributed

22 / 23

Summary

23 / 23