<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Krithika Subramaniyan</title>
    <description>The latest articles on Forem by Krithika Subramaniyan (@krithika_subramaniyan_5b6).</description>
    <link>https://forem.com/krithika_subramaniyan_5b6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3569120%2F74d0e76f-f42c-4f61-9ec3-56b8c7c75296.png</url>
      <title>Forem: Krithika Subramaniyan</title>
      <link>https://forem.com/krithika_subramaniyan_5b6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/krithika_subramaniyan_5b6"/>
    <language>en</language>
    <item>
      <title>[Boost]</title>
      <dc:creator>Krithika Subramaniyan</dc:creator>
      <pubDate>Fri, 17 Oct 2025 20:46:52 +0000</pubDate>
      <link>https://forem.com/krithika_subramaniyan_5b6/-1h4l</link>
      <guid>https://forem.com/krithika_subramaniyan_5b6/-1h4l</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/krithika_subramaniyan_5b6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3569120%2F74d0e76f-f42c-4f61-9ec3-56b8c7c75296.png" alt="krithika_subramaniyan_5b6"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/krithika_subramaniyan_5b6/designing-a-centralized-rate-limiter-for-java-microservices-the-why-the-how-and-the-lessons-25nl" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Designing a Centralized Rate Limiter for Java Microservices — The Why, The How, and The Lessons&lt;/h2&gt;
      &lt;h3&gt;Krithika Subramaniyan ・ Oct 17&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#java&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#ratelimiter&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#architecture&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#microservices&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>java</category>
      <category>ratelimiter</category>
      <category>architecture</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Designing a Centralized Rate Limiter for Java Microservices — The Why, The How, and The Lessons</title>
      <dc:creator>Krithika Subramaniyan</dc:creator>
      <pubDate>Fri, 17 Oct 2025 20:20:07 +0000</pubDate>
      <link>https://forem.com/krithika_subramaniyan_5b6/designing-a-centralized-rate-limiter-for-java-microservices-the-why-the-how-and-the-lessons-25nl</link>
      <guid>https://forem.com/krithika_subramaniyan_5b6/designing-a-centralized-rate-limiter-for-java-microservices-the-why-the-how-and-the-lessons-25nl</guid>
      <description>&lt;p&gt;When you work with distributed systems long enough, you start to realize that the hardest problems aren’t just about scaling up — they’re about staying consistent while scaling.&lt;/p&gt;

&lt;p&gt;A few months ago, I realized that off-the-shelf API rate limiting packages wouldn’t meet our needs.&lt;/p&gt;

&lt;p&gt;Most packages enforce limits per service, which means if you want to allow 100 requests per second across the entire system and you have 4 services, you’d have to split the limit arbitrarily — 25 requests per second per service. This doesn’t account for uneven traffic or dynamic usage, and it quickly becomes hard to maintain as the system scales.&lt;/p&gt;

&lt;p&gt;To address this, I designed a &lt;strong&gt;centralized rate limiting&lt;/strong&gt; mechanism that allows each service to enforce limits locally while still respecting a &lt;strong&gt;global threshold&lt;/strong&gt;, ensuring fairness and predictable behavior across the distributed system.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;In a distributed environment, rate limiting isn’t just a performance safeguard — it’s a &lt;strong&gt;fairness mechanism.&lt;/strong&gt;&lt;br&gt;
It prevents one service from monopolizing shared resources, protects downstream APIs, and helps maintain predictable latency.&lt;/p&gt;

&lt;p&gt;But implementing it independently in every microservice led to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Different algorithms (fixed window, token bucket, sliding window)&lt;/li&gt;
&lt;li&gt;Hardcoded thresholds scattered across code&lt;/li&gt;
&lt;li&gt;No consistent behavior across services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our goal was to build a &lt;strong&gt;reliable, consistent mechanism&lt;/strong&gt; that works across all services for a team, while allowing teams to maintain &lt;strong&gt;independent limits&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Design Journey
&lt;/h2&gt;

&lt;p&gt;The first question was whether to implement a &lt;strong&gt;central throttling service&lt;/strong&gt; or a &lt;strong&gt;distributed enforcement mechanism.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Option 1: Central Service
&lt;/h3&gt;

&lt;p&gt;A separate rate-limiting service (e.g., a REST endpoint) seemed appealing at first. It could manage rules dynamically and give real-time control.&lt;/p&gt;

&lt;p&gt;But after some design exploration, I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every microservice would have to make a network call to this service.&lt;/li&gt;
&lt;li&gt;Under heavy traffic, that meant &lt;strong&gt;hundreds of thousands of extra calls per second&lt;/strong&gt; — creating new latency and potential points of failure.&lt;/li&gt;
&lt;li&gt;Ironically, the rate limiter itself could become the bottleneck.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Option 2: Distributed Enforcement with Shared Logic
&lt;/h3&gt;

&lt;p&gt;Instead, I implemented the logic so that &lt;strong&gt;all services for a given team access a shared Redis cluster&lt;/strong&gt; containing the global limit and counters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each service reads the Redis configuration from its local service config.&lt;/li&gt;
&lt;li&gt;The library updates counters atomically in Redis to enforce the global team limit.&lt;/li&gt;
&lt;li&gt;Different teams can maintain separate limits and separate Redis clusters, so one team’s traffic doesn’t affect another team.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach ensures consistent throttling within a team while keeping the system distributed and resilient. The shared library makes integration straightforward, but the &lt;strong&gt;primary goal remains enforcing global limits reliably in a distributed system.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Architecture Overview
&lt;/h2&gt;

&lt;p&gt;At a high level, the design looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Team A Redis Cluster (100 req/sec)
┌───────────────┐   ┌───────────────┐   ┌───────────────┐
│ Service A1    │   │ Service A2    │   │ Service A3    │
│ RateLimiter   │   │ RateLimiter   │   │ RateLimiter   │
└───────────────┘   └───────────────┘   └───────────────┘

Team B Redis Cluster (200 req/sec)
┌───────────────┐   ┌───────────────┐
│ Service B1    │   │ Service B2    │
│ RateLimiter   │   │ RateLimiter   │
└───────────────┘   └───────────────┘

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each service embeds the rate limiter library, which:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connects to Redis to store and retrieve counters.&lt;/li&gt;
&lt;li&gt;Applies per-client or per-API rate limits.&lt;/li&gt;
&lt;li&gt;Throws a &lt;code&gt;TooManyRequestsException&lt;/code&gt; when thresholds are
exceeded.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This keeps throttling logic centralized in behavior, but &lt;strong&gt;distributed in execution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation Snapshot
&lt;/h2&gt;

&lt;p&gt;Tech stack&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java 17&lt;/li&gt;
&lt;li&gt;Spring Boot 3&lt;/li&gt;
&lt;li&gt;Redis for distributed counter management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1️⃣ Configuration
&lt;/h3&gt;

&lt;p&gt;Each microservice adds the library in its &lt;code&gt;pom.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;com.example&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;rate-limiter-library&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;1.0.0&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And configures limits in application.yml:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rate-limiter:
  redis:
    host: redis-host
    port: 6379
  limit:
    requests-per-second: 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2️⃣ Usage
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!rateLimiter.isAllowed("serviceA-client")) {
    throw new TooManyRequestsException("Rate limit exceeded");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3️⃣ Core Logic (Simplified)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public boolean isAllowed(String clientId) {
    String key = "rate:" + clientId;
    long count = redisTemplate.opsForValue().increment(key);

    if (count == 1) {
        redisTemplate.expire(key, 1, TimeUnit.SECONDS);
    }

    return count &amp;lt;= limitPerSecond;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses Redis atomic operations to maintain distributed counters safely across services.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Impact
&lt;/h2&gt;

&lt;p&gt;Once packaged and documented, adoption was fast. The library was quickly embraced across the microservices ecosystem.&lt;/p&gt;

&lt;p&gt;The results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Consistent throttling behavior&lt;/strong&gt; across all services&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero downtime&lt;/strong&gt; during configuration changes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reduced rate-related downstream errors by ~70%&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most importantly, developers no longer had to think about rate limiting — it just worked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Make adoption effortless.&lt;/strong&gt;&lt;br&gt;
Developers love tools that “just work.” Providing a simple interface and YAML-based configuration made integration straightforward and low-effort.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Centralization ≠ Single Point of Failure.&lt;/strong&gt;&lt;br&gt;
True centralization is about shared logic, not shared runtime. The library model balanced both autonomy and consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Design for change.&lt;/strong&gt;&lt;br&gt;
Rate limits evolve as usage grows. Externalizing configuration meant teams could adapt without code changes or redeployments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Reliability matters&lt;/strong&gt;&lt;br&gt;
Even small inconsistencies can cascade in distributed systems; focus on atomic operations and predictable behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Building this rate limiter taught me that good architecture is as much about empathy as it is about engineering.&lt;br&gt;
The goal wasn’t just to stop traffic — it was to help dozens of teams &lt;strong&gt;build faster, safer&lt;/strong&gt; systems without worrying about the plumbing.&lt;/p&gt;

&lt;p&gt;Rate limiting is more than an implementation detail.&lt;br&gt;
It’s a quiet, invisible layer of reliability — one that keeps the whole system graceful under pressure.&lt;/p&gt;

</description>
      <category>java</category>
      <category>ratelimiter</category>
      <category>architecture</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
