Rust 101 for Java developer: Rust Crossbeam vs Java Disruptor — A wow feeling

Andrew Huynh
3 min readSep 11, 2022

--

As I explore Rust with the aim of building low-latency trading applications, I naturally wonder: is there a Rust library that can outperform the Java Disruptor framework I often use in my microservices

(You can watch a short video if you don’t want to read: https://tinyurl.com/4vy9epk2)

Rust has a standard library mpsc (Multi Producer Single Consumer), this lib is playing a role very similar to the Java Disruptor. But there is much feedback from the Rust community that this library is not optimal. And the community has another library that is even better than the mpsc, which is Crossbeam.

So in this blog, I would like to share with you all my stupid simple benchmark results between Rust Crossbeam and Java Disruptor

  1. Java Disruptor: https://lmax-exchange.github.io/disruptor
  2. Rust Crossbeam: https://github.com/crossbeam-rs/crossbeam

In this benchmark, I build the simplest model of a micro-service. The service only has three threads, two producer threads, one consumer thread

Producer 1, and Producer 2 threads shall produce 10 million events to the Consumer thread. So totally the Consumer thread will process 20 million events

Simplest micro-service model

Java Version

Below are the codes of the Java version. To make a fair run for the Java version, I add extra pre-measuring code to help JVM warm up a bit.

https://github.com/cseblog/disruptor-2p1c-demo

Rust version

https://github.com/cseblog/crossbeam-demo

Result

I ran 100 times for each version of the implementation. Then we can see the processing time in milliseconds of the Java version about 100% more.

Summary:

  • Even as a senior Java developer, I must admit that the Rust code is shorter and simpler than the Java version, and easier to understand.
  • The Rust version easily outperforms the Disruptor by reducing latency by over 100%. The mean processing time is 286ms for Rust versus 436ms for Java.
  • Fine-tuning the buffer size can improve performance in both versions. In general, a larger buffer size improves performance when throughput is very high.
  • The smaller the buffer size, the faster the Rust version is versus the Java version.

--

--