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:
- 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.
Use an instance provided by ObjectRocket for Redis or install Redis locally.
To install locally, download, extract and compile the code:
Start the local instance:
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:
If you are using a local instance, the host is localhost, the default port is 6379, and there is no password by default.
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:
Now read the value of foo with the GET command.
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.
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.
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 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.
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.
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:
Next we'll rank the teams based on points total using ZRANGE:
Remember, sets are sorted in ascending order, so to see the rank of teams based on their points total we need to use ZREVRANGE
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.
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.