Hi! In this article, I’ll explain to you how to install and use Redis Ubuntu 16.04, main Redis command and will overview of Node.js module for working with Redis.
The content table:
At first shortly what is Redis?
Redis Installation on Ubuntu 16.04
Executeing a few Redis commands from a terminal
Node.js and Redis, What shall we do with?
The Redis publish/subscribe mechanism
—How does it works
To figure out how to install a php redis client read the article How to install PHP Redis client
#At first shortly what is Redis?
Redis is a database that allows us to create a special database where we’ll be storing data like in global scope. I think this is very powerful. I’m thinking of creating a chat app and have a difficulties saving common data something like online users, and personal data for each user who is online. Redis is going to solve this problem. For simplicity you can imagine or compare Redis with a global scope, for me this is much easier to understand.
#Redis Installation on Ubuntu 16.04
For installing we have to execute the next command:
1 |
sudo apt-get install redis-server |
That’s is, and we’ll have got installed Redis, but I have to warn you that Ubuntu repository doesn’t have a modern version of Redis, it’s old 3.0.6. For my needs, it’s very appropriate variant and I will use it. A stable up to date version 4.0, for a while, even there on official site is offered last version 3.2. If you want to have additional trouble and set up the last version you could go to an official site, download from there and set up manually. Well, we have already set up our database.
We need to do one step to run this command
1 |
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.bak |
It will save a default config file and we can in any moment return back a default configuration.
For installing the newer last version Redis use PPA, execute below commands
1 2 3 |
sudo add-apt-repository ppa:chris-lea/redis-server sudo apt-get update sudo apt-get install redis-server |
and you’ll get last version 4.0.10
to verify what’s the version use this command
1 2 |
neo@neo:~$ redis-cli 127.0.0.1:6379> info |
# Executeing a few Redis commands from a terminal
For reaching a terminal type in terminal
1 2 |
neo@neo:~$ redis-cli 127.0.0.1:6379> |
Now we are inside a Redis command line interface and we can run a few commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# make this command ping # it should respond PONG # output “hello world” echo "Hello world" # close the connection quit # GET and SET # put data to database set foo 100 # get data get foo # in output we get 100 # set “Hello world!” set bar “Hello world!” # get bar get bar # output “Hello world!” # to see all keys use this command KEYS * # INCR and DECR # foo + 1 INCR foo # foo - 1 DECR foo # check if EXISTS return 1 if not return 0 EXISTS foo # removing data # remove key pair DEL foo # it return 1 # for removing all databases use FLUSHALL # check now, if variable doesn’t exist it will return (nil) get bar # expirations set greeting "Hello world" # set variable get greeting # get variable expire greeting 50 # set up 50 seconds ttl greeting # to see how much seconds are left get greeting # after 50 seconds should be 0 # SETEX SETEX greeting 30 "Hello world" # set value and expiration at the same time ttl greeting # show expiration # PERSIST SETEX greeting 130 "Hello world" # set value and expiration ttl greeting # show expiration PERSIST greeting # remove expiration ttl greeting # show expiration # for installing more than one value use MSET MSET key1 “Hello” key2 “world” # set two values get key1 get key2 # appand append key1 " World" get key1 # “Hello World” # rename varialbe RENAME key1 greeting get key1 |
We won’t break down what each command does. It’s crystal clearly understandable.
# Node.js and Redis, What shall we do with?
Good question. Redis could be used with many others languages. For node.js exists a special module called “redis”. Go to npm site and look at there.
for installing redis module enter this command
1 |
npm install redis --save |
and that’s it. Now we can work with Redis in our project. See this test example of code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
var redis = require("redis"), // create a new client // by default it will use 127.0.0.1 and 6379 // we can change it by pass createClient( port, host ); client = redis.createClient(); // if you'd like to select database 3, instead of 0 (default), call // client.select(3, function() { /* ... */ }); client.on('connect', function() { console.log('connected'); }); // set client.set( 'foo', 100, function( err, reply ) { console.log( reply ); } ); //get client.get( 'foo', function( err, reply ) { console.log( reply ); } ); // creating an object, // framework is key word, all rest are key pairs client.hmset('frameworks', 'javascript', 'AngularJS', 'css', 'Bootstrap', 'node', 'Express'); client.hgetall('frameworks', function(err, object) { console.log(object); }); // alternative syntax of creating objects // frameworks1 - key word // all reast are JSON object client.hmset('frameworks1', { 'javascript': 'AngularJS', 'css': 'Bootstrap', 'node': 'Express' }, function ( err, object ) { console.log(object); }); client.hgetall('frameworks1', function( err, object ) { console.log(object); } ); // remove client.del('frameworks', function(err, reply) { console.log(reply); }); // increment client.set('key1', 10, function() { client.incr('key1', function(err, reply) { console.log(reply); // 11 }); }); // decrement client.set('key1', 10, function() { client.decr('key1', function(err, reply) { console.log(reply); // 11 }); }); // flush all keys client.flushdb( function (err, succeeded) { console.log(succeeded); // will be true if successfull client.quit(); }); |
# The Redis publish/subscribe mechanism
Redis has pub/sub mechanism. It is used for scale your application, pub/sub is acync method for sending messages between two processes. it serves as a so calls “an event bus” or “event queue“.
#How does it work?
Simply we can subscribe to a channel by using a command SUBSCRIBE [channelname]
We can publish something to the channel by using a command PUBLISH [channelname] [a message]
This is the main commands, there exists a few additional and useful such as PSUBSCRIBE, it means pattern subscribe
and used as PSUBSCRIBE r*. r* will match any channel beginning from r, such as redis, red, reeee… etc. it could be whatever.
To showing all channels use a command PUBSUB CHANNELS