A two-phase-commit protocol for leveldb.
Provides strong-consistency for local-cluster replication. Injectable transport (so it should work in the browser).
The algorithm for how this works is here
.
There are trade-offs to every type of replication. I will try to create some specific numbers for you soon.
var level = require('level');
var replicate = require('level-2pc');
var net = require('net');
var db1 = level('./db', { valueEncoding: 'json' });
var a = replicate.createServer(db1);
a.peers = [
{ host: 'localhost', port: 3001 },
{ host: 'localhost', port: 3002 }
];
net.createServer(function(con) {
a.pipe(con).pipe(a);
}).listen(3000);
var b = rs.createServer(db2);
b.peers = [
{ host: 'localhost', port: 3000 },
{ host: 'localhost', port: 3002 }
];
net.createServer(function(con) {
b.pipe(con).pipe(b);
}).listen(3001);
var c = rs.createServer(db3);
c.peers = [
{ host: 'localhost', port: 3000 },
{ host: 'localhost', port: 3001 }
];
net.createServer(function(con) {
c.pipe(con).pipe(c);
}).listen(3002);
Now go ahead and write some data to one of the servers and watch the data magically appear in the other servers!
setTimeout(function() {
db1.put('x', 100, function(err) {
console.log(err || 'ok');
});
setTimeout(function() {
db2.get('x', function() {
console.log(arguments);
db3.get('x', function() {
console.log(arguments);
});
});
}, 100);
}, 100);
When the server wants to connect to the peers
that have been specified, it defaults to using
tcp from the net
module. You can inject any
transportation layer you want like this...
var net = require('net');
var opts = {};
opts.transport = function() {
return net.connect.apply(null, arguments);
};
var a = replicate.createServer(db1, opts);