Introduction to Redis + Node.js

Redis is used as a database and for cache since it’s super fast due to the fact that the data is stored “in-memory” contrary to other databases in which the data is usually stored “on-disk”.

Persistence

Even though the data is in memory, a snapshot is taken to save the current database contents into the disk which is great to recover from unexpected server shutdowns with only loosing the last few minutes of information, this is called RDB and it’s enabled by default saving, every couple of minutes after X number of changes in the database.

If you don’t want to take the risk of loosing your last few minutes of information in case of an unscheduled shutdown you can activate the AOF persistence which creates bigger files but saves on every change made to your database.

To enable AOF run the following command in the redis installation folder:

redis-cli config set appendonly yes

Likewise, to disable RDB just do:

redis-cli config set save ""

Make it work in node.js

In case you don’t have it install node.js (here)

Install redis (here)

Install Redis Desktop (here) which should help you navigate your current DB’s (optional)

Go to redis folder and run redis-server with the command line or run it as a service/daemon (instruction at the end of the article)

with the command line or run it as a service/daemon (instruction at the end of the article) To check if the server is running you can either use Redis Desktop and connect to the server (127.0.0.1 for localhost) or in the command line type redis-cli ping (it should respond with ‘PONG’)

(it should respond with ‘PONG’) Create a folder for the project and run npm init

Install redis for node.js with npm install redis --save

Create an index.js file with the code below and run it with node index.js :

var redis = require('redis');

var client = redis.createClient(); client.on('error', function(err){

console.log('Something went wrong ', err)

}); client.set('my test key', 'my test value', redis.print);

client.get('my test key', function(error, result) {

if (error) throw error;

console.log('GET result ->', result)

});

This will create a record in the database which you can access with Redis Desktop

or in the command line:

redis-cli get 'my test key'

And that is it for a simple record creation in redis using node.js. You can find more of redis with node.js here.

Data types

The data types include:

Strings

Lists

Sets (sorted or otherwise)

Hashes

Bitmaps

HyperLogLogs

There are various ways to access and set those data types in redis, to show you a couple:

Strings

client.set('my string', 'this is a string', redis.print); client.get('my string', function(err, result) { console.log(result); // this is a string });

How it looks in Redis Desktop

Sets

client.hset('HSET record', 'key', 'value', redis.print); client.hset('HSET record', 'second key', 'second value', redis.print); client.hgetall('HSET record', function(err, result) { console.log(JSON.stringify(result)); // {"key":"value","second key":"second value"} });

How it looks in Redis Desktop

You can find a complete list of commands in the redis documentation page here.

Run as a daemon (OSX/Linux)

To create a daemon on Linux just launch the server with:

redis-server --daemonize yes

To check if the service is running as intended you can run:

ps -ef | grep redis

To stop the server again:

redis-cli shutdown

Run as a service (Windows)

To create a service for windows just run the following command in the redis directory:

redis-server — service-install redis.windows.conf — loglevel verbose — maxmemory 100mb

If it doesn’t work for your redis version try maxheap instead of maxmemory .