Java · Kotlin · Scala
llingr-demux for the JVM
The llingr-demux engine for the JDK 21+ holding all per-key ordering and clean, fast rebalance invariants. It bundles an elastic dispatcher with Loom-aware locking to make per-key concurrency efficient, 200x speed-ups conservative, and to provide the performance this patent-pending technology makes possible.
The engine uses coroutines for precise cancellation and backpressure control, while avoiding pinning to stay Loom-safe in host applications running blocking handlers on virtual threads.
The public interfaces have been thoughtfully refined for the JVM, aligning with the type system and the expressiveness of Java, Kotlin and Scala: contracts feel native for the ecosystem.
- Extremely fast, leaning on Kotlin coroutines
- Pure Java interfaces for first-class interoperability
- Formally verified with TLA+: invariants hold
- Same safe and orderly commits ensuring no message loss
- Same orderly rebalance coordination avoiding duplicates
- Same chaos testing suite
- Same 'bring your own config' operational simplicity
- Kafka Client implementation bundled; still treated as pluggable to support alternatives
- Prometheus metrics implementation bundled; still treated as pluggable to support alternatives
- Custom JVM features, e.g. slf4j with Service Loader option
package com.example.llingr;
import com.llingr.adapter.kafka.KafkaAdapter;
import com.llingr.demux.Demux;
import com.llingr.nexus.Consumer;
import com.llingr.nexus.Message;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Main {
private static final Logger log = LoggerFactory.getLogger("consumer-java");
public static void main(String[] args) throws InterruptedException {
// The host application (this application) provides
// the onMessage and onDeadLetter handlers
var consumerBuilder = Demux.newBuilder(
"customer_orders", // topic name
Main::onMessage, // for processing messages
Main::onDeadLetter // for 'this::onMessage' exceptions
);
// Broker clients are instantiated through an adapter
KafkaAdapter adapter = KafkaAdapter.create(kafkaProperties());
// The adapter accepts a consumerBuilder and binds adapter callbacks.
// When the consumerBuilder 'builds', the llingr-demux engine initializes
Consumer<ConsumerRecord<byte[], byte[]>> consumer =
adapter.createConsumer(consumerBuilder);
// The host application decides when to stop
Runtime.getRuntime().addShutdownHook(new Thread(consumer::shutdown));
// subscribe blocks until the first assignment
// then delivers into the Main::onMessage callback
log.info("consumer-java subscribing to '{}'", "customer_orders");
consumer.subscribe();
// keep the JVM alive - llingr-demux consumes on its own threads
Thread.currentThread().join();
}
// see: com.llingr.nexus.ProcessMessage<T>
private static void onMessage(Message<ConsumerRecord<byte[], byte[]>> msg) {
log.info(
"Java consumer received pk: {} value: {}",
msg.getKey(),
new String(msg.getPayload().value(), StandardCharsets.UTF_8)
);
}
// see: com.llingr.nexus.WriteDeadLetter<T>
private static void onDeadLetter(
Message<ConsumerRecord<byte[], byte[]>> msg, // the message being dead-lettered
Throwable reason // reason for the dead-letter
) {
log.warn(
"Java consumer dead-letter pk: {} reason: {}",
msg.getKey(),
reason.getMessage()
);
}
private static Properties kafkaProperties() {
Properties p = new Properties();
p.setProperty("bootstrap.servers", "localhost:9092");
p.setProperty("group.id", "consumer-java");
p.setProperty("auto.offset.reset", "earliest"); // never miss a message
p.setProperty("enable.auto.commit", "false"); // llingr-demux manages commits
p.setProperty("key.deserializer",
"org.apache.kafka.common.serialization.ByteArrayDeserializer");
p.setProperty("value.deserializer",
"org.apache.kafka.common.serialization.ByteArrayDeserializer");
return p;
}
}
package com.example.llingr
import com.llingr.adapter.kafka.KafkaAdapter
import com.llingr.demux.newBuilder
import com.llingr.nexus.Message
import java.util.Properties
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.serialization.ByteArrayDeserializer
import org.slf4j.LoggerFactory
private val log = LoggerFactory.getLogger("consumer-kotlin")
fun main() {
// The host application (this application) provides onMessage and onDeadLetter handlers
val consumerBuilder = newBuilder("customer_orders", ::onMessage, ::onDeadLetter)
// Broker clients are instantiated through an adapter.
val adapter = KafkaAdapter.create(kafkaProperties())
// The adapter accepts a consumerBuilder and binds adapter callbacks.
// When the consumerBuilder 'builds', the llingr-demux engine initializes.
val consumer = adapter.createConsumer(consumerBuilder)
// The host application decides when to stop.
Runtime.getRuntime().addShutdownHook(Thread { consumer.shutdown() })
// subscribe blocks until the first assignment, then delivers into onMessage.
log.info("consumer-kotlin subscribing to '{}'", "customer_orders")
consumer.subscribe()
// keep the JVM alive - llingr-demux consumes on its own threads
Thread.currentThread().join()
}
// see: com.llingr.nexus.ProcessMessage<T>
private fun onMessage(msg: Message<ConsumerRecord<ByteArray, ByteArray>>) {
log.info(
"Kotlin consumer received pk: {} value: {}",
msg.key,
String(msg.payload.value(), Charsets.UTF_8),
)
}
// see: com.llingr.nexus.WriteDeadLetter<T>
private fun onDeadLetter(
msg: Message<ConsumerRecord<ByteArray, ByteArray>>, // the message being dead-lettered
reason: Throwable, // reason for the dead-letter
) {
log.warn(
"Kotlin consumer dead-letter pk: {} reason: {}",
msg.key,
reason.message,
)
}
private fun kafkaProperties() = Properties().apply {
setProperty("bootstrap.servers", "localhost:9092")
setProperty("group.id", "consumer-kotlin")
setProperty("auto.offset.reset", "earliest") // never miss a message
setProperty("enable.auto.commit", "false") // llingr-demux manages commits
setProperty("key.deserializer", ByteArrayDeserializer::class.java.name)
setProperty("value.deserializer", ByteArrayDeserializer::class.java.name)
}
package com.example.llingr
import com.llingr.adapter.kafka.KafkaAdapter
import com.llingr.demux.Demux
import com.llingr.nexus.Message
import java.nio.charset.StandardCharsets.UTF_8
import java.util.Properties
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.serialization.ByteArrayDeserializer
import org.slf4j.LoggerFactory
object Main:
private val log = LoggerFactory.getLogger("consumer-scala")
def main(args: Array[String]): Unit =
// The host application (this application) provides onMessage and onDeadLetter handlers
val consumerBuilder =
Demux.newBuilder[ConsumerRecord[Array[Byte], Array[Byte]]](
"customer_orders", onMessage, onDeadLetter)
// Broker clients are instantiated through an adapter.
val adapter = KafkaAdapter.create(kafkaProperties())
// The adapter accepts a consumerBuilder and binds adapter callbacks.
// When the consumerBuilder 'builds', the llingr-demux engine initializes.
val consumer = adapter.createConsumer(consumerBuilder)
// The host application decides when to stop.
Runtime.getRuntime.addShutdownHook(Thread(() => consumer.shutdown()))
// subscribe blocks until the first assignment, then delivers into onMessage.
log.info("consumer-scala subscribing to '{}'", "customer_orders")
consumer.subscribe()
// keep the JVM alive - llingr-demux consumes on its own threads
Thread.currentThread.join()
// see: com.llingr.nexus.ProcessMessage[T]
private def onMessage(msg: Message[ConsumerRecord[Array[Byte], Array[Byte]]]): Unit =
log.info(
"Scala consumer received pk: {} value: {}",
msg.getKey,
String(msg.getPayload.value, UTF_8)
)
// see: com.llingr.nexus.WriteDeadLetter[T]
private def onDeadLetter(
msg: Message[ConsumerRecord[Array[Byte], Array[Byte]]], // the message being dead-lettered
reason: Throwable // reason for the dead-letter
): Unit =
log.warn(
"Scala consumer dead-letter pk: {} reason: {}",
msg.getKey,
reason.getMessage
)
private def kafkaProperties(): Properties =
val p = Properties()
p.setProperty("bootstrap.servers", "localhost:9092")
p.setProperty("group.id", "consumer-scala")
p.setProperty("auto.offset.reset", "earliest") // never miss a message
p.setProperty("enable.auto.commit", "false") // llingr-demux manages commits
p.setProperty("key.deserializer", classOf[ByteArrayDeserializer].getName)
p.setProperty("value.deserializer", classOf[ByteArrayDeserializer].getName)
p
// ---------- Application.java ----------
package com.example.llingr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
// keep the JVM alive - llingr-demux consumes on its own (daemon) threads, and a
// non-web Spring app has nothing else holding the process open.
Thread.currentThread().join();
}
}
// ---------- OrderConsumer.java ----------
package com.example.llingr;
import com.llingr.adapter.kafka.KafkaAdapter;
import com.llingr.demux.Demux;
import com.llingr.nexus.Consumer;
import com.llingr.nexus.Message;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
@Component
final class OrderConsumer implements SmartLifecycle {
private static final Logger log = LoggerFactory.getLogger("consumer-spring");
private final Consumer<ConsumerRecord<byte[], byte[]>> consumer;
private volatile boolean running = false;
OrderConsumer() {
// The host application (this application) provides
// the onMessage and onDeadLetter handlers.
var consumerBuilder = Demux.newBuilder(
"customer_orders", // topic name
this::onMessage, // for processing messages
this::onDeadLetter // for 'this::onMessage' exceptions
);
// Broker clients are instantiated through an adapter
KafkaAdapter adapter = KafkaAdapter.create(kafkaProperties());
// The adapter accepts a consumerBuilder and binds adapter callbacks.
// When the consumerBuilder 'builds', the llingr-demux engine initializes
this.consumer = adapter.createConsumer(consumerBuilder);
}
// Spring starts lifecycle beans once the context is ready
@Override
public void start() {
log.info("consumer-spring subscribing to '{}'", "customer_orders");
// subscribe blocks until the first assignment, then the
// engine consumes on its own threads into 'onMessage'
consumer.subscribe();
running = true;
}
// Spring stops lifecycle beans on context shutdown (SIGTERM).
@Override
public void stop() {
consumer.shutdown(); // drain and final commit
running = false;
}
@Override
public boolean isRunning() {
return running;
}
// see: com.llingr.nexus.ProcessMessage<T>
private void onMessage(Message<ConsumerRecord<byte[], byte[]>> msg) {
log.info(
"Spring consumer received pk: {} value: {}",
msg.getKey(),
new String(msg.getPayload().value(), StandardCharsets.UTF_8)
);
}
// see: com.llingr.nexus.WriteDeadLetter<T>
private void onDeadLetter(
Message<ConsumerRecord<byte[], byte[]>> msg, // the message being dead-lettered
Throwable reason // reason for the dead-letter
) {
log.warn(
"Spring consumer dead-letter pk: {} reason: {}",
msg.getKey(),
reason.getMessage()
);
}
private static Properties kafkaProperties() {
Properties p = new Properties();
p.setProperty("bootstrap.servers", "localhost:9092");
p.setProperty("group.id", "consumer-spring");
p.setProperty("auto.offset.reset", "earliest"); // never miss a message
p.setProperty("enable.auto.commit", "false"); // llingr-demux manages commits
p.setProperty("key.deserializer",
"org.apache.kafka.common.serialization.ByteArrayDeserializer");
p.setProperty("value.deserializer",
"org.apache.kafka.common.serialization.ByteArrayDeserializer");
return p;
}
}
// build.gradle.kts
repositories {
maven {
// Google Cloud Artifact Registry
url = uri("https://europe-west2-maven.pkg.dev/llingr-licensing/maven")
credentials {
username = "llingr_license_username" // normally in /.gradle/gradle.properties
password = providers.gradleProperty("llingr_license_key").get()
}
authentication {
create<BasicAuthentication>("basic")
}
}
mavenCentral()
}
dependencies {
implementation("com.llingr:llingr-demux:0.1.0")
}
<!-- pom.xml -->
<repositories>
<repository>
<!-- Google Cloud Artifact Registry -->
<id>llingr-licensing</id>
<url>https://europe-west2-maven.pkg.dev/llingr-licensing/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.llingr</groupId>
<artifactId>llingr-demux</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
<!-- credentials: normally in ~/.m2/settings.xml -->
<servers>
<server>
<id>llingr-licensing</id>
<username>llingr_license_username</username>
<password>${llingr_license_key}</password>
</server>
</servers>