🚦 Smart Limits: Rate Limiting Explained

🚦 Smart Limits: Rate Limiting Explained

What is Rate Limiting ?

Rate limiting is like your dad setting some rules to make sure you don’t play games too much in a short time. It's a way to control how often you can do something, like playing games, to make sure everyone gets a fair chance and things don’t get too busy.

So, think of it as your dad saying, "You can only play X number of games in Y time," and if you’ve played enough, you’ll have to wait until you’re allowed to play again. It helps keep things balanced and stops things from getting out of control.

  1. Token bucket

    Imagine you want to play a game, and your dad says you will be given 5 tokens per day. If you want to play, you take a token from the jar, give it to him, and then you can play. So, if you want to play, you take a token from the jar, give it to your dad, and then play the game. Now you have 4 tokens left. You keep doing this until you have no tokens left. Now, let's say you want to play a game, but you don't have any tokens, so you have to wait until tomorrow to play.

  2. Leacky bucket

    Imagine your dad sets a rule that only two kids can play games at the same time. Even if you haven't played a game for a long while, if your brothers are already playing, you won't be able to join in. You'll have to wait until one of them finishes before you can start playing. This means you need to be patient and wait for your turn, even if you're eager to play. This rule ensures that everyone gets a fair chance to enjoy the game, but it also means you might have to wait longer than you'd like.

  3. Fixed counter window

    It's like your dad saying you can play games twice every 5 hours. Even though he wants you to take breaks and enjoy playing, you can actually play four times in those 5 hours. How does this work? Let's say your dad sets the time intervals from 8 AM to 1 PM and then from 1 PM to 6 PM. You start playing from 12 PM to 5 PM, which means you are playing at the end of one interval and the beginning of the next. By doing this, you manage to fit in more play sessions within the given time frames, effectively using the overlap between the intervals to your advantage. This clever timing allows you to maximize your playtime while still following the rules.

  4. Sliding window log

    To counter this, your dad has a clever trick up his sleeve. He keeps a detailed log of your gaming sessions to ensure you are actually following the rules. He adjusts the intervals based on when you play the game. Let's consider the example above: when you play the game at 12 PM, your dad records that you have played twice in the interval from 8 AM to 1 PM. Then, when you ask to play again at 1 PM, your dad checks the log and sees that you played at 12 PM. Since only one hour has passed, he will not allow you to play again just yet. Unfortunately for you, this means you have to wait a bit longer before you can play again. This system ensures that you adhere to the rules, even if it means you have to be patient.

  5. Sliding window counter

    In the previous scenario, the problem arises because your dad has to maintain a detailed record of every time you play, which is not efficient and consumes a lot of storage space. To address this, your dad comes up with a new approach. Instead of keeping a complete log of each gaming session, he decides to track only the number of times you play within a given interval.

    For instance, using the earlier example, if you play twice between 8 AM and 1 PM, your dad simply notes that you played twice during that period. Now, when you ask to play again at 2 PM, your dad considers the time that has passed since the last interval. He calculates that 25% of the new interval has elapsed since 1 PM, which means 75% of the previous interval has also passed. Assuming you played twice in that interval, he calculates (0.75 \times 2 = 1.5). Since this value is less than 2, your dad concludes that you haven't played excessively and allows you to play again. This method efficiently ensures you stick to the rules without requiring extensive record-keeping.

Â