Nov 18, 20266 min read

NATS vs Kafka for Backend Messaging

How NATS with JetStream and Apache Kafka compare on delivery guarantees, retention and replay, ordering, operations and managed options, with Go code for JetStream and the managed Pub/Sub alternative for backends on AWS or GCP.

Illustration for NATS vs Kafka for Backend Messaging

NATS and Apache Kafka both move messages between services, and they start from opposite ends. NATS began as a lightweight publish-subscribe system that keeps nothing on disk, with JetStream added for persistence. Kafka began as a distributed commit log on disk, where every consumer reads the same ordered history at its own offset.

For a backend team the choice turns on how much of that history you need and who runs the brokers. This guide compares delivery, retention, ordering and operations, shows JetStream code in Go, and covers the case where managed Pub/Sub in your own cloud account, declared with Encore, is enough.

NATS vs Kafka at a glance

NATS (core and JetStream)Apache Kafka
ModelSubjects with wildcards; JetStream streams store themTopics split into partitions, an append-only log
PersistenceNone in core NATS; JetStream stores to file or memoryAlways on disk, retained by time or size
DeliveryCore at-most-once; JetStream at-least-once with acksAt-least-once by default; exactly-once with idempotent producers and transactions
DeduplicationJetStream message IDs inside a duplicate windowIdempotent producer per partition, transactions
OrderingPer stream, or per subject with a filtered consumerPer partition, by record key
ReplayFrom a sequence or time on a JetStream consumerFrom any retained offset per consumer group
OperationsA single Go binary, clusters with Raft for JetStreamBrokers and KRaft controllers, partition planning
Current versionnats-server v2.15.3 (November 2026)Kafka 4.4.0
Managed on AWS or GCPNo first-party serviceAmazon MSK; Google Cloud Managed Service for Apache Kafka

Versions come from the nats-server releases and the Kafka downloads page, checked on November 18, 2026.

Delivery and deduplication

Core NATS delivers a message to whoever is subscribed at that moment and forgets it, which suits request-reply and live fan-out but loses messages for a subscriber that is down. JetStream stores messages in a stream and tracks acknowledgments per consumer, so a crashed worker gets them again. A publisher that sets a message ID lets JetStream drop the duplicate when a publish is retried inside the stream's duplicate window, two minutes by default.

Kafka keeps every record until retention removes it, and consumers commit offsets instead of acknowledging single messages. An idempotent producer avoids duplicates from producer retries within a partition, and transactions extend that to read-process-write pipelines inside Kafka. Neither system makes side effects in your own database exactly-once, so the consumer still has to be idempotent on a business key.

Ordering and parallelism

Kafka orders records within a partition, and the number of partitions caps how many consumers in a group can work in parallel. Picking the record key, usually a customer or order ID, decides which events stay in order. JetStream orders messages within a stream, and parallel consumption comes from pull consumers or from splitting the subject space, so ordering per key is something you design into subjects.

Running them

A NATS server is one Go binary with a small config file, and a three-node cluster with JetStream replication is quick to stand up on VMs or Kubernetes. Kafka needs brokers and KRaft controllers, disk sizing for retention, and partition counts that are hard to reduce later. Kafka 4.0 removed ZooKeeper, which took one system out of the picture. On AWS and GCP the managed path exists only for Kafka, through Amazon MSK or Google Cloud Managed Service for Apache Kafka, while NATS runs on your own instances or on a third-party host.

JetStream in Go

This creates a stream for orders.>, publishes with a message ID, and consumes with explicit acks and a delivery limit, using nats.go v1.56.0 and its jetstream package:

package main import ( "context" "log" "time" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" ) func main() { ctx := context.Background() nc, err := nats.Connect(nats.DefaultURL) if err != nil { log.Fatal(err) } defer nc.Drain() js, err := jetstream.New(nc) if err != nil { log.Fatal(err) } _, err = js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{ Name: "ORDERS", Subjects: []string{"orders.>"}, MaxAge: 7 * 24 * time.Hour, Duplicates: 2 * time.Minute, }) if err != nil { log.Fatal(err) } // The message ID lets JetStream drop a retried publish inside the duplicate window. _, err = js.Publish(ctx, "orders.placed", []byte(`{"order_id":"o-1"}`), jetstream.WithMsgID("o-1")) if err != nil { log.Fatal(err) } cons, err := js.CreateOrUpdateConsumer(ctx, "ORDERS", jetstream.ConsumerConfig{ Durable: "send-receipt", FilterSubject: "orders.placed", AckPolicy: jetstream.AckExplicitPolicy, MaxDeliver: 8, }) if err != nil { log.Fatal(err) } cc, err := cons.Consume(func(msg jetstream.Msg) { log.Printf("receipt for %s", msg.Data()) msg.Ack() }) if err != nil { log.Fatal(err) } defer cc.Stop() time.Sleep(time.Second) }

The Kafka equivalent is a topic with a partition count and retention, a producer with enable.idempotence=true keyed by order ID, and a consumer group named send-receipt that commits offsets after processing.

What if you need neither?

Encore

Many backends reach for NATS or Kafka to decouple services, retry failed work and fan events out to several consumers, and never replay last week's events. For that job, Encore brings infrastructure from code: a topic and its subscriptions are Go declarations, encore run runs them locally, and a deploy provisions SNS and SQS on AWS or Google Cloud Pub/Sub on GCP in your own account, with a dead-letter queue per subscription, per the Pub/Sub docs. There are no brokers to run, and each subscription works like its own consumer group.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

// orders/events.go package orders import ( "context" "time" "encore.dev/pubsub" ) type OrderPlaced struct { OrderID string `json:"order_id"` CustomerID string `json:"customer_id"` } var OrdersPlaced = pubsub.NewTopic[*OrderPlaced]("orders-placed", pubsub.TopicConfig{ DeliveryGuarantee: pubsub.AtLeastOnce, }) // Two subscriptions each get every message, like two consumer groups. var _ = pubsub.NewSubscription(OrdersPlaced, "send-receipt", pubsub.SubscriptionConfig[*OrderPlaced]{ Handler: func(ctx context.Context, e *OrderPlaced) error { return nil // send the receipt, keyed on e.OrderID }, RetryPolicy: &pubsub.RetryPolicy{MinBackoff: 10 * time.Second, MaxRetries: 8}, }) var _ = pubsub.NewSubscription(OrdersPlaced, "reserve-stock", pubsub.SubscriptionConfig[*OrderPlaced]{ Handler: func(ctx context.Context, e *OrderPlaced) error { return nil // reserve inventory in a transaction }, })

Replay is where Encore Pub/Sub stops: it delivers and retries messages, and once a subscription acknowledges a message it is gone, so event sourcing, stream processing or rebuilding a read model from history still calls for Kafka or a JetStream stream. A coding agent adding a new consumer here writes one more NewSubscription in the service that needs it and tests it with encore run, without broker addresses or credentials in the code. Encore provisions an IAM role for the service, or a service account on GCP.

Best fit: Go or TypeScript backends on AWS or GCP that need decoupled services, retries and fan-out, without operating a broker.

How to choose

Pick Kafka when the log is the product: event sourcing, replaying history into new consumers, stream processing and data pipelines with Kafka Connect, ideally on a managed service. Pick NATS when you want low-latency request-reply and fan-out with optional persistence from one small binary you are happy to run. Pick Encore when the goal is decoupling services in your own AWS or GCP account with managed queues, retries and dead-letter handling, and replay is not a requirement.

Frequently asked questions

Is NATS faster than Kafka?

Yes, for small request-reply and fan-out messages: core NATS has lower latency because it does not write to disk, and JetStream adds persistence when you need it. Kafka is built for sustained high-throughput streams written to a partitioned log, where it batches and compresses records. The faster choice depends on whether the workload is many small messages or a large ordered stream.

Does NATS replace Kafka?

Yes, for many event-driven backends. JetStream covers persistent streams, replay, at-least-once delivery and deduplication, which can replace Kafka for those workloads. Kafka still has the larger ecosystem for stream processing and data pipelines, such as Kafka Connect, Kafka Streams and the many sink and source connectors built on them.

Does Kafka still need ZooKeeper?

No. Kafka 4.0 removed ZooKeeper, and clusters run in KRaft mode, where Kafka's own controllers manage metadata. The current release on the Apache downloads page in November 2026 is Kafka 4.4.0.

Can I use managed Pub/Sub instead of NATS or Kafka with Encore?

Yes. Declare a topic and subscriptions with encore.dev/pubsub in Go, and Encore provisions SNS and SQS on AWS or Google Cloud Pub/Sub on GCP in your account, with retries and dead-letter queues per subscription. It does not keep a replayable log of acknowledged messages, so workloads that replay history still need a log like Kafka or a JetStream stream.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders