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

  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. nc, err := nats.Connect(strings.Join(servers, ","), nats.DontRandomize())
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. defer nc.Close()
  10. // Do something with the connection

Java

  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. // Do something with the connection
  7. nc.close();

JavaScript

  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. });

Python

  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. # Do something with the connection
  11. await nc.close()

Ruby

  1. require 'nats/client'
  2. 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|
  3. # Do something with the connection
  4. # Close the connection
  5. nc.close
  6. end

TypeScript

  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();

C

  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. s = natsOptions_Create(&opts);
  6. if (s == NATS_OK)
  7. s = natsOptions_SetServers(opts, servers, 3);
  8. if (s == NATS_OK)
  9. s = natsOptions_SetNoRandomize(opts, true);
  10. if (s == NATS_OK)
  11. s = natsConnection_Connect(&conn, opts);
  12. (...)
  13. // Destroy objects that were created
  14. natsConnection_Destroy(conn);
  15. natsOptions_Destroy(opts);