Complete Intro to Databases

Redis Data Types

Brian Holt

Brian Holt

SQLite Cloud
Complete Intro to Databases

Check out a free preview of the full Complete Intro to Databases course

The "Redis Data Types" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to use the list data type on Redis, and creates a list of notifications that is linked to a given user. Notifications are an example of data that can be stored using Redis, because the loss of notifications would not be a drastic loss of data within a given application. Hash and set datatypes in Redis are also discussed in this segment.

Preview
Close

Transcript from the "Redis Data Types" Lesson

[00:00:00]
>> Everything you've been looking at so far is a string. Believe it or not, even if I say, set num 5. And I say TYPE num. It says num is a string. It's just everything is a string in that particular capacity in redis. It doesn't care about numbers.

[00:00:19]
It really is just saying, all right, well, that's a string too. It doesn't care. Even though you can do INCR num, and it's like, cool, that's now a number, that's 6, right? It'll store everything as a string so that's totally fine. Strings are binary safe. So technically, you could put whole images in here, right?

[00:00:41]
You could have a string, but it's actually really a JPEG. It's fine. I wouldn't recommend it. I'd recommend using something like nginx for like that, but it's possible. If you needed to temporarily store something in a cache, you could put it in Redis. They're also lists, ie arrays, sets, sorted sets, hashes, hyper log logs.

[00:01:04]
I didn't have a stroke. It's that's what they're called hyper log logs and streams. We're not gonna really be talking about streams when they're brand new and I'm. There's not really too much that we could talk about with them. And we're gonna really gloss over what a hyper log log is.

[00:01:21]
I just wanted to say that as many times as I could on air. Okay, let's scroll down here. Why did this look so upset, okay. Let's do some lists first. So a great time to be using a list or an array Redis calls them lists, but arrays lists, they're the same thing relatively.

[00:01:47]
Let's create one. Let's say we're doing an app and this app does to do reminders of you or for you. This would be a great time to use Redis. Because if you accidentally drop all their notifications on the ground, like you just lose all that data, probably not the end of the world.

[00:02:06]
You just lost some notifications. Applications you still have that source of truth in the database. So let's use Redis for that. So let's say we want to remind our user B to hold to call his mom feed the dog and take out the trash. Let's say RPUSH and notifications: btholt Call your mom, feed the dog.

[00:02:34]
And Take out trash. There's a nonzero chance that my mom is watching the stream right now and she's gonna like send me texts like you better call me. I didn't call her this Sunday so she's gonna be upset about that one. Okay, so RPUSH it's pushing on the right.

[00:02:51]
This is kind of like you remember queues and stacks and stuff like that. So you're doing like stack.push, right?. That's what our push does. It pushes on the right side or the end of the array. This array doesn't exist so it will create one on the fly for us.

[00:03:06]
And so now it says integer 3. That means I successfully pushed three things on to a brand new array. And I can say LRANGE notifications: btholt, and this is how you do length in Redis. I'm gonna say start from zero and go to the end which is what -1 is and this will actually give me back everything that I have in my array at the moment non destructive.

[00:03:32]
So if I do it again, it's gonna just it's basically like get for the arrays and I can do -2. Notice that this doesn't say take out the trash. Or I can say two numbers 3, right? And this will give me everything it gives me to the third one.

[00:03:48]
So -1 is the end, -2 is two from the end, or 3 is the to the third index. Does that make sense. It's kind of a strange way of doing it, but that's the way they architected it. Okay, so let's say we show the notification take out the trash to our user, Brian and he's a, busy little boy and actually does the things that the notifications tells him to do.

[00:04:16]
We can say RPOP notifications: btholt give us back take out the trash. But if we do LRANGE one more time now notice that this is no longer in the database or in our, in our notifications, right? You can pop things in and out of the list. As you might imagine, we were doing LPOP, if you wanted to do shift in JavaScript terms, which who uses that terminology.

[00:04:47]
But a DQ if you're using normal terminology, or LPOP in this where it's going to remove it from the front of the list. So if Brian does call his mom, we can do this. It'll say LPOP. And we can do our range again, and now the only thing left down, there's feed the dog.

[00:05:10]
You can also do LTRIM, and LTRIM, this will actually take off multiple things in the array, as opposed to POP, it's just as one. So, it's a tie remove multiple things as both LTRIM, and LTRIM. So that's lists. Let's talk about hashes. Hashes are objects like if you've ever worked with Python, they like to call things hashes or other languages.

[00:05:36]
So they have a thing called HMSET. And we're gonna say, let's say we have a btholt: profile. And here you just do key value pairs. So my title Principal Program Manager, my title is too long. Can barely spell it. Company, Microsoft, city, Seattle, state, WA country, USA, okay.

[00:06:14]
So this is all key value pair so title will go the next thing will be the first is the key then the value key value, key value. And now, I can say HGET like this. And then, I can say btholt:profile. So, I have to reference wherever that hash is.

[00:06:33]
And then, I just asked for whatever I need. Let's say, I wanna know what city Brian lives in? I can say city and this will give me back Seattle. So it's all relatively straightforward. It's the same thing there's HGETs there's HSETs, there's HINKERs. So all of these commands I've been showing you have hash equivalent.

[00:06:56]
And then if you wanna do HGETALL btholt: profile, you'll see that it'll give you everything back in key value pairs, right? title Principal Program Manager company, Microsoft city Seattle. It comes back as an array, Or a list in this, as it is. Cool, good so far, there's a bunch more commands that you can do.

[00:07:23]
I'm not going to show you all of them because they're relatively copies of each other. But I left a link there in the course notes for you to see what all the different hash commands. Okay, sets in sorted sets, a set is just a group of things right?

[00:07:40]
So let's say I had a thing called colors. So I can do set add or SADD as I like to say. I would like to SADD colors. And you can just say red, blue, yellow, green, black, pink, brown, okay. And I just added 7 colors to the, color set.

[00:08:09]
And then you can do things like SISMEMBER, colors green. And it's gonna say, yes, I do know green, but I can say is dog member of color. And it'll say, no, dog is not member of colors. If I wanna see all of the members of it, I can just say SMEMBERS colors and this will give me everything that I have in the set.

[00:08:34]
And I can say SPOP colors and it's just gonna give me black a random color. So in this case I got brown black. I don't know if it's necessarily the last one you added. Nope, it's not because I added black then pink. So it just gives you back a random color.

[00:08:52]
And that's basically what sets are is you can just keep a bunch of random things in there. Like, I wouldn't necessarily like keep every user's email address in there but you I guess potentially could say like, well, if you're a part of this set, then I can't let you do something like that.

[00:09:07]
So it'd be good for like a allow list banlist kind of scenarios. So yeah, that would work for that. sorted sets are relatively similar. Instead of doing SADD and S members, you can do ZADD and Z is now a sorted set. So let's do ordinals. And it's kind of like a priority queue if you're familiar with those that you give something a priority or like a number.

[00:09:39]
And if you ask for a back, it'll give you back at that those things in that order. So let's just look at an example that. So if I add with a priority of 3 third. So key here is the third and this is the priority and ordinals is the name of the set of things, okay.

[00:09:59]
And then I say ZADD ordinals or the ordinals as an ordinal number numbers. That's why I use the term ordinal. One first ZADD ordinals. 2 second ZADD ordinals 10 tenth and then I came back and said. ZRANGE just like we do with lists ordinals and gives me all of them.

[00:10:31]
Sorry, like that. Notice it actually gives them despite the fact that third, first second tenth it actually gives them back to me in the correct order, because of these numbers, right? So it's a sorted set. That makes sense. I'm not entirely sure when I would use this. I guess if I was implementing a priority queue, this would be a good way to implement a priority queue on Redis.

[00:10:58]
So maybe if I, like I was using Redis to order print jobs Might be a good way to do it that way. I don't know why I would use Redis to order print jobs, but that's what came to my mind. Ordering network traffic. That would be another good reason.

[00:11:12]
Any reason you'd use a priority queue this is a way that you could do that with Redis,

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now