Home

Awesome

This is an implementation of the LMAX Disruptor for Node.js.

The LMAX Disruptor is a lock-free data structure and algorithm for fast message passing. The reference implementation is written in Java and supports passing messages between threads.

The implementation here supports passing messages between Node.js processes or worker threads. It’s written in C++ as a Node Addon which is callable from Javascript.

A set of tests is provided for single and multiple processes and multiple workers.

API documentation is available here. Promises (async/await), callback style and synchronous methods are supported.

Currently only POSIX shared memory is supported.

Example

Here’s a program which writes a million random numbers to a Disruptor. Run this first in one terminal.

<div class="formalpara-title">

producer.js

</div>
const { Disruptor } = require('shared-memory-disruptor');
const d = new Disruptor('/example', 1000, 4, 1, -1, true, true); // 
async function test() {
    let sum = 0;
    for (let i = 0; i < 1000000; i += 1) {
        const n = Math.floor(Math.random() * 100);
        const { buf } = await d.produceClaim(); // 
        buf.writeUInt32LE(n, 0, true);
        await d.produceCommit(); // 
        sum += n;
    }
    console.log(sum);
}
test();

Here’s another program which reads a million random numbers from the same Disruptor. Run this in a second terminal.

<div class="formalpara-title">

consumer.js

</div>
const { Disruptor } = require('shared-memory-disruptor');
const d = new Disruptor('/example', 1000, 4, 1, 0, false, true); // 
async function test() {
    let sum = 0, i = 0;
    while (i < 1000000) {
        const { bufs } = await d.consumeNew(); // 
        for (let buf of bufs) {
            for (let j = 0; j < buf.length; j += 4) {
                sum += buf.readUInt32LE(j, true);
                i += 1;
            }
        }
        d.consumeCommit(); // 
    }
    console.log(sum);
}
test();

Both programs print the same result.

Streaming example

You can produce and consume arbitrary data through streams.

<div class="formalpara-title">

consumer.js

</div>
// You must run the consumer before the producer
const { Disruptor, DisruptorReadStream } = require('shared-memory-disruptor');

const d = new Disruptor('/stream', 1000, 1, 1, 0, true, false);
const rs = new DisruptorReadStream(d)

rs.pipe(process.stdout);
<div class="formalpara-title">

producer.js

</div>
const { Disruptor, DisruptorWriteStream } = require('shared-memory-disruptor');

process.stdin.pipe(
    new DisruptorWriteStream(
        new Disruptor('/stream', 1000, 1, 1, 0, false, false)
    )
);

First start the consumer by running on a terminal window:

node consumer.js

This will initialize the memory.

On a new terminal, pipe any data to the producer. For example,

{ while true; do echo $RANDOM; sleep 0.1; done; } | node producer.js

Install

npm install shared-memory-disruptor

Licence

MIT

Test

grunt test

Coverage

grunt coverage

LCOV results are available here.

Coveralls page is here.