What is Redis?

What is Redis?

And why do developers love it so much?!

Introduction

Welcome! Today we'll be talking about Redis, where it came from, how to use it, and why developers love it so much! Although it's a relatively new technology (created in 2009), it already has a large and dedicated following in addition to being used in production at some big companies. It has amazing docs, is easy to setup, and has awesome clients for every major language. Without further ado, let's jump into its history!

Quick History

Redis (Remote Dictionary Server) was created by Salvatore Sanfillippo in 2009, as a proof-of-concept, when trying to build a real-time web traffic analyzer at his startup. With great internal success, he decided to re-write it in C and open-source it. Since then it has grown tremendously and is now maintained by Redis Labs which has a great cloud offering. In that time it has garnered widespread praise including being the most-used key-value database and the most loved database on Stack Overflow's Developer Survey since 2017.

What does Redis do?

So now you must be wondering "what does Redis do!?" and that's a great question! At the most basic level, it's an in-memory database. This means data for reading is stored at the memory level which makes for lightning-fast reads (almost like it's a variable locally stored in memory). With this, you're able to use it as a highly-performant key-value store (think a remote dictionary/map), a cache, a message broker, and much more! With that, let's see how we'd use it as a key-value store and a message broker!

Setup

If you haven't done so yet, follow the official Getting Started docs to get Redis setup locally.

Key-value

To start let's look at the most straightforward (and initial use) for Redis. It's a simple key-value store in which you set a value to a specific ID which can then be read, updated, and/or deleted later on.

Lets start our instance,

> redis-cli
127.0.0.1:6379>

Now, let's set a value...

> SET foo bar
OK

...and then get the value

> GET foo
"bar"

As you can see it's quite basic functionality but can be extended to so many use cases. As it "simply" is storing a remote dictionary, you can use this as a cache between a client and a database, as a way to communicate information across multiple clients, or whatever you can think of! Now let's look at how this can be used as a message broker and have multiple clients talking to each other at once.

Message broker (PubSub)

One of the great things about Redis is how easy it is to send and receive messages from multiple clients to create a chat-like experience. To start we'll go over what is PubSub and then move into how to implement it!

What is PubSub?

PubSub is shorthand for publish and subscribe. This means that a connection can both publish to a channel while it also subscribes to the said channel which works perfectly for chats! While you want to receive everyone's messages, you also what to be able to send messages to everyone else (and see your own messages being sent out). Let's see how we could build that.

Building a chatroom with PubSub

For this, we'll use two clients to show how they are both sending and receiving data throughout the chat.

Let's start with subscribing to the first client,

# Client 1
> SUBSCRIBE chat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chat"
3) (integer) 1

NOTE: The redis-cli client can't do anything while subscribed but typical language clients (like Python) can then do subsequent actions.

Now lets have a second client publish to the channel,

# Client 2
> PUBLISH chat hey
(integer) 1

And now in Client 1,

# Client 1
1) "message"
2) "chat"
3) "hey"

Now in a client-based system, we could have Client 2 subscribed as well and then Client 1 could publish and have a two-way conversation. For an example of this, check out my Chatly.chat web app which is using Redis with Websockets to power the chat.

Redis in production

My suggestion for getting started with a managed remote instance of Redis is to visit Redis.com. You can get a lightweight database (with no CC needed) to start with and scale as you need! Lets take a quick look on how you can connect to the remote database.

Connect to remote database

After signup, we'll get the connection URL and password so we can create an authenticated connection to our remote database.

First, we find the connection URL, redis-url.png

And we use it to connect remotely,

> Redis-cli -h redis-18137.c280.us-central1-2.gce.cloud.redislabs.com -p 18137
> SET foo bar
(error) NOAUTH Authentication required

Oh no we need to authenticate to use it!

Grab your password,

redis-password.png

> AUTH [password]
OK

> GET foo
(nil)

And it worked! We were able to quickly connect to a remote instance and use it!

Note: You can also use a url with the username and password contained in there but thought this was better to show how it's secure from the public without it.

Summary

Thanks for reading! I hope I was able to communicate the amazing parts of Redis succinctly. With a quick history of Redis, some of its main features, and how to use it locally and in production, you're on the way to using it in your next app! For how I built Chatly.chat with Redis check out my previous article,