Complete Intro to Databases

More Redis Concepts

Brian Holt

Brian Holt

SQLite Cloud
Complete Intro to Databases

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

The "More Redis Concepts" 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 Redis has the ability to evaluate Lua, a scripting language, adds that when engineers need Redis to accept entire programs they use Lua, and demonstrates how to use the Lua language in a Redis database. The least recently used data, or LRU, and how Redis has the capacity to delete it from a database is also discussed in this segment.

Preview
Close

Transcript from the "More Redis Concepts" Lesson

[00:00:00]
>> Let's talk more about Redis. So this is the more Redis concepts part of the notes. And we're going to be talking about some of the more advanced things that Redis can do. Yeah, and we're gonna do one of them, the other ones we're just gonna kind of chat about.

[00:00:20]
So Redis does have a peculiar ability to do to evaluate Lua I don't know if you're familiar with Lua. It's a very simplified, it's a programming language that doesn't have a lot of syntax. We'll go with that. You can do a lot of things with Lua, but it is a fairly small language in that particular capacity.

[00:00:48]
So, if you need to do something like really complicated with Redis, it does have the ability to actually accept entire program. To do that within Lua. Now, my general advice for you is if you're coming at a problem and you're saying, I need Redis to evaluate Lua for me, you might reconsider what you're doing.

[00:01:12]
It might not necessarily be a good idea. I really can't fathom of like what I really wanted to do right now I wanted to evaluate Lua for me. So, I was actually talking to one of the engineers that used to work on mixer right mixer is like the twitch streaming platform that Microsoft I've used to have apparently mixer used this capability quite a bit.

[00:01:36]
So, I don't know, that was a big website that handle a lot of traffic. They did it. So, I've never had to do this. I actually kind of think this is a bad idea, but I could see eventually being useful and we're gonna do it for science. So, let's just clear this our a little bit.

[00:01:54]
So I'm gonna say EVAL, which is how you tell us I'm about to give you some Lua's, so buckle up. And then you're gonna say, like we're gonna write a little bit of Lua. I don't know Lua so I actually had to look up how to do this.

[00:02:06]
For i = 9, or i = 0 ,9,1. So, from 0 to 9 increment by 1 Do redis.call, SET, and then we gonna do a Lua key. So lua underscore key, underscore dot dot, this is all lua stuff. I, i times 5, End 0 So what does this do?

[00:03:00]
Again this is not a Lua class, so if you don't understand this. I don't particularly understand it super well either. This is for loop from 0 to 9. And it's doing a Redis called sets, how you actually call into Redis and calling set on Lua key 0, Lua key 1, Lua key 2, Lua key 3 and setting those.

[00:03:21]
So if I say GET Lua key 5 like that, it's 25 right, because it's 5 times 5, again Lua key 630. So we're doing this all just so you can see you can provide a bit of Lua code it will go through you can do multiple sets. This is kind of where I think that Lua could be useful as if I had to do kind of a bunch of stuff all at once and I wanted to send it all at once to Redis.

[00:03:52]
I still think I'd rather do that in my Node JS code rather than sending Lua to be evaluated by Redis. But, I just wanted you to be aware that there is a Lua interpreter built into Redis. Sorry about that 0 at the end here, Redis has the ability to run in a cluster and in a sharded cluster as well.

[00:04:17]
If when that's the case, you actually have to tell Redis I'm sending it to these keys. So I'd actually have to come in here and say 10 Lua key, 0, Lua key. And I'd actually have to go out and say, here are all the keys that I'm about to change, because it has to shard those correctly.

[00:04:38]
And it has to know that upfront before you do it in Lua. So, that's what that 10 means. Just so you know. Again, I don't particularly see this being super useful to many students in this class, but it's kinda fun. We wrote a little bit of Lua today.

[00:04:58]
How many of you expected to write Lua today? If the answer is yes,. I have more questions for you [LAUGH] all right. I think that's enough. Lua and Redis for today. I have another example in there but the relatively the idea is you can actually pass arguments into the Lua, and use those with dollar signs or keys, 1, keys 2, so on and so forth.

[00:05:31]
In fact, we'll just copy and paste that so you can see it. It looks like we're down here, more Redis concepts. So, here, you can actually pass in arguments. That's what the ARGV is, is you can actually pass those arguments into the the Redis pro or the Lua program as well.

[00:05:57]
Okay, good enough. Pub Sub, we actually did talk about this. You can do this with streams. This is better for example, if you were gonna try and pull reddish for changes, you could use a stream to do that in better. So, publish and subscribe. That's a pub sub stands for, it's actually a very useful pattern.

[00:06:15]
I'd never done it with redness before, but I'll probably give it a shot after this to see how useful it is. It's useful for real time notifications and stuff. Things that you would, yeah, you would use something like that for. Transactions, so we've talked about this in previous where you wanna send multiple things at the same time.

[00:06:38]
Redis also has this capability of doing transactions. And it has a couple different ways of doing it. You can do with multi exec, discard and watch. I'm not gonna really get into it because I've never had to do transactions in Redis If that's really interesting to you, conceptually understand what a transaction is.

[00:06:56]
So you click here, that'll take you into the Redis docs that'll tell you how to do transactions in Redis. But the one I did wanna spend some time on is LRU. So, one of the ways actually literally the only way that mem cache works is you say, all right, mem cache, you have 100 megabytes of memory all to yourself, right?

[00:07:23]
And you can store whatever you want in there, but you get no more than 100 megabytes or a gigabyte or 10 gigabytes or whatever you wanna give it. What will happen is, once it hits 100 megabytes, it has to evict something from its cache, right? Something has to go away so it can fit more stuff in there.

[00:07:40]
And do that does that with a strategy called LRU. Last or least recently updated, right. Not even updated least recently used is actually the correct one. So what mem cached is gonna do is it's gonna go look inside of itself and say what key in here. Is the least recently read or written to, and then it'll take that key in evicted out of there so that it's gone.

[00:08:09]
And this is called an LRU strategy. This is useful for things that you're just, setting cash on the fly. And it's making the fundamental assumption, which is probably a pretty good assumption that, if you're never using a key, it's probably okay if you evicted from the cache. You can set Redis to act in this exact same way as well.

[00:08:29]
And it's a little bit of a step up from mem cache because everything's getting written to disk so that if your server accidentally goes down, you can just say, hey Redis, restart yourself. And it'll reload its cache because everything was getting written to disk. That is the only way LRU is the only way that mem cached can work.

[00:08:46]
Redis actually has a couple of different strategies. I left a link to that here, but the idea here is if I can show you this actually give you a whole thing of how to do LRU. So, that's good too. Yeah, so I had a good example here with gravatar.

[00:09:07]
So imagine you're using, like gravatar is it people imagine most people have seen gravatar before which the idea is that you have an email address. And then anywhere that anyone that goes to another website that uses gravatar can get your same Avatar and they just use that same gravatar service.

[00:09:23]
You're probably gonna cache users that are frequently using gravatar and there's probably a lot of people that use gravatar that very rarely hit gravatar. So an LRU would be very useful to them, because those people would get read from the database and that's fine. But you'd have a high cache hit percentage.

[00:09:41]
And that's actually probably what you're mostly looking for with caching is you want caches to have hits, right. So you want to find things in the cache. That's what a cache hit percentages. And you wanna have low cache misses, right? And cache misses when you look for something in the cache, it's not there.

[00:10:00]
All right, so LRU that's one way that Redis can do it. There's a bunch of other ways that Redis can do it as well. So again, just wanna call out here, you can always go to this DB samples page. There's a Redis Directory here that has the complete code in here.

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