Contrasting NATS with Apache Kafka

Emil Koutanov
ITNEXT
Published in
6 min readSep 2, 2020

--

TL;DR Kafka is an Event Streaming Platform, while NATS is a closer to a conventional Message Queue. Kafka is optimised around the unique needs of emerging Event-Driven Architectures, which enrich the traditional pub-sub model with strong ordering and persistence semantics. Conversely, NATS is highly optimised around pub-sub topologies, and is an excellent platform for decoupling systems where message order and reliable delivery is a non-issue.

I’ll preface this post by pointing out that there is another product — NATS Streaming — which is a different beast and is closer to Kafka. You may want to take a detour to NATS Streaming if you after an alternative event streaming platform; otherwise, read on.

Subscriptions

At its core, NATS is about publishing and listening for messages. These depend heavily on subjects which scope messages into streams or topics. Consumers subscribe to topics either verbatim (matching the topic name precisely), or using wildcards. Below is an illustration of publisher-subject-consumer relationship in NATS.

On the face of it, this isn’t diametrically opposed from Kafka, which also decouples producers and consumes by way of topics. However, their semantics differ. Kafka organises its topics into partitions — unbounded, totally ordered streams of records (Kafka’s substitute terminology for messages). A topic comprises one or more partitions, and exhibits partial order. (In other words, while records are totally ordered within their respective partition, their order across partitions is arbitrary.) This flexible arrangement makes Kafka highly suited to applications where order matters; for example, state machine replication, event sourcing, log shipping, log aggregation, SEDA (staged event- driven architecture) and CEP (complex event processing).

Speaking of topics, the equivalent NATS subject is a lightweight construct that is created automatically based on demand (subscriptions) and is pruned automatically when the demand ceases. NATS subjects are cheap to create, which makes them great for hierarchically organised data, allowing for a fine-grained subscription model. Anyone who’s used MQTT-style brokers (such as HiveMQ) should feel right at home with NATS. By comparison, Kafka’s topics are heavyweight entities that take time to…

--

--

Software architect, an engineer, and a dad. Also an avid storyteller, maintainer of Kafdrop, and author of Effective Kafka.