Interacting with Redis Cheat Sheet

Redis is elegant solution to provide fast storage while its supported by Node.js. However, with its different data structures such as List, Set and Hashes, the API of Redis takes time to memorize. Here is quick cheat sheet to help

Installation

Step 1 – Download & Compile

To install Redis:

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make

Here, we installed Stable version of Redis, but if you need some older version then see Google Code Redis repo for version of your need

Step 2 – Test It

Try if your build works correctly by typing “make test”

Step 3 – Configure Redis

After the compilation the src directory inside the Redis distribution is populated with the different executables that are part of Redis:

  • redis-server is the Redis Server itself.
  • redis-sentinel is the Redis Sentinel executable (monitoring and failover).
  • redis-cli is the command line interface utility to talk with Redis.
  • redis-benchmark is used to check Redis performances.
  • redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.

It is a good idea to copy both the Redis server and the command line interface in proper places, either manually using the following commands:

  • sudo cp src/redis-server /usr/local/bin/
  • sudo cp src/redis-cli /usr/local/bin/

Or just using:

 cd src/
 make install.

NOTE: We assume that /usr/local/bin is in your PATH environment variable so that you can execute both the binaries without specifying the full path.

Starting Redis

To start redis with default configurations:

redis-server

Or if you like to start with custom settings then pass them in at the start up as following:

redis-server /etc/redis.conf

You should use the redis.conf file included in the root directory of the Redis source code distribution as a template to write your configuration file.

Testing Redis

Redis provides a command line utility “redis-cli” that can be used to send commands to Redis. To check if Redis is working properly is sending a PING command using redis-cli

redis-cli ping

This should respond with “PONG” if Redis working properly

To see all options with redis-cli utility such as running different port,host, etc:

redis-cli --help

To start Redis in interactive mode so you can type different commands and see their replies.

$ redis-cli                                                                
redis 127.0.0.1:6379> ping
PONG
redis 127.0.0.1:6379> set mykey somevalue
OK
redis 127.0.0.1:6379> get mykey
"somevalue"

If able set values, then Redis is working properly and your are ready to next step

Connecting And Other

Command Redis Node.js
CREATE CONNECTION
 redis-cli
db = redis.createClient();
CLOSE CONNECTION
 exit 
db.end
FLUSH DATABASE
 FLUSHALL 
db.flushdb(function (err, succes) {
...
}

Hashes

Purpose: good for saving objects that have properties
For example, we have object review ->[id:1,title:’some title’,…] that contains properties and values

Command Redis Node.js
SET
Single Attribute
 HSET myhash field1 "Hello"
db.hset('cache:review:1', 
   {id: '1', title: 'some title'}, 
   function(err, args){..})
GET
Singe Attributes
 HGET myhash field1
???
SET
Multiple Attributes
 HMSET myhash 
   field1 "Hello" 
   field2 "World"
db.hmset('review:2', 
   this, 
   function(err, args){..})
GET
Multiple Attributes
 HMGET myhash field1 field2 
db.hmget('cache:review:6', 
   'remote', 
   'local', 
   function(err, args){..})
LIST ALL
attribute = values
HGETALL myhash
db.hgetall('review:3',
    function(err,review){..}
LIST ALL
ONLY values
 HVALS myhash 
???
DELETE
 HDEL myhash field2 
db.del('review:2')

Sorted Set – non repeating collections of Strings

Purpose: Its good to keeping track of Objects Stored in Redis.
For example, we have set ‘Reviews’ -> [1,3,4,6] that contains all IDs of each review saved

Command Redis Node.js
ADD
ZADD myzset 1 "one"
db.zadd('myzset', 1, "one", function(err, args){
        if(err) {...});
LIST
 ZRANGE myzset 0 -1
db.zrange('reviews', 
  from, 
  to, 
  callBackFunc)
DELETE
 ZREM myzset "two"
db.zrem('Å—eviews', 
  this.id)

Lists

coming soon…

Security

Securing from CLI

Through command line you can setup security as follows:

redis-cli //to login redis cli
>CONFIG get requirepass //see if security & password setup already
1) "requirepass"
2) ""
>CONFIG set requirepass "MY_PASSWORD" //sets up password
OK
>AUTH "MY_PASSWORD" //this will test your new password
OK
>exit

This will setup your security with password

Securing from Configuration File

Instead of command line, the security can be configure via redis-config file as follows:

Either case the security is configured, once configured the apps need to authenticate. Here is sample from NodeJs app

     redis_client = redis.createClient();
     redis_client.port = port;
     redis_client.host = host;
     redis_client.auth('PASSWORD');

The highlighted line, authenticates the connection via the password provided in the code

Maintenance

To see Redis logs:

cat /var/log/redis_6379.log

To stop Redis server manually on Ubuntu:

/etc/init.d/redis-server restart

–OR–

redis-cli shutdown

–OR–

ps -ef | grep -i 'redis-server'
kill -9 PID owned by redis

To find version of Redis and other info:

redis-cli
>INFO

So, we login first Redis command line and then execute “INFO” to see info about current version of Redis

Useful Links

  • https://github.com/mranney/node_redis
  • http://redis.io/topics/data-types-intro
  • http://redis.io/topics/quickstart

Leave a Reply

Your email address will not be published. Required fields are marked *