Powered By GitBook
Buffering Messages During Reconnect Attempts
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.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Set reconnect buffer size in bytes (5 MB)
2
nc, err := nats.Connect("demo.nats.io", nats.ReconnectBufSize(5*1024*1024))
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer nc.Close()
7
8
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://demo.nats.io:4222").
3
reconnectBufferSize(5 * 1024 * 1024). // Set buffer in bytes
4
build();
5
Connection nc = Nats.connect(options);
6
7
// Do something with the connection
8
9
nc.close();
Copied!
1
// Reconnect buffer size is not configurable on NATS JavaScript client
Copied!
1
# Asyncio NATS client currently does not implement a reconnect buffer
Copied!
1
# There is currently no reconnect pending buffer as part of the Ruby NATS client
Copied!
1
// Reconnect buffer size is not configurable on NATS TypeScript client
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
// Set reconnect buffer size in bytes (5 MB)
8
s = natsOptions_SetReconnectBufSize(opts, 5*1024*1024);
9
if (s == NATS_OK)
10
s = natsConnection_Connect(&conn, opts);
11
12
(...)
13
14
// Destroy objects that were created
15
natsConnection_Destroy(conn);
16
natsOptions_Destroy(opts);
Copied!
As mentioned throughout this document, each client library may behave slightly differently. Please check the documentation for the library you are using.
Last modified 10mo ago
Export as PDF
Copy link