how to

Getting started with Redis

By October 28, 2014 August 18th, 2022 No Comments
ObjectRocket skyline

The speed and flexibility of Redis makes it an extremely powerful tool for developers and it can be used in a variety of different ways. Although Redis is often referred to as a key-value store it is much better described as a Data Structure Server, as it also supports 5 different data structure types, namely:

  • Strings
  • Hashes
  • Lists
  • Sets
  • Sorted sets

Each structure type has shared commands as well as some commands that are specific to a particular structure type.

This introduction will cover the basics of how to use Redis and an overview of the different data structures. We will cover some of the more basic commands, though bear in mind that Redis has over 160 at the time of writing and you can find excellent documentation at redis.io/commands.

Starting Redis

Use an instance provided by ObjectRocket for Redis or install Redis locally.

To install locally, download, extract and compile the code:

$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make

Start the local instance:

$ src/redis-server

Using Redis

Now you’re ready to interact with Redis using the built-in client. For an ObjectRocket for Redis instance start the redis-cli with the hostname, port, and password:

$ redis-cli -h my-host -p 1234 -a mypassword

If you are using a local instance, the host is localhost, the default port is 6379, and there is no password by default.

$ redis-cli

GET and SET

At the very simplest level, Redis can be described as a key-value store. By issuing the command SET foo bar you set the value of foo to bar. For example, issue the following command from the redis-cli you just started:

redis> SET foo bar
OK

Now read the value of foo with the GET command.

redis> GET foo
"bar"

EXPIRE and TTL

You can set keys to expire in a given amount of time by using the command, EXPIRE. TTL reports the time remaining before the key expires.

redis> SET foo bar
OK
redis> EXPIRE foo 120
(integer) 1
redis> TTL foo
(integer) 113

Lists

One of the distinguishing features of Redis is that the values of a key can be data-structures, rather than just values.

To create a list use LPUSH or RPUSH. If a list already exists, LPUSH will add the given value to the beginning of the list and RPUSH will add it to the end.

redis> LPUSH cities "San Francisco"
(integer) 1
redis> RPUSH cities "Austin"
(integer) 2
redis> LRANGE cities 0 -1
1. "San Francisco"
2. "Austin"
redis> SORT cities alpha
1. "Austin"
2. "San Francisco"
redis> LPOP cities
"San Francisco"
redis> LRANGE cities 0 -1
1. "Austin"

The SORT command sorts the list lexicographically in ascending order with the ALPHA argument. To sort in descending order, append the DESC argument to the SORT command.

The RPOP command pops an element from the list’s end. LPOP pops an element from the list’s beginning.

Sets

Sets are similar to lists, except each element can occur only once. In the example below we create a small set of US States. The SADD command adds an item to the set, unless the item already exists in the set. If the item does not exist, it is added and 1 is returned; otherwise, 0 is returned. SMEMBERS returns all items in the set. SCARD returns the number of elements of the set. SREM removes an item from the list.

redis> SADD states "Vermont"
(integer) 1
redis> SMEMBERS states
1. "Vermont"
redis> SADD states "Texas"
(integer) 1
redis> SCARD states
(integer) 2
redis> SADD states "Vermont"
(integer) 0
redis> SMEMBERS states
1. "Vermont"
2. "Texas"
redis> SADD states "California"
(integer) 1
redis> SMEMBERS states
1. "Vermont"
2. "Texas"
3. "California"
redis> SREM states "California"
(integer) 1
redis> SMEMBERS states
1. "Vermont"
2. "Texas"

Hashes

Using hashes, you can assign and map string values to fields in each key. A hash with a few fields is stored in a way that takes very little space, so you can store millions of objects in a small Redis instance In the example below, the name of user:1 is set to “john racker” using the HSET command. The HGET command is used to get the name value of user. HGETALL returns all the keys and values related to the specified key.

redis> HSET user:1 name "john racker"
(integer) 1
redis> HGET user:1 name
"joe racker"
redis> HSET user:1 company "objectrocket"
(integer) 1
redis> HGET user:1 company
"ObjectRocket"
redis> HSET user:1 city "austin"
(integer) 1
redis> HGET user:1 city
"austin"
redis> HGETALL user:1
1. "name"
2. "john racker"
3. "company"
4. "objectrocket"
5. "city"
6. "austin"

SORTED SETS

Sorted Sets are similar to sets in that they are non repeating collections of strings, though every member is associated with a score. Sorted sets are sorted by their score in an ascending way. The same element only exists a single time, no repeated elements are permitted, although scores may be repeated.

In this example we’ll add the points totals of the top 5 teams in the English Premier League as of 10/23/2014:

redis> ZADD EPL 22 "chelsea"
(integer) 1
redis> ZADD EPL 17 "man city"
(integer) 1
redis> ZADD EPL 16 "southampton"
(integer) 1
redis> ZADD EPL 13 "liverpool"
(integer) 1
redis> ZADD EPL 13 "west ham"
(integer) 1
redis> ZRANK EPL "chelsea"
(integer) 3
redis> ZRANK EPL "liverpool"
(integer) 0
redis> ZRANK EPL "arsenal"
(nil)

Next we’ll rank the teams based on points total using ZRANGE:

redis> ZRANGE EPL 0 -1
1) "liverpool"
2) "west ham"
3) "southampton"
4) "man city"
5) "chelsea"
redis> ZRANGE EPL 2 3
1) "southampton"
2) "man city"

Remember, sets are sorted in ascending order, so to see the rank of teams based on their points total we need to use ZREVRANGE

redis> ZREVRANGE EPL 0 -1
1) "chelsea"
2) "man city"
3) "southampton"
4) "west ham"
5) "liverpool"
redis> ZREVRANGE EPL 0 -1 WITHSCORES
1) "chelsea"
2) "22"
3) "man city"
4) "17"
5) "southampton"
6) "16"
7) "west ham"
8) "13"
9) "liverpool"
10) "13"

Next, Southampton plays a game and wins, gaining 3 points. We use ZINCRBY to increment the score, and ZREVRANGE to see how Southampton has moved into second place in the league. We can also use ZREVRANK to see the rank of Southampton. Note: The rank is 0-based, which means that the member with the highest score (Chelsea) has rank 0.

redis> ZINCRBY EPL 3 "southampton"
"19"
redis> ZREVRANGE EPL 0 -1 WITHSCORES
1) "chelsea"
2) "22"
3) "southampton"
4) "19"
5) "man city"
6) "17"
7) "west ham"
8) "13"
9) "liverpool"
10) "13"
redis> ZREVRANK EPL "southampton"
(integer) 1
redis> ZREVRANGEBYSCORE EPL 25 (15
1) "chelsea"
2) "southampton"
3) "man city"

In coming weeks we’ll dive deeper into Redis and some of the use cases for this wonderfully versatile developer tool. Please drop us a line if you have any questions about running Redis or would like to tell us about how you’re using Redis in your application stack.