Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

somrcha1_186924 avatar image
somrcha1_186924 asked ·

How does the node.js driver pick a node to connect to?

nodejs driver uses a list of nodes and makes the connections based on availability but how to decide which node it will connect.


1> Lets say we have nodejs configuration as below

const cassandra = require("cassandra-driver"); const clientOptions = {    contactPoints: ["host1", "host2"],    keyspace: "test"

In above case lets say we have 2 datacenters. host1 in housed in DataCenter1 and host2 in DataCenter2.

So incase of nodejs configuration should we use Contactpoints

contactPoints: ["host1", "host2"] for Client near DC1

And contactPoints: ["host2", "host1"] for client near DC2??

So that Client near DC2 will connect to host2 first rather than host1??

[Edited with second question posted in #8622 and third question posted in #8623]

node.js driver
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered ·

The drivers use the contact points to establish a control connection to discover information about the cluster.

Dedicated admin connection

A control connection is a dedicated connection used by the driver for administrative tasks such as:

  • querying the system tables to learn the cluster's topology and schema,
  • check schema agreement, and
  • react to topology changes (nodes added or decommissioned) and schema changes.

This

A control connection is separate and different from regular pooled connections used for read and write requests.

The Java driver docs has further details on Control connections which applies to the Node.js driver and all DataStax drivers.

Picking a coordinator

The default load balancing policy in the Node.js driver (like the other DataStax drivers) only picks nodes in the local datacentre. But you need to tell the driver which DC is local.

Here's an example nodetool status output to show nodes in two DCs:

Datacenter: DC1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load      Tokens  Owns (effective)  Host ID  Rack
UN  10.1.2.100  12.69 GB  8       100.0%            ...      rack1
UN  10.1.2.200  12.71 GB  8       100.0%            ...      rack1
UN  10.1.2.300  12.82 GB  8       100.0%            ...      rack1
Datacenter: DC2
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load      Tokens  Owns (effective)  Host ID  Rack
UN  10.1.3.500  13.04 GB  8       100.0%            ...      rack1
UN  10.1.3.400  12.93 GB  8       100.0%            ...      rack1
UN  10.1.3.600  13.15 GB  8       100.0%            ...      rack1

If the application server is located in DC1, the driver should be configured with DC1 as the local DC:

const client = new cassandra.Client({ 
  contactPoints: ['10.1.2.100', '10.1.3.400'],
  localDataCenter: 'DC1'
});

With this configuration and using the default DefaultLoadBalancingPolicy, the driver will only pick nodes in DC1 as coordinators for requests.

For a more detailed explanation of how the default load balancing policy works, see the Java driver load balancing document. Note that the concepts in that document also apply to the Node.js driver's default load balancing policy. Cheers!

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks a lot Erick. Its helpful.

Could you please let me understand in case a single node ring can expand through 2 datacenters if we those are not too far (30 miles apart) or A datacenter can contain only one ring.

0 Likes 0 · ·

I get the sense that you haven't read the other 2 posts. Please have a look at them. Cheers!

0 Likes 0 · ·