java
58 lines · 9 steps
A thread-safe round-robin load balancer in Java
How an atomic cursor and copy-on-write list rotate through healthy backends without locks.
Explained by
highlit
1package com.example.lb;
2
3import java.util.List;
4import java.util.concurrent.atomic.AtomicInteger;
5import java.util.concurrent.CopyOnWriteArrayList;
6
7public final class RoundRobinBalancer<T> {
8
9 private final CopyOnWriteArrayList<Backend<T>> backends = new CopyOnWriteArrayList<>();
10 private final AtomicInteger cursor = new AtomicInteger(0);
11
12 public RoundRobinBalancer(List<T> targets) {
13 for (T target : targets) {
14 backends.add(new Backend<>(target));
15 }
16 }
17
18 public T next() {
19 int size = backends.size();
20 if (size == 0) {
21 throw new IllegalStateException("no backends available");
22 }
23 for (int attempt = 0; attempt < size; attempt++) {
24 int index = Math.floorMod(cursor.getAndIncrement(), size);
25 Backend<T> candidate = backends.get(index);
26 if (candidate.healthy) {
27 return candidate.target;
28 }
29 }
30 throw new IllegalStateException("all backends are unhealthy");
31 }
32
33 public void markDown(T target) {
34 setHealth(target, false);
35 }
36
37 public void markUp(T target) {
38 setHealth(target, true);
39 }
40
41 private void setHealth(T target, boolean healthy) {
42 for (Backend<T> backend : backends) {
43 if (backend.target.equals(target)) {
44 backend.healthy = healthy;
45 return;
46 }
47 }
48 }
49
50 private static final class Backend<T> {
51 final T target;
52 volatile boolean healthy = true;
53
54 Backend(T target) {
55 this.target = target;
56 }
57 }
58}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1An AtomicInteger cursor lets many threads pick the next target without explicit locking.
- 2Bounding the scan to the list size guarantees termination even when every backend is down.
- 3Marking a volatile flag instead of removing entries keeps selection and health updates safe under concurrency.
Related explainers
java
public final class Levenshtein { private Levenshtein() { }
Two-row Levenshtein distance in Java
dynamic-programming
edit-distance
space-optimization
Intermediate
8 steps
go
package editor import ( "context"
How a debouncer coalesces bursts in Go
debounce
concurrency
timers
Intermediate
8 steps
rust
#[derive(Debug, Default)] pub struct RequestBuilder { url: String, method: String,
The builder pattern in Rust
builder-pattern
method-chaining
ergonomic-api
Intermediate
8 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json}; use dashmap::DashMap; use serde::Serialize; use std::sync::Arc;
Request coalescing in an Axum handler
caching
concurrency
request-coalescing
Advanced
8 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-thread-safe-round-robin-load-balancer-in-java-explained-java-badd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.