<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ayush Makwana]]></title><description><![CDATA[Ayush Makwana]]></description><link>https://blog.ayushmakwana.com</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 03:14:24 GMT</lastBuildDate><atom:link href="https://blog.ayushmakwana.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How Rate Limiters Protect Systems and Boost performance]]></title><description><![CDATA[Rate limiters are used to control the number of requests sent by clients or services. They ensure that requests are sent at an acceptable rate to the target machine or service. If the request count exceeds the threshold defined by the limiter over a ...]]></description><link>https://blog.ayushmakwana.com/how-rate-limiters-protect-systems-and-boost-performance</link><guid isPermaLink="true">https://blog.ayushmakwana.com/how-rate-limiters-protect-systems-and-boost-performance</guid><category><![CDATA[ratelimit]]></category><category><![CDATA[backend]]></category><category><![CDATA[System Architecture]]></category><dc:creator><![CDATA[Ayush Makwana]]></dc:creator><pubDate>Fri, 03 Jan 2025 03:28:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/CGWK6k2RduY/upload/7ff7b71314f59b05ff90d79822e0afa7.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Rate limiters are used to control the number of requests sent by clients or services. They ensure that requests are sent at an acceptable rate to the target machine or service. If the request count exceeds the threshold defined by the limiter over a specified period, subsequent requests are managed according to predefined rules or policies.</p>
<p>Depending on the use cases, there are three main strategies to limit the rate of requests:</p>
<ol>
<li><p><strong>Slowing down the requests:</strong></p>
<ul>
<li><p>Incoming requests are buffered, and the system processes them at a predetermined, controlled rate.</p>
</li>
<li><p>A typical example of this is queues and message brokers. Incoming requests are stored in a queue, and consumers process them at their own pace.</p>
</li>
</ul>
</li>
<li><p><strong>Rejecting requests:</strong></p>
<ul>
<li>There can be multiple scenarios where we have to reject excess requests. For example, when a large volume of requests overwhelms the system's capacity, excess requests are rejected, and clients receive a <code>429 (Too Many Requests)</code> error code in response.</li>
</ul>
</li>
<li><p><strong>Ignoring requests:</strong></p>
<ul>
<li><p>Similar to rejecting requests, but instead of returning an error code, excess requests are silently dropped without notifying the client.</p>
</li>
<li><p>In the case of a DDoS attack, it is used to create an illusion for the attacker that all the requests are being accepted.</p>
</li>
</ul>
</li>
</ol>
<p>As per the use cases, we can apply these strategies individually or in combination to efficiently manage our system resources and prevent overload.</p>
<p>Rate limiters are typically deployed on the server side. Depending on the architecture and needs of the system, they can be implemented at the application layer (Layer 7) or network layer (Layer 3) of the OSI model.</p>
<p>Rate limiters can be used to handle both external requests (such as those coming from users or APIs) and internal requests (from internal services or third-party integrations). Here are some common use cases:</p>
<ol>
<li><p><strong>External Rate Limiters</strong></p>
<ul>
<li><p>External rate limiters are client-facing and use metrics like time windows and IP addresses to determine whether a request should be allowed or not.</p>
<ol>
<li><p><strong>Preventing DDoS attacks</strong></p>
<ul>
<li><p>DDoS attacks are common, and no matter how big the attack is, our system should not go down. So, external rate limiters ensure that we're only forwarding the requests which are legitimate by observing things like IP addresses or user access tokens.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726938325543/e325393d-e4be-456e-9b62-ac431ed9e54e.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
</li>
<li><p><strong>Managing traffic spikes gracefully</strong></p>
<ul>
<li><p>It may happen that there is a lot of legitimate traffic coming in, and none of them are attackers, but our infrastructure just isn't ready to handle it.</p>
</li>
<li><p>In these scenarios, it's okay to block excess requests and let users know that we're expecting too much traffic; please try again later.</p>
</li>
</ul>
</li>
</ol>
</li>
</ul>
</li>
<li><p><strong>Internal Rate Limiters</strong></p>
<ul>
<li><p>In internal rate limiters, resource utilization is considered to make decisions.</p>
<ol>
<li><p><strong>Managing subscription-based applications</strong></p>
<ul>
<li><p>For example, you're running a blogging website that allows free-tier users to read only 3 blogs in a month. In that case, if a user crosses the free limit, you need to block the request for the rest of the month.</p>
</li>
<li><p>To achieve this, data about usage and requests are stored in a database or cache so that the rate limiter can check it and make a decision.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726938337326/2fee6a8e-74cb-46d0-ad54-9ed6b70b1a5b.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
</li>
<li><p><strong>Controlling usage of third-party apps</strong></p>
<ul>
<li><p>When the internal services make calls to third-party services, you need to ensure that it is regulated as per the defined rules and not just overused.</p>
</li>
<li><p>Suppose in our website, we have integration with a third-party vendor who provides an API for LLM. And of course, the calls are expensive, so we have to ensure the number of calls we make stays within the agreed-upon limits.</p>
</li>
</ul>
</li>
<li><p><strong>Preventing cascading failures</strong></p>
<ul>
<li><p>In scenarios where you want to execute some commands or database queries that could have cascading effects.</p>
</li>
<li><p>For instance, if you need to delete millions of rows from a table, doing it all at once can lead to catastrophic failures and make the database unavailable. Instead, you might want to delete a thousand rows every hour. In this case, a rate limiter helps to distribute the load uniformly across a longer time span, reducing the risk of system overload.</p>
</li>
</ul>
</li>
</ol>
</li>
</ul>
</li>
</ol>
<h3 id="heading-algorithms-to-implement-rate-limiter">Algorithms to Implement Rate Limiter</h3>
<p>Rate limiting can be implemented using different algorithms, each with its own advantages and limitations. Here is a list of some popular algorithms:</p>
<ul>
<li><p>Token bucket</p>
</li>
<li><p>Leaking bucket</p>
</li>
<li><p>Fixed window counter</p>
</li>
<li><p>Sliding window log</p>
</li>
<li><p>Sliding window counter</p>
</li>
</ul>
<h3 id="heading-what-if-a-client-exceeds-the-rate-limit">What if a Client Exceeds the Rate Limit?</h3>
<p>When a request exceeds the rate limit, the API returns a <code>429 (Too Many Requests)</code> response to the client. According to use cases, rate-limited requests may be queued for later processing, such as delaying order processing during system overload.</p>
<h3 id="heading-how-does-a-client-know-whether-it-is-being-throttled">How Does a Client Know Whether It Is Being Throttled?</h3>
<p>Clients can track their rate limit status through these data in HTTP headers:</p>
<ul>
<li><p>Number of remaining allowed requests.</p>
</li>
<li><p>Total allowed requests per time window.</p>
</li>
<li><p>Time to wait before making another request.</p>
</li>
</ul>
<hr />
<h3 id="heading-references">References</h3>
<ul>
<li><p><strong>System Design Interview – An insider's guide</strong> by <a target="_blank" href="https://www.amazon.com/Alex-Xu/e/B08BNMFT7P/ref=dp_byline_cont_book_1">Alex Xu</a></p>
</li>
<li><p><a target="_blank" href="https://youtube.com/@asliengineering?si=pidz1fpZQJp7mwe3"><strong>Arpit Bhayani YouTube</strong></a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>