Kest

Search

Search the documentation

Reliability

Supervision

Let it crash: restart what fails, and give up loudly when it keeps failing.

Some failures are not worth handling inside the code that caused them. A connection drops, a library throws something nobody expected, a loop gets stuck. The Erlang answer is to let the failing piece crash and have a supervisor decide what to do: restart it, restart its neighbours too, or, if it keeps failing, fail itself and let its own supervisor decide. Kest has that model, and the framework uses it for its own background work.

val queue = Supervisor("emails", Strategy.ONE_FOR_ONE, maxRestarts = 5, withinSeconds = 60, heartbeatTimeoutMillis = 30_000)
    .add(ChildSpec("sender") { ctx ->
        while (true) {
            ctx.heartbeat()                  // tells the watchdog this child is alive
            sendNext()                       // if this throws, the child is restarted
        }
    })
Thread.ofVirtual().start { queue.run() }     // blocks until stop() or until it gives up
OptionMeaning
Strategy.ONE_FOR_ONErestart only the child that failed
Strategy.ONE_FOR_ALLrestart every child when one fails, for children that depend on each other
Strategy.REST_FOR_ONErestart the failed child and the ones started after it
Restart.PERMANENTrestart it whenever it stops, even after a normal return
Restart.TRANSIENTrestart it only after a crash
Restart.TEMPORARYnever restart it
maxRestarts, withinSecondsthe restart intensity. Past it, the supervisor stops its children and fails
heartbeatTimeoutMillisa child that has not called heartbeat() for this long is treated as crashed, and restarted

Restart pauses grow. The first restart waits 50 ms, then 100, 200, and so on up to 10 seconds, so a child that fails at once does not spin the CPU.

Escalation. When a supervisor exceeds its restart intensity, it stops its children and throws SupervisorEscalation. Put it under a parent with ChildSpec.supervisor(child), and the parent restarts the whole subtree, with its own intensity limit. The tree therefore always has a point where a problem is loud: the top of the tree, which stops the process.

Actors. An actor is a state and a message handler, under a supervisor. A message that makes the handler throw crashes the actor, which starts again from its initial state. Its mailbox is kept, so the messages sent to it while it was crashing are still handled:

val counter = supervisor.actor<Int, String>("counter", init = { 0 }, handle = { n, msg ->
    if (msg == "boom") error("crash") else n + 1
})
counter.tell("hit")

What Kest already supervises. Queue workers, the outbox relay, the workflow scheduler, the Kafka consumer, the TCP accept loop, the Redis subscriber, and every message handler: a crashing handler answers its caller with an error and the service carries on. The process itself is protected too: an exception that escapes a thread is logged and reported, and an out-of-memory error in a worker started by kest --processes=N ends that process so the cluster supervisor starts a clean one (see Concurrency model).

Supervision keeps running work alive, but it does not make a non-idempotent call safe to repeat: use @Retry for the calls and the idempotency keys for the writes.

Read more