Check out a free preview of the full Complete Intro to Databases course:
The "Key-Value Store Databases" 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 that key-value store databases are rarely used as the only database in a company, and are generally used for data that is less important like cache data. This section focuses on Redis, a key-value store database. but in this segment Brian introduces other key-value store databases that could also be used. How to set and get data from a Redis store is also discussed in this segment.

Get Unlimited Access Now

Transcript from the "Key-Value Store Databases" Lesson

[00:00:00]
>> So the three things I've talked about so far, document databases, relational databases, and graph databases. They're more used for storing data. They're more for being original sources of truth. And then yeah, they're meant to be original sources of truth. I think that's what I wanted to say about that.

[00:00:24] A key-value store is actually an entirely different beast, and used in an entirely different way. Usually it's a companion to another database. I'm gonna say usually, cuz I have seen people do very strange and unexpected things with key-value stores. But suffice to say, That's not the common use case for these.

[00:00:43] So you can think of a key-value store as just the world's largest JavaScript object, right? You can set a key on an object to be some sort of value. And then later, you can come back and say give me back the key that I stored in this particular object.

[00:01:02] This ends up being really useful for a couple of things. One of them is for caching, right? So if you have some sort of very expensive operation to do, and you don't want to have to redo it every single time that someone makes a request to a particular endpoint, a key-value store is actually one of the best things you can use for that.

[00:01:22] If you had a very expensive SQL query that queries every user in the database, you can you can cache that temporarily in a key-value store, such as Redis. What else is this used for? One of the things about key-value stores is normally, you don't store data in there that you can't afford to lose.

[00:01:45] So caching is a good example, you can afford to lose your cache. It might suck to lose your cache, but you're not gonna drop data. Another good use case would be telemetry. If you lose all of your telemetry for a day, or something like that, it's probably not the end of the world.

[00:02:01] You don't want to, but if you lost it, you wouldn't cry about it. Whereas, if you were storing bank transactions in Redis, might be a problem. And a bigger example of this will actually be memcache, and we'll talk about that here in just a second. Again, people are gonna disagree with me.

[00:02:17] But I don't think it's a controversial thing to say, is that you typically don't wanna use a key-value store for mission critical data. So we'll get into it. I'll show you how to use Redis here in just a second. But I just wanna let you know that there are a couple other options out there that are not Redis.

[00:02:38] One of them is Postgres has an ability called hstore, which is really a key-value store. So if you're already running Postgres, and you have key-value kind of things that you wanna store, and performance is not necessarily the reason why you're doing it, then hstore would be a good option.

[00:02:57] hstore, it's a query against Postgres. And a lot of times, that you're gonna use a key-value store is to get away from overloading your database servers. And so you don't wanna just add more things to your database server that kinda defeats the purpose of why you would use a key-value store in the first place.

[00:03:13] So hstore is not gonna solve all of these problems, but I just wanted to throw it out there that it is actually possible to do it with Postgres. The most common one I would say is memecached. I imagine many of you've probably heard of memcache, probably have used memecached.

[00:03:27] Memcache is like the SQLite of key-value stores, in the sense that it's very simple. It's very small, and it's meant to do huge amounts of operations, because it's not gonna write anything to disc ever. So if you turn off a computer, guess what? Your entire memcache goes away, so that can be a little scary to people, so don't store essential stuff in memcache.

[00:03:51] But if you're just doing really quick caching kind of things, memcache is actually a really good option for that. It's, yeah, the most simple program in the world. All you do is give it keys and ask for keys. That's really the entire capability of what memcache can do.

[00:04:06] Today we're gonna talk about one called Redis. So I hear people say Redis, just sounds weird to me, so I'm gonna stick to Redis. Redis stands for Remote Dictionary Server, I think that kinda makes sense if you're a Python developer. It's a remote dictionary that you can read and write to.

[00:04:25] I've used it a lot in my career. I'd say it's one of the most common pieces of infrastructure that I see. I know Azure offers a managed Redis. I know AWS does, a lot of companies offer Redis. It's an open source project as well. It doesn't have any restrictive licensing, so it's pretty free to use for you as well.

[00:04:45] And it's a great companion to any other database. So you'll use MongoDB and Redis. So you'll use Postgres and Redis together. So let's go ahead and get started with it. So I'm gonna copy this one right here, And I'm gonna pop over to my commandline. I'm just gonna close this.

[00:05:09] And I'm gonna say docker ps, and I'm gonna first kill my Neo4j. Again, you don't have to do this. If you wanna keep playing with Neo4j, feel free to just leave it up and running. And now I'm going to run this Redis server. So this will get Redis running with my Redis.

[00:05:29] It'll expose port 6379, which is the port that Redis runs on, and we're running 6.0.8. Okay, and then I'm gonna copy this one here, this Docker exec, and this should drop me directly into Redis. Okay, you can see here one, two, so let's make this a lot bigger.

[00:05:55] So you can see here, I've just dropped directly into 127.0.0.1:639, cuz I'm actually running this from inside of the container. So I'm connecting locally, technically, to Redis. So let's get this with the most simple use case possible. I don't know, just ignore that. I'm just gonna say SET name, Brian Holt, like that.

[00:06:26] And it says, okay, I did that. I did the thing that you asked me to do, I set Brian Holt. Now, what happens if I wanna get Brian Holt? You can probably guess, GET name Brian Holt. I mean, that's, let's be honest, about 80% of how to use Redis right there.

[00:06:47] We could probably end the class, and you would just be like, okay, I know enough about Redis right now. We're not gonna stop there. I'm gonna show you a bunch of other really cool stuff you can do with Redis. But getting and setting keys is about 90% of why you wanna use Reddit, Reddit, Redis.

[00:07:01] Too close, we did use Redis at Reddit. That was confusing. Okay, so the primary use case here would be, let's say I queried my Neo4j cluster, and I got a count of all the various different facets of people. And that was a very expensive query to run. What I would do is I would take that query result, and I would put it in Redis.

[00:07:26] And I'd say all right, I have to rerun this query every day to make sure this is fresh. But I can just come in here and say get Neo4j results, and it would just give me back the results, so I don't have to recalculate it every single time, right?

[00:07:38] So it's a cache of what I did from Postgres.