node.js and cassandra

18

Click here to load reader

Upload: stratio

Post on 10-May-2015

4.318 views

Category:

Technology


3 download

DESCRIPTION

Jorge Bay explaining his driver Node.js and Cassandra at the Madrid Cassandra Users meetup.

TRANSCRIPT

Page 1: Node.js and Cassandra

Node.js and CassandraFor highly concurrent systems

Page 2: Node.js and Cassandra

Software as a service

Most common scenario

• I/O Boundo dbo other serviceso file system

• Low CPU usage

• Peaks and valleys

Page 3: Node.js and Cassandra

Why Node.js

Page 4: Node.js and Cassandra

Why Node.js

Event-based

• Single threaded

• Minimum overhead per connection

• Predictable amount of memory under load

• Apache / IIS vs Nginx: Process-based vs event-loop

Everything runs in parallel except your code

Page 5: Node.js and Cassandra

Why Node.js

Event-based: Apache vs Nginx

source webfaction.com

Page 6: Node.js and Cassandra

Why Node.js

Async I/O

• Uses OS network interfaces + fixed thread pool

• Time spent: db connections / files

Page 7: Node.js and Cassandra

Why Node.js

Javascript: Closures

• CPS: Continuation passing style

• The scope of the outer function -> inner function

• Javascript: Functional / Dynamic / Object oriented

• ... packet manager / os community / V8 / ubiquitous...

Page 8: Node.js and Cassandra

Why Node.js

Stream everything

• Avoid buffering

• HTTP: Chunked requests and responses

• TCP: Chunks readable in a stream

• Stream Piping (UNIX like)

Page 9: Node.js and Cassandra

The Driver for Cassandra

Page 10: Node.js and Cassandra

Features

• Connection pooling to multiple hosts

• Load balancing

• Automatic failover / retry

• Row and field streaming

• Queuing: Concurrent connecting / preparing

The Driver for Cassandra

Page 11: Node.js and Cassandra

The Driver for Cassandra

A

B

CD

G E

F H

App Cassandra nodes

Page 12: Node.js and Cassandra

Sample: Json Web Api

The Driver for Cassandra

app.get('/user/:id', function (req, res, next){

var query = 'SELECT * FROM users WHERE id = ?';

cassandra.executeAsPrepared(query, [req.params.id], function (err, result) {

if (err) return next(err);

var row = result.rows[0];

//Response: expose some properties of the user

res.json({id: req.params.id, name: row.get('name')});

});

});

1

2

3

4

5

6

7

8

9

Page 13: Node.js and Cassandra

The Driver for Cassandra

How row streaming works

Socket Protocol Parser

Readablestream

Transformstream

Chunks

Header and body chunk

s

Row

Client

Transformstream

Page 14: Node.js and Cassandra

Sample: Field streaming

The Driver for Cassandra

app.get('/user/:id/image', function (req, res, next){

var query = 'SELECT id, profile_image FROM users WHERE id = ?';

cassandra.streamField(query, [req.params.id], function (err, row, image) {

if (err) return next(err);

//pipe the image stream to the response stream

image.pipe(res);

});

});

1

2

3

4

5

6

7

8

Page 15: Node.js and Cassandra

Sample: Field streaming + image resizing

The Driver for Cassandra

app.get('/user/:id/image', function (req, res, next){

var query = 'SELECT id, profile_image FROM users WHERE id = ?';

cassandra.streamField(query, [req.params.id], function (err, row, image) {

if (err) return next(err);

//pipe the image stream to a resizer stream

image.pipe(resizer).pipe(res);

});

});

1

2

3

4

5

6

7

8

Page 16: Node.js and Cassandra

Moving forward

Next features• Multiple data centers support.

• Cassandra query tracing

Contribute! :)

Page 17: Node.js and Cassandra

Jorge Bay Gondra

@jorgebg

[email protected]

github.com/jorgebay/node-cassandra-cql

npm install node-cassandra-cql

Thanks!

Page 18: Node.js and Cassandra

References and further reading

A Design Framework for Highly Concurrent Systems by Matt Welsh, Steven D. Gribble, Eric A. Brewer, and David Culler @ UC Berkeley

Concurrency is not Parallelism (it's better) by Rob Pike @Google Go lang

How the single threaded non blocking IO model works in Node.js