System Design Rate Limiter | Leaky Bucket Implementation | System Design Interview

Поділитися
Вставка
  • Опубліковано 13 жов 2024

КОМЕНТАРІ • 23

  • @abhinabroy4803
    @abhinabroy4803 Рік тому

    Nice video. Thanks. I had a small doubt, shouldn't the type of bucket be Map instead of Map ? For strategy pattern we should not use concrete definitions. What do you think?

  • @gursharanaulakh6882
    @gursharanaulakh6882 2 роки тому +2

    Thanks for the explanation. I see a few corrections in your implementation:
    1. UserBucketCreater should not be initialised for each and every user. In the constructor you are keeping Map which is initialised per user and for every user there will be one map with only one key. The map will be initialised for the whole app and should have different LeakyBucket per user.
    2. If there is no entry in the map for a specific user, we have to add one and the increment.

    • @TheTechGranth
      @TheTechGranth  2 роки тому

      How will you scale this implementation? What happens when we have application running on distributed server?

    • @gursharanaulakh6882
      @gursharanaulakh6882 2 роки тому +2

      Yes, that's what my point is, a local map will be replaced by redis and then each user in redis will have a separate bucket. Here you are initialising map every time as part of UserBucketCreator and it should be common to the application, be it local or distributed. Am I missing something?

    • @aniruddhkhera510
      @aniruddhkhera510 2 роки тому

      Great implementation and very well explained! I think one minor fix on the code, instead of initializing the map in the BucketCreator constructor, you could take the map as a static variable.

  • @Rohit-hs8wp
    @Rohit-hs8wp 9 місяців тому

    Buddy, you have to make the UserBucketCreator Map to be static otherwise your Map will only contain 1 userEntry.

  • @WeilongYou
    @WeilongYou 2 роки тому +7

    This doesn't look like a leaky bucket? You are not leaking requests at a fixed rate.

  • @shervilgupta92
    @shervilgupta92 2 роки тому

    Do we need the capacity check, I think the add method will throw an exception in case of no remaining space in the queue. Can we wrap this in a try catch and return the exception instead of false ?

  • @ankitnigamrocks
    @ankitnigamrocks 2 роки тому +3

    Good One. But couldn't find any where you removing the elements from blocking queue. Am i missing something?

    • @TheTechGranth
      @TheTechGranth  2 роки тому +1

      You are right. I did not add any consumer, to keep the focus on leaky bucket implementation, rather than complicating it into a producer, consumer model.

    • @ankitnigamrocks
      @ankitnigamrocks 2 роки тому +1

      @@TheTechGranth Noted Thanks :)

    • @TheTechGranth
      @TheTechGranth  2 роки тому +1

      @@ankitnigamrocks Hope it was helpful. Do like and subscribe and share with others 🙂

    • @ankitnigamrocks
      @ankitnigamrocks 2 роки тому +1

      @@TheTechGranth yes i liked. Am i already a subscriber. You are doing good job. Keep up. Thnxks

    • @ashishkarn068
      @ashishkarn068 2 роки тому +1

      One Could simply add a AtomicLong lastLeakedTime & Leak() method. Inside Leak(), take() from blocking queue number of expected leaks till now.

  • @priyankataneja7347
    @priyankataneja7347 11 місяців тому

    I have few questions here, In non distributed system when u had leaky bucket for every IP and you restrict 10 request per min from 1 IP. Lets say queue size is 20 and we get request from multiple IP's and queue gets filled for first IP we could use only 3 requests out of 10 and queue got filled, it still had quota but it is not allowed to send the request, Is this correct, Please correct me if my understanding is wrong here.

    • @mysymbol
      @mysymbol 5 місяців тому

      there is " leaky bucket for every IP" which means there is a blocking queue for every IP. In a queue size of 20, queue can not reach it limit with just 3 requests from that IP. It will still have the quota for that IP and it will allow

  • @aneksingh4496
    @aneksingh4496 2 роки тому

    lets say you are using redis for distributed cache ...would not we have race conditions there ? if yes how to deal ?

  • @mehulparmar9976
    @mehulparmar9976 Рік тому +3

    You are not removing requests from bucket! Need to fix this code.

    • @sudhanshukumar-yu7fj
      @sudhanshukumar-yu7fj 5 місяців тому

      He did say that in the real world services would consume the items from the queue

  • @kapilarora4415
    @kapilarora4415 2 роки тому +1

    good one, can you share the code link?

  • @ranjithk9638
    @ranjithk9638 Рік тому

    try testing this with more than one user and multiple requests.