The NATS client libraries try as much as possible to be fire and forget. One of the features that may be included in the library you are using is the ability to buffer outgoing messages when the connection is down.
During a short reconnect, the client can allow applications to publish messages that, because the server is offline, will be cached in the client. The library will then send those messages once reconnected. When the maximum reconnect buffer is reached, messages will no longer be publishable by the client and an error will be returned.
Be aware, while the message appears to be sent to the application it is possible that it is never sent because the connection is never remade. Your applications should use patterns like acknowledgements to ensure delivery.
For clients that support this feature, you are able to configure the size of this buffer with bytes, messages or both.
// Set reconnect buffer size in bytes (5 MB)nc,err:=nats.Connect("demo.nats.io",nats.ReconnectBufSize(5*1024*1024))iferr!=nil{log.Fatal(err)}defernc.Close()// Do something with the connection
Optionsoptions=new Options.Builder().server("nats://demo.nats.io:4222").reconnectBufferSize(5*1024*1024).// Set buffer in bytesbuild();Connectionnc=Nats.connect(options);// Do something with the connectionnc.close();
// Reconnect buffer size is not configurable on NATS Javascript client
# Asyncio NATS client currently does not implement a reconnect buffer
# There is currently no reconnect pending buffer as part of the Ruby NATS client
// Reconnect buffer size is not configurable on NATS Typescript client
As mentioned throughout this document, each client library may behave slightly differently. Please check the documentation for the library you are using.
natsConnection *conn = NULL;
natsOptions *opts = NULL;
natsStatus s = NATS_OK;
s = natsOptions_Create(&opts);
if (s == NATS_OK)
// Set reconnect buffer size in bytes (5 MB)
s = natsOptions_SetReconnectBufSize(opts, 5*1024*1024);
if (s == NATS_OK)
s = natsConnection_Connect(&conn, opts);
(...)
// Destroy objects that were created
natsConnection_Destroy(conn);
natsOptions_Destroy(opts);