What is Redis and redis-cli?
- Welcome to the section Using Redis, which is part of the course Build Complex Express Sites with Redis and Socket.io. In this section we will learn what Redis is and use the CLI, learn about data types, the commands, and what we can do with these data structures. So in this first video, we're going to get an introduction to Redis, and how it works using the CLI. So what we're gonna do is discuss what Redis is, what the CLI is, and how to connect and use the CLI.
So let's go ahead and get started. First we must cover what Redis is. Redis is a self-described, in-memory data structure store. Let's unpack that statement. First it is in-memory. What does that do for us? The fact that it is in-memory makes lookups fast. Without getting too technical and out in the weeds, let's think about how memory is set up. Memory is set up as one long list of addresses. Each of these addresses stores some data. If we know the memory address, then we can get the data very, very quickly.
And this is a main design of Redis. Redis is one long list of keys. If we know the key, we can get the data out very quickly. If you're coming from a classic database background, there's not much of a comparison. There are no indexes. You either know where the data is, or you do not. So next is a data structure store. What this means is there are multiple types of data that we can store at each key. We can store string and integer and many more types. This gives lots of flexibility when designing how to store the data.
The final thing we'll take note of is that Redis is a network server. This means it makes itself available for multiple devices over the network. This means Redis is essentially making another computer's memory available. And also, technically the network connection is the source of most of the latency in the request for a Redis value. The actual memory lookup, latency-wise is essentially nothing compared to the network connection setup and transmission. So let's talk about getting Redis working.
I'm using Mac OS X, and I'm also using Docker for Mac. This allows me to create small containers that run software. Software like Redis. Now if you were running Windows, you can download and run Docker for Windows. Now this is not a Docker tutorial, so we'll not cover in-depth coverage of the commands and what they mean. Just know that one command will bring up an instance of Redis, and the other will connect to it. What is great about this, is that we do not need Redis installed on our local machine at all.
It will all run inside Docker. So as you can see here, we can go to Docker.com, and you can search for Docker for Mac or Docker for Windows. And then you can follow this to install it. And at that point, you will have Docker installed that you can then run these Docker commands. So let's go ahead and take a look at these commands. So the first to create Redis, we're going to say "docker run", and then we're gonna give it a name.
And we're gonna call it "packt-redis". And then we're gonna say, here's a port. And we're going to say bind to 16379 from the internal port on the Docker 6379, which 6379 is the port that Redis runs on. And then we're gonna say run in the background, and we're gonna use the image of Redis, and we're gonna use the same version for every video so that nothing changes. And we're going to use version 3.2.4, so when we run this, Docker kicks in.
And here it has created the package. Now here it started up very quickly, but when you run it, it may take a little longer as it needs to download other images to build this container. Although once that's been done, as you can see bringing up one is very quick. So we have this out there, how do we use it? Well, we now run another Docker command. Now this command built a Redis server, and we're going to connect to it and use a Redis CLI, which we'll talk about what and how to use a Redis CLI shortly in this video.
So the next one we run is "docker run". And then we're gonna say "it" which means that we're gonna be able to run it interactively. It will not just drop to the background like the other one did. And we're going to say "link", and we're going to say "packt-redis" which was the name of our other container is gonna be linked as "redis" in this container. Then we're gonna say "rm". So when we get done with this container, it'll actually destroy itself which is what we want.
We don't want this one hanging around. The other one we do want to hang around. This one we don't. And we're gonna say use the redis image, and then we're gonna give it a command to run, which is "redis-cli" use the host of redis, which we've defined here. And we're gonna use port 6379. Now remember, internally up here, this thinks it's running on 6379. So we're gonna connect this one, and it's gonna essentially make a little network that connects these together and says, okay, these can communicate to each other.
Again, this is not a Docker tutorial, and I'm just kind of doing some high-level stuff. But what's great about this, is that when you run these docker commands, and I run them, we are literally running the same exact software in the same way. So let's go ahead and kick this off. And there we go. So this actually created that container and now it's connected. And this is actually running the Redis CLI, and we are connected to that other container. Now from here, we can run all the commands needed for this section and pretty much anything we're doing with Redis.
So I'm going to exit out of here really quick, so that destroyed the other Docker server. But I do want to note one thing, we can only have one packt-redis running or created. If we try to run the initial command again, which says create another container called packt-redis, it will tell us that that already exists. We can't run that again. So if you're following along, and you're doing these in order, I may tell you to start this up, but you may already have it created.
In that case, you don't have to recreate it if it's already running. Now, some other things can happen. For example, it could stop. And you can stop it by saying, "docker stop", and then you give it the name of the container. So that just said that it stopped. So if we come back, and we try to run this to link to it, it will say, hey the other one is not running. What you can do then is you can actually run docker start and give it the name.
And now it is running again. So remember that we can only have one Redis server that is named "packt-redis". And I'm gonna keep that name so that you can easily follow along in any of the next sections. So just remember that if it's running, you either have to stop it or start it, and that way you can continue on. Now I do want to note one more thing is that I will have these commands. So let me bring that up really quick. So here, I'll have in each section, I'll have a docker_commands.txt.
And this will write out any of the Docker commands that we'll have to run. So you don't have to watch the video and type in exactly what I'm typing. You can come to this file and open it up and see. So here, I'm saying here's how we start the Redis server. Here's the stop and the start. And then here's how to connect, and we're even gonna run this at some point. So any of the Docker commands that I run in the video, you'll have access to. And this is also true with any of the Redis commands. Since this is just an initial, hey this is Redis, let's get it working.
There's not too many commands we're gonna run in Redis. So in the other videos there will be many, many commands that we will run. Okay. So let's actually use Redis now. So we're not going to cover every command because we do have other videos to do that. So this is really just to get you acquainted, if you've never used Redis, how we can get it going. So first of all, let's talk about the CLI and how to use it. So CLI stands for command line interface. So this is a command line interface that we can use with Redis.
So we can see all of the arguments that the Redis CLI uses by putting in "-help". Now we can do this with Docker. So we're gonna say "docker run -it", then we're gonna remove this. Run "redis" and "redis-cli". Now we don't have to connect it to anything because we're just going to run "redis-cli --help". So this will take a second, create that container and then output it. So as you can see, here's a bunch of things that we can run.
For the most part, we'll mainly just be using "-h" to set the host name and "p" for the port to connect. But as you can see, there's a lot of other stuff that we can do. So let's actually use the command line interface. So we can reuse the command that we ran before to say "link to packt-redis" and use the redis CLI. Now remember, we're gonna need packt-redis running in the background, which I had started it before here. So this is creating it and connected. So this is the redis-cli. So starting here we can actually press TAB, and this will go through all of the different things that we can actually run.
And as you can see, there's a lot of commands that we can use. Now one of them is "help". So we can type "help" and then we can type TAB here, and see all the different things that we can get help on. So here we're gonna run "help set" and "help get", because these are the two commands we will use here. So as you can see, it tells you the command, all the parameters you can give it, including any optional ones, and what it does.
So let's do the same with "help get". And so we can see this sets the value or gets the value. So let's use it. So we will run "SET", and we'll put the key value in here. And then we're gonna give it a value of "test value", and run that, says okay. So this should have worked. So then we say "GET" and we give it the key, and it returns the value. So again, like we said, in-value data structure store.
You have keys, you put some data in, you can get the data out. Now at this point, we've spent enough time getting introduced to Redis, and like I said, we're gonna have some more videos where we go much more in-depth on how to use Redis. But for this video we are done using Redis. So in this video we have covered what Redis is, which is an in-memory data structure store, and what that means. So at a really basic level, Redis allows us to take some data, throw it to memory address, and then quickly retrieve it.
We then covered how to use Docker to easily run Redis and the Redis CLI which is the command line interface. We will definitely use these commands throughout the rest of the sections to easily run our examples and make sure that we are using the same, exact software. And finally, we actually then connected to Redis and used Redis just a little bit. Now in the next video, we'll look at the different data types that Redis can store. This is really where Redis starts becoming more useful and important.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Using Redis
- Structures, channels, and subscribing
- Adding messaging
- Integrating Redis and Node.js
- Working with geospacial indexes
- Using Node.js with Socket.IO
- Working with rooms
- Creating namespaces
- Using Socket.IO and Express
- Sharing states
- Using routes