All projects

j-broker

A Java 21 message broker with Raft consensus, replicated logs, consumer groups, and a web admin console.

Solo, in progress, 20261

j-broker demo

165,499

records a second, batched, all replicas acknowledged, three brokers on one laptop

0 lost

of 60,671 acknowledged records across 15 broker kills

20,000

seeded failure scenarios run against Raft on every CI run

1,185

tests: 42,000 lines of them for 39,000 lines of broker

Why

I wanted to understand distributed systems by writing one rather than reading about one. Kafka was the obvious shape, because it packs consensus, a replicated log, consumer groups, exactly-once delivery and an admin plane into one bounded thing.

The rule I set myself was no cheating: no Kafka libraries. The broker, the client and the Raft are written from scratch. I allowed gRPC and Protobuf for the wire, Spring Boot for the admin app only, and Micrometer for metrics.

How it works

Every broker does two jobs. It votes in a Raft group that agrees on the cluster's metadata, and it hosts partition logs. Raft carries only metadata: topics, leaders, permissions, membership. Record data moves a cheaper way, with followers pulling from each partition's leader, and a write that asks for all replicas is answered only once enough in-sync copies have it.

Trace a replicated write

The client locates the partition leader and sends the batch there.

produce, fetchadmin callssame codeoffsetsappend, readstored as topicsproposesfollowers pullheartbeatsreplicatesClient libraryJava, and a small Python oneAdmin consoleSpring Boot, htmxSimulator20,000 seeds per CI runBroker servicesNine gRPC services, one virtual thread per callControllerWhichever broker leads RaftRaftMetadata onlyCoordinatorsGroups, transactionsPartition logSegments, sparse indexOther brokersReplicas and voters

Decisions

Raft as a pure function, so it can be simulated

The Raft core takes an event and returns effects; threads, timers and the network live outside it. The same class runs in production and in a seeded simulator, 10,000 scenarios in each of two suites on every CI run. It caught two real bugs in my first version: a wrong commit rule and a wrong log backoff.

Consistency over availability, after losing one record

A chaos run lost one acknowledged record out of 10,579. Stalled replication had shrunk the in-sync set to one broker, that single copy satisfied the write, and a replica with a shorter log was promoted. Six fixes later, a partition with no in-sync replica goes offline instead.

Replication that wakes instead of polls

Followers fetched on a 25 ms tick and the leader polled every 10 ms, so every batch paid about 60 ms. Now an append wakes waiting fetches, and follower progress wakes the waiting write. Batched durable throughput went up 43 times. The cost: single-record writes that ask for one copy got slower.

Coordinators are just partition leaders

Group and transaction state live in internal topics rather than in Raft. Whichever broker leads that partition is the coordinator, so failover needs no second mechanism. The price is a new on-disk format version and compaction that refuses logs holding transaction markers.

Liveness outside the Raft log

I first tried to track broker liveness with heartbeat records through Raft and lost two days to it: only the leader can propose, so followers' heartbeats were silently dropped. Brokers now heartbeat each other directly, and the controller fences on silence.

Virtual threads, with a gate against pinning

Every call gets a virtual thread, which meant replacing synchronized with explicit locks wherever a lock is held across disk I/O. Two tests fail the build if the JVM reports a single pinned carrier thread under load.

Built with

Broker
  • Java 21
  • Virtual threads
  • Raft
Transport
  • gRPC
  • Protobuf
Admin console
  • Spring Boot
  • Thymeleaf
  • htmx
Observability
  • Prometheus
  • Grafana
  • Java Flight Recorder
Testing
  • JUnit
  • Deterministic simulator
  • Chaos scripts
Build & deploy
  • Gradle
  • Docker
  • Kubernetes
  • Helm