NATS Keep-Alive Subscription in Golang

Noval Agung Prayogo
The Startup
Published in
5 min readFeb 27, 2021


Image by Marc-Olivier Jodoin

NATS as API Gateway

NATS is indeed a secure, simple, and scalable technology for our microservices platform.

Microservices itself is a huge topic. Talking about NATS implementation on this architectural style can be very broad, so let’s move on to a more detailed case, such as API gateway using NATS.

The idea of this approach is for every incoming request to the API gateway will be directed to destination service via synchronous call of nats.Conn.Request() method. The return value will be used as the response of the particular API call.

We are going to use the request-reply feature of NATS, because of that the destination service needs to subscribe to the NATS server first, only then the particular service will be able to receive messages sent by API gateway.

The publisher is the API Gateway. Incoming requests to API gateway will be directed to the destination service (which is the subscriber). Each request has a topic. And subscribers need to reply to that particular request.

Pretty straightforward, isn’t it.

High Availability on NATS

HA is a very important topic in microservices. In real life, most of the time, the HA aspect will be handled by the orchestrator.

  • On Docker Compose, use the restart: always