What is Redis | Spring Data Redis as Cache | Coded Tribe

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
remote dictionary server yes this is what Redis means so if you take two letters of each and every word are e di NS then it makes us Redis okay what is Redis Allred is basically is an open-source BSD license in-memory data structure store and by data structure store it means that it makes use of various data structure to store the data okay and it is widely used as databases caching mechanism then as a message broker and there are multiple other use cases as well like it can be used as LRU cache and other aspects but the most important and most common use cases are like caching rate limiting message broker and get a basis why it is so popular today because it offers high performance it offers replication of the data it offers persistence and it offers a unique data model like if you compare it with other know sequel database let's say there is MongoDB release Cassandra there is CouchDB so MongoDB let's say for instance it supports the persistent of data in our document for Mulligan in JSON format right but it really supports multiple data structure so it provides the capability to use multiple data structure as can be into the application so it is basically stores data in key value formats it is very similar to memcache DB which also store values in key value format but the main restrictions with memcache is that they only use make use of blob as a data type so whenever you want to retrieve something from memcache you actually have to serialize it into a certain data so that which you want to display into the UI or somewhere so reddy's actually provides various data such as supports as you can see the value of data can be strings can be hashes can be a list can be a set can be a sorted set even and there are multiple data structure as well which is supported such as bitmaps streams hyper blocks and geospatial indexes this is how it is provides a wide variety of a resistor to be used in your application as we know in modern applications like in today's world all the modern applications or all the applicants are becoming data centric right so they are making use of data to reach to the larger audience and all those step and in that scenario ladies is basically suited for these modern applications because it is very high performance and it can make use of those data and it can handle those data more efficiently and there are major popular applications today if we interact socially with are actually making use of Redis in their application architecture and we will see what are those applications but first let's see what are the capabilities of it is so ladies since its high-performance so it can be used for high-speed transactions the most important use case offer is actually is caching so you will see whenever you will discuss your application architecture with your product team or with your technical architect you must encounter this word term that is when it comes to a technical decision on caching mechanism right so it can be used as messaging so it can be used as message broker it can be used in pub sub model which is tutor making use of it it can be used as in Paris part of machine learning it can be used for searching techniques it can be using analytics it can be used in rate limiting as well our focus today will be on caching and we will actually make use of a springboard application and we will see how Redis can be used as a caching mechanism in Springwood application and how the data can be actually stored in cache and we can reduce a network call to database one of the major application with which we interact daily and socially is making use of Redis and that is to it and Twitter actually makes use of radius in the timeline so we said let's say you follow a person a and that person a is being followed by let us say 10,000 other people on Twitter so whenever that person a tweet something that tweets get persisted into Twitter's data source and at the same time that tweets should fan out to all those 10,000 for the first time and as well okay so this panning out process this tweet fanning out process in a real near real-time to do this actually Twitter makes use of Redis okay so they make use a pub sub model basically to publish something and subscribe to the places where it it needs to be displayed so this is how total makes use of Redis then snapchat uses red is then been interest uses that is and when the most major application which I think me and you all interacts daily with is Stack Overflow they uses Redis and github uses Redis so these are some of the applications which makes use of radius somewhere in their application architecture let's now move ahead and see how we can actually configure Redis in hours between good application and we can make use of readies to let us know see how we can actually configure Redis innerspring good applications and we can cache some of that API is transaction for that first we have to go to that is dot IO and we have to download the stable version so I'll click on that is stable versatile download it and meanwhile it's getting downloaded so we have this interactive tutorial shell provider actually we can go ahead and we can learn some basic commands of readies to give an overview let's say I am sending a name with my name and here the key is this name and if I do this let's take name then it will return me the name so these are some of the basic commands which you can learn through this interactive shell and it's a highly advisable to go through that once because will not cover those commands in this particular video so let's jump back to the downloaded version of Redis and I will go open it let me extract it first okay so I have extracted it let me open the terminal at this particular position yeah so the first thing which we have to do after extracting the that is zip file is we have to run the make come on so what is make command will do it will compile all those binaries inside the Redis folder which we just downloaded and once it is compiled we'll be able to start the rad7 it is CLI okay so now it is compiled and what I'll do I'll just clear the screen so what I will do is so see if you do LS you will see there is a folder named as SRC so all the all your it is server and it is CLI command lies inside so what I'll do I'll do SRC sass readies - server okay and this has actually started my ready server which is running in port number 67 9 by default and it's running in local hosting standard one more ok so what I'll do as well is I will create one more tab and I really open them C&I so for that we have to die SRC slash ladies - CLA so once it is opened once your server is up and running so this command will actually connect to that server this server it will give you this clear shell very you can write chose that is come on so to give an example let's say I will set name and type it's a stat for okay so this returns okay and I can actually get the name and it will in the value so what we have to do now is we have to create a spring applications I'll go to start dot string dot IO and I will add a dependency first dependencies web okay then I am making use of my sequel in my application so I will be adding JP as well okay and then I'll be adding my sequel driver as well and the most important part is that is so once you type Redis will be seeing do things when it's pin get of reactive ladies and one is printed already so we will be making use of no secret string later it is access plus driver so let me just add it so once all these things are added you can name anything to your application let me just keep the name as Redis and I will generate it this so I have actually imported one application and I have created some basic stuff if you see here I have created a user controller which is basically get one user then create a user then get all user update a user and delete a particular user similarly I have created a user domain which will be persisted in my sequel database and these are the service impl which is actually doing all the business logic of saving retrieving updating and editing the user record okay so this is the basic stuff I will provide the link of this particular application might it have repo so no need to worry so the most important thing is in combat XML so if you so this spring boot starter data ready so this is the dependency which allowed application to enable Redis caching mechanism okay so make sure when you download your application from starter spend at IU and cross-check your convert XML that you should have this particular dependency in here okay so let's jump back to the controller so right now all the controller API is all the rest endpoints are basically a normal distant points which will go to a database and fetch that it is so to be able to distinguish whether the API request is making a call to database or it is actually retrieving the data from a disguise say I have actually included one logger which will log the messages whenever a request comes in to this particular controller method and if that particular API request has been cast then it will not go inside this particular method and hence nothing will be locked so let me first start this application okay so now the application is started I will go back to postman and I will try to retrieve some of the users which is already persisted in the database so if I hit this API slash users it will give me the list of users which I have already created and it is already available as part of this particular table okay I put it some 10 dummy users so that is why I am getting all these tentacles here so what I'll do I will go ahead and look into this Redis line okay so whenever you want to see whether any key is existing in Redis server you just have to type keys and then stop so it will restart all the keys which has been created so right now we have created this set name okay so that's why the name is present here so if you want to clear out these keys from your data servers you just have to do flush all okay and it will turn okay so it's not case-sensitive you can do flush all in small case as well if you do keys and then start you will say that it's an empty array so I can nothing is cached already server okay and let me just go ahead and retrieve let's say the fourth record okay and I am getting this fourth record if I go to IntelliJ and if I see the logs you can see that this was the first call which we made by getting all the users and this is the pulp which we just right now made which will reach retrieved us with the user of ID for okay now let me go ahead and again hit this particular end point again I am getting the same response and if we see the logs again it has made a call to database okay so twice it has made a call to database so if you want if we configure Redis in our application and we want to actually cache certain amount of ID so whenever let's say for example whenever we fire a request to create a user whose ID is greater than 4 okay then all those IDs which are greater than for like 5 6 7 8 9 all of those will be cached so the first time it will be retrieved from database and henceforward if any request comes in with the same ID it will just go ahead and it it from kashi it will not make a data squat how can we achieve that so writing nothing is configured in this application which is ready to caching so the first thing which we have to do is we have to go to our main application class that is where we find the spring good application annotation then we have to add add enable fashion ok so this will let spring know that this application that in this application caching is enabled ok so let me go ahead into the controller so this was a API which we were making use of to retrieve the user details of a particular ID so what to cache a particular request on the basis of some condition we have to add one annotation which will be at cacheable ok so you can see this is coming from screen frame l dot casual or annotation ad cashable so I keep that and this will take some value some parameters so we want to give the value okay so you want to give the name of the key which will be created once the value will be cached so let me just give the name as users right now what we have to provide that we have to cash a particular record but on the basis of what that record can have multiple fields it can have ID it can happen a.m. it can have email ids and value so since our records will have a unique ID will be caching on the basis of ID ok as we are retrieving on the basis of idea so what I'll do I'll add key and then you have to give hash and then ID so whatever parameter you are passing in to this request on the basis of that parameter it should be cached ok so right now if we leave up to this it will cache everything ok so any difference which is coming in for the first time it will cache everything into the Redis castle so let me just restart this server you yeah it has started so I'll go ahead and make a call too and it's a third record this time so this will take some longer time because it is written the first time from the database right so I have caught this record and if I see the logs we can see that it has made a request to database to face 30 days okay let me go ahead to the CLI and let me just search for keys if any keys has been created or not so as you can see it has created a key it says users : : 3 so users is the name which we have Cape while you configuring a detachable and 3 is the ID so if you do get users : : 3 you will get something like this and you can see that it is having the details about that user 3 so this is the email of the user this is the contact number and somewhere we can name yes it is ok so now let us go back to the application and since we have not provided any condition right so let me just provide one condition and with the key word unless you can provide one condition so what you can do is you can write hash and then write the result which will be having the result of that particular record which which is pretty lean so if result dot ID is less than 3 which means if the result whatever the result we are trading from database if the ID of that result is having less than 3 then it will not be cached okay if the ID is greater than 3 then only it will be cached so let's say instead of 3 I will make it as 5 okay and I will restart the application you okay the application has been started and let me just go back to the CLI and let me clear all the keys okay so I have cleared all the t's and what I'll do I'll go ahead in the postman and I will invoke let's say second ID okay so it is giving you back the details of second ID and if we see it has actually made a database called so since we have applied some condition that results should be greater than five Simulink again the second key of the ID so if we see here we do keys and star it has an empty array and if we go to the application we can see that it has again make the call to the user so basically twice we just make the call so since it is not being cached so now let's say I will go to the post when I will create let us say the sixth ID and since it is between the condition of ID should be greater than five then the first time it should invoke it should get it from the database and the second time it should get from the cache it so if I do please star we have this user six and if I again go ahead and run this it will not be calling getting from database it is actually get it from test because we have only one called which was the previous call okay now let me go ahead and actually invoke one update element okay so let me just grab let's say the tenth one okay and I will update it so I will go ahead and put it into the body okay and I will make the user ten as user twelve is why I am updating the details of user twelve okay so if I hit it it says that ID ten and the update value is user will if I go to the database and if I read three so as you can see the tenth ID is user twelve now so basically when I will be fetching this particular ID its get me sorry the tenth one right so the tenth one it should actually get me the user twelve okay so I have caught user twelve it is actually it is actually making a called user ten and it is retrieving the details and in case a as well it will be adding that value uses the ten is there now let me go ahead and update the user ten once again so at this time what I will do well make it as users fourteen okay and I'll do an update okay so I have updated users thirteen so if I do get all user we can see the tenth ID is having user thirteen not the user will now I will go ahead and I will retrieve the user ten so if I retrieve user ten as you can see is you can observe multiple times if I love invoke it will only always give me user dual because this was cached earlier and until and unless we remove this from cash she will not be able to get the latest updated record but this is not the recommended way so what should happen ideally is whenever we update a record that should also get updated into the cache that record is was already cached so to avoid it inconsistency what we can do is we can update this particular API update API with at cashy put it so what this will do is this we have to prove in this we have to provide on the value which will be the key which is actually getting stored in through the cache a and then we have to provide the key with which we want to actually search the caches that is hash ID and that's it so whenever a request for a particular ID or a particular user with this ID we get updated it will also update the key which is already saved in that is so here since we are getting the user as request body so what we'll do is we'll do user dot ID okay and if we rerun the application so now the application has been started let's go back to post one and let me update this time it's ID okay so I will update user aid and the user aid do something else 8 - 1 ok 8 - 1 but before that let me get that user so that it will be stored in the cache so I will retrieve this 8 ID and this should get populated in here so we have this user aid in here and what I will do I will again invoke this so I will get user it all the time so I will update this to user 8 - 1 so let me update this so I have ability to user 8 - 1 and let me go back to the CLI and let me see if users foreign : 8 what the value is surprisingly this is having user 8 - 1 at gmail.com which means when we updated that record it actually updated the Cassie as well so if I do eight now initially it was having user eight and this was cached in Redis but when we invoke update API it actually updated the recording DB as well as in the ready stash it so now if I invoke this it will return me the new value yeah so it is running the new value so this is how we can use add cash you put annotation to actually update a record whenever it gets updated into the Redis cache a as well okay the next thing is basically to delete a record whenever that record is not existing so so assume that scenario wherein you are actually deleting a record but that particular record is already cached into the red is so whenever you will make a get called to that record again it will actually go ahead and leave it from your cache and you don't want that particular situation where in the user record is not existing in TV but you are getting in from the cache a so to avoid that particular situation what we have we have this cache a evict option and in this particular option you can actually provide that what happens when our record is deleted so basically what you do is you have to add a value so this value will be user so this is the key which is getting stored into the cache and then all entries no we don't want to delete all the entries we just want to believe the entry from be discussing which is being deleted from database for that purpose we have to add a key okay so key will be hash ID so whenever a request for deleting this particular user ID comes into the controller so that that particular ID if it is present in the ready sky say that will also get deleted okay so this is - it's not a server yeah so the server is started now and I'll go back to the postman and I will work a delete command so let me I let me see since if I so it is already present in Redis - right so I want to delete the H sorry completely record let me delete it so it has now deleted so if I do a get of 8 it should throw me an error favorite the survivor because it is not present and if I go to this particular CLI if I do E's and star as you can see the user 8 is not present so it has liquid from register as well and it has rated from the DB as well because okay so this is how we can actually make use of radius as a caching mechanism in us to in good application this is it I will provide the application link in the description box and you can go ahead and download it from the github and you can follow along so that's it [Music]
Info
Channel: Coded Tribe
Views: 8,145
Rating: undefined out of 5
Keywords: redis as cache in spring boot, redis cache spring boot, spring data redis, spring boot redis tutorial, spring boot redis example, spring boot redis, redis as cache spring boot, spring boot cache redis, spring boot cache implementation example, spring boot cache redis example, spring boot cache jpa repository, spring boot cacheable with redis, spring boot cache implementation, stackfortech, spring boot redis cache example, coded tribe, spring data redis as cache
Id: AiNL1X-dhkc
Channel Id: undefined
Length: 24min 14sec (1454 seconds)
Published: Sat Jul 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.