Visualization of Raft consensus algorithm for 3d printers using JavaFx (GUI), Spring Boot (REST API Endpoints GET/POST) and RabbitMQ (Message Broker) in Java 21.
This project implements and visualizes the Raft lifecycle for 3D printers using a 3 node cluster. Each node is a Spring Boot application that communicates with the others using RabbitMQ. The state of each node is visualized in real-time using a JavaFX GUI.
For a quick visual understanding refer to the image below:
imgsrc: RAFT Protocol by Swayam Raina on Medium
In a distributed system, Raft ensures all nodes agree on a single source of truth, even if some nodes fail.
Nodes transition through three states:
- 🔵 FOLLOWER (Blue): The default state.Nodes remain here as long as they receive regular "heartbeats" from a Leader.
- 🟠 CANDIDATE (Orange): If a follower doesn't receive a heartbeat within its randomized election timeout (3-5s), it becomes a candidate and requests votes.
- 🟢 LEADER (Green): The winner of the election. If a Candidate receives a majority of votes, it becomes the Leader and begins sending periodic heartbeats (every 1s) to maintain authority.
- Note: To simulate a realistic and dynamic cluster, the leader automatically steps down after a randomized tenure (4-7s), forcing a new election sequence.
- Java 21 (e.g., BellSoft Liberica JDK with JavaFX bundled)
- RabbitMQ running on
localhost:5672(default guest/guest credentials) - Maven (Wrapper included in project)
- Language : Java 21 LTS
- REST APIs: Spring Boot 4.0
- Build Tool: Maven
- Message Broker: RabbitMQ
- GUI: JavaFX
The cluster consists of 3 Spring Boot nodes running on different ports (8080, 8081, 8082). You need to launch each node in a separate terminal window, and then launch the JavaFX GUI.
Ensure your RabbitMQ server is running on localhost.
Open 3 separate terminals, navigate to the project root, and run:
Terminal 1 (Node 1 - Port 8080):
./mvnw spring-boot:run -Dspring-boot.run.profiles=node1Terminal 2 (Node 2 - Port 8081):
./mvnw spring-boot:run -Dspring-boot.run.profiles=node2Terminal 3 (Node 3 - Port 8082):
./mvnw spring-boot:run -Dspring-boot.run.profiles=node3Open a 4th terminal and run the GUI:
./mvnw javafx:run- The GUI updates dynamically every 2 seconds by polling the REST endpoints
/raft/stateof each node. - The visualization runs for exactly 30 seconds. After 30 seconds, the simulation halts, displays a
"Hope You Enjoyed Visualization of Raft"alert popup, and gracefully shuts down.
RaftConsensusService.java: Core Raft logic. Manages election timeouts, leader heartbeats, state transitions, and RabbitMQ message broadcasting/consumption.Raft3DGui.java: JavaFX application. Uses asynchronousHttpClientto poll node states without blocking the JavaFX Application Thread.RaftMessageProducer.java/RaftMessageConsumer.java: RabbitMQ integration for inter-node communication.application.yaml: Uses Spring Profiles (node1,node2,node3) to isolate ports and node IDs while sharing the same codebase.