Rate Limiting system design | TOKEN BUCKET, Leaky Bucket, Sliding Logs

Rate limiting protects your APIs from overuse by limiting how often each user can call the API.
In this video following algorithms are discussed
Token Bucket
Leaky Bucket
Sliding Logs
Sliding window counters
Race Conditions in distributed systems
Donate/Patreon: / techdummies

Пікірлер: 268

  • @rabbanishahid
    @rabbanishahid3 жыл бұрын

    Best explanation, almost searched everywhere for my scenario, but found this tutorial very very helpful, once again thanks man.

  • @khalidelgazzar
    @khalidelgazzar Жыл бұрын

    04:16 Token bucket 10:40 Leaky bucket 12:50 Fixed window counter 16:15 Sliding logs 20:36 Sliding Window counter 25:21 Distributed system setup (Sticky sessions | locks)

  • @logeshkumar8333
    @logeshkumar83334 жыл бұрын

    This channel is just hidden Gem!

  • @vcfirefox
    @vcfirefox2 жыл бұрын

    i was reading alex xu, i did not get good idea about sliding window and sliding window counter. now after i watched your explanation it is crystal clear and with pros and cons. thank you for doing this!!

  • @r3jk8
    @r3jk84 жыл бұрын

    This video was a clear and concise explanation of these topics! Great job! You have a new subscriber.

  • @ajaypuri1837
    @ajaypuri18375 жыл бұрын

    Narendra L! You doing good job! I watched your couple of videos. Keep it up!

  • @terigopula
    @terigopula5 жыл бұрын

    you have my respect Narendra.. great work! :)

  • @nikhilchopra9247
    @nikhilchopra92475 жыл бұрын

    Good Stuff Naren! Even famous profs are not able to explain this kind of stuff so clearly.

  • @TechDummiesNarendraL

    @TechDummiesNarendraL

    5 жыл бұрын

    Thanks

  • @CrusaderGeneral
    @CrusaderGeneral2 жыл бұрын

    My implementation takes advantage of Redis expiration.. When a call comes in, I create a record and the increment the value. Consequent calls will increment the value until the quota is reached. If the quota is not reached by the time the record expires, consequential request will cause a creation of new record and restart the counter.. This way I dont need to check and compare dates at any point. Code is very simple. Albeit, I am not maintaining a perpetual quota, I am only preventing abuse, which is really the main gist of request throttling

  • @varshard0

    @varshard0

    2 жыл бұрын

    This is the way I implemented for my org also. Simple and served its purpose well.

  • @shelendrasharma9680

    @shelendrasharma9680

    2 жыл бұрын

    how would you manage the concurrancy here in redis.

  • @bazzalseed

    @bazzalseed

    2 жыл бұрын

    @@shelendrasharma9680 redis is single thread.

  • @dhruveshkhandelwal8104

    @dhruveshkhandelwal8104

    2 жыл бұрын

    this is indirectly fixed window counter

  • @sid007ashish

    @sid007ashish

    2 ай бұрын

    This is fixed window counter only.

  • @prabudasv
    @prabudasv3 жыл бұрын

    Narendra, your video are great resources for learning system design. Your explanation of concepts is crystal clear. Big thumbs up for you

  • @JoshKemmerer
    @JoshKemmerer3 жыл бұрын

    I love your voice brother. It makes it exciting to listen to what you have to say about this very interesting design topic.

  • @abasikhan100
    @abasikhan100 Жыл бұрын

    Great explanation. The pattern you followed is very good i.e. when you mention a problem with some approach, you also provide the solution for that instead of just identifying problems.

  • @1qwertyuiop1000
    @1qwertyuiop10002 жыл бұрын

    I love your cap.. Looks like a trademark for you.. Thanks for all your videos..

  • @praveenakarapu
    @praveenakarapu5 жыл бұрын

    Narendra, very informative video, keep it up. About locking in case of distributed token bucket you can use following technique Optimistic locking or conditional put - many no sql databases support conditional put. This is how it works * Read current value, say 9 * You do a conditional put with value 10 only if current value is 9. * When 2 concurrent requests try to update the value to 10, only one of them will succeed and other will fail as current value for that request will be 10.

  • @RandomShowerThoughts
    @RandomShowerThoughts Жыл бұрын

    I think you're easily the best youtuber for system design content

  • @PankajKumar-mv8pd
    @PankajKumar-mv8pd4 жыл бұрын

    One of best explanation, thanks man :)

  • @Awaarige
    @Awaarige4 жыл бұрын

    Bro, You saved my months. Love from Pakistan

  • @rajeshd7389
    @rajeshd73893 жыл бұрын

    Narendra L !! This is just superb ... keep going.

  • @valeriiryzhuk4126
    @valeriiryzhuk41265 жыл бұрын

    One additional case, were sliding logs should be used: limit a bitrate of video/audio/internet signal. In such case you need to store a packet size with a timestamp

  • @saip7137
    @saip71374 жыл бұрын

    You have a new subscriber. Thanks for making this video.

  • @nishathussain3672
    @nishathussain36724 жыл бұрын

    I love your videos. Thank you for making such detailed videos which explain the concepts so clearly. :)

  • @rangak7502
    @rangak75025 жыл бұрын

    Awesome work sir.. 👍🏼

  • @divyeshgaur
    @divyeshgaur5 жыл бұрын

    thank you for sharing the video. neatly explained.

  • @vinodcs80
    @vinodcs802 жыл бұрын

    very comprehensive video. Great work. subscribed

  • @ishansoni8494
    @ishansoni84943 жыл бұрын

    Great work Narendra..! I am currently planning to switch jobs and your videos on system design are amazing...!!

  • @mostaza1464
    @mostaza14645 жыл бұрын

    Great video! Thank you!

  • @mohammadfarseenmanekhan4820
    @mohammadfarseenmanekhan48202 жыл бұрын

    very underrated youtube channel for system design

  • @sbylk99
    @sbylk995 жыл бұрын

    Great tutorial. Tricky part comes at 25:12:)

  • @amitchaudhary6199
    @amitchaudhary61994 жыл бұрын

    Great work Narendra👍👍

  • @aeb242
    @aeb2429 ай бұрын

    Great lesson! Thank you!

  • @md.abdullahal-alamin8059
    @md.abdullahal-alamin80595 жыл бұрын

    Great tutorial :)

  • @saurabhchako89
    @saurabhchako89 Жыл бұрын

    Great video. Well explained.

  • @adityagoel123able
    @adityagoel123able3 жыл бұрын

    Awesome Narendra..

  • @rationalthinker3223
    @rationalthinker3223 Жыл бұрын

    Outstanding Explanation

  • @madhusogam5823
    @madhusogam58234 жыл бұрын

    very nice tutorial .. great work :)

  • @rbsrafa
    @rbsrafa Жыл бұрын

    Great video, congrats!!

  • @themynamesb
    @themynamesb3 жыл бұрын

    Great video.. Thanks for the knowledge.

  • @princenarayana
    @princenarayana3 жыл бұрын

    Sliding window can be optimized by setting the size of the queue to Max Requests allowed and try to remove the old entries only if max size is reached by comparing timestamp

  • @cantwaittowatch
    @cantwaittowatch4 жыл бұрын

    Well explained Narendra

  • @arun5741
    @arun57415 жыл бұрын

    As usual naren rocks !!!

  • @vishalkohli3953
    @vishalkohli39533 жыл бұрын

    What a guy!! bless you bro

  • @VirgiliuIonescu
    @VirgiliuIonescu4 жыл бұрын

    For the last example with concurrency. How about optimistic locking on the counter. Number of req has a version. If you try to update from 2 different RL, one of them will have the NoReq version smaller than the current one and will fail. The RL can retry or drop

  • @ashutoshbang5836
    @ashutoshbang58362 жыл бұрын

    Great video, keep up the good work :)

  • @codetolive27
    @codetolive275 жыл бұрын

    Informative !!

  • @dragonmohammad
    @dragonmohammad4 жыл бұрын

    Distributed Systems, a necessary evil.. very nicely explained Narendra !!

  • @DenisG631
    @DenisG6315 жыл бұрын

    A good one. Thanks

  • @keatmin
    @keatmin3 жыл бұрын

    Thanks for the great tutorial, but I have a question as how would a rate limit service obtain lock of a record in separate db affect another rate limiter service obtain the count from different db within a node?

  • @screen189
    @screen1895 жыл бұрын

    Hi Narendra - You are doing a good job in your knowledge transfer. I suggest you cover these topics as well - a) Job Scheduler b) Internals of Zoo Keeper c) Dist.Sys concepts like 2PC, 3PC, Paxos d) DB Internals.

  • @TechDummiesNarendraL

    @TechDummiesNarendraL

    5 жыл бұрын

    Added to TODO, Thanks

  • @screen189

    @screen189

    5 жыл бұрын

    Thanks for your response. Looking forward for her videos!!@@TechDummiesNarendraL

  • @anand2009ish
    @anand2009ish2 жыл бұрын

    Excellent..hats off

  • @shrimpo6416
    @shrimpo64162 жыл бұрын

    Perfect! I wish I can give you 1,000,000 likes!

  • @akhashramamurthy8774
    @akhashramamurthy87744 жыл бұрын

    Thank you Narendra. The incredible content archive that you are building is invaluable. Thank you.

  • @SanjayKumar-di5db
    @SanjayKumar-di5db3 жыл бұрын

    You can solve this with the help of increment or decrement method on redis which works atomically on any key so there is no chance for data inconsistencies and no need to put any lock 😊

  • @himanshu111284

    @himanshu111284

    3 жыл бұрын

    2 services firing increment concurrently will still face the same problem, so i think it will not work without locking. Read + Write has to be an atomic transaction.

  • @SanjayKumar-di5db

    @SanjayKumar-di5db

    3 жыл бұрын

    @@himanshu111284 in redis increment and decrement methods on id are atomic so no need for lock

  • @rajsekharmahapatro

    @rajsekharmahapatro

    2 жыл бұрын

    @@SanjayKumar-di5db First time i am learning something new by going through KZread comments bro. Thanks for it man.

  • @xuanwang7400

    @xuanwang7400

    2 жыл бұрын

    "compare and set" kind of logic works perfectly without explicit locking in simple operation case. But in complex situation, the app server may need a few requests. e.g. read the data first, the do some processing, then write back. and then two servers can do the same thing with same data at same time, thus race condition.

  • @imranhussain8700
    @imranhussain87003 жыл бұрын

    Great content. Thanks for sharing. Just one question, there should be only 1 LB which will send the request to either A1 or A2?

  • @chikudholu
    @chikudholu5 жыл бұрын

    Awesome!

  • @DharaVisual
    @DharaVisual3 жыл бұрын

    Great work! Would you be able to system design Elevators? Parking Lot?

  • @manasranjan4
    @manasranjan42 жыл бұрын

    Good bro. Awesome

  • @DebasisUntouchable
    @DebasisUntouchable4 жыл бұрын

    thanks for this video

  • @biboswanroy6699
    @biboswanroy66994 жыл бұрын

    Amazing content

  • @poojachauhan1509
    @poojachauhan15093 жыл бұрын

    Great work, Searching for System design like leetcode or Hackerank...

  • @helishah6719
    @helishah67193 жыл бұрын

    For the Local Memory solution that you provided, how is it different from the solution that you explained just before (where the rate limiter is connected directly to the Redis)?

  • @ramlakhan-fp7ct
    @ramlakhan-fp7ct5 жыл бұрын

    Thanks Man

  • @javacoder1986
    @javacoder19864 жыл бұрын

    Thanks for great video, very informative, however last several minutes of video is not very clear and crisp like other part of the video.

  • @santoshdl
    @santoshdl4 жыл бұрын

    thanks Narendra

  • @ravisoni9610
    @ravisoni96104 жыл бұрын

    great explanation (y)

  • @krishankantsharma3655
    @krishankantsharma36554 жыл бұрын

    Sir, for amazon any particular series of questions you want to suggest.

  • @NAVEENkumar-vz6qv
    @NAVEENkumar-vz6qv4 жыл бұрын

    This is really nice.. Do you have anything on how a coupon system works, for example : Doordash, Ubereats or rideshare coupons

  • @lolnikal6851
    @lolnikal68514 ай бұрын

    20:36 Sliding Window counter The rate limit is 10R/M While in explanation , he considered 10R/S so please don't get confuse and think he is wrong

  • @andresantos-yx3bh
    @andresantos-yx3bh Жыл бұрын

    amazing video

  • @Ghost_1823
    @Ghost_1823 Жыл бұрын

    Your content is good. But please try to change your voice modulation. It really helps for long videos.

  • @andriidanylov9453
    @andriidanylov9453 Жыл бұрын

    Thank You

  • @resetengineering
    @resetengineering Жыл бұрын

    Why are you using two caches? Your sync issues are solved by keeping one single cache. Then, coming to race conditions, redis automatically acquires a lock on the transaction since it is atomic and therefore, the other request(second) should get an updated value. For SPOF on one cache, we can keep a master slave nodes for redis

  • @anuraggupta6890
    @anuraggupta68904 жыл бұрын

    Narendra from where do you get such a great understanding of system

  • @ayaskanta100
    @ayaskanta1005 жыл бұрын

    Sir thank you what is internal implementation of token bucket from data structure side

  • @dev-skills
    @dev-skills5 жыл бұрын

    Redis provides INCR and DECR commands which are atomic operations for increment and decrement of its Integer Data Type. Will this not take care of distributed access without any lock ?

  • @victoryang7734

    @victoryang7734

    4 жыл бұрын

    I think his assumption is redis is seperate

  • @Priyam_Gupta

    @Priyam_Gupta

    3 жыл бұрын

    Yes this will be taking care as they are atomic.

  • @abcdef-fo1tf

    @abcdef-fo1tf

    Жыл бұрын

    @@victoryang7734 what does separate redis mean. Is distributed redis not a shared cache?

  • @prasukjain8488
    @prasukjain848811 ай бұрын

    Why he is looking like varun singla sir from Gate smashers , btw nice lecture

  • @molugueshwar1
    @molugueshwar14 жыл бұрын

    Hi Narendra, In token bucket scenario above, I would like to add one point that in order to reset the requests count after one minute to 5 again, we have to store the time(start time) of the first request so that we can check the difference of one minute to reset the count

  • @nikhilneela

    @nikhilneela

    4 жыл бұрын

    Yes, I agree. If you simply reset the tokens to 5 when the minute changes, it would allow more than 5 requests/minute. Storing the start time and always comparing it with the current request time and if the delta is equal to or more than a minute, only then we can reset the tokens. @Eshwar, is this what you meant ?

  • @molugueshwar1

    @molugueshwar1

    4 жыл бұрын

    @@nikhilneela yes Nikhil. That's right

  • @michael4799
    @michael47993 жыл бұрын

    For the situation of distributed race limit, even though one user send two requests at the same time in one server, it dosen't mean that the actual two processing threads will deal them serially, so the inconsistency problem seems still exist. I think to address this problem we can make the read and update operation as atomic with redis+Lua.

  • @prajwal9610

    @prajwal9610

    2 жыл бұрын

    Redis does this by having a lock which is already suggested in the video

  • @rekhakalasare4910

    @rekhakalasare4910

    Жыл бұрын

    ​@@prajwal9610 yea but in case of local memory suppose single user two request going to 2 regions and regions local cache first read from db and then update in cache and db. Then also there is inconsistency as both req operating parellely

  • @singhalvikash
    @singhalvikash3 жыл бұрын

    Nice explanation. Could you please make a video for Google ad sense analytics collection system ?

  • @karthikrangaraju9421
    @karthikrangaraju94214 жыл бұрын

    The inconsistency problem is basically a common DB problem called "lost update" due to two threads reading committed data concurrently and performing writes without any locks. Solution is to introduce locking to enforce ordering. Or enforce ordering by sticky session at a much higher level

  • @InfinteMotivation
    @InfinteMotivation Жыл бұрын

    you are the best

  • @knimr3
    @knimr33 жыл бұрын

    Hi Naren, what's the essential differences between sliding window log and leaky bucket. They look like pretty much the same in functionality.

  • @akshaytelang4532
    @akshaytelang45324 жыл бұрын

    can't we use Zookeeper for synchronization to manage requests along multiple regions

  • @anonym705
    @anonym7052 жыл бұрын

    Excellent videos, just lacking good sound system.

  • @feitongyin3291
    @feitongyin32914 жыл бұрын

    Question about using locks. In example where two requests come in, RL1 gets the lock first, and then "locks" the key. How does it lock the key though such that it somehow impacts RL2? I'd imagine it only has access to the db in its own region (the one on the right on your drawing), so how does it lock the key such that when RL2 access the db in its own region (the one on left), that it knows to wait for the lock to be released? Thanks for the video as always.

  • @ShivamSingh-jw8ey
    @ShivamSingh-jw8ey3 жыл бұрын

    04:15 Rate Limting Algorithms 25:11 Race Conditions in distributed systems

  • @DeepakMishra117
    @DeepakMishra1175 жыл бұрын

    Is there any way to use sticky session for a user on the seconds level, so that we can use sliding window counter and the nodes can sync themselves over a minute?

  • @springtest540
    @springtest5405 жыл бұрын

    Sir please make video on elevator design and google doc design as well.

  • @sanjanind

    @sanjanind

    5 жыл бұрын

    I also want for these two.

  • @TechDummiesNarendraL

    @TechDummiesNarendraL

    5 жыл бұрын

    Sure I will work on it.

  • @reaganrosario
    @reaganrosario4 жыл бұрын

    Why cant we use the distributed counter method that you had explained in some other video? Like having zookeeper manager the counters?

  • @rahuljaiswal6646
    @rahuljaiswal66465 жыл бұрын

    Also on Movies Reviews Aggregator System

  • @mritunjayyadav3788
    @mritunjayyadav37884 жыл бұрын

    Hi Narendra great work I loved your content but i have one question . why not keep only one Redis DB instance instead of two in that case we dont have to sync them ? or is there any significance of having diff instances of Redis (per LB , RL , App instances) .

  • @anirbanghosh1176

    @anirbanghosh1176

    2 жыл бұрын

    @mritunjay yadav - in ditributed system you cannot have single point of failure

  • @namanmishra08

    @namanmishra08

    Жыл бұрын

    That's because the entire point of having multiple regions is to have fault tolerance. For a single region, we can have a primary-secondary model with asynchronous replication between them but for a multi-region setup, each component should have a replica. One approach to solve this is to use distributed locks that Redis provides.

  • @bhaskargurram94
    @bhaskargurram944 жыл бұрын

    Thanks for the nice explanation. One question - What is the difference between fixed window counter and token bucket? Are they not doing the same?

  • @paraschawla3757

    @paraschawla3757

    3 жыл бұрын

    token bucket is the number of tokens in a bucket, there is refill() happening in bucket after nth min/sec. Number of tokens represent number of request that can be served. with every new request, it keeps going down...but tokens keep increasing based on ratelimit as well. Fixed window counter is having User+TimeStamp as key and count as value for particular window and then start again. Essence of both alogos are very different.

  • @curiousbhartiya8410

    @curiousbhartiya8410

    3 жыл бұрын

    @@paraschawla3757 But the underlying problem of both algorithms is the same is what the original comment meant. That they both might end up serving twice the amount of the desired RPM.

  • @PABJEEGamer

    @PABJEEGamer

    3 жыл бұрын

    With token bucket algorithm we have control over cost of each operation(we can associate how many tokens an operation costs), where as in fixed window we dont, since we increase the counter by 1 each time

  • @VasQuezadilla
    @VasQuezadilla5 жыл бұрын

    Can you do system design for Groupon?

  • @JitendraSarswat
    @JitendraSarswat4 жыл бұрын

    There is one con to all your videos. If you skip 10 sec of this video, you are doomed :-P Exceptional work, Narendra.

  • @thejaswiniuttarkar620
    @thejaswiniuttarkar6203 жыл бұрын

    the threshold is calculated per second, for example AWS API gateway 5000 req/sec .. we can just declare an Array Queue or Array stack and start pushing elements in to it and keep flushing it every second ... + or - 10/20 request would not matter .. if the stack/Queue fills up it would throw an error and that error could be propagated to the user !!

  • @153deep
    @153deep4 жыл бұрын

    Consider this scenario for token bucket: We can only serve 5 request/5 min. One request (10.05), Two request(10.06), Two request(10.07) we have served all the 5 requests so at 10.07 we will have 0. Now when we get new request at 10.11 it should be the valid request because request at 10.05 & 10.06 should be removed but as per token bucket it won't be served because 10.07 is set to 0 & will be reset at 10.12

  • @vaidyanathanpk9221

    @vaidyanathanpk9221

    4 жыл бұрын

    Not really. Read about the token bucket algorithm. Before serving the operation at 10.12, it'll try to figure out the time elapsed so far ( 10.12 - 10:07 ) Then it'll figure out the number of tokens to add for this time elapsed ( For 5 minutes, we need to add 5 tokens ) So before doing the serving calculation, these addition of tokens will be done and then when you do the calculation, you should be able to serve these requests. The key point is maintaining something called as lastUpdateTime in the bucket.

  • @Sudarshansridhar
    @Sudarshansridhar4 жыл бұрын

    can we have sync service + memory between RL and Redis/Casandra ? So all RLs will go via sync service to get quick response. Sync service is responsible to write to Redis/Casandra. If sync service is not available, RL will make direct call to Redis/Casandra. Not sure how optimal this change is .

  • @OmarGuntaue
    @OmarGuntaue4 жыл бұрын

    Is it ok that the LB is before the RL? Shouldn't be the other way around?

  • @pratyushprateek2503
    @pratyushprateek2503 Жыл бұрын

    In case of token bucket algorithm, isn't Redis thread safe or can't we enforce synchronization using locks if requests from multiple application servers are meant to be served concurrently?

  • @mukulchakravarty8788
    @mukulchakravarty87885 жыл бұрын

    Hi is it possible to lock a key across replicas ? They are essentially copies of each other right ?

  • @shamshparvez9377
    @shamshparvez93774 жыл бұрын

    can you make a video for 'system design of live video broadcasting software'

Келесі