What is Redis? A gentle introduction using Ruby

Redis logo

Redis is an in-memory data structure store used to help building high-performance and scalable web applications, similar to Memcached, but with these main differences:

  • Redis can persist data to disk, making it a kind of NoSQL database, so data won’t disappear when restarting the server. This feature can be optionally disabled.
  • Redis can store more data types besides strings, such as Hashes, Lists, Sets and Sorted Sets. I will talk about them later.

The persistence feature is interesting, but some applications may be affected if there is some power failure and Redis didn’t have time to persist the latest data. For these cases there is another interesting feature: it can append each command to a log. So if a failure occurs, after restarting Redis it can apply all the commands over the data to get back to the latest state.

Redis can be used as a database, cache system, or a message-queue broker. It can also be used in a server group, using master-slave asynchronous replication.

Installation

If you’re using macOS, it’s very easy to install Redis using Homebrew:

Then you can start it using this command:

The redis.conf file is where the server is configured.

If you are using Linux, you can use your distribution’s package manager to install it easily.

I will also install the redis-rb gem to deal with Redis from Ruby:

Connecting

Let’s use the Redis CLI utility to connect to the server and check the installation:

It should display a prompt with the loopback IP address and the port where the server is running. When entering the ping command, the server should respond with PONG:

Yay! The installation is working!

From now on I will switch to the Ruby gem. The commands are the same, but using them as object methods.

Please open another terminal tab and fire up irb. Let’s connect to the server:

You are connected to the server using the default configuration (localhost and port 6379).

Now ping the server:

Strings

Redis can store any value in a string using up to 512 MB.

Another interesting way to set a string is by assigning an expiry in seconds:

There are lots of commands for strings. I will show a few below:

Hashes

A hash is a collection of key-value pairs that is initialized using a series of strings:

A few more interesting things we can do with hashes:

Lists

Lists are a series of strings sorted by insertion order. More can be added on the head or on the tail.

Values can be extracted from the head of from the tail as well:

Sets

Sets are unordered and non-repeating collection of strings.

Sorted sets

A sorted set sets a score to each member, from the smallest to the greatest score.

Conclusion

I have scratched the surface of Redis features, talking about the basic data types and how to use them through the Ruby gem. There are other interesting features such as messaging, where an instance can create a channel and receive messages from other instances.

Redis is a very useful and efficient piece of software that can be used to enhance our web applications. In fact there are some important gems that use it, like Sidekiq.

Did you like it? Please share it:

Get my ebook for free

10 ideas that helped me become a better developer (and may help you too)

Subscribe to my mailing list and get my ebook on 10 ideas that helped me become a better developer.

About Me

David Morales

David Morales

I'm David Morales, a computer engineer from Barcelona, working on projects using Ruby on Rails and training on web technologies.

Learn More