Laubling

Tech

Every Laubling is its own Erlang process.

Laubling is written in Gleam and runs on the BEAM, the virtual machine behind Erlang. It was built for telephone networks that must never go down. That is exactly what a game needs in which thousands of little clocks tick at once.

15,000new Laublings a minute
1 KBmemory per Laubling clock
2.7 msper live update
0lost cuttings

Measured in a container with 1 core and 1 GB, without a single error. Raw data is in the project.

One clock per Laubling.

Every Laubling has 24 hours until someone opens its cutting. For that it gets its own tiny process that sleeps until the deadline. A hundred thousand of them are no problem.

about 1 KB per clock

A link works exactly once.

A hundred phones tap the same link at the same moment. One gets the Laubling, 99 get a friendly no. Not luck, but a condition in the database.

tested with 100 at once

If something falls, it gets back up.

If a part crashes, a supervisor restarts it and the rest never notices. After a hard crash the server restores every clock from the database.

0 lost cuttings

Node.js has one thread for everything.
The BEAM gives every Laubling its own.

In Node.js one slow calculation holds up everything else, and one unhandled error can bring it all down. On the BEAM no process waits for another, and if one crashes, the others never notice. 5,000 open screens are simply 5,000 more processes.

Why Gleam?

Gleam is a young language for the BEAM: the robustness of Erlang, plus strict types and very friendly error messages.

  • No null, no undefined. What can be missing is an Option, what can go wrong is a Result. The compiler insists on both cases.
  • Every case is covered. A forgotten case is a compile error, not a crash at three in the morning.
  • Small. The whole server is about 5,000 lines, plus more than 50 automated tests, at least one for every game rule.
For the curious: the code
%% A Laubling's clock (abridged, names translated)
start_clock(Id, RestMs, OnExpiry) ->
    stop_clock(Id),
    Pid = spawn(fun() ->
        clock_sleep(Id, RestMs, OnExpiry)
    end),
    ets:insert(laubling_clocks, {Id, Pid}).

clock_sleep(Id, RestMs, OnExpiry) ->
    erlang:send_after(RestMs, self(), expire),
    %% sleeps until the deadline, about 1 KB
    erlang:hibernate(?MODULE, clock_wake, [Id, OnExpiry]).
// writer.gleam (abridged, names translated)
use hit <- result.try(query(
  "update cutting set opened_at = ?1
   where code = ?2 and opened_at is null
   returning id, from_id, to_name",
  z.conn,
  [sqlight.int(now), sqlight.text(code)],
  ...
))

Small on purpose.

  • Gleam 1.19.2 on Erlang/OTP 29.1.2, web server mist, database SQLite 3.54.1. One container, one file, moving means copying.
  • Every dependency checked: latest version, no known security advisory, before every deploy.
  • Fails in a controlled way: If it gets too much, new requests immediately get a friendly reply instead of the memory filling up.

And if it grows?

  • Right now Laubling runs on a single core and handles 15,000 new Laublings a minute. The BEAM spreads its processes across every core it gets, all by itself. If it grows, Laubling simply gets more cores, without rewriting a single line.