Core Java

BEAM Supervision Trees and “Let It Crash”: What Java Services Can Steal from Erlang

Most Java code tries hard to never fail. Erlang takes the opposite view, and the idea travels better than you might expect.

Where “Let It Crash” Comes From

The phrase sounds reckless at first. In practice, it is a careful design rule. Joe Armstrong, one of Erlang’s creators, laid it out in his December 2003 doctoral thesis, Making reliable distributed systems in the presence of software errors. The thesis has dedicated sections called “Let some other process fix the error” and “Let it crash”.

His reasoning is simple. We know how to survive hardware faults, because we can replicate. Software bugs are different. As Armstrong wrote on the Erlang mailing list, replicating faulty software just gives you two programs failing for the same reason. Therefore, the goal shifts from preventing every error to recovering from it quickly.

How a Supervision Tree Works

Workers and Supervisors

In OTP, the Erlang platform’s library set, processes are arranged in a tree. Workers do the real job, while supervisors only watch them and restart them when they die. Because the tree nests, a supervisor can itself be supervised.

Restart Strategies

The OTP design principles define the main strategies. The default is one_for_one.

StrategyWhat happens when one child dies
one_for_oneOnly that child is restarted (the default)
one_for_allAll children are restarted
rest_for_oneThe failed child and those started after it are restarted

The Safety Valve

Restarting forever would only hide a real bug. For that reason, the supervisor manual defines a maximum restart intensity. If more than intensity restarts happen within period seconds, the supervisor terminates all its children and then itself. The defaults are 1 restart in 5 seconds. As a result, the failure climbs one level up the tree, where a wider restart can be attempted.

What Java Teams Can Borrow

Java does not have a built-in supervision tree, and its threads share one heap. Still, the ideas transfer in four useful ways.

First, keep the supervisor separate from the work. The code that restarts a task should not be the code that performs it. Second, always put a limit on restarts, as OTP does. Third, escalate when the limit is hit, instead of retrying silently. Finally, restart into a clean state, rather than patching a half-broken object.

You do not have to build this from scratch. Apache Pekko, the open-source fork of Akka, offers supervision for actors, and its fault tolerance guide shows a restart strategy limited to a number of retries within a time window. Note that in typed actors the default is to stop the actor after an exception, not restart it.

A Tiny Supervisor You Can Run

To see the idea clearly, I wrote a minimal version in plain Java with no libraries. It mirrors the OTP rule: give up when more than three crashes occur within one second. The worker handles 1,000 tasks, and it crashes on every Nth task. I tested it on JDK 21, and it needs JDK 11 or newer. Save it as Sup.java.

import java.util.ArrayDeque;
import java.util.concurrent.atomic.AtomicInteger;

public class Sup {
    static final AtomicInteger done = new AtomicInteger();

    // Like OTP: give up when more than maxRestarts crashes happen within windowMs.
    static int supervise(Runnable worker, int maxRestarts, long windowMs)
            throws InterruptedException {
        var crashes = new ArrayDeque<Long>();
        int restarts = 0;
        while (true) {
            Throwable[] failure = new Throwable[1];
            Thread t = new Thread(worker);
            t.setUncaughtExceptionHandler((th, ex) -> failure[0] = ex);
            t.start();
            t.join();
            if (failure[0] == null) return restarts;        // finished normally
            long now = System.currentTimeMillis();
            crashes.addLast(now);
            while (now - crashes.peekFirst() > windowMs) crashes.removeFirst();
            if (crashes.size() > maxRestarts) {
                throw new IllegalStateException("gave up after " + restarts + " restarts");
            }
            restarts++;
        }
    }

    static Runnable worker(int crashEvery, int total) {
        return () -> {
            int n;
            while ((n = done.incrementAndGet()) <= total) {
                try { Thread.sleep(2); } catch (InterruptedException e) { return; }
                if (n % crashEvery == 0) throw new RuntimeException("boom at task " + n);
            }
        };
    }

    public static void main(String[] args) throws Exception {
        for (int every : new int[] {25, 50, 100, 200, 400}) {
            done.set(0);
            String result;
            try {
                int r = supervise(worker(every, 1000), 3, 1000);
                result = "finished, restarts=" + r;
            } catch (IllegalStateException e) {
                result = e.getMessage();
            }
            System.out.println("crash every " + every + " tasks: " + result
                + ", tasks done=" + Math.min(done.get(), 1000));
        }
        // unsupervised: one thread, dies at first crash
        done.set(0);
        Thread t = new Thread(worker(400, 1000));
        t.setUncaughtExceptionHandler((th, ex) -> { });
        t.start(); t.join();
        System.out.println("unsupervised (crash every 400): tasks done=" + Math.min(done.get(), 1000));
    }
}
java Sup.java

I ran it twice and got identical output both times. Timing-based results can shift on a slower machine, so treat the exact crash counts as illustrative. First, an unsupervised worker that crashes every 400 tasks stops at task 400. With the supervisor, the same worker finishes all 1,000 tasks after two restarts.

Next, the safety valve matters. When the worker crashes often, the supervisor stops restarting and escalates instead of looping.

Both charts demonstrate the restart-limit behavior of this small program and are not benchmarks.

Crashing every 25, 50 or 100 tasks hit the limit after three restarts, so the supervisor gave up. Crashing every 200 or 400 tasks stayed under the limit, and the work completed.

Where the Analogy Breaks

TopicErlang/BEAMTypical Java service
Failure isolationProcesses are isolated and share nothingThreads share one heap
Restart costRestarting a process is part of the designYou must build and test it yourself
Bad stateDies with the processCan leak into shared objects

Moreover, a restart cannot fix a bug that fails the same way every time. It only buys you recovery from transient faults, which is why the limit exists. Also, “let it crash” never means skipping input validation at the system boundary.

Quick checklist

  • Separate the code that supervises from the code that works.
  • Cap restarts per time window, and escalate when the cap is hit.
  • Restart into clean state, not into a half-damaged object.
  • Make restarted work idempotent, so a retry does no harm.
  • Still validate input at the boundary.

What We Have Learned

Armstrong’s 2003 thesis argued that software errors cannot be replicated away, so systems should recover instead. OTP puts this into supervision trees, where supervisors restart workers under strategies such as one_for_one, one_for_all and rest_for_one. A restart limit, which defaults to 1 restart in 5 seconds, makes a supervisor escalate rather than loop forever. Java lacks a built-in tree, but the habits carry over, and Pekko offers a ready-made version. My small supervisor showed both sides: a worker that crashed every 400 tasks finished all 1,000 under supervision, while a worker that crashed too often triggered the safety valve. In short, supervise separately, limit restarts, and escalate.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button