Powered By GitBook
Avoiding the Thundering Herd
When a server goes down, there is a possible anti-pattern called the Thundering Herd where all of the clients try to reconnect immediately, thus creating a denial of service attack. In order to prevent this, most NATS client libraries randomize the servers they attempt to connect to. This setting has no effect if only a single server is used, but in the case of a cluster, randomization, or shuffling, will ensure that no one server bears the brunt of the client reconnect attempts.
However, if you want to disable the randomization process for connect and reconnect, so that servers are always checked in the same order, you can do that in most libraries with a connection option:
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
servers := []string{"nats://127.0.0.1:1222",
2
"nats://127.0.0.1:1223",
3
"nats://127.0.0.1:1224",
4
}
5
6
nc, err := nats.Connect(strings.Join(servers, ","), nats.DontRandomize())
7
if err != nil {
8
log.Fatal(err)
9
}
10
defer nc.Close()
11
12
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://demo.nats.io:4222").
3
noRandomize(). // Disable reconnect shuffle
4
build();
5
Connection nc = Nats.connect(options);
6
7
// Do something with the connection
8
9
nc.close();
Copied!
1
let nc = NATS.connect({
2
noRandomize: true,
3
servers: [
4
"nats://127.0.0.1:4443",
5
"nats://demo.nats.io:4222"
6
]
7
});
Copied!
1
nc = NATS()
2
await nc.connect(
3
servers=[
4
"nats://demo.nats.io:1222",
5
"nats://demo.nats.io:1223",
6
"nats://demo.nats.io:1224"
7
],
8
dont_randomize=True,
9
)
10
11
# Do something with the connection
12
13
await nc.close()
Copied!
1
require 'nats/client'
2
3
NATS.start(servers: ["nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"], dont_randomize_servers: true) do |nc|
4
# Do something with the connection
5
6
# Close the connection
7
nc.close
8
end
Copied!
1
// will throw an exception if connection fails
2
let nc = await connect({
3
noRandomize: true,
4
servers: [
5
"nats://127.0.0.1:4443",
6
"nats://demo.nats.io:4222"
7
]
8
});
9
nc.close();
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
const char *servers[] = {"nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"};
5
6
s = natsOptions_Create(&opts);
7
if (s == NATS_OK)
8
s = natsOptions_SetServers(opts, servers, 3);
9
if (s == NATS_OK)
10
s = natsOptions_SetNoRandomize(opts, true);
11
if (s == NATS_OK)
12
s = natsConnection_Connect(&conn, opts);
13
14
(...)
15
16
// Destroy objects that were created
17
natsConnection_Destroy(conn);
18
natsOptions_Destroy(opts);
Copied!
Last modified 10mo ago
Export as PDF
Copy link