Skip to content

Widhian Bramantya

coding is an art form

Menu
  • About Me
Menu
nats

Introduction to NATS: A Lightweight Messaging System

Posted on August 27, 2025August 27, 2025 by admin

Why Messaging Systems Matter

In modern software, applications rarely work alone. A mobile app might need to talk to a backend service. That backend service might need to talk to a payment gateway, and the payment system may need to update analytics. Instead of building a direct web of connections, developers often use a messaging system.

A messaging system acts like a mail delivery service. You don’t hand every letter directly to the receiver. Instead, you drop it at the post office, and the system takes care of delivery. This pattern makes applications simpler, faster to scale, and easier to maintain.

There are many messaging systems out there: Kafka, RabbitMQ, ActiveMQ, MQTT. Each has its own strengths and weaknesses. Among them, NATS has become popular for developers who need something lightweight, extremely fast, and cloud-native.

What is NATS?

NATS is an open-source messaging system originally built by Derek Collison in 2011. Its philosophy is very clear: simplicity, performance, and scalability.

At its core, NATS is:

  • A publish/subscribe system (you publish a message, others subscribe to it).
  • A request/reply system (like sending a question and waiting for an answer).
  • A queueing system (messages can be load-balanced between workers).

Unlike heavier systems such as Kafka, NATS can be run with a single small binary file. It takes only a few seconds to set up and can handle millions of messages per second with very low latency.

Think of NATS as the WhatsApp for your services. You send a message to a group (subject), and everyone in that group can instantly receive it.

Core Concepts in NATS

Publish/Subscribe

In this model, a publisher sends messages on a subject (like a channel name). Any number of subscribers can listen to that subject.

See also  Queue Groups in NATS: Load Balancing for Subscribers

Example:

  • A service publishes OrderPlaced events.
  • Multiple services subscribe: one for billing, one for analytics, and another for shipping.

This decouples the services. They don’t need to know each other directly, only the subject they care about.

Request/Reply

NATS also supports direct request/response messaging.

  • A client asks a question (e.g., “What is the user profile for ID 123?”).
  • Another service replies with the data.

This feels similar to making an API call but with the advantage of NATS’s speed and reliability.

Queue Groups

When multiple subscribers join the same queue group, NATS will deliver each message to only one member of the group.

This is perfect for load balancing:

  • Imagine you have 5 worker services processing incoming images.
  • Instead of every worker processing the same message, NATS ensures each worker gets a unique one

JetStream (Persistence Layer)

By default, NATS is fire-and-forget. If a subscriber is offline, it will miss the messages.

For cases where you cannot lose messages, NATS offers JetStream.
JetStream allows:

  • Message persistence (saved on disk or memory).
  • Replay (consume from the past).
  • Retention policies (keep all messages, only last X, or only while subscribers exist).

This makes NATS powerful enough for event sourcing, audit logs, or data pipelines.

Key-Value (KV) Store

Built on top of JetStream, NATS can act as a distributed key-value database.

  • Each bucket stores key-value pairs.
  • Changes are versioned, so you can watch updates like a log.
  • Useful for configuration management, feature flags, or lightweight state sharing between services.

Comparison Table of NATS Features

FeatureHow It WorksBest Use CasePersistenceDelivery Pattern
Publish/SubscribeOne publisher, many subscribers. Everyone interested in the subject gets the message.Broadcasting events (e.g., OrderPlaced)NoFan-out to all subs
Request/ReplyA client sends a request and waits for one service to reply.RPC-like interactions (e.g., get user profile)NoOne-to-one
Queue GroupsSubscribers in the same group share the load. One message goes to one worker.Task distribution, worker poolsNoLoad balancing
JetStreamMessages are stored durably, can be replayed, with policies (limits, work queue, interest).Event sourcing, audit logs, guaranteed deliveryYesConfigurable (at-least-once, etc.)
Key-Value (KV)Key-value buckets stored on top of JetStream, with versioning and watch support.Config sharing, feature flags, lightweight stateYesChange notifications
See also  NATS JetStream: Persistence and Streaming Made Simple

Related posts:

Queue Groups in NATS: Load Balancing for Subscribers

Scalability and Reliability in NATS

Request–Reply in NATS: One-to-One Messaging

Pages: 1 2
Category: NATS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Linkedin

Widhian Bramantya

Recent Posts

  • Smart Automation in PostgreSQL: Managing Time-Based Data with pg_partman and pg_cron
  • Understanding PostgreSQL WAL, Slot, Publication, LSN, and Replication Lag
  • PostgreSQL Write-Ahead Log (WAL): Durability, Performance Tuning, and Recovery Explained
  • PostgreSQL Replication Deep Dive: From High Availability to Multi-Master Clusters
  • Finding Nearby Merchants in a Ride-Hailing App Using Elasticsearch Polygon Search
  • Advanced Text Search in Elasticsearch: N-Gram, Reverse, Fuzzy, and Search-as-you-type
  • Understanding and Customizing Analyzers in Elasticsearch
  • Log Management at Scale: Integrating Elasticsearch with Beats, Logstash, and Kibana
  • Index Lifecycle Management (ILM) in Elasticsearch: Automatic Data Control Made Simple
  • Blue-Green Deployment in Elasticsearch: Safe Reindexing and Zero-Downtime Upgrades
  • Maintaining Super Large Datasets in Elasticsearch
  • Elasticsearch Best Practices for Beginners
  • Implementing the Outbox Pattern with Debezium
  • Production-Grade Debezium Connector with Kafka (Postgres Outbox Example – E-Commerce Orders)
  • Connecting Debezium with Kafka for Real-Time Streaming
  • Debezium Architecture – How It Works and Core Components
  • What is Debezium? – An Introduction to Change Data Capture
  • Offset Management and Consumer Groups in Kafka
  • Partitions, Replication, and Fault Tolerance in Kafka
  • Delivery Semantics in Kafka: At Most Once, At Least Once, Exactly Once

Recent Comments

No comments to show.

Archives

  • October 2025
  • September 2025
  • August 2025
  • November 2021
  • October 2021
  • August 2021
  • July 2021
  • June 2021
  • March 2021
  • January 2021

Categories

  • Debezium
  • Devops
  • ElasticSearch
  • Golang
  • Kafka
  • Lua
  • NATS
  • PostgreSQL
  • Programming
  • RabbitMQ
  • Redis
  • VPC
© 2026 Widhian Bramantya | Powered by Minimalist Blog WordPress Theme