What are the Redis commands?
- [Instructor] Welcome to Redis Commands, part of the course Build Complex Express Sites with Redis and Socket.io. In this video, we'll look at some of the Redis commands in more depth. We'll do this by first looking at some of the ways that we can find help about commands, and then by actually using the commands. Let's go ahead and get started. One of the biggest problems with learning any new technology is that you do not know what questions to ask. Redis is a great example of this.
One of the biggest problems with learning any new technology is that you do not know what questions to ask. Redis is actually great about this. One of the reasons is that it is a focused technology. It does not try to do too much. This means that we can easily see every command that we can use. Just go to Redis.io/commands as I have in the browser here. Now, we don't have time to look at every one of these commands, but we will cover the commands that you mostly likely will use the most.
So as just a quick example here, you actually can look for a command. We've used Get before, and we will use it again. So we can see here it runs on everything that we will need to know about Get. So if you have any questions about exactly how to use a certain command, or you don't know what Redis can do, this is a perfect place to go to learn about all the commands. Now, one other thing that we'll touch on is Big O Notation. So without getting to far off topic, Big O Notation allows us to talk about how long it will take for a process to execute.
Now, I think the best way to show this is just talking about a couple of examples, and these kind of fit with what we're gonna talk about when we get to the Redis commands. So for example, and O-1 will take the same amount of time, no matter what size the input or the size of the array it's working on. An example of this will be Get, when we talk about it. So no matter if you have one key or you have a million keys, when you run Get, it will take the same amount of time to find that key. Now compare that to O-N, and this grows with the input.
So for example, if we were doing a process on an array, and it took us X amount of time to work on one and we had to do it 10 times, it'll take 10 times the amount of time, and that's where the N comes in. So the more input we add, the longer it's gonna take. And then, there are some Redis commands that take this amount of time and more. So, what we will do is we'll talk about the command, we'll use it and at the end, I'll give you quick overview of what the Big O Notation is.
So the idea is if it's an O-1, it is a fast operation, and that it doesn't matter if we have a million keys or one, whereas in N, the more keys we have, we will have to start planning about when an how we use that command. So let's go ahead and get started. So the first things is, just like we've been using before, we're using Docker so make sure the pack Redis server is running. And if you need to know those commands, look in the Docker Commands Text File at the root of this section.
So I'm gonna paste this in. This is essentially saying, "Connect to our Redis server "and give us a Redis command line interface." And this is where we're gonna run all our commands. Also, all the Redis commands that we are going to use in this video are going to be in the file Redis Commands.txt. So let's go ahead and use some commands. So let's start off with ones that we've used before and are the simplest. We're going to say, "Set user: one test." Okay, and we'll do exactly how we expect it to.
We give it a key, we give it a value, it sets it. And if we want to get it, then we use the command Get and we give it a key, that'll return the value. Let's talk about another thing that we can do. So we can set Set Counter and we're going to give it a number. So we have Counter Set to One, and then we can call Increment Counter, so this takes it, takes that number and increments it by one. And we see it's returned there or we can call Get Counter, and it returns it, too. Now, one thing to remember with the Increment command is that it expects an -Int, although there isn't an -Int type, you do have to put an -Int.
So if we set that value to something that we can't increment, for example, User One, 'cause it's a string and we try to call -Int on it, it will tell us that you cannot do that. So that is just something to remember that if you're going to increment something, it has to be an -Int even though Redis technically doesn't have -Int's. So another thing that we can do is we can check to see if a key exists by using Exists. So if we say, "Exists User One," we see that it returns with a One, tell us that this exists.
Or, if I type it right, we say, "Exists N," just put in a key here that doesn't exist, we see that it returns No. Now, there are some commands that deal with expiring keys. If we need to expire a key, we can set that. So we can say, "Expire User One," and then give it the amount of seconds that it's going to expire. So give it One, this should expire in one second, and in fact by now, if I try to get it, we actually see that's null.
So we expired it and it went away. Now, we can give it a longer time and we can actually set the Expireree when using Set. So we can say, "Test Key," we'll give it a test, and then by saying, "E-X," and then giving it an amount of seconds, this will actually have it expire in 120 seconds. So, to check that, we can then use TTL, which is Time To Live. And then, we will ask for that key, and we see that the time to live is one eleven.
Call again, we can see that it's slowly counting down. And once it gets down to zero, it will be removed. So before we move on, all the things that we just spoke about, all use Big O Notation of O-1. And this makes sense, because each of them are specifically working on just one key. And because one-key lookup is really, really fast in Redis, it doesn't matter if we have one or a million keys, each of these commands will run in the same amount of time, and that time is actually very quick.
So let's move on to Hashes. So, they all start with an H, so we have H-Set, and we give it a key, and then, you have to give it a field and a value. So these are kind of like rows in that you have a key and then we can actually add multiple columns to it. So if we want to see what's set there, we can say, "H-Get," and say in this hashkey for the Field field return what's in there, and it returns value.
Now, if we have a bunch of things to set, we can use H-M-Set, which Hash Multiple Set. So again, we can say, "Hash_key," and then we're going to say, "first," Field is first, value is one, second in two. Run that, we see that that's set, and then we can run. So now, we have multiple keys to get. So we're going to say, "Hgetall," which will return everything, and then we can get all the keys.
Now, I just noticed that I mistyped this, and I said, "Has_key," so I'll change that to a Hash and then run the Get All, and then we'll see everything set. Another thing we can do is see what keys are set with H Keys. So this tells us that this Hash has a field, a first and a second fields that are set, that we can get values for.
Now, for the Big O Notation for these commands, all the single sets, like H-Set and H-Get, are O-1 notation. And this makes sense 'cause we have a specific key and we're getting a specific field, and that's gonna take the same amount of time. But, any time we start using any of the multi-Sets or the multi-Gets, it's gonna take a little longer, it's gonna be O-N. And how long it takes is going to depend on how many fields it has.
The more fields, it'll take longer. So that's definitely something that we want to keep in mind when using these Hash commands. So the next group of commands we want to run are with Lists. So here, we're going to Lpush, which is Left Push, which adds a item to the list from the left, meaning to the front. So we're gonna create a list called Dogs, and we're going to add Dexter.
So this returns one, that's telling us that there's one in this list. We can even run it again say, "Dogs," and add Gismo. Let's go ahead and add Gismo. So we see that there's three items in this. Now we can run LRange, which is saying, "In this list, "get the range of these items." So we'll say, "Dogs," we'll start at zero and we'll go to the last item. So this returns all the items.
Now remember, we're using Left Push, so each time we ran that command, it added it to the first, so the oldest command is the furthest into the list. Now, we can do an Rpush, which pushes in from the right side. So we added Fido to the end, and if we run the Range again, we'll see that it is at the end of it. Now, we can remove by L-R-E-M and also Lset if we want to set a specific number in here, but most of the time, we're going to be using Lpush and Rpush.
And the reason for this is that Lpush and Rpush, from a Big O Notation perspective, are O-1, and this makes sense 'cause we're either adding something to the front or to the end, it's gonna take a constant amount of time. And then, pretty much everything else, if we're doing an Lset, it's gonna be an O-N. Or, if we do the Range, it's going to be an O kind of an N 'cause it depends on how many it needs to read in and then how many it needs to read out.
So again, the more reading out or the bigger the list, the longer it's going to take. So when using Lists, this is definitely the point where when you are using these commands, you do have to worry about how fast they take. If you have a 10,000 item list, that may take a long time if you're trying to read out all of them or you're trying to add the 5,000th item. But if you're just pushing something onto the front or end, it's actually going to fire really quick. So now, let's go to Sets.
Just like all the other commands, these all start with S for Set. The main difference with a Set and a List, they're very similar, is that a List, you can have multiple items that are the same. In a set, you cannot. Each item has to be unique. So if we do Sadd, and we're going to say, "Sdogs," for Set Dogs, and add Dexter, we see that adds it. But if we try to run it again, we see that it doesn't add it. You can only add unique items.
So if we try this again and add Gismo, we see that adds that as well. Now to see what's in there, we can say, "Snumbers," and that'll return all the items in that set. So, we have Gismo and Dexter. Another thing we can do is say, "Ismember." So on this key, is Dexter a member of this set? The answer is yes.
Or, we can add for something that doesn't exist, and we see that it tells us no. Finally, we can remove something by running Srem and then passing in what we want to remove. And then when we see that, now that it doesn't exist and we run Smembers, it's only down to Gismo. So I'm going to re-add into Sdogs Dexter, and I'm going to create another key called Dogs2, and we're going to add Fido, and then, we're going to add Dexter.
And the reason for this is that we can actually start using Set Theory, so we can do things like Difference, Intersection or Union and it will give us the answers to those. So for example, if we wanted a Dif, we can run Sdiff, we give it the sets that we want to Dif. So we'll say, "Sdogs, "Dogs2," and it gives us the difference. So it takes all the items in Sdogs and compares it to Dogs2 and then tells us what the difference is, and that is Gismo.
So, we can now say, "Intersection," which now this is a difference, or this is different from difference in that these are the things that are the same in both of these lists, so we can put in Dogs2 and we see that's Dexter, which makes sense because Dexter's the only name that is in both of those lists. And finally, we can add S-union, pass in the same two, and see that returns all three unique names. So once we start using sets, we see that this opens up some more things that we can do with the data.
Instead of just having a list, we now can add specific reasons and we can add that each item has to be unique, and because of that, we can actually move into Set Theory. So now, when we're talking about Sets, pretty much all of these run at Big O Notation O-N, which just kind of makes sense because we're using a key, but then, we're looking at members inside of that key. So, the bigger the key is, the bigger the Set is, the longer it's gonna take to do any of these items, because it's not just a simple lookup, it's having to go inside that key and then find information about it.
And that brings us to the final commands that we're going to cover. And these are Sorted Sets, so they work a lot like a set in that each item has to be unique, but they have the key difference that we add a score with it, and then, it's going to sort the Set by that score. Now, these all start with a Z. So, we'll do Zadd and we'll continue with the dogs and say, "Zdogs." And now, we give it a score and then we give it what we want to add.
So here, we added Dexter with a hundred. We'll do the same thing with Gismo, giving him a 200. So we have these two items in the Set. We run Zrange, and this will say, "Give us all the items "in this set." And we need to tell it, "Zdogs." And we see it returns Dexter and Gismo.
Now, if we say, "WithScores," we'll see that it's tied Dexter to a hundred and Gismo to 200. Now let's update Dexter to 300. And then, let's rerun this with scores. So now, as you can see, Dexter was first, now he's second. It's because it's sorting it by the score.
Now, another great thing that we can do is we can actually even look up specific items in here specifically by the score. So if we say, "Zrange by score," we can now say, "Looking in Zdogs, "we want a minimum score of 100 "and a maximum score of 250." And we see that just returns Gismo 'cause that's the only item in this set with a score that's between 100 to 250.
So now, when we're talking Big O Notation of Sorted Sets, these are actually commands that take the longest amount of time to run. And this should make sense as well, because everything we've done so far has not been as complex as using a Sorted Set, because not only do you have a key and you have to look at the items inside the key, we also have to talk about the order of the items. So if add a specific item in there, and we have to change the order of another hundred items, that's going to take N.
That's going to take longer time. So these are actually Big O Notation of Long N, meaning that the more items that we have, this can actually grow quite a bit for each item that we add. So again, when we talk about Sets, this is definitely a command that is powerful, but we do have to be careful with how we use it knowing that if we start adding 1,000, 2,000, 10,000 items, that it's just gonna start running slower and slower with each additional item.
So that's it with the commands we're going to cover. So in this video, we've looked at many commands for Redis, and we've actually used them in Redis. Now, there technically are more commands, and like we demonstrated, you can go to Redis.io/commands and look at all the commands that you can actually run in Redis. But these are definitely the most applicable and the ones that you are going to be using the most. Now in the next video, we're going to discuss how to design the data structure for storage in Redis.
Now this is important, because if you've never stuck data in Redis, you could be sticking the data in a way that is very hard to pull back out, so we definitely have to approach this the correct way. And in the next video, we'll talk about what the correct way is.
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