Redis Crash Course Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the reddit crash course in this video I'll be going over the fundamentals of Redis we'll talk about what it is some of its advantages with supported data types and we'll get reticence Talde and we'll go over some of the commands for the different data types I do plan on building a node.js app that uses Redis but I don't think we'll get to that in this video okay now some of this information or actually all this information is from a Redis course that I created with edge wanax in that course we talked about all this and more and we do build a node.js app so I will put the link to that course in the description alright so what exactly is Redis many of you may have never even heard of it but it's a pretty awesome technology it's an open source in-memory data structure store and it can be used as a database and/or a caching system and a message broker so if you really wanted to classify it as a certain type of know SQL database I think it best fits into a key value store the structure is actually incredibly simple alright so one of if not the best feature of Redis is the huge range of data types or data structures that it supports such as lists sets and so on and I'll be going over these in depth throughout this course Regis's also has built-in replication master-slave replication that allows Redis servers to be exact copies of master servers masters can have multiple slaves and replication is done asynchronously meaning that the master will continue to handle queries when slaves are synchronizing all right I'm not going to really get into replication because there's a lot to it and this is just a really basic guide on learning about what Redis is and learning some of the commands and how to set it up so I want to focus more on that then stuff like replication and stuff like running Redis and production all right so let's take a look at the data types that reddit supports so we're going to cover these the ones on the left mostly the ones on the right are a little more complicated and beyond the scope of this core so we have strings which are the most basic kind of rennet a string can contain any kind of data then we have lists which are simply lists of strings and we'll be going over all the commands to push on just onto lists and take off take strings off of them and so on sets are an unordered collection of strings we can add remove and we can test for existing members of a set then we have sorted sets which are non repeating collections of strings okay each member of a set is associated with something called a score and will go on to that we'll get into that later and then we have hashes which are basically key value pairs these are the perfect data types to represent objects you can almost compare hashes with JavaScript objects or JSON when we build our node application this is what will mostly be working with all right and then bitmaps and hyper logs these are actually based on strings but have their own set of semantics all right we're not going to really get into that it's more important for you to understand the ones on the left all right so let's take a look at some of the advantages of Redis it's incredibly flexible in terms of storing data just like many know SQL databases we don't need to define a schema or column names or specific data types for each value or anything like that it's much different than SQL or relational databases where everything needs to be defined before you can actually use it where this is also exceptionally fast it can actually perform around 110 thousand steps per second and about 80 1,000 gets per second alright and as we saw it supports rich data types such as lists and sets and it's also both a caching system and a database that can persist data to to the disk so in my opinion Redis is almost the mix of something like MongoDB which is a flat-out data test not no SQL database and then something like memcache which is strictly a caching system not a persistent database so we really get the most of the best of both worlds so let's take a look at what Reddit can be used with and I think better question would be what can it be used with so this is actually from the documentation and this gives us a list of all the languages and clients that rhetta's have alright so you can see we have JavaScript or nodejs PHP Python we have higher-level languages like Java C++ just about anything so no matter what type of developer you are whether you're a Java programmer or a PHP web developer Retta is definitely worth learning all right now as far as security goes Redis was designed to be accessed by trusted clients it's not something that you want to expose to the Internet it shouldn't it should be used internally it does have security features like simple authentication and you can restrict it to certain interfaces unfortunately data encryption isn't supported so again only trusted clients should have access to your renovated base all right guys so enough with the slides we're going to go ahead and jump in and install Redis and we're going to start to look at some of the commands and some of the data types all right guys so we're going to set up Redis and I'm on Linux Ubuntu the blood registers cross-platform so you can install it whether you're in Mac or Windows or Linux now if you want to get it from the website you can go to download at Retta Scott I hope and here you can download the tar file tar.gz file but if you're on Windows you want to go down to where it says Windows and go to learn more and on the github page go down to this release page link right here kind of hidden and you want to grab the MSI file and that's the with the Windows installer just run it go through click Next and it'll get everything set up all right now if you're on w in a bun too like I am you want to use your your apt-get package manager so let's say sudo apt-get update all right and I'm just going to run sudo apt-get upgrade yes all right I made this really big so hopefully you guys can see this because we'll be working in the command line interface quite a bit all right so now I'm going to do is run sudo apt-get install Redis - server okay so once that's done before we started I just want to copy the Redis config file just in case we need to change that so let's do CP x a1 through sudo CP / SC / reddit / rediff.com and we're going to put that in the same same file so at C / our same folder and we're going to call it rediff.com dot default all right so now that's coffee you should be able to run the CLI by saying Redis - CLI okay your prompt should change to your server and default port is 63 79 all right so Redis is now installed and running we can clear the console by typing clear and just minimize this because we're going to be working in the CLI for a while so let's start with just some basics so if we want to test to see if there is a connection that's still alive we can save ping and that's going to respond with pong okay that lets us know that there is a connection with Redis all right we can also echo things out and it's common convention to use caps but you don't have to and we can echo out let's say hello world and that goes out the string if we want to close the connection we can say quit I'm not going to run it they don't want to close it but we can so let's start out with getting set and this is really simple basically just set a key value pair okay so let's say we want to set and just call this we'll just call it fubar or just foo all right so let's set this food to value and we'll set it to a hundred and it can be a number or it can be a string and we get okay so if we want to get that we can simply say get food okay really simple if we wanted to set the set bar - we'll just say hello world okay and we can go ahead and get ba and we get hello world so that's getting set very simple we can also increment numbers so we have the 100 in foo so we could say incur and they incur what I call it through and that gives us 101 so now if we get through that's going to be 101 it's just incremented by 1 if we want to bring it back down we could say Decker foo and that brings it back to 100 we can also check to see if something exists by saying exists foo and you'll see that gives us a 1 if we say exists through 1 we get a 0 because we didn't set that now if we want to get rid of a key value pair we could use dealt so let's say del will delete bar so now if we say exists bar we get a 0 all right if we try to get something that isn't there so let's say get far we're going to get nil okay which means that nothing's there empty now if we want to clear everything out entirely we can use flush all okay so if we do that and now we try to get foo which we didn't clear out you'll see that it's not there okay anything that we had set up to that point is going to be gone flushed out all right now when you're saving key value pairs you can also use key spaces so let's say we wanted to set say we have some server info we could say set server and then : name and we'll set that to some server okay and then we could say get server name and that will give us some server and then let's say we wanted to set the port if you could say set server port and set that to let's say 8,000 okay so then we could get server port okay pretty simple we can also set values to expire at a certain point so let's say let's set whoops okay like set will say greeting and I'll set that to hello world okay we can get greedy now if we wanted this to expire at a certain point there's a certain number of seconds we could say expire greeting and we'll save 50 okay and that will expire in 50 seconds now if we say TTL where they call it greeting reading that will give us the amount of seconds that are remaining you'll see that at 40 seconds if I run it again thirty five so it's counting down alright and then it's going to just expire completely when it gets to zero all right so we'll just let that count down 14 seconds nine eight two-one-zero and now we get negative - okay and if we say get breeding that's nil it's gone all right so that's how you can set expirations so let's clear this out now if we want to set the expiration along wit setting the actual value we could use set X okay so set X and then let's say greeting and then we want to put the number of seconds we want we'll say 30 and then we want the value so we'll say hello world okay and then if we do our TTL greeting you'll see that it's counting down again all right so that's if you want to set the value and the expiration at the same time now if you have if you set an expiration and while it's counting down you want to basically take away that expiration and persist it and you can use persist so let's go back to this right here and let's just set this to 1:30 all right if we say TTL greeting 126 now let's say persist greeting and then if we do our TTL you'll see negative 1 because it's not it's not going to expire at all and if we say get greeting you'll see it's still there all right so we've gone over again set but didn't get set will only set one value if you want to set more than one key value pair you can use em set all right so let's say M set I will say let's do key one and we'll set that to hello and then let's say key 2 and we'll set that to world so now if we get T 1 we get hello key 2 we get world you can set more than one pair in one command we also have a command called append where we can append one on to another so let's say we want to append two key 1 and we'll just say space world and that will say get T 1 and we get hello world now we can also rename keys or variables whatever you want to call them we can say rename and let's rename key one to greet so now if we say get T 1 there's nothing there but if we say get greeting we get hello world ok all right so now what I want to do is get into data types other than just strings so I start with list I'm actually going to pull up the documentation here and let's search for reddit list see data types okay so a list is simply a list of strings sorted by insertion order so we can add to a list by using L push or R push so if we look at L push and like I said the documentation is pretty good any command you want to look up you can you can see what it does you can see some examples all right so this will insert all the specified values at the head of the list ok so at the beginning and then our push will push it on to the end so let's say we want a list called people so let's say L push actually going to just flush all before we do this okay and then we'll say L push people and let's add Brad okay so now we have a list called people which has one value in it and that's Brad so let's add another person so say Jen and let's have another one we'll say Tom okay and then this is returning the number of people that are in the list now if we want to output the list we can use the L range command so let's say L range and we want our list which is people and we want to say from zero to negative one which is going to give us everybody okay so you can see Tom Jen and Brad we can specify our range so that will give us everybody but if we want to say say one and two that gives us Jen and Brad and the reason for that is because it's zero based so Tom is at position zero okay Jen is one Brad's too and notice when we did L push it pushed it on to the beginning okay Tom was the last person we pushed on if we want we can say our push on two people and let's say I don't know Harry okay so now if we do a range and we look at everybody you'll see Harry was added on to the end okay was added on to the right we also have this L Len or is it right here and this will return the length of the list stored at story at a certain key so let's say L when Jen look do that right what we didn't do the name of the list people gianni play with now on my list okay so that gives us that's not right gives us four also it just gives us the length of the list in general I thought I said something I'll start with that all right so that's ll n now we can also use L pop and our pop and those will basically remove from the front or the back so if we say L pop people we get Tom because that's what it's going to remove okay if we want to now look at the list you'll see Tom was basically popped off from the left if we do our pop people we get Harry and now that popped off of the other side so now we only have Jenn and Brad in our list now let's say we want to insert in the middle here and we don't want to insert from the left or right for that we can use L insert so let's say L insert people and we're going to say before Brad yeah before Brad and then Jenny and now if we look at our list whoops that's not right it is the for Brad but I added another Jen all right so let's do L pop people all right so now we just have Jenna and Brad and let's go back and do the insert and we'll say before before Brad Tom and I there we go so that inserted Tom before Brad okay so now he's in the middle all right so let's list and those are these are the commands that go with lists you can look up these there are more I'm not going to go over every single command if you want to get more in-depth I suggest watching the edge' wanax course that I'll link in the description so let's go ahead and flush all and we're going to just clear this and now we're going to take a look at set okay so let's say reddit steps let's go down here so a set is an unordered collection of strings it's possible to add remove and test for existence of members okay so let's see let's look at sad Sadd this will add specified members to the set stored at key all right so let's go ahead and say sad Sadd and we'll call this list or the set cars okay we're going to add say Ford okay you'll see that returns a 1 let's add another one will say conduct and let's add say BMW okay so now we have a set with three members so let's look at the command sis members dismembered and this will return if a number is a number of the set stored at a key so let's do this number cars and Ford okay so that's going to return a 1 if we search for let's say Chevy we get a 0 because there is no Chevy in our set we also have a command called X numbers and that's going to return all the members of the set so let's do X numbers cars and that's going to give give us all the members of the set we also have s card so returns a set number of elements so let's do s card cars and that gives us three okay so that gives us how many elements or how many members there are we can also move a member to another set if we want so let's say s move and we want to move from cars to let's say my cars and we want to move Ford ok so now if we do X number cards you'll see that 4 is gone but if we do s member my cars there's Ford if we want to just remove a member from a set we can use that's REM ok so we'll say X REM from cars and specify BMW ok so now if we look at cards it only has Honda BMW has been removed all right and there's some other things we can do as well as this will return the members of a set resulting from the difference between another set we have what else s pop which will remove and return one or more random elements from the set I'm not going to go and I'm not going to do all of these but just know that those are available for sex so next we have sorted sets I'm going to flush all here and let's clear let's look at sorted set the documentation wearing boots datatype strings or you went over that stuff with okay all stuff we just did and see hashes we're not going to do that just yet we're sorted set all right we'll just search for Redis sorted set oh maybe it doesn't have its own section right here sword sets are similar to where it's sex the difference is that every member of a sorted set is associated with a score okay see that it's used in order to take a sorted set ordered from the smallest to the greatest score while members are unique scores may be repeated so never can have a score can have a score that another member has as well all right so let's take a look at some of these so we have Dadd or dad this is just like sad for the regular set except this is for sorted sex so let's say that we want to add say dad and let's call this users so call the set users and then we wanted to find a score and what we're going to do is we're going to set the user's birth year as the score so let's say 1980 or let's do 81 and I'll put mine any okay so that's going to add it's going to add this user to our sorted set and has the score of 1981 all right so let's do another one we'll say John Doe and let's say 1975 ok we'll add another one say Mike Smith and say 1990 all right so now we have a couple members in our sorted set and just to show you we can have the same score for multiple people so for this let's say C Broadus alright so let's look at G rank so Z rank will return the rank of a member in the sorted set stored at a certain key with scores or it's high-low so let's do the rank people and let's stay like this I always thought people with users okay so that gives us three let's say Z rank John Doe and that gives us zero so the reason for that is because john doe's score is lower than the rest of them all right so if we do me we get one okay so it's just the the ranked in order of the score let's clear this out so we also have the Z range go to Z range users 0 negative 1 and that will print out all of the numbers in the sorted set we can also increment so we can say Z anchor by and let's say we want to increment we can say the name of the set so incurred by users and let's increment by 1 and we'll say John Doe ok so now John Doe is 1976 and we can do more than one as well so if we say by 10 now it's 1986 all right so I'm going to flush all and then clear it and we're going to now look at hashes all right so let's get rid of cache and actually I want to look at H set okay so H set will allow us to basically create a key value pair within the hash now let's say we want to create a hash for each user so we can use H set and then let's say user : Brad and then name and then we'll say Brad Travis ooh okay so we have a hash called user Brad and we set the name value to my name okay so if we wanted to set something else for you too Brad let's say email and we'll change this to let's just say Brad at gmail.com now we have a user that has a name and an email and if we use H get so H get user Brad let's want to say H get user Brad and then name and we can get user brad email as well now if we want to get everything we can use H get is it each get all yeah H get all and then user Brad okay and that will give us the key name the value the key email and the value so when we build our node.js application this is what we'll be using is a hash now if we want to set we just clear the so we want to set multiple values at the same time for a user we can say hm set and let's say user a user John and we'll set the name to shondo and then looks that email to Jago at yahoo.com and it's also set an age of [Music] 25 okay we get okay and now let's do H get all four user John and that gives us the key and the value for all the data we set and if we want to get just the keys we can use H keys for user John that gives us all the keys we can also do H Val which valve which gives us just the values we can also increment so let's say H pinker by and we want to put a user John and we want to increment age HC and then one and then that makes that brings the age to 26 okay we can also delete values so let's say we want to delete the age all together we could say H del for user John and we want to delete the age of age okay so now if we do our get all you'll see that John doesn't have an age anymore we also have h Len so if we say HL n user John that gives us - it lets us know in the number of key value pairs that it has all right so that is the hash datatype and clear this so those are the main types of data okay the main data structures with set sword sets and hashes and of course strange now when we build our nodejs application I'm not exactly sure when that's going to be could be my next video or maybe a little down the line but the commands we learned such as H get H set we can use those with the nodejs driver okay so it's important for you to know those commands what I want to do now is talk a little bit about data persistence there's a few different ways to do it in Redis so let's search for a read of data persistence all right and let's take a look at this so reddit provides a different range of persistent options our DB persistent performs point in time snapshots of the data set at specified intervals okay so that's one way and then we have the AOF persistent which blogs every write operation received by the server so this is a little more solid than than the snapshotting okay and this page gives you a lot of information as far as the advantages of our DB over AOS and you can use both okay you don't have to pick just one but basically with snapshotting we can use the save command and we can run that at any time so we know we have some users we have the user John and user Brad so let's just go ahead and say save and that's going to create a snapshot of that data on to disk and then you can also specify intervals here so if we say save 61,000 this is going to save it every 60 seconds it's at least 1,000 keys have changed okay so you can manually save you can also setup snapshotting integrals alright now when this data is saved it's going to be in a dump file which is going to be in C bar Lib and go down here to Redis there's a dump dot our DB file so if we open that up I can't open it with G edit I'll select the application and let's open it with G at it alright and then here you can see it's not very readable but you can see that our user John is there and our user Brad is there so it took a snapshot of the data and saved it to disk okay now you can set up some kind of backup system like our disk which I do in the core so I'll show you how to do that in the agile onyx course and you can back this file up every whatever whatever you want okay so that's one way to do it now this append-only file this will basically log every single thing you do and you can set this this to yes in the config file and this is in the SD folder the SC and we should have a Redis folder here right there it is and we have our contry also have the backup we created in the beginning let's see we can't open this here let's actually go to our terminal I'm going to open up another one alright so from here let's go cbet see Redis and let's say sudo nano Redis dots con all right and from here this is our main configuration file for Redis you can see we have the port and we should have the AOF option somewhere in here the hell is it all right let's do control W here and search for a Oh F let's do Oh append only that's what we want to search for okay so right here you'll see my default attend only is that - no but we could simply change that to yes and exit and save okay so now we're will use append only so now we have the snapshotting and the AOF persistent method setup all right and there's a little more to it well there's a lot more to it if you want to really go in depth but basically that's pretty much the fundamentals of Retta some of the basic commands the data types persisting to disk and like I said before what's great about Redis is that you can use it as a simple caching system or you can use it as a full-fledged database like MongoDB or something like that okay and it's incredibly fast so hopefully you guys learned a little something here and you enjoyed this video like I said we will be building an application using the nodejs driver for Redis I'm not exactly sure what the project will be yet and it'll be pretty simple but you'll you'll learn a lot from it alright so if you guys are subscribed please do so please leave a like if you liked the video and also if you want to look more in depth on Redis and check out the course in the description alright thanks for watching
Info
Channel: Traversy Media
Views: 396,050
Rating: 4.9240937 out of 5
Keywords: redis, redis tutorial, redis database, cache, redis cache, redis crash course
Id: Hbt56gFj998
Channel Id: undefined
Length: 39min 34sec (2374 seconds)
Published: Sun Mar 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.