Introduction to Redis

Posted on November 28, 2016 by abraunton

What is Redis?

Redis is a key-value database which is often grouped under the NoSQL category of databases. It was initially released by the developer Salvatore Sanfilippo on the 10th of April 2009.

Redis is an in-memory database which means that it’s data is stored on its host’s RAM memory. This makes Redis blazingly fast because it does not have to wait for traditional hard drive seek and read times.

Being a NoSQL database, Redis allows you to create schema-less data structures that fit your applications needs. You can use Redis for simple things, like creating a visitor counter, or for more detailed information, such as storing user information.

What is Redis used for?

Redis shines when you require something that’s fast, really fast. Redis can be utilized for a range of different use cases, here are just a few:

Caching

Queuing

Pub/sub

Machine learning

Which companies use Redis?

Redis is used by a range of companies but most notably: Snapchat, Twitter, StackOverflow, GitHub and Pinterest. There are some really great technical presentations and blog posts where you can learn how these and other companies are using Redis:

How to install Redis

We’ve spoken about the benefits of Redis and some of its use cases, so let’s get it installed and start interacting with it. We will be installing the Redis server and Redis command line tool on Ubuntu 14.04 . Let’s add a new repository:

sudo add-apt-repository ppa:chris-lea/redis-server

Now let’s update our system:

sudo apt-get update

We now need to install the Redis server:

sudo apt-get install -y redis-server

Redis should now be installed. We can confirm this by accessing the Redis command line tool by running the following command:

redis-cli

Here’s an example:

You can now exit the command line tool by running the ‘quit’ command. Redis uses port 6379 by default.

Redis data types

Similar to other databases, Redis supports several data types:

Strings

Lists

Sets

Hashes

Sorted Sets

Bitmaps

Choosing the correct data type for your data and requirements is key to getting the most out of Redis for performance and scalability. Let’s learn about each data type:

Interacting with the Redis server

Now that we have the Redis server installed, we can start interacting with it. To do so, we will be using the Redis command line tool that we tested earlier. To get started, run the following command:

redis-cli

Run the ‘help’ command:

Type: "help @<group>" to get a list of commands in <group> "help <command>" for help on <command> "help <tab>" to get a list of possible help topics "quit" to exit

We can use the ‘help’ command to see how to use commands. For example, “help HGET” returns the following:

HGET key field summary: Get the value of a hash field since: 2.0.0 group: hash

We can see that this command is part of the “hash” group, if we wish to learn more information about that group we can run the following command:

help @hash

This will then output all of the commands available in that group, such as HGET, HGETALL and HKEYS. If you are not sure where to start with the help command then type ‘help ‘ (please note the space) and then press the tab key. Redis will then suggest a topic, you can press enter to learn more about that topic if you are interested or keep pressing tab to cycle through topics.

Playing with data

Let’s start adding some data into Redis and modify that data. We’ll create a basic key name “firstkey” by running the following command:

SET firstkey “1”

We’ve set the value of ‘firstkey’ to 1. We can confirm this by running the ‘GET’ command:

GET firstkey

The value returned will be “1.” Great, you have now set and retrieved your first key in Redis. To continue from here, we can update our key’s value by incrementing it. By running the “INCR” command, our key will be incremented by one and the new value will be returned:

127.0.0.1:6379> INCR firstkey(integer) 2

You can also decrement your key by using the “DECR” command. This will decrement the value by one and return the latest value of the key.

Modeling data

Redis can be used for much more than a simple counter. Let’s look at how we could model a traditional data structure such as a user table. If we were using MySQL, then a row in our simple users table might look something like this:

Please note: the password in this example has been hashed. This means that the password is not stored in the database as plain text. Normally a hashed password would be a lot longer compared to the one I use in this example. For example, if you used a sha256 function to hash the password then the length would be approximately 64 characters.

You can see that Redis returned “OK” and when we check the key’s value, we see that it has been updated. Let’s say one of our users needs to be deleted from Redis. We can do this by running the “DEL” command:

We passed the hash into the DEL command as an argument and Redis returned an integer of 1 to let us know the operation had been completed. If we tried to run the “DEL user:1” command again then Redis would return an integer of 0. The second command we ran was the “KEYS *” one to confirm that it has been deleted.

Using Redis with your PHP application

Now that you have the Redis server installed and you are familiar with the command line tool, let’s look at integrating Redis with our PHP web application.

For a PHP code base, I would recommend using the Predis library as it is very fast and incredibly easy to get started with but also is very configurable for when need it.

You can find lots of great documentation such as installation instructions and how to get started on the official Predis GitHub page – http://github.com/nrk/predis

Conclusion

Here’s what we learned:

We’ve learned how to install Redis on Ubuntu 14.04.

We’ve learned what Redis is and what it’s useful for.

We have been introduced to the Redis command line tool and learned some of the Redis commands.

We have learned how to interact with Redis using our PHP web application using the Predis package.

Learn more / Resources

If you have enjoyed this guide and would like to learn more about Redis then here are some links to help get you started: