Learn about Redis data types.
- [Instructor] Welcome to Redis Data Types. In the previous video we learned about what Redis is and just briefly used Redis. In this video we're going to take a look at the data types. First we will discuss what the data type is, then we'll discuss the types that Redis uses, and we'll look at examples as we go through the types. So remember that Redis is an in-memory data structure store. The second part of that statement is what we'll talk about in this video. The data structures in Redis are important and knowing what they are and how to use them is key.
So we will talk about them and then look at some simple examples of how they work. So, I want to note that this is not all the types but is definitely the ones that are used the most. So let's go ahead and get started. First up is strings. So we've already used this in our previous video. This is really the most basic type that you can use in Redis and technically really anything can be turned into a string. In fact, Redis doesn't really have an integer type, we will just store that as a string. Now the max size of a string is 512 megs.
So let's go ahead and look at some examples. Okay, so here we're at the console to look at examples for strings and one thing to remember is that we need to launch our docker command. So I will do that. And remember this is the one that connects and allows us to use the Redis C-L-I. And there it is. Again, at the root of the code, you should see a docker command, that'll cover any of the commands that you would need to use for docker, and also all the commands that I'm going to use here.
You can also find them in a Redis commands dot text file. So let's go ahead and get started. So we're going to call "set user:1:username", this is a key. And then we're going to set the value, and we're going to set it to "josh". And we see it comes back "OK". So here, if we just give it a single string with no spaces, it'll take that as a string. If we have spaces, or some characters in it, we would just want to quote it, and that would work. Now, to get it out, we can call get user one, well if I spelled that right, username.
We see that it returns "josh". So this is kind of be the core way that we can throw strings in and just get them out. This makes sense, its the simplest, it's easy to do. Let's jump to the next, and that is hashes. So this is essentially a map of keys and values tied to a key. This is probably the closest thing to a real row of data that you can get in Redis. So continuing on with our example of a user and some fields, we will add some fields about the user using hash. So to do this, we'll use "hmset" which is hash multiple key set, and then we have to give it a key, user one, and then we can give it a bunch of fields.
So we will say first_name Joshua, last_name Johanan. So we run that and now we've set user one as a hash. To get it out, we can call "hgetall" and give it a key. Again, everything is indexed by a key, you have to know the key. And so there we go. It returns first_name Joshua, last_name Johanan. So in this way, we can keep adding fields and values to a key to get more information, where as, you know, with a string, it is just that value.
One thing to keep in mind are the commands that we are using will be covered more in depth in the next video. We're just more talking about the data types. And its kind of hard to show data types without actually running an example with a command. So next up are lists. These are just lists of data that are stored in the order they are added. Now we can add an item to the front or the back. And we will cover how that works in the next video. An important thing to remember, that these lists in Redis are fastest when accessed in order by index.
This means starting at item one and going through the list will be fast, while starting at the 100,000th item would be much slower. Now another thing is that lists can have duplicate items. So let's use a list. So let's extend our kind of example that we're doing, and create a list of the last five other user profiles this user has looked at. We're going to call it user one profile views, and then we will use L push to add to the head. So let's go ahead and do that.
So we're going to say "lpush" and we're going to call it "user:1:profile_views" and then we give it a value. So we'll just give it an index and say this person looked at user five. And then, we're going to add a bunch, since this is a list. Say they look at user ten. Let's do 15. Let's do 18. Okay so we have some data in there, now you can see the integer returned is the amount of items in that list.
So now, how do we look at that? Well, we're going to use lrange. And we have to give it a key, just like always. So we're going to say "user:1:profile_views" and we have to give it a start to a stop item. So we'll say zero, which is starting at the first item, and negative one, which is the last item. So this will give us everything. We run it, and as you can see, because we added them with an lpush, that's to the front of the list.
So it's in reverse chronological order that we added them. Which actually kind of makes sense in that 18 was the last one we put in, which means it's the first one out. So we have 18, 15, ten, five. Let's say we just wanted to see the top three, because it's zero indexed, we can say two. So from zero to two, which should be three items. And as you can see, we get 18, 15, ten. Okay, so that is lists. Now that brings us to our next type, sets. Sets are unordered, unlike lists.
This means that items can be returned in any order, so that is something important to remember. Another difference is that sets have unique members. If you try to add the same member twice, it will not have two items that are the same. So this also means that we can do set theory operations like union, difference, intersection, and more. Set should be used for anything that needs to be unique. So let's say we have some posts in our example that we've been kind of working with here, and in these posts you mention user.
So let's go ahead and create it like so. So S-add, which is sadd, we need a key, so we're going to say post:1:users that are mentioned, we're going to say one, two. So this mentioned user one and user two. So we run that, we see that it's at two. So let's try and do that again. Well again, I got to type it right, and let's retry to add user one.
We see, it returned zero. Now if we run "Smembers", give it the key, we will see that it has one and two. Now we can of course add one that doesn't exist, and that will add it to it, and if we run members, we see it returns one, two, three. Okay, so as you can see, sets are similar to lists, but they are unique, and this is key. And again, when we talk about the commands that you can use, some of this theory will kind of come together.
Because without commands, it's hard to talk about data, and without data, it's hard to talk about commands. So here we're just kind of giving a quick overview of what the data types are and then we will build upon this in the next video. So that brings us to our final type, and that is sorted sets. So, as you can guess, these are a lot like sets. But, we can add a score to each item. The score can then be used to pull items out of the set. So let's go ahead and see how that works.
So for here, we'll use a sorted set to log the last logged in users. So, the score we'll use is a time stamp. Now I'll just be arbitrarily making up numbers, but just know that you can pretty much take the time stamp of now, and then use that, and it will sort correctly. So let's go ahead and look at that. We will run zadd with the key logins and then we give it a score, we'll say 500, and then we will put the value of one.
So, this is saying that user one logged in at a time stamp of 500. We will do it again, this time we'll put a time stamp of 600 and then we will say it was user 15. And then we will do it again, this time giving a score of 650 and say it was user 18. Okay, so all these are stored into a set, but it's sorted by that score.
So let's go ahead and look at all of the users in this. So it's zrange, just like the list is lrange, and then we will say "logins" and we will start at zero and go to the last. And if you run that, you see that it's sorted. For example, if we added another one in here and gave it a score of 550, and said it was 20, and then ran the zrange again, we can see that it sorts correctly.
It put 20 at 550, which is in between. And as you can see, you can call this with scores. And it returns one, which it's score is 500, 20, which is a score 550, 15 which is 600 and 18 which is 650. Now if you want to see it reverse, you actually can do reverse range, give it the key, and again say from beginning to end.
And as you can see, it's inverted. Okay, so that ends all the data structures we're going to use today. So in this video, we've looked at the most popular data types in Redis. We discussed what they are and then used them in an example scenario. Now in the next video we'll look at the commands of Redis more in depth. So far, we've just used commands to demonstrate Redis in action, but we haven't really discussed anything about each command. And this of course will build upon what we've learned about the Redis types.
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