Installing and Using Redis on Ubuntu/Mac OS X


Mac OS X

Install Redis using homebrew.

brew install redis

After installing it, you will see some warning signs, just ignore those and move on with the rest of the tutorial.

To initiate Redis when your computer starts, run the following command.

ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents

Start Redis server via “launchctl”.

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Start Redis server using configuration file.

redis-server /usr/local/etc/redis.conf

Stop Redis from starting automatically when your computer starts.

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Redis's configuration file is located here.

/usr/local/etc/redis.conf

Uninstall Redis and remove its files.

brew uninstall redis
rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Get Redis package information.

brew info redis

Test if Redis server is running.

redis-cli ping

If it replies “PONG”, we are ready to rumble!!!

Ubuntu

Run this command to install Redis from the CL.

sudo apt-get install redis-server

Then check to see if it is running.

ps aux | grep redis

If it shows up, you are set to go! You can also check for a PONG response by using:

redis-cli ping

FYI, Redis should automatically run in the background upon closing the terminal, if it doesn't, you can start it as a background process by using nohup redis-server &.


Using Redis

You can connect to redis typing redis-cli in the terminal. Redis has two main commands as a NoSQL database and along with that, it is extremely fast. It also only deals with Key Value pairs. The two commands are Get and Set. If you try to grab a record that doesn't exit, redis will return nil instead of an error.

To store a record in redis, you will do the following

SET my_var 200
# => OK

And then to retrieve the value of my_var, you will type

GET my_var
# => "200"

Using redis to track additional information

To track additional information such as a post like, you can assign the post id to the variable using the following strategy using SET var_name count.

SET post_thumbs_up_count:44 2

You can also increment and decrement the count using redis commands

INCR post_thumbs_up_count:44
# => 3

DECR post_thumbs_up_count:44
# => 2

INCRBY post_thumbs_up_count:44 50
# => 52

DECRBY post_thumbs_up_count:44 33
# => 19

Deleting in Redis

Set up a few variables to test.

SET car Mercedes

SET color "white"

With Redis, you can delete several records at once in one command, and it will return the number of records deleted afterwards.

DEL car color
# => 2

Check to see if a key already exists

There is a more efficient way to check if a key already exists than by trying GET var and seeing if it returns nil or a value. Note that redis will not return nil if it doesn't exist. If you are using redis for caching, you can do a check to see if it returns a 1 or 0 and then either GET or SET based off the response.

EXISTS var
# => (integer) 1 - if it's present
# => (integer) 0 - if it does not exist

Expiring a key

SET image "picture.png"

EXPIRE image 100
# Will expire the image in 100 seconds

TTL image
# => 95   - will tell you how many seconds are left until it expires

Using GET/SET to override key values

There are several ways to do this.

SET var "Jake"
# => Jake

SET var "Sam"
# => Sam

GETSET var "Olga"
# => Sam

GET var
# => Olga

GETSET var2 "Sam"
# => (nil)

GET var2
# => Sam

Querying Keys

KEYS *
# => 1) var
# => 2) var2
# => 3) image
# => 4) post_thumbs_up_count:44

KEYS *post*
# => 1) post_thumbs_up_count:44

KEYS post*
# => 1) post_thumbs_up_count:44

KEYS post????
# => nil -- since there is no 9 character key

Setting hashes in redis

HSET post:12 creator 'Sam'
# => (integer) 1

HSET post:12 type 'Guide'
# => (integer) 1

HGET post:12 creator
# => Sam

HGET post:12 type
# => Guide

You can also hash them out all at once

HMSET user:1 name 'Sam' email 'sam@gmail.com'
# => OK

HGET user:1 name
# => "Sam"

HMGET user:1 name email
# => 1) "Sam"
# => 2) "sam@gmail.com"

HGETALL user:1
# => 1) "name"
# => 2) "Sam"
# => 3) "email"
# => 4) "sam@gmail.com"

HKEYS user:1
# => 1) "name"
# => 2) "email"

HEXISTS user:1 email
# => (integer) 1

HEXISTS user:1 altemail
# => (integer) 0

HSET user:1 id 1
# => (integer) 1

HGETALL
# => (error) ERR wrong number of arguments for 'hgetall' command

HGETALL user:1
# => 1) "name"
# => 2) "Sam"
# => 3) "email"
# => 4) "sam@gmail.com"
# => 5) "id"
# => 6) "1"

Incrementing, Deleting, and Length of Hashes

HINCRBY user:1 id 123
# => (integer) 124

HGETALL user:1
# =>  1) "name"
# =>  2) "Sam"
# =>  3) "email"
# =>  4) "sam@gmail.com"
# =>  5) "id"
# =>  6) "124"

HDEL user:1 id
# =>  (integer) 1

HGETALL user:1
# =>  1) "name"
# =>  2) "Sam"
# =>  3) "email"
# =>  4) "sam@gmail.com"

HLEN user:1
# => (integer) 2

HGETALL user:1
# => 1) "name"
# => 2) "Sam"
# => 3) "email"
# => 4) "sam@gmail.com"

Redis-cli Info Commands

To tell how big your database is or just to see certain information about your Redis setup, try out some of the following commands.

Go into your redis-cli.

redis-cli

Then to check out how big your database is or cpu information.

INFO

For memory only, type the command below.

INFO Memory

To checkout how big certain keyspaces are. You can select a certain keyspace by using Select DBNUMBER and then dbsize.

INFO keyspace

To delete info of the current database or all databases, use one of the following commands.

FlushDB    # deletes all keys in the current database
FlushALL   # deletes all keys in all databases on the current host