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

The "Namespaces" 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 explains how namespaces are used to avoid name collision within a key-value store database, and demonstrates how to use namespaces in a Redis database.

Preview
Close

Transcript from the "Namespaces" Lesson

[00:00:00]
>> So Redis doesn't have the concept of databases or anything like that, or collections or tables, it's really just a big grab bag of keys. So now anytime I can just ask for name, and it's just a bunch of keys. The thing that you should be worried about is how do I make sure that I don't have naming collisions, right?

[00:00:16]
How do I make sure that I don't use name later, and you're gonna do that with something called namespaces. It's just the way that you're gonna name your keys to make sure that you're not overriding those keys later, and I'll show you how to do that. Let's say I was storing addresses for users, or let's say city.

[00:00:34]
So we're sorting cities for users. So what I could do, let's say I had set, and I'm gonna be working with my users and my users all have addresses. I'm gonna put a colon kind of just separate these different things. And then user:address, let's do this way. It's kind of a trick to figure out how you wanna name these things.

[00:00:58]
So, this is gonna be my user namespace. And then I'm gonna have a use:btholt for just my users namespace. And then I'll have a city in here, where I live, and I'll put Seattle there. And then later, I'm gonna add another user to this database, and it's gonna be sarah_edo is her her username and put city, and she lives in Denver.

[00:01:27]
And then we'll have another user here, user:1mark:city lives in Minneapolis. I'm impressed that I remembered how to spell that. There's a lot of Ns in there, actually there's just two Ns it feels like a lot. But you can see here we kinda use these namespaces to kinda separate these out.

[00:01:50]
Now I can be kinda assured that user and then username and then kinda city in second establish this kinda hierarchy just by using nomenclature. Like if you're familiar with the BEM way as naming CSS, it's kind of the same ideas, these names in another cell don't have any power.

[00:02:07]
But we're using these kind of hierarchy to describe things to make sure we don't have naming collisions, right? Again, namespacing is kind of how you refer to that. I mostly see people use colons with Redis. Just the way it is, I don't know why people chose to do it that way.

[00:02:26]
But I also have seen people do user/btholt/city or they'll mix and match to user:bthold/city, I'll see people use slashes as well. So however you wanna do that. And then again later, we can do Get user:btholt:city, and it'll give us back that I live in Seattle. Make sense? It's kind of just a general tip of how to keep things separated while in Redis.

[00:02:59]
>> Okay, I can choose whatever element I can, like other than that columns.
>> Can use any delimiter that you want to with Redis. I think so, let's go with yes, I think you can use whatever delimiter you want. I think, they'll again generally see people use colons.

[00:03:18]
And I think there's some tooling that they've actually built around people, assuming you use colons and slashes. But there's nothing from stopping me of saying user.btholtd.city Seattle pretty sure. Yeah, I mean that should work just fine. It's a good question though. I'm trying to strongly encourage you to use colons though because that's what people generally use, cool.

[00:03:47]
So something else really kind of interesting and strange I've seen people do is, let's say you have a list of photos that you've banned from your website and people keep trying to upload this photo. So what they'll do is they'll actually use the photo binary as the key, cuz these keys can actually be almost arbitrarily large they can be up to 512 megabytes of length for the key.

[00:04:13]
I don't do that, I think it's actually a poor idea because then you slow down your lookups because it has to traverse through these giant keys, but it is technically possible to use binaries as the keys. In that particular case, what I would say, I would take a hash an MD5 or an SAJ, a SHA, as people say, of the photo and then use that as the key and then you would know if I can upload or not that particular photo.

[00:04:40]
But there's a lot of clever things that you can do 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