Architecture
This page is for people changing Simba. It explains how the pieces fit and which rules they rely on; ADR 0010 records why. The user-facing model (sessions, deadlines, terms, handover) is on How Simba Works; this page assumes it.
Design Goals
The hard part of a lease library is not the happy path but the interleavings: a reply that arrives after its deadline, a release racing a takeover, a keep-alive in flight while a lease is acquired. Three decisions follow from that:
- Put every decision in one pure function. The engine is
step(state, event, now): no threads, clock or I/O. A race is then just an order of events, which a unit test can write down and a simulation can enumerate. - Keep the backend dumb. The
LeaseBackendSPI stores and answers; it never schedules, notifies (beyond optional hints) or keeps local lease state. All backends then share one engine and one TCK. - Measure time only locally. The backend returns durations; the engine turns them into monotonic deadlines. No two clocks are ever compared.
Modules
simba-core knows no backend and depends only on kotlin-logging. simba-redis must not depend on Spring; simba-coroutines is the only module that depends on kotlinx-coroutines. The starter depends on the backends as optional features. simba-bom and simba-dependencies are publication metadata; simba-example is a runnable sample per backend.
Layers
| Layer | Where | Role |
|---|---|---|
| User API | Simba, Election, DistributedLock, PartitionGroup, Term | Handles; no state of their own beyond what the engine publishes |
| Engine | engine.step(state, event, now) | Pure state machine: no threads, clock or I/O |
| Shell | EngineShell | Runs step on one loop thread and executes its effects: backend calls, timers, callbacks, group views |
| SPI | LeaseBackend | A bounded number of calls per method: sessions, leases, groups, optional hints |
From Event to Effects
fun step(state: EngineState, event: Event, now: Long): Pair<EngineState, List<Effect>>Events are Boot, Start, Stop, StepDown, Timer, Reply (a backend result, tagged with its request id), Hint, WorkExited, Join, Leave and Close. Effects are Call (a backend request with its timeout), Arm (a timer), Deadline (publish the session's local deadline to its terms), Elected / Revoked, Complete (a future), Publish (a SimbaEvent) and Members (a group view). Because step is pure, unit tests feed events and assert effects, and the simulation explores interleavings over thousands of seeds.
The Election State Machine
The user-visible states are on How Simba Works. Inside the engine the transitions carry the conditions that make them safe:
Rules the engine keeps:
- A term is adopted only from the reply to the election's current attempt, before the session's local deadline, and only with a token above every token the session released for that name.
- A term is revoked before its lease is released; releases carry the token, so a late one is a no-op.
- Keep-alive replies list the leases the session owns: a term adopted before the request but missing from the reply is revoked (
TAKEN_OVER); an owned lease that nobody holds or is acquiring is released. - An expired session is never revived: every term is revoked and a new session is opened.
- A hint is never dropped because its request is in flight (the reply may predate the change): a release hint makes a losing in-flight attempt retry at once, a membership hint repeats an in-flight members read at once.
- Handover: an election started with work keeps the lease of a stopped or stepped-down term until
WorkExitedfor that term or the session's local deadline at the stop, whichever comes first; terms without work release at once.
Threads
| Thread | Runs |
|---|---|
| Loop (one platform daemon thread per client) | step, timers; the only thread touching engine state |
| I/O executor (default: a virtual thread per call) | Backend calls, each with a timeout of the renew period in effect; at most one keep-alive per session and one acquire per election in flight |
| Callback executor (default: virtual threads) | onElected / onRevoked through one serial dispatcher per election; SimbaEvents through one serial dispatcher, never on the loop |
| Work executor (default: a virtual thread per term) | runWhileLeader / schedule / group work; interrupted on revocation |
The engine never waits for a callback except through the futures of stop() and stepDown(). Blocking close() methods do not wait when called from a callback or leader work. On a loop thread (reachable only with a direct callback executor) blocking calls throw IllegalStateException instead of deadlocking.
Time
- The client never reads a wall clock for lease decisions. Every remaining time comes from the backend as a duration, and the local deadline is
sentAt + remaining × (1 − drift) − marginon the monotonic clock (Ticker). Term.isValidandremaining()read the session's volatile deadline and the ticker; no watchdog thread has to run. Monotonic instants are compared by difference, never withmaxOf.- Backends decide with their own clock: database
now(JDBC), RedisTIME, Zookeeper session expiry.
Wire Contracts
Nodes of the same major version may run together, so these must not change within 5.x:
- Lease names: 1–128 characters of
[A-Za-z0-9._:-], not.or..; partitionpof groupgis{g}.{p}. - Partition assignment:
PartitionAssignment.weight(FNV-1a 64 of"$partition@$member"mixed with SplitMix64, members are session ids). - JDBC: tables
simba_session,simba_lease,simba_member;simba_leaserows are never deleted and release clearssession_idonly; every time comparison uses the dialect'snow(UTC epoch ms); the take-over reads the holder's session rowfor share, serializing with its keep-alive; the MySQL acquire assignstokenbeforesession_id(left-to-rightSET). - Redis:
simba:lease:{name}={session, token, expires}(never expires, never deleted),simba:session:{sid}(PX) with:leases/:groups,simba:group:{g}scored by expiry,releasedpublished on the lease key; withrouteBySlotevery script call stays within one hash slot and only durations cross nodes. - Zookeeper: ephemeral
/simba/leases/{name}(token =czxid),/simba/groups/{g}/{sid}.
The full list, kept with the code, is in AGENTS.md.
Design Records
| ADR | Decision |
|---|---|
| 0010 | Simba 5: sessions, terms, a pure engine, one LeaseBackend SPI (supersedes 0001–0009 where they conflict) |
| 0002 | Fencing tokens; still holds |
| 0001, 0003–0009 | The 4.x design: lease engine, 4.0, leader scheduling, observability, Actuator endpoint, lease validity and health, relinquish and handover, partitioned leadership |