Skip to content
/ redjs Public

Embedded Redis-like in-memory data store written in Nodejs

License

Notifications You must be signed in to change notification settings

ctoesca/redjs

Repository files navigation

redjs

Build Status Maintainability Test Coverage

Redjs is a Redis-like in-memory data store. It can be used with Ioredis, NodeRedis and by other applications (nodejs or not).

Very often, a Nodejs application uses 'cluster' module, and is running on several processes (or several servers). Then you need to share data and send messages (pub/sub) between processes.

Redis is a good solution to do that, but sometime you want to provide a standalone application without dependencies. You can then embed Redjs server in your nodejs application (or in a dedicated nodejs application) and use it like Redis.

Notes:

  • The purpose of this module is not to compete with Redis (the performance of Redjs is about 2 to 3 times less than Redis, and there is no replication or cluster) but to provide a shared memory and a pub-sub system between nodejs processes, using "standard" client modules like ioredis, node_redis etc.
  • Redjs implements Redis protocol and behaves like Redis: so it can be used by any application.
  • All operations are performed in-memory, on master process.
  • Persistence is not yet implemented

Quick Start

Install

$ npm install ctoesca/redjs

Create and start Redjs

In a single process application:

/* create RedjsServer */
const RedjsServer = require('Redjs')      
new RedjsServer().start(6379)

If you use 'cluster' module, RedjsServer must be created in master process:

if (cluster.isMaster){

    /* MASTER PROCESS */
    
    const numProcesses = 2
    for (var i=0; i<numProcesses; i++)
	cluster.fork();
		
    cluster.on('exit', (worker, code, signal) => {
    	console.log(`worker ${worker.process.pid} died code=`+code);
	cluster.fork();
    })
    
    /* create RedjsServer */
    const RedjsServer = require('Redjs')      
    new RedjsServer().start(6379)
    
}else{
    /* 
    WORKER PROCESS
    */				
}

Using Redjs server with your favorite client library (Ioredis in this example)

const Redis = require('ioredis');
var redis = new Redis();

redis.set('foo', 'bar');
redis.get('foo', function (err, result) {
  console.log(result);
});

// Or using a promise if the last argument isn't a function
redis.get('foo').then(function (result) {
  console.log(result);
});

// Arguments to commands are flattened, so the following are the same:
redis.sadd('set', 1, 3, 5, 7);
redis.sadd('set', [1, 3, 5, 7]);

Using Redjs with redis-cli

Of course, you can connect to redjs with redis-cli program (provided with redis):

redis-cli -p 6379
127.0.0.1:6379> hset hash1 var1 foo
(integer) 1
127.0.0.1:6379> hget hash1 var1
"foo"
127.0.0.1:6379> hgetall hash1
1) "var1"
2) "foo"

Available commands (work in progress...)

Pub/sub

  • subscribe
  • unsubscribe
  • publish
  • psubscribe
  • punsubscribe

Lists

  • lpush
  • rpush
  • llen
  • rpop
  • lpop
  • lindex
  • lset
  • linsert
  • lrange
  • lset

Hashes

  • hkeys
  • hlen
  • hget
  • hset
  • hmget
  • hmset
  • hdel
  • hexists
  • hgetall
  • hincrby
  • hincrbyfloat
  • hsetnx
  • hstrlen
  • hvals

Keys

  • del
  • exists
  • keys

Sets

  • sadd
  • srem
  • smembers
  • spop
  • sismember
  • sunion
  • scard
  • srandmember

Strings

  • get
  • set
  • incr
  • strlen

Server

  • monitor
  • flushdb
  • flushall
  • info
  • time

Connection

  • ping
  • echo
  • quit

Transactions

  • multi
  • exec
  • discard

About

Embedded Redis-like in-memory data store written in Nodejs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published