level-rpc

Super fast rpc mechanism for LevelUp

npm install level-rpc
3 downloads in the last week
12 downloads in the last month

level-rpc

Super fast rpc mechanism for LevelUp

Build Status

browser support

Usage

Fire up a server:

var net = require('net');
var Server = require('level-rpc').Server;

var server = new Server('/tmp/db');

net.createServer(function (con) {
  con.pipe(server.createStream()).pipe(con);
}).listen(8999);

And connect to it from a client:

var net = require('net');
var Client = require('level-rpc').Client;

var db = new Client();

var rpcStream = db.createRPCStream();
rpcStream.pipe(net.connect(8999)).pipe(rpcStream);

db.put('foo', 'bar', function (err) {
  if (err) throw err;

  db.get('foo', function (err, value) {
    if (err) throw err;
    console.log(value);
    // => bar
  })
});

Status

  • [X] stringify
  • [X] parse
  • [X] string encoding
  • [ ] buffer encoding
  • [X] rpc server
  • [X] rpc client
  • [ ] reconnection logic
  • [X] db#get(key, cb)
  • [X] db#put(key, value, cb)
  • [ ] db#del(key, cb)
  • [ ] db#batch(opts, cb)
  • [ ] db#create*Stream()
  • [ ] db#approximateSize()

Motivation

multilevel is nice but slow and doesn't support binary data. Instead of going after feature parity first this module is all about speed and binary support, feature parity with levelup will come later.

Protocol

| METHOD | CB ID | FIELDCOUNT | F1LENGTH | F1 | ...

METHOD = {METHOD}{EVENT}, e.g. 10 for {GET}{REQUEST}.

Examples

GET request:

| 10    | CB ID  | 1     | KEYLENGTH | KEY  |
| UINT8 | UINT32 | UINT8 | UINT8     | UTF8 |

GET response:

| 11    | CB ID  | 2     | ERRLENGTH | ERR  | VALUELENGTH | VALUE |
| UINT8 | UINT32 | UINT8 | UINT8     | UTF8 | UINT8       | UTF8  |

PUT request:

| 20    | CB ID  | 2     | KEYLENGTH | KEY  | VALUELENGTH | VALUE |
| UINT8 | UINT32 | UINT8 | UINT8     | UTF8 | UINT8       | UTF8  |

PUT response:

| 21    | CB ID  | 1     | ERRLENGTH | ERR  |
| UINT8 | UINT32 | UINT8 | UINT8     | UTF8 |

Benchmarks

 ∴  bench (master) : node index.js
client-server-local.bench.js (1/5)
put x 54,369 ops/sec ±1.75% (94 runs sampled)
get x 56,759 ops/sec ±1.39% (93 runs sampled)

client-server-tcp.bench.js (2/5)
put x 56,506 ops/sec ±1.98% (80 runs sampled)
get x 54,801 ops/sec ±1.83% (89 runs sampled)

levelup.bench.js (3/5)
put x 119,362 ops/sec ±1.53% (78 runs sampled)

parse.bench.js (4/5)
get x 1,412,276 ops/sec ±0.57% (94 runs sampled)
put x 1,090,251 ops/sec ±0.38% (88 runs sampled)

stringify.bench.js (5/5)
get x 787,139 ops/sec ±0.58% (96 runs sampled)
put x 564,162 ops/sec ±0.31% (89 runs sampled)
npm loves you