Advanced Laravel Rate Limiting: How to Control Traffic and Improve Performance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
assalamualaikum guys in this video we'll learn how to use the rate limiter for a specific logic not for whole HTTP request so let's start first of all here's an example of a rate limiter so here I can use this middleware called throat I think most of us know about it and it means I can send requests to the serial three times each 60 seconds so for example here I will back send one 2 3 and if I try to consume it again it will through this error because it's allowed to visit only three times per minute so I will create my cache because storage stores it in the cache back again and here I will remove this and my learner controller we can use it for a specific logic if you want I will say red limiter this class here and we have attempt method so as you can see here it takes four brand the first one is the key of that limiter so I will say it online users and how many time can be consumed per minute I will say consume it or the maximum of atoms per minute is 5 so we can call it five times at most every 60 seconds and the Callback function here that will work when we consume it so I will say a log right in my log here M4 rate right and the last thing after how many seconds remove this attempts and reset again from zero by default it resisted after 60 seconds so I will keep it here you can change it to any value and in my look it's clear now I will send the request here send a request one it wrote a rate here two three four here five one last time and if I try to send the request again it will write anything because it's allowed maximum to be consumed five times per minute a use case for that for example imagine you use a third part A API and it's constrained and or have the restrictions to send a specific number of requests each a minute I think it's a good idea so imagine here we have a Firebase database that we can read online users and instead of reading it each time from Firebase we can read it five times per minute and store it in a cache so we can say here for that online users equal to Ahmed for the first time and I will put it in a cache put and I will call it a fire base here online and I will store this online users array and then I can see return cash get and return this key now let's try to send a request for that next 60 seconds send it returns Ahmad send again nothing changed imagine now a new user be online send it's joined another user for example a Muhammad sent again again again and now let's remove one of them and let's see refresh now nothing removed because I called it five times and the last time result was that so there is a case for example for reducing the cost of reading from Firebase or like maybe for Amazon apis it consumes a very specific number of requests per second to be allowed to consume as their employees and most of eBay is actually doing that right so again we have some other methods for that called available n is this available in Texas is K also the same key of that rate limiter of all attempts uh shows you how many after or after how many seconds it will be available again so here I will close it send now I finished my five times I will finish them sorry one two three four five and if I try to use in a single here it's still and I will say the undump after how many seconds it will be available so change as you can see reduced after a 41 37 and it will be changed right and then I can call them again so even if I end or something like that call again still same results and let's see here refresh after 60 Seconds when it becomes zero now it's ready to call a this rate limit attempts again right and if you want to remove them immediately you can use red limiter clear but let me show it first now it becomes zero and if I here remove it remember in the last time it is only Ahmad and Ali now it must return all of them send right and I will consume all of them now if I try to remove it it will not a cold as you can see and now let's see our available in method it's available after 42 seconds but if you want to clean it immediately you can use rate limiter clear and the same keys that we used for attempt so back here again and I will die on dump now it's ready and we can start again and so on and so on right I will add here another value it will be added because I was activating this method now remove remove remove here it will work and if I try to add another here it will not work but I will clear it immediately Now it worked here is a quick tip about using grate limiter inside or for a specific logic in your larva application if you found it useful support to me by clicking one lecture and subscribe see you in next video
Info
Channel: Laravel boy
Views: 3,289
Rating: undefined out of 5
Keywords: Laravel, rate limiting in Laravel, Laravel middleware, Laravel performance optimization, Laravel API management, Laravel throttling, Laravel scalability, Laravel security, Laravel error handling, Laravel network management, Laravel latency reduction, Laravel throughput enhancement, Laravel bottleneck prevention, Laravel API governance, Laravel request management, Laravel API protection.
Id: wAQ6gEldYps
Channel Id: undefined
Length: 7min 29sec (449 seconds)
Published: Fri May 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.