Redis Tutorial In 16 Minutes | Learn What, Basics and How to Implement FastAPI Redis

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Facebook Google all major tech companies are  relying on Redis to increase performance and   speed of their applications in this video I will  be covering everything Redis from what it is how   it works the basics of Redis and how to implement  Redis into your application you might be wondering   what exactly is Redis well Redis is an in memory  database which runs completely on your machine's   Ram Random Access Memory but don't mistake speed  for Simplicity Redis is a Powerhouse supporting   a wide range of different data structures from  strings to lists to different types of indexes   but how does Redis work as it's an inmemory  database which means it relies on caching   but the first thing to note about Redis is that  it's also a nosql database but it's completely   different from other nosql databases like mongodb  the reason is that it's a Json structure so it's   completely dependent on key value pairs now  this makes working with Redis a complete   Breeze compared to other kind of databases where  you're going to be working with intricate tables   of rows and columns the best way I can explain  it is just think of Redis as a Json table but   there's a catch and that's because Redis runs  on the ram of your machine so if your computer   crashes you will lose all the data that Redis  currently has and that's because Ram is volatile   so while Redis is ultra fast and can retrieve  data in milliseconds compared to other types   of databases it is not a good long-term solution  so you'll still want to use other databases for   the everyday a transaction of data but use Redis  for pieces of data that you need extremely fast   and because Ram is extremely accessible that is  why Redis is a Top Choice for increasing your   application's performance and speed now let's  go over an example of how reddis might be able   to fit into your application Let's Pretend your  application is a middleware an application calls   your application and then your application will  call a third-party API endpoint whether that's   to fetch the weather stock prices anything you  want well you have no control over how fast that   response comes back back to your application to  feed to the user so what we're going to do here in   the next application that we build is we are going  to have a Redis database that stores the response   from the first time we call the API endpoint so  then all other calls to our application will be   able to fetch that data from Redis instead of  always having to rely on the third party API   endpoint so let's dive in and first go over how  to install and go over the RIS Basics and Then   followed by an implementation of Redis in our  application now install in Redis is fairly easy   if you have a Mac we can just use home brew and  just do a brew install Redis if you have a Linux   you can use the package manager of choice but  if you have a Windows machine it's a little bit   harder because you can't install Redis directly  on the Windows operating system we will have to   use a subset of Linux that we can run inside of  Windows to download reddis it's fairly easy and   I'll leave the link below so just to start off  I'm going to be doing this through a Mac point   of view but if you have home brew installed on  your machine already we can do through install Redis now what we can see is that we already have  Redis installed so we don't have to go through the   entire download so it's going to take a little bit  longer on your side than mine but I already have   Redis installed all right now the very first thing  you need to do is now run Redis on your machine   and we can do this by saying Redis Das server now  once you do this we can see that Redis is going to   start running automatically it starts by running  on the port of 6379 that's the default port for   Redis and now it's running and the next thing we  need to do is just open up another terminal and   inside this new terminal once we already have the  Redis server running we can say redis-cli and now   we'll have Redis running and we can save data  get data do all the cool reddest things before   we implement it into our application but before  we start let's go over some of the main items   that we're going to be saving and fetching and  creating the key value pair relationship in our   reddis Json database now the very first thing we  can do is set a key to a value so we can say set   name Now setting the name is going to be the  name it's going to be the key so we can say   set name of Eric now all this other stuff is just  predefined stuff so we can ignore that completely   I don't even really know why it's popping up but  we can just say set name Eric we're going to get   okay response back now if we say get name that'll  return Eric because again in our rdest database   name is the key and the value is Eric so when we  say get name which is the key it's going to return   Eric to see it again we can say set OS to Mac and  then get OS which return Mac back so again now in   our R datab base we have two keys name and Os with  their values of Eric and Mac if we want to delete   a key value pair all we have to do is say deel  and then pass in the key which is going to be   OS so let's go ahead and just delete that we can  see that we got a one one is true zero is false   so we got a successful deletion so we try to get  a get OS now we're going to get nil because the   value does not exist in the rdus database and  we can check that by doing exists so we can say   exists OS where we're going to get zero for false  but if we did the same thing for name we can see   that we're going to get one because that's true so  zero false one is true if we want to see all the   keys in our database we can say keys and then  just an asct and we can see that we only have   name currently in our database because we deleted  OS so if we want to set a new type of data we can   say set dog Milo and that is my dog Milo so now  if we go ahead and say we want to look at all the   keys we can see that we have dog and name in our  reddest database now one thing we can do do is   type in TTL which is time to live so a good thing  about reddis is since it's all cash and we are not   using this as the main data within our application  we can set a TTL which means the data will delete   after a certain amount of time now this is super  helpful where in reddis you might want your data   to be deleted every 24 hours because if you're  fetching it from a third party application you   might want to only fetch it once per day save  it in reddest to increase performance and then   delete it at the end of the day so if we wanted  to do this we could say TTL name 10 and we can   see that we're getting an error and that's because  a TTL command is not actually a command but what   we can say here is expire name 10 so what we're  doing here is we are going to expire the key value   pair of name within 10 seconds so if we do this  and then we say get name we can see we have Eric   get name Eric and here in a little bit it will not  exist and we can see now that when I said get name   that time we get nil because Eric has been deleted  after 10 seconds when you create an item we can   create the item with the TTL already included so  we can say set expiration of dog 10 Milo so now if   we say get dog we can see that we get the value  of Milo but then after 10 seconds the value of   Milo will disappear so if we try it again we now  have nil because the value has been deleted all   right so now that we got the basics of how the key  value pairs are going to work let's dive into our   application and build a real life Rus application  into our python fast API now the very first thing   we need to do is PIP install some dependencies on  our application so let's go ahead and just say pip   install rdus fast API uicorn and httpx this will  download all the dependencies that we need for our   application and a recap of what the application  is that we are going to be building we are going   to be building a fast API application where when  you call our fast API application our fast API   application will contact another application  and then will return all of that data so our   fast API application is more or less just going  to be a middleware here so the first thing we   need say is from Fast API import fast API from  Redis import Redis import httpx and then import   Json we then now need to say app equals our fast  API application and now we need to have a startup   a shutdown and then our application so let's  first start with our startup so we can say a app   do on event where we can say startup we want this  to be an async def startup event where we can say   app. state. Redis equals Redis where we can pass  in our host that is equal to Local Host and our   Port which is equal to to 6379 which is the same  port that we can see here so we go back to our Rea   server 6379 6379 on our Local Host we then need to  say app. state. httpcore client will be equal to   our htpx do async client all right now let's do  the same thing for a shutdown so we can say aa.   onevent and inside here we can say shutdown we  can say async def shutdown event which is app.   state. rus. close now what the startup and the  shutdown do is that means when your application   runs for the first time we're going to connect to  our app our Redis environment and our httpx which   we're going to be using as a library to call Api  endpoints from our fast API application and then   on shutdown when we close our Redis application  we will also just close down that connection to   our Redis environment now the next thing we want  to do is just create our API endpoint so we can   say app.get and now inside here I'm just going  to call this our [Music] entries async def read   item and now we want to create a value and the  very first thing we can do here is say response   equals await app. state. httpclient doget and  here I'm going to pass in a free API endpoint   that's going to return a ton of data just so we  can see how long it takes to call it and this   is going to be from https coliu api.org entries  and then I'm just going to return our response do   Json all right so let's run this application by  doing a uicorn main colon app-- reload let's now   open up our application say try it out and click  execute and the first thing we can see is that   it failed from our entries so what we need to do  here is go back to our application and make this   a slash entries let's reopen up our browser click  try it out and execute and now we can see that it   was calling that API and it didn't seem like it  took that long but it's definitely taken a second   or two so if we had to call multiple of these this  would really slow down our application but you can   see that we're getting an absolute ton of data  back so we can click it again we can see that   it's loading and I actually clicked it already  so one 2 3 so it's taking about 3 seconds to   get the results every single time but now we can  Implement reddis to store all this data because   we know that this data is the same at least for  the next 24 hours we can use red to save all this   information which will greatly increase the speed  of our application so if we come back into here   instead of just saying our response we want to set  a new key value pair in Redis and what we want to   do is say this value pair is going to be value  equals app.st state. Redis do get and we will   name this entries we then want to say if if value  is none we want to do something so right here what   we're doing is we're fetching it from reedit so  remember git name git OS which is returning Eric   and Mac git entries will return all the values  for the key entries that we haven't created yet   but we just have that value looking so we can say  value equals app.st state. rdus get entries if the   value is none well that means we need to fetch  it from the API and then save it within in our   Rus database under the key of entries so this  is good if value is none we want our response   to equal that we can then go ahead and say value  equals our Json response so we know our value is   now going to be equal to the Json response that we  get from the API we can say data string because we   need to be able to convert this data into a string  to set it for a key value pair so what we can say   is data string equals Json dumps and we pass in  our value and so now data string is going to be   the value of our Json response in a string format  so we can save it to our Redis database and then   we simply we can say app. state. Redis do set and  now we need to say for the key of entries we want   to set the value of our data string and then  simply we want to return the value so now if   we come back to our application and we refresh we  can see that the very first call is going to be   about that 3 second call so if we click it 1 two  almost 3 seconds and we get all of the data now   that we saved this Json data as a string and we  saved it to Redis now next time we call it it'll   take approximately like like 0 seconds so execute  done execute done you see how fast it's flashing   the screen's flashing and that's because we are  getting the data from redis this time instead of   the thirdparty API endpoint now one thing that we  can also do is let's go back into this and instead   of just saying return value let's go ahead and say  return json. loads and now let's pass in our value   now if we go back back to our application and we  refresh it and we try it again we are going to   get it in we are going to fetch it the first time  and it's going to look like a Json it's no longer   going to look like a string and that's because  our json. loads will transform our string into a   Json format on our Swagger application so there  we have it we just added Redis to our fast API   application to increase performance performance  by I mean this is like 300 times from 3 seconds   to instant and installing Redis and using Redis  is not a scary task as many people think if you   want to keep learning about fast API go check out  this playlist and I will see you in the next video
Info
Channel: Eric Roby
Views: 1,625
Rating: undefined out of 5
Keywords: redis tutorial, redis cache, redis database, redis, what is redis, redis crash course, learn redis, redis tutorial for beginners, redis data structure, why redis, redis commands, josh tried coding, python fastapi, fastapi redis, python, programming, fastapi, fastapi tutorial, fastapi python, flask redis
Id: 6nY-kci1rlo
Channel Id: undefined
Length: 16min 15sec (975 seconds)
Published: Tue Aug 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.