Introduction to NATs [Part 1] : A lightweight messaging platform

Ansab Iqbal
3 min readAug 5, 2023

--

NATS is a highly performant, open-source messaging system designed for building distributed systems and microservices architectures. It was developed by Derek Collison and is currently maintained by the CNCF (Cloud Native Computing Foundation). The core principles of NATS are simplicity, performance, and scalability, making it an excellent choice for various messaging needs.

The NATS Server acts as a central nervous system for building distributed applications. Official clients are available in Go, Rust, JavaScript (Node and Web), TypeScript (Deno), Python, Java, C#, C, Ruby, Elixir and a CLI in addition to 30+ community-contributed clients. Real time data streaming, highly resilient data storage and flexible data retrieval are supported through JetStream , the next generation streaming platform built into the NATS server. NATs has a really cool video on their official documentation page.

This is the first part in this multipart series so we will just cover the key concepts and why should we consider NATS. In the next parts we will implement all important patterns and concepts in code and build a real world system.

Key Concepts of NATS:

  1. Subjects: In NATS, messages are published on named subjects (topics). Subjects are simple strings, forming a hierarchical namespace. They follow a dot-separated convention, enabling pattern-based subscriptions and allowing fine-grained control over message distribution.
  2. Publishers and Subscribers: NATS follows a publish-subscribe (pub-sub) model. Publishers are responsible for sending messages to specific subjects, and subscribers express interest in receiving messages from one or more subjects. Subscribers can subscribe to subjects using wildcards for broader topic matches.
  3. Messages and Payloads: A message in NATS consists of two main parts: the subject (the destination) and the payload (the data). The payload can be any data format, such as JSON, XML, Protobuf or plain text. NATS itself is payload-agnostic, meaning it does not impose any restrictions on the message data format.
  4. Channels and Queue Groups: NATS provides the concept of channels and queue groups for message distribution. Channels are used to distribute messages to multiple subscribers evenly. Queue groups allow multiple subscribers to share the load of processing messages by implementing a load-balancing mechanism.
  5. Request-Reply Pattern: NATS supports request-reply messaging, enabling synchronous communication between services. A requester sends a message to a specific subject and waits for a response from the service that handles that subject.
  6. Security: NATS provides various security mechanisms, including authentication and encryption, to ensure secure communication between clients and servers.

Features of NATS:

  1. High Performance: NATS is known for its exceptional performance and low-latency message delivery. It achieves this by using a lightweight protocol and a non-blocking design that minimizes overhead.
  2. Scalability: NATS is designed to scale efficiently, supporting large-scale distributed systems with thousands of clients and millions of messages per second.
  3. Language Support: NATS offers client libraries for a wide range of programming languages, making it accessible and easy to integrate into various applications and services.
  4. Clustering and Redundancy: NATS supports clustering, which allows multiple NATS servers to form a cluster and share message loads. Clustering provides high availability and fault tolerance.
  5. Monitoring and Observability: NATS provides tools and integrations for monitoring and observability, allowing users to gain insights into the message traffic and system health.

In conclusion, NATS is a lightweight, high-performance messaging system that provides a flexible and efficient way to build distributed systems and microservices. Its simplicity and focus on performance make it a popular choice for cloud-native applications, IoT (Internet of Things) systems, and other scenarios that require fast and reliable messaging communication.

In the next few parts we will go over implementation of each feature and build out a real world application using NATS.

Happy Hacking !

References :

https://docs.nats.io/

--

--

Ansab Iqbal

Software/Data Engineer, passionate about Data and ML solutions, Write about anything that might make a difference. All opinions my own.