Multi-tenancy with NATs

Shantanu Bansal
4 min readJan 31, 2023

--

NATS is a powerful message broker that enables seamless communication between two systems through the exchange of messages. Its lightweight design makes it an ideal solution for developers looking to create distributed, scalable client-server applications that can easily store and distribute real-time data across a wide range of environments, languages, cloud providers, and on-premise systems.

NATS is considered a “message oriented middleware” — a software infrastructure that facilitates the exchange of data segmented into messages between various computer applications and services. This allows developers to easily and efficiently create applications that can handle high levels of traffic and data transfer, without the need for complex and resource-intensive solutions.

One of the key benefits of NATS is its ability to handle large volumes of data in real-time, making it a popular choice for applications that require low latency and high performance. Additionally, its support for multiple languages and cloud providers means that it can be easily integrated into a wide range of systems and environments, making it a versatile solution for developers across a variety of industries.

Overall, NATS is a powerful and efficient message broker that provides a simple and effective way for developers to create distributed, scalable applications that can handle large volumes of real-time data. Whether you’re working in a large enterprise environment or a small startup, NATS can help you create powerful and reliable applications that can handle even the most demanding workloads. So, NATS is a perfect solution for developers who are looking to create distributed scalable client-server applications and generally store and distribute in real-time data between applications across different environments, languages, cloud providers and on-premise systems.

How NATs handle Multi-Tenancy?

NATS is a message broker that utilizes the concept of “subjects” to scope and organize messages. A subject is essentially an address that is used to specify where a message should be delivered. Publishers will send messages on a particular subject, and subscribers will listen for messages on the same subject. This allows for efficient and targeted communication between different systems and applications.

Subjects in NATS are simply strings of characters that are used to form a name that both the publisher and subscriber can use to find each other. This naming convention allows for a high degree of flexibility and organization in message exchange.

To create a hierarchical structure within the subject naming system, NATS uses the “.” (dot) character. For example, a message exchange system that is based on countries might group their messages logically, like:

continent.asia
continent.northamerica
continent.asia.country.india
continent.northamerica.country.us
Publisher Publishes at 
country.india.state.rajasthan.msg
There are three subscribers
country.india.state.*.msg
country.india.state.rajasthan.msg
country.india.state.karnataka.msg

The message will be delivered to the first two subjects.

This allows for easy and logical grouping of messages, making it simple to target specific regions or countries. Additionally, it allows for more granular control over messaging, so that messages can be directed to specific subsets of subscribers, rather than broadcasting to all subscribers indiscriminately.

In conclusion, NATS uses the concept of “subjects” to scope and organize messages, which allows for efficient and targeted communication between different systems and applications. The subject naming convention is simple, flexible and logical, making it easy to group and target messages to specific subsets of subscribers. This makes NATS a powerful and efficient message broker that can be used in a wide range of industries and environments.

In order to achieve multi-tenancy in NATS, the system utilizes NATS Accounts. This is particularly useful in modern micro-service architecture, where it is common to share infrastructure while maintaining isolation between various tenant contexts.

NATs Accounts are isolated communication contexts that provide a secure way to separate technology from business use-cases. This enables the concept of multi-tenancy in communication, making it easy to implement and manage different tenants within the system.

Each NATS account can be thought of as a tenant, with its own unique flow of communication. Other accounts will not able to publish and subscribe to messages outside a given account. If there is a need to share data between accounts, it can be done using streams. This helps to maintain a clear separation between the different tenants and their respective communication flows, while still allowing for the sharing of data as needed.

Overall, NATS Accounts provide a powerful and flexible way to implement multi-tenancy in communication systems. By providing isolated and secure communication contexts, it becomes easy to manage and maintain different tenants within the system, while still allowing for the sharing of data as needed. This makes NATS a powerful and efficient message broker that can be used in a wide range of industries and environments that require multi-tenancy support.

--

--