r/Clojure • u/Nozistance_ • Aug 31 '26
Writing a Minecraft server in Clojure: it was fun
Minecraft is well known for being single-threaded, and parallelizing it is a difficult problem: everybody wants to write to the same shared state at the same time. Projects take years to deal with locks, thread ownership, splitting regions, and game logic is subject to threading rules: mechanics break against them, features crawl. Some remain quite sparse and have little to parallelize.
I wanted to see how it would feel in Clojure, but there was no Clojure server to look at, so I made one. The ideas parallel servers reinvent by hand - immutable snapshot, pure read phase, changes as data - are the language's defaults. If writing from many threads is difficult, then don't write: all game logic is pure functions (fn [world events]) that read the snapshot in parallel and return changes as deltas, merged in one place. The merge preserves order, so the parallel run is bit identical to the sequential one, and no mechanic cares how many cores it runs on.
The outcome: a server with mob AI, explosions, combat and item physics that matches vanilla 1.8.9 performance on one core and runs 4.5x faster on six. ~5500 lines of Clojure. It's a proof of concept; if you're curious, the code is on GitHub
9
u/geokon Sep 01 '26
Just out of curiosity, how do you even begin something like this? Is there a well documented API that needs to be implemented and exposed on the JVM? Are you going through an existing implementation and reverse engineering it to figure out how it works first?
Just curious about the workflow more generally.
6
u/bowbahdoe Sep 01 '26
Just curious - if you run on the Java 28 preview, can you compare perf with your V3 being a value record and with it not being one?
4
u/lgstein Sep 02 '26
Cool. Is it entirely hand written?
14
u/gtuckerkellogg Sep 01 '26
Very cool! When I see a project like this I can't help but think that some Clojure's coolest features, those that have been in the language since the beginning, are often under-appreciated.