Avoiding the Thundering Herd

    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:

    {% tabs %} {% tab title=”Go” %}

    {% endtab %}

    1. server("nats://demo.nats.io:4222").
    2. noRandomize(). // Disable reconnect shuffle
    3. build();
    4. Connection nc = Nats.connect(options);
    5. // Do something with the connection
    6. nc.close();

    {% endtab %}

    {% tab title=”JavaScript” %}

    {% endtab %}

    1. nc = NATS()
    2. await nc.connect(
    3. servers=[
    4. "nats://demo.nats.io:1224"
    5. ],
    6. dont_randomize=True,
    7. )
    8. # Do something with the connection
    9. await nc.close()

    {% endtab %}

    {% tab title=”Ruby” %}

    {% endtab %}

    1. natsConnection *conn = NULL;
    2. natsOptions *opts = NULL;
    3. natsStatus s = NATS_OK;
    4. s = natsOptions_Create(&opts);
    5. if (s == NATS_OK)
    6. s = natsOptions_SetServers(opts, servers, 3);
    7. if (s == NATS_OK)
    8. s = natsOptions_SetNoRandomize(opts, true);
    9. if (s == NATS_OK)
    10. s = natsConnection_Connect(&conn, opts);
    11. (...)
    12. // Destroy objects that were created

    {% endtab %} {% endtabs %}