Messaging and streaming

NATS

NATS is a lightweight, very fast messaging system written in Go with pub/sub, request-reply and the durable JetStream layer.

What it is

NATS was created by Derek Collison, is developed by the company Synadia and the project belongs to the CNCF. The server is one small Go binary. Core NATS is at-most-once: messages fly fast but are not stored.

Addressing is by subjects with a hierarchy (orders.eu.created) and wildcards. It has pub/sub, request-reply and queue groups for balancing across workers.

JetStream adds durability: persisted streams, redelivery, acknowledgements, a key-value store and an object store. That makes NATS fit both as an event broker and as a light bus for microservices and IoT.

Use it when

  • You need a light low-latency bus between microservices.
  • You need request-reply and pub/sub without heavy infrastructure.
  • You work with IoT or edge where small size and fast start matter.
  • You want simple streaming with JetStream instead of Kafka.

Avoid it when

  • You need a mature connector and stream processing ecosystem like Kafka's.
  • You need rich AMQP routing with exchanges.

Key concepts

Subject
A dotted hierarchical name used to publish and subscribe; supports * and >.
Queue group
Subscribers in one group share messages, each message goes to just one.
JetStream
A layer of durable streams with persistence, replay and acknowledgements.
Request-reply
A synchronous pattern: a request on a subject and a reply to a temporary inbox.

Example

Starting a server with JetStream, creating a stream, publishing and subscribing.

Interview questions

  1. 1. What are core NATS and JetStream?

    Core NATS is fast at-most-once pub/sub with no storage. JetStream adds durable streams, redelivery and acknowledgements, so messages can be stored and replayed.

  2. 2. How do you balance load between workers?

    With queue groups: all workers subscribe under the same group name, and each message goes to just one of them.

  3. 3. When is NATS better than Kafka?

    When you want simplicity, low latency, request-reply and a small footprint, for example between microservices or at the edge. For huge streams with rich ecosystem tooling Kafka is better.

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).