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”.
Redis is a data structures server. It runs in memory and you can communicate with it over the network thanks to the Redis protocol.
It shines when you want to use data structures such as sets, lists or hashes in a distributed program, be it across different machines or processes in the same machine.
Redis can also do other things such as publish/subscribe.
There are lots of use cases for such a piece of software. Here are a few typical ones in the context of a complex Web application:
* storing user session data;
* caching things like user feeds (timelines);
* implementing rate-limiting;
* implementing a URL shortener.
Pub/Sub: You can keep track of subscribers and what information they are interested in. You can use SUBSCRIBE, UNSUBSCRIBE, and PUBLISH.
Queues: You can use normal queue data structure, Redis has blocking queuecommands as well.
Real time analysis of events(stats, anti spam etc) – Using Redis primitives it’s really simple to implement a real-time tracking filtering system or a spam filtering system.
Order by user votes and time: Leaderboard type functionality(Quora/Reddit/Digg), where the score is changes over time.
Transactional support: Provides transactional support. See Transactions – Redis.
Caching: Can be used as a general purpose cache. keys can strings, or more complex types (lists, sets, hashes etc). Also allows LRU eviction of items.
Due to it’s persistence capabilities, it can be used as a datastore and not just a cache. Redis has many other builtin operations which you would otherwise have to develop at your own.
Before we begin, let’s look at the installation of Redis on the various platform.
To install Redis in Mac and Linux you can either do the manual build which is mentioned here OR use these commands to install it on go.
sudo apt-get install redis-server
brew install redis
Windows has no official package from Redis team but there is some port available which you can use at your own risk.
You can check whether redis service is running with the following command:
$ sudo systemctl status redis
As you can see, redis service is running.
If redis service is not running in your case, start it with the following command:
$ sudo systemctl start redis
After installation, you can use the following command to start the Redis server.
redis-server
You should see the following screen.
To access the Redis command line interface, run the following command from a separate terminal.
redis-cli
You should see the following screen.
Try running “PING” command and you should recieve “PONG”.
First you need to install the Redis client for NodeJS via npm.
npm install redis
Now create a file called redisDemo.js in your NodeJS project.
// redisDemo.js var redis = require('redis'); var client = redis.createClient(); // this creates a new client
By default redis.createClient() will use 127.0.0.1 and port 6379. If you have a customized ip and and a port use
var client = redis.createClient(port, host);
Now, we want to listen for the connect event to see whether we successfully connected to the redis-server. We can check for a successful connection like this.
client.on(‘connect’, function() {
console.log(‘Redis client connected’);
});
Likewise, we want to check if we failed to connect to the redis-server. Well we can listen for the error event for that.
client.on(‘error’, function (err) {
console.log('Something went wrong ’ + err);
});
Enough of Introduction. Let see example now.
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.
The data types include:
There are various ways to access and set those data types in redis, to show you a couple:
All the Redis commands are exposed as different functions on the client object. To store a simple string use the following syntax:
client.set(‘framework’, ‘AngularJS’);
Or
client.set([‘framework’, ‘AngularJS’]);
The above snippets store a simple string AngularJS against the key framework. You should note that both the snippets do the same thing. The only difference is that the first one passes a variable number of arguments while the later passes an argsarray to client.set() function. You can also pass an optional callback to get a notification when the operation is complete:
client.set(‘framework’, ‘AngularJS’, function(err, reply) {
console.log(reply);
});
If the operation failed for some reason, the err argument to the callback represents the error. To retrieve the value of the key do the following:
client.get(‘framework’, function(err, reply) {
console.log(reply);
});
client.get() lets you retrieve a key stored in Redis. The value of the key can be accessed via the callback argument reply. If the key doesn’t exist, the value of reply will be empty.
Many times storing simple values won’t solve your problem. You will need to store hashes (objects) in Redis. For that you can use hmset() function as following:
client.hmset(‘frameworks’, ‘javascript’, ‘AngularJS’, ‘css’, ‘Bootstrap’, ‘node’, ‘Express’);client.hgetall(‘frameworks’, function(err, object) {
console.log(object);
});
The above snippet stores a hash in Redis that maps each technology to its framework. The first argument to hmset() is the name of the key. Subsequent arguments represent key-value pairs. Similarly, hgetall() is used to retrieve the value of the key. If the key is found, the second argument to the callback will contain the value which is an object.
Note that Redis doesn’t support nested objects. All the property values in the object will be coerced into strings before getting stored.
You can also use the following syntax to store objects in Redis:
client.hmset(‘frameworks’, {
‘javascript’: ‘AngularJS’,
‘css’: ‘Bootstrap’,
‘node’: ‘Express’
});
An optional callback can also be passed to know when the operation is completed.
All the functions (commands) can be called with uppercase/lowercase equivalents. For example, client.hmset() and client.HMSET() are the same.
If you want to store a list of items, you can use Redis lists. To store a list use the following syntax:
client.rpush([‘frameworks’, ‘angularjs’, ‘backbone’], function(err, reply) {
console.log(reply); //prints 2
});
The above snippet creates a list called frameworks and pushes two elements to it. So, the length of the list is now two. As you can see I have passed an args array to rpush. The first item of the array represents the name of the key while the rest represent the elements of the list. You can also use lpush() instead of rpush()to push the elements to the left.
To retrieve the elements of the list you can use the lrange() function as following:
client.lrange(‘frameworks’, 0, -1, function(err, reply) {
console.log(reply); // [‘angularjs’, ‘backbone’]
});
Just note that you get all the elements of the list by passing -1 as the third argument to lrange(). If you want a subset of the list, you should pass the end index here.
Sets are similar to lists, but the difference is that they don’t allow duplicates. So, if you don’t want any duplicate elements in your list you can use a set. Here is how we can modify our previous snippet to use a set instead of list.
client.sadd([‘tags’, ‘angularjs’, ‘backbonejs’, ‘emberjs’], function(err, reply) {
console.log(reply); // 3
});
As you can see, the sadd() function creates a new set with the specified elements. Here, the length of the set is three. To retrieve the members of the set, use the smembers() function as following:
client.smembers(‘tags’, function(err, reply) {
console.log(reply);
});
This snippet will retrieve all the members of the set. Just note that the order is not preserved while retrieving the members.
This was a list of the most important data structures found in every Redis powered app. Apart from strings, lists, sets, and hashes, you can store sorted sets, hyperLogLogs, and more in Redis. If you want a complete list of commands and data structures, visit the official Redis documentation. Remember that almost every Redis command is exposed on the client object offered by the node_redis module.
Now let’s have a look at some more important operations supported by node_redis.
Sometimes you may need to check if a key already exists and proceed accordingly. To do so you can use exists() function as shown below:
client.exists(‘key’, function(err, reply) {
if (reply === 1) {
console.log(‘exists’);
} else {
console.log(‘doesn’t exist’);
}
});
At times you will need to clear some keys and reinitialize them. To clear the keys, you can use del command as shown below:
client.del(‘frameworks’, function(err, reply) {
console.log(reply);
});
You can also give an expiration time to an existing key as following:
client.set(‘key1’, ‘val1’);
client.expire(‘key1’, 30);
The above snippet assigns an expiration time of 30 seconds to the key key1.
Redis also supports incrementing and decrementing keys. To increment a key use incr() function as shown below:
client.set(‘key1’, 10, function() {
client.incr(‘key1’, function(err, reply) {
console.log(reply); // 11
});
});
The incr() function increments a key value by 1. If you need to increment by a different amount, you can use incrby() function. Similarly, to decrement a key you can use the functions like decr() and decrby().
Redis is very powerful in-memory data-store that we can use in our applications. It’s very simple to save and get data without much overhead. refer https://www.npmjs.com/package/redis for more use cases and refer https://redis.io/commands for more redis commands.
#node-js #javascript