0

I have built an event api using php with use of cassandra and phpcassa.

Recently, I authored a node.js + helenus replacement of the same api. After I finished, I started to benchmark the node.js code with ab tool. However, after 1000+ inserts on cassandra, the connection can be lost and failover code is running. I'm inserting async to cassandra after each request occur.

I am instantiating ConnectionPool object when the node app starts. However, I use pool.connect() function in each request.

Does pool.connect() starts a new connection or binds the existing connection from ConnectionPool object ?

Notes:

In phpcassa, I didn't have such problems probably because I'm inserting to cassandra sync.

I'm using 5 cassandra nodes

aacanakin
  • 2,844
  • 4
  • 25
  • 42

1 Answers1

2

Yes, pool.connect() creates a new connection to the pool everytime it's called, so you usually only have to call it one time when the application is started, and pass that connection to any module or method that needs it.

I usually use a module to do this

module.exports = function(app, cb) {

    var domain  = require('domain'),
        d       = domain.create(),
        helenus = require('helenus'),

    d.run(function() {
        var pool = new helenus.ConnectionPool({
            hosts      : ['localhost:9160'],
            user       : "",
            password   : "",
            keyspace   : 'something',
            cqlVersion : '3.0.0'
        });

        pool.connect(function(err, keyspace){
            if(err){
                cb(err, null);
            } else {
                cb(null, pool);
            }
        });

        pool.on('error', function(err) {
            cb(err, null);
        });
    });

    d.on('error', function(err) {
        console.log('error', err.stack);
        cb(err, null);
    });
}

and call it my app with something like this (really simplified)

var express  = require('express');
var app      = this.express();
var database = require('db');

database(app, function(err, conn) {
    if (err==null) {
        // connected, do stuff
        conn.cql("SELECT * FROM table WHERE KEY = ?", [what], function(err, result) {
            if (err==null) {
                // get result
            }
        });
    }
});

I've used this with millions of records, with continuos inserts and a large number of lookups, without any issue, Cassandra performs better for me than any other DB I've tried.

adeneo
  • 312,895
  • 29
  • 395
  • 388