Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Designing Microservices Platforms with NATS

You're reading from  Designing Microservices Platforms with NATS

Product type Book
Published in Nov 2021
Publisher Packt
ISBN-13 9781801072212
Pages 356 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Chanaka Fernando Chanaka Fernando
Profile icon Chanaka Fernando

Table of Contents (15) Chapters

Preface 1. Section 1: The Basics of Microservices Architecture and NATS
2. Chapter 1: Introduction to the Microservice Architecture 3. Chapter 2: Why Is Messaging Important in the Microservice Architecture? 4. Chapter 3: What Is NATS Messaging? 5. Section 2: Building Microservices with NATS
6. Chapter 4: How to Use NATS in a Microservice Architecture 7. Chapter 5: Designing a Microservice Architecture with NATS 8. Chapter 6: A Practical Example of Microservices with NATS 9. Chapter 7: Securing a Microservices Architecture with NATS 10. Chapter 8: Observability with NATS in a Microservices Architecture 11. Section 3: Best Practices and Future Developments
12. Chapter 9: How Microservices and NATS Coexist with Integration Platforms 13. Chapter 10: Future of the Microservice Architecture and NATS 14. Other Books You May Enjoy

Chapter 3: What Is NATS Messaging?

Modern enterprise applications are distributed in nature. These distributed applications require a communication medium to share data between components. Messaging is the approach that distributed systems use for this purpose. NATS is a modern messaging framework that allows developers and operators to build distributed systems. NATS makes it easier for programs to communicate across different environments, languages, cloud platforms, and on-premises systems. NATS is designed to meet the needs of modern distributed systems while supplying a secure and simple API for users. It is used in real-world applications such as the following:

  • Microservice-based applications:
    • Service mesh and inter-service messaging
  • Event streaming applications:
    • Observability/analytics
    • Machine learning
    • Artificial intelligence
  • Command and control-based systems:

Internet of Things (IoT) and edge computing

  • Telemetry/sensor data/command and control
  • ...

Technical requirements

In this chapter, we will install the NATS server and configure it to demonstrate certain aspects of the NATS messaging system. Hence, you should have a basic understanding of using the command-line tools based on the operating system you are using. As a prerequisite, you can install the NATS server by following the instructions mentioned in the official NATS documentation: https://docs.nats.io/nats-server/installation. We will be using NATS server version 2.x (v2.2.2) to demonstrate the examples in this chapter.

History of NATS

NATS messaging was initially developed for the Cloud Foundry platform as the message bus that was used by internal components to share data. It was initially written in the Ruby programming language since most of the Cloud Foundry components were developed using the same language. The Ruby-based implementation was performing well enough with over 75,000 messages per second being processed by a single NATS server running on commodity hardware. But NATS creator Derek Collison wanted to go even faster.

That is when he decided to rewrite the NATS server and the client using the Go programming language in 2012. This version of the server was processing 2 million messages per second, and it kept improving with time. By now, it can process around 18 million messages per second. NATS also supports cloud-native systems such as microservices and works well with containers and container orchestration platforms such as Kubernetes. Due to the cloud-native support provided by...

Introduction to NATS

NATS is a messaging platform built to meet the distributed computing needs of modern applications. It addresses distributed computing needs such as the following:

  • Secure communications between services
  • Service discovery
  • Resiliency
  • Agile development
  • Scalability
  • Performance

These requirements are fulfilled by the unique design of the NATS messaging framework, which comes with features such as the following:

  • High performance: NATS performs better than most of the existing message broker products, including Kafka and RabbitMQ.
  • Lightweight: It does not need sophisticated hardware and complex deployment models to support large message volumes.
  • Simple to use: It supplies a simple API to use the system.
  • At most once and at least once delivery: Supports message guarantees required by applications.
  • Support for event handling: It can handle event streams with better performance.
  • Support for different languages...

How does the NATS protocol work?

At the beginning of this chapter, we mentioned that NATS is designed to cater to the requirements of modern distributed systems while providing a simple interface to work with the clients. That is why the NATS protocol, which defines the interaction between the NATS server and the NATS client, has a simple set of commands that are based on plain text messages with a publish and subscribe type model. A simple TCP client such as Telnet can communicate with the NATS server using these protocol commands.

Protocol conventions

The NATS protocol uses a set of conventions to make sure that both the client and server understand the messages that are shared:

  • Control line with optional content: Every communication that happens between the client and the NATS server consists of a command message followed by optional data. Only the PUB and MSG commands carry a payload (data) with the command.
  • Field delimiters: The different parameters and options...

Advanced NATS concepts

Using NATS in production-grade systems requires advanced capabilities such as clustering, monitoring, and security. We will cover these advanced concepts in this section.

Clustering the NATS server

NATS is designed to support complex distributed systems at a global scale. NATS supports clustering to deploy multiple NATS-server nodes connected in a full mesh topology to provide improved reliability and failure handling. If a server goes down for some reason, the client can automatically connect to a different server that contains a replica of the message. NATS servers form a mesh dynamically by gossiping and connecting to all the known servers without needing to preconfigure the nodes. Hence, clusters can grow, shrink, and self-heal.

Once the cluster has been set up, the NATS servers replicate messages by forwarding the messages to connected servers. It has a forwarding limit of 1 node. Every server on the cluster forwards the messages to one of the...

Advantages of NATS

So far, we have discussed the features of the NATS messaging system and how these features can be configured with a few examples. In this section, we'll discuss the advantages of NATS in general and its advantages when comparing it with other popular messaging platforms. The following is a set of advantages of NATS as a messaging system (or message broker) in general:

  • High performance: NATS has the best performance compared to many other brokers, such as Kafka, RabbitMQ, and ActiveMQ. It can process millions of messages per second within a single server with moderate hardware. It can reduce hardware costs as well as network and management costs.
  • Resilient: NATS provides automatic scaling and self-healing capabilities to build a resilient messaging platform for users by hiding internal node failures. Also, it allows you to scale the servers up and down according to the load without letting the clients know about these changes.
  • Secure: NATS provides...

Summary

In this chapter, we discussed the concepts of the NATS messaging platform by going through the NATS protocol, messaging models, clustering, monitoring, and security with a few practical examples. We also covered some of the advantages of NATS and how it compares to other messaging platforms, such as Kafka, RabbitMQ, and gRPC. This knowledge helps developers and architects understand the NATS messaging technology concepts and how it works in the real world so that they can build effective enterprise platforms while using NATS messaging. This chapter concludes the first section of this book, where we discussed the basic concepts of the microservice architecture, messaging technologies, and the NATS technology.

In the next section, we will discuss how to use this knowledge to design a microservice architecture alongside the NATS messaging platform. In the next chapter, we will discuss how to use the NATS messaging platform in a microservice architecture for inter-service communication...

Further reading

To learn more about what was covered in this chapter, please take a look at the following resources:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Designing Microservices Platforms with NATS
Published in: Nov 2021 Publisher: Packt ISBN-13: 9781801072212
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}