Skip to content

Latest commit

 

History

History
59 lines (42 loc) · 6.23 KB

0004-implementation-language-java.md

File metadata and controls

59 lines (42 loc) · 6.23 KB

ADR-0004: Implement the system in Java instead of C++

Amendment (2026-12-02): Consensus now uses Apache Ratis (ADR-0005), which brings its own shaded gRPC/Netty transport for peer traffic. The "no Netty, gRPC" row below now applies to our own networking code, which carries the client protocol. The Raft-specific mitigations below (deterministic Raft simulation, our own WAL with FileChannel.force) are replaced by the testing and durability plan in ADR-0005. Deployment moved from AWS EC2 to GCP Compute Engine (ADR-0006); the JVM-footprint concern applies equally to small e2 VMs.

Context

The project started in C++20 (milestones M0–M2: build setup, configuration, logging, and an epoll networking layer). The developer is much more fluent in Java than in C++. The hard parts of this project are distributed-systems correctness: Raft safety, durable state, lease and fencing semantics, and failure testing. Language friction takes time and attention away from those.

AGENTS.md permits a different stack when "a documented architectural decision justifies an alternative". This is that record.

Problem

Which implementation language lets the developer build, test and reason about a correct, low-latency coordination system most effectively, without giving up the project's goals of implementing the core mechanisms from first principles?

Considered alternatives

  1. Stay with C++20. Best control over memory layout, allocation and system calls, plus sanitizers. But every milestone costs more for a developer less fluent in it, and memory-safety bugs compete with distributed-systems bugs for attention.
  2. Go. Simple concurrency and a fast runtime, but the goroutine scheduler hides the event-loop and threading model the project wants to build explicitly. The developer is not more fluent in it either.
  3. Rust. Memory and data-race safety with C++-level control, but a steep learning curve for the same developer.
  4. Java (25 LTS). The developer's strongest language. It is memory-safe and has a mature standard library for nonblocking I/O (NIO, epoll-backed on Linux), concurrency and file durability. Tooling is strong: JUnit, JMH, JDK Flight Recorder, async-profiler.
  5. Kotlin. Similar runtime trade-offs to Java, with a less familiar language.

Decision

Implement the system in Java 25 (LTS).

Concern Choice
Language level Java 25 via Gradle toolchains
Build Gradle 9 (Kotlin DSL), multi-module, wrapper committed; shared conventions in buildSrc
Compiler checks -Xlint:all -Werror
Networking java.nio channels + a single-threaded Selector event loop (epoll on Linux). No Netty, gRPC or similar.
Tests JUnit Jupiter; integration tests as a separate Gradle test suite; shared test fixtures
Benchmarks (M11) JMH; JDK Flight Recorder and async-profiler for profiling
Serialization protobuf-java when message schemas arrive (M3), per ADR-0002
Logging The project's own small logger, passed explicitly (no static loggers, no logging framework)
Durability (M5) FileChannel.force(true) for data and metadata; directory force after renames

The architecture, safety requirements, milestones, wire format (ADR-0002) and Raft structure (ADR-0003) are unchanged. All M0–M2 functionality was ported one-to-one, with equivalent tests.

Tradeoffs

  • Garbage-collection pauses. A stop-the-world pause is a process pause: it delays heartbeats, can trigger elections, and stretches a lease holder's view of time. AGENTS.md already requires safety under process pauses (fencing tokens, leases that do not rely on local clocks alone), so pauses must not break safety. They do affect availability and tail latency. Mitigations: keep allocation low on hot paths, run a low-pause collector (ZGC), log GC pauses, set election and lease timeouts with measured pause times in mind, and include pause injection in failure tests (M6, M8).
  • No ThreadSanitizer equivalent. Java removes whole classes of memory bugs, so ASan and UBSan are not needed, but data races are not detected automatically. Mitigations: strict thread confinement with runtime checks (every loop-owned object rejects calls from other threads), deterministic simulation for Raft (ADR-0003), jcstress for any lock-free code, and static analysis to be added later.
  • Less control over memory layout and system calls. Objects carry headers, NIO adds a layer over epoll (and allocates its selected-key set and per-call temporary direct buffers for heap ByteBuffers), and false sharing is harder to control. These costs are measurable and will be measured in M11 before any optimization (direct buffers, buffer pooling, @Contended).
  • JIT warm-up. Startup latency is higher and early requests are slower. Benchmarks must use JMH or explicit warm-up and report steady-state and startup behaviour separately.
  • Larger runtime footprint. The JVM needs more memory than a native binary, which matters on small EC2 instances. Heap sizing (-Xmx, container-aware defaults) becomes part of deployment (M12).

Consequences

  • Development platform (amends ADR-0001). Java builds and tests run natively on macOS and Linux, so ./gradlew on the host is the primary workflow. The Docker image (deployment/docker/dev.Dockerfile, now based on eclipse-temurin:25-jdk) is kept for running the build on Linux and for failure injection with iptables and tc (M6), which needs Linux. CI builds natively on an Ubuntu runner with JDK 25.
  • Sanitizer presets are gone. Their role is taken by -Xlint:all -Werror, -ea in tests, runtime thread-confinement checks, and the other measures above.
  • The C++ implementation is retired. The committed C++ code remains in git history (up to commit 31d2fc0).
  • Future milestones use Java facilities: FileChannel.force for the WAL (M5), protobuf-java for messages (M3+), JMH and JFR for performance work (M11), and a JRE container image for deployment (M12).