localdata01
localdata01

Reputation: 657

Why is a test node.js app slow compared to running a query in the Astra CQL console?

I made an test account in datastax (https://astra.datastax.com/) and want to test cassandra.

In there homepage is an cqlsh console. If I select datas is goes very fast maybe 1ms.

If I use it with nodejs and cassandra driver it takes 2-3 seconds. And I have only ONE row.

Why it takes time? Its my code fault?

const { Client } = require("cassandra-driver");

async function run() {
  const client = new Client({
    cloud: {
      secureConnectBundle: "secure-connect-weinf.zip",
    },
    keyspace: 'wf_db',
    credentials: {
      username: "admin",
      password: "password",
    },
  });

  await client.connect();

  // Execute a query
  const rs = await client.execute("SELECT * FROM employ_by_id;");
  console.log(`${rs}`);

  await client.shutdown();
}

// Run the async function
run();

Upvotes: 0

Views: 110

Answers (1)

Erick Ramirez
Erick Ramirez

Reputation: 16293

Unfortunately, it's not an apples-for-apples comparison.

Every time your app connects to a Cassandra cluster (Astra or otherwise), the driver executes these high-level steps:

  1. Unpack the secure bundle to get cluster info
  2. Open a TCP connection over the internet
  3. Create a control connection to one of the nodes in the cluster
  4. Obtain schema from the cluster using the control connection
  5. Discover the topology of the cluster using the control connection
  6. Open connections to the nodes in the cluster
  7. Compute query plan (list of hosts to connect to based on load-balancing policy)
  8. And finally, run the query

In contrast when you access the CQL Console on the Astra dashboard, the UI automatically connects + authenticates to the cluster and when you type a CQL statement it goes through the following steps:

  1. Skipped (you're already authenticated to the cluster)
  2. Skipped (it's already connected to a node within the same local VPC)
  3. Skipped (already connected to cluster)
  4. Skipped (already connected to cluster)
  5. Skipped (already connected to cluster)
  6. Skipped (already connected to cluster)
  7. Skipped (already connected to cluster)
  8. And finally, run the query

As you can see, the CQL Console does not have the same overhead as running an app repeatedly which only has 1 CQL statement in it.

In reality, your app will be reusing the same cluster session to execute queries throughout the life of the app so it doesn't have the same overhead as just re-running the app you have above. The initialisation phase (steps 1 to 6 above) are only done when the app is started. Once it's already running, it only has to do steps 7 and 8. Cheers!

Upvotes: 1

Related Questions