<?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[Code With Prashant]]></title><description><![CDATA[Code With Prashant]]></description><link>https://spring-boot-resilience4j-rate-limiter.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 15:54:31 GMT</lastBuildDate><atom:link href="https://spring-boot-resilience4j-rate-limiter.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Spring Boot Magic: Taming Traffic with Resilience4j's Rate Limiting]]></title><description><![CDATA[Introduction to Rate Limiting
Rate limiting is a technique used to control the number of requests a client can make to an API or service within a defined time window. This is crucial for modern applications to avoid overload, ensure fair usage, and p...]]></description><link>https://spring-boot-resilience4j-rate-limiter.hashnode.dev/spring-boot-magic-taming-traffic-with-resilience4js-rate-limiting</link><guid isPermaLink="true">https://spring-boot-resilience4j-rate-limiter.hashnode.dev/spring-boot-magic-taming-traffic-with-resilience4js-rate-limiting</guid><category><![CDATA[Java]]></category><category><![CDATA[General Programming]]></category><category><![CDATA[Springboot]]></category><category><![CDATA[resilience4j]]></category><dc:creator><![CDATA[Prashant Babel]]></dc:creator><pubDate>Tue, 26 Aug 2025 08:29:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/b4rv12kTWf4/upload/59fb825f57f086f90629cf8d75f79162.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction-to-rate-limiting">Introduction to Rate Limiting</h2>
<p>Rate limiting is a technique used to control the number of requests a client can make to an API or service within a defined time window. This is crucial for modern applications to avoid overload, ensure fair usage, and protect services against spikes, abuse, or denial-of-service attacks. Implementing rate limiting helps maintain system stability, performance, and availability under heavy or malicious traffic.</p>
<p>Resilience4j, a lightweight fault tolerance library for Java, offers a powerful RateLimiter module designed to integrate seamlessly with Spring Boot applications. This guide will walk through setting up Resilience4j’s rate limiting feature in your Spring Boot app, complete with code examples and practical tips.</p>
<h2 id="heading-why-use-resilience4js-ratelimiter">Why Use Resilience4j’s RateLimiter?</h2>
<ul>
<li><p><strong>Prevents overload:</strong> Controls request rate, ensuring backend resources are protected from excessive calls.</p>
</li>
<li><p><strong>Easy integration:</strong> Its Spring Boot support lets you add resilience patterns with simple annotations and YAML configuration.</p>
</li>
<li><p><strong>Customizable:</strong> Configure request limits, time windows, and timeouts to suit use cases.</p>
</li>
</ul>
<h2 id="heading-step-1-add-dependencies">Step 1: Add Dependencies</h2>
<p>Add these dependencies to your <code>pom.xml</code> for Maven:</p>
<pre><code class="lang-xml">xml<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.github.resilience4j<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>resilience4j-spring-boot3<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.1.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-aop<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<h2 id="heading-step-2-configure-the-ratelimiter">Step 2: Configure the RateLimiter</h2>
<p>In <code>application.yml</code>, define instances and custom settings.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">textresilience4j.ratelimiter:</span>
  <span class="hljs-attr">instances:</span>
    <span class="hljs-attr">simpleRateLimit:</span>
      <span class="hljs-attr">limitForPeriod:</span> <span class="hljs-number">2</span>           <span class="hljs-comment"># Max requests per window</span>
      <span class="hljs-attr">limitRefreshPeriod:</span> <span class="hljs-string">15s</span>     <span class="hljs-comment"># Window size</span>
      <span class="hljs-attr">timeoutDuration:</span> <span class="hljs-string">5s</span>         <span class="hljs-comment"># How long to wait for a permit</span>
      <span class="hljs-attr">registerHealthIndicator:</span> <span class="hljs-literal">true</span>
      <span class="hljs-attr">eventConsumerBufferSize:</span> <span class="hljs-number">100</span>
</code></pre>
<h2 id="heading-parameter-insights">Parameter Insights</h2>
<ul>
<li><p><strong>limitForPeriod:</strong> Maximum allowed requests per window.</p>
</li>
<li><p><strong>limitRefreshPeriod:</strong> Window duration.</p>
</li>
<li><p><strong>timeoutDuration:</strong> Time to wait for an available request slot.</p>
</li>
</ul>
<h2 id="heading-step-3-protect-endpoints-with-the-ratelimiter-annotation">Step 3: Protect Endpoints With the <code>@RateLimiter</code> Annotation</h2>
<p>Add the annotation directly to service classes or methods.</p>
<pre><code class="lang-java">javaimport io.github.resilience4j.ratelimiter.annotation.RateLimiter;
<span class="hljs-keyword">import</span> org.springframework.stereotype.Service;

<span class="hljs-meta">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieService</span> </span>{

    <span class="hljs-meta">@RateLimiter(name="simpleRateLimit")</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieDetails</span><span class="hljs-params">(String movieId)</span> </span>{
        <span class="hljs-keyword">return</span> fetchMovieDetails(movieId);
    }
}
</code></pre>
<h2 id="heading-step-4-monitoring-amp-metric-exposure">Step 4: Monitoring &amp; Metric Exposure</h2>
<p>With <code>spring-boot-starter-actuator</code>, Resilience4j exposes metrics on <code>/actuator</code> endpoints for inspection. Integrate Prometheus and Grafana for advanced dashboarding and alerts.</p>
<hr />
<h2 id="heading-step-5-handling-rate-limit-breaches">Step 5: Handling Rate Limit Breaches</h2>
<p>Requests exceeding the limit trigger exceptions. Catch such cases and implement a fallback:</p>
<pre><code class="lang-java">java<span class="hljs-meta">@RateLimiter(name="simpleRateLimit", fallbackMethod="rateLimitFallback")</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieDetails</span><span class="hljs-params">(String movieId)</span> </span>{
  <span class="hljs-comment">// call external API</span>
}

<span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">rateLimitFallback</span><span class="hljs-params">(String movieId, RequestNotPermitted exception)</span> </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Movie(<span class="hljs-string">"Unavailable"</span>, <span class="hljs-string">"Rate limit exceeded, please try again later."</span>);
}
</code></pre>
<hr />
<h2 id="heading-conclusion">Conclusion</h2>
<p><strong>Resilience4j’s RateLimiter</strong> is a powerful, configurable, and efficient tool for safeguarding Spring Boot APIs from excessive traffic and ensuring robust microservices. With simple configuration and Spring-native annotations, rate limiting becomes easy to adopt and extend—while Actuator and observability tools deliver rich operational insights.</p>
<hr />
<p>Use this guide to boost reliability and control of Spring Boot services in production or at scale. Implement resilient APIs for a more stable application ecosystem.</p>
]]></content:encoded></item></channel></rss>