Kest

Search

Search the documentation

Microservices

NATS

Use NATS for fast request/reply and load sharing.

NATS is a small, very fast message server with request/reply built in. It is popular in microservice systems because it is simple to run and has very low latency.

app.connectMicroservice(NatsOptions("nats://nats:4222", queue = "orders"))                   // queue: instances of one service share the work
import(ClientsModule.register { client("ORDERS", NatsOptions("nats://nats:4222")) })   // in the caller's module
  • Requests use NATS's own request/reply.
  • Give the instances of a service the same queue name: each message then goes to one of them (load balancing). Without it, every instance would receive every message.
  • The client library reconnects and resubscribes by itself after a restart (tested).
  • Core NATS does not store messages: a message sent while no subscriber is connected is lost (for persistence NATS has JetStream, which this transport does not use).

When to choose it

Low-latency calls between services, many small messages, simple operations.

Your handlers are the same on every transport: see Overview. Compare all of them in Transports compared.

Read more