<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Trịnh Đức Bảo Linh on Medium]]></title>
        <description><![CDATA[Stories by Trịnh Đức Bảo Linh on Medium]]></description>
        <link>https://medium.com/@trinhducbaolinh?source=rss-318f89aa5489------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wKMe0v7DYrqny_zf.</url>
            <title>Stories by Trịnh Đức Bảo Linh on Medium</title>
            <link>https://medium.com/@trinhducbaolinh?source=rss-318f89aa5489------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 22:09:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@trinhducbaolinh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Composite Distributed Rate Limiter in Go with Redis]]></title>
            <link>https://medium.com/@trinhducbaolinh/building-a-composite-distributed-rate-limiter-in-go-with-redis-9d103c21f39e?source=rss-318f89aa5489------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d103c21f39e</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[rate-limiting]]></category>
            <category><![CDATA[uber]]></category>
            <category><![CDATA[redis]]></category>
            <dc:creator><![CDATA[Trịnh Đức Bảo Linh]]></dc:creator>
            <pubDate>Fri, 09 Jan 2026 03:45:33 GMT</pubDate>
            <atom:updated>2026-01-09T03:45:33.395Z</atom:updated>
            <content:encoded><![CDATA[<p>Rate limiting sounds simple — until you run real systems at scale.</p><p>In production, it’s rarely enough to say <em>“limit to 10 requests per second.”</em><br> What you usually need is a combination of rules:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e281cWXIiSKW1tDKiF1xGw.png" /></figure><ul><li>Short-term burst control (per second)</li><li>Sustained traffic limits (per minute)</li><li>Long-term quotas (per hour or per day)</li><li>All enforced <strong>globally across multiple instances</strong></li></ul><p>To solve this cleanly, I built an open-source Go library:<br> 👉 <strong>redis-ratelimiter</strong></p><p>This post explains <strong>why it exists</strong>, <strong>how composite rate limiting works</strong>, and <strong>how it helps protect both internal and external resources in real systems</strong>.</p><h3>Why Another Rate Limiter?</h3><p>The Go ecosystem already has excellent rate-limiting libraries:</p><ul><li><a href="https://github.com/uber-go/ratelimit">uber-go/ratelimit</a><br> → Simple, elegant, in-memory rate limiting with a familiar API</li><li><a href="https://github.com/go-redis/redis_rate">go-redis/redis_rate</a><br> → Distributed rate limiting backed by Redis</li></ul><p>In real projects, I often wanted <strong>the advantages of both</strong>:</p><ul><li>A <strong>familiar interface</strong> like uber-go/ratelimit</li><li><strong>Distributed enforcement</strong> using Redis</li><li>The ability to <strong>combine multiple independent rate limits</strong> without complex logic</li></ul><p>That’s the gap redis-ratelimiter aims to fill.</p><h3>Design Goals</h3><p>The library is built around a few core principles:</p><ol><li><strong>Familiar API</strong><br> Easy to adopt if you’ve used uber-go/ratelimit.</li><li><strong>Distributed by Default</strong><br> Redis is the single source of truth, so limits are enforced consistently across pods, services, and workers.</li><li><strong>Composable Limits</strong><br> Real systems don’t have one limit — they have many.</li></ol><h3>The Core Feature: Composite Rate Limiting</h3><p>The most powerful feature of this library is the <strong>composite rate limiter</strong>.</p><p>Instead of defining one large, complex rule, you define <strong>small, independent limiters</strong> and combine them together.</p><blockquote><em>A request is allowed </em><strong><em>only if all limiters permit it</em></strong><em>.</em></blockquote><p>This mirrors how real production rate-limit policies work.</p><h3>Example: Per-Second + Per-Minute Limits</h3><p>Let’s say we want:</p><ul><li><strong>10 requests per second</strong></li><li><strong>100 requests per minute</strong></li></ul><h3>Individual Limiters</h3><pre>perSecLimiter := ratelimiter.New(<br>    rdb,<br>    &quot;api:per_second&quot;,<br>    10,<br>    ratelimiter.Per(time.Second),<br>)</pre><pre>perMinLimiter := ratelimiter.New(<br>    rdb,<br>    &quot;api:per_minute&quot;,<br>    100,<br>    ratelimiter.Per(time.Minute),<br>)</pre><p>Each limiter is independent and backed by Redis.</p><h3>Composite Limiter</h3><pre>composite := ratelimiter.NewComposite(<br>    perSecLimiter,<br>    perMinLimiter,<br>)</pre><p>Now, every request must pass <strong>both</strong> limits:</p><pre>_, err := composite.Take()<br>if err != nil {<br>    // Rate limit exceeded<br>}</pre><p>This gives you:</p><ul><li>Burst protection</li><li>Sustained traffic control</li><li>Clean, readable logic</li></ul><h3>Dynamically Add Limits</h3><p>Need to enforce a per-hour quota later?</p><pre>perHourLimiter := ratelimiter.New(<br>    rdb,<br>    &quot;api:per_hour&quot;,<br>    1000,<br>    ratelimiter.Per(time.Hour),<br>)</pre><pre>composite.AddLimiter(perHourLimiter)</pre><p>No refactoring required.</p><h3>Real-World Use Case: Worker Pools &amp; External Resources</h3><p>This library is especially useful for <strong>services that control access to limited resources</strong>, such as databases and third-party APIs.</p><p>A very common architecture is the <strong>worker pool pattern</strong>.</p><h3>The Problem</h3><p>Imagine a system where:</p><ul><li>You launch many workers concurrently</li><li>Each worker processes jobs independently</li><li>Each job may:</li><li>Query a database</li><li>Call an external API using a <strong>shared API key</strong></li></ul><p>Scaling workers increases throughput — but also risk.</p><p>Without proper control, you can easily:</p><ul><li>Overload the database</li><li>Exceed external API rate limits</li><li>Get throttled, blocked, or banned</li><li>Cause cascading failures across services</li></ul><h3>High-Level Architecture</h3><pre>            Job Producer<br>                 |<br>              Job Queue<br>                 |<br>        -------------------------<br>        |       |       |       |<br>      Worker  Worker  Worker  Worker<br>        |       |       |       |<br>        +-------+-------+-------+<br>                |<br>           Redis Rate Limiter<br>             |            |<br>          Database     External API</pre><p>All workers share the <strong>same Redis-backed rate limiters</strong>, ensuring <strong>global enforcement</strong>, not per-worker limits.</p><h3>Rate Limiting by Task Type</h3><p>Instead of limiting workers, we limit <strong>operations</strong>.</p><h3>Database Access Limiter</h3><pre>dbLimiter := ratelimiter.New(<br>    rdb,<br>    &quot;task:db_access&quot;,<br>    50,<br>    ratelimiter.Per(time.Second),<br>)</pre><p>This ensures:</p><ul><li>At most <strong>50 database operations per second</strong></li><li>No matter how many workers are running</li></ul><h3>External API Limiter (Composite)</h3><p>External APIs usually require stricter control.</p><p>Example policy:</p><ul><li><strong>10 requests per second</strong></li><li><strong>500 requests per minute</strong></li></ul><pre>apiPerSec := ratelimiter.New(<br>    rdb,<br>    &quot;task:external_api:sec&quot;,<br>    10,<br>    ratelimiter.Per(time.Second),<br>)</pre><pre>apiPerMin := ratelimiter.New(<br>    rdb,<br>    &quot;task:external_api:min&quot;,<br>    500,<br>    ratelimiter.Per(time.Minute),<br>)</pre><pre>externalAPILimiter := ratelimiter.NewComposite(<br>    apiPerSec,<br>    apiPerMin,<br>)</pre><p>A request is allowed <strong>only if all limits pass</strong>.</p><h3>Worker Pool Example</h3><pre>func worker(<br>    ctx context.Context,<br>    jobs &lt;-chan Job,<br>    dbLimiter ratelimiter.Limiter,<br>    apiLimiter ratelimiter.Limiter,<br>) {<br>    for job := range jobs {</pre><pre>        if _, err := dbLimiter.Take(); err != nil {<br>            log.Println(&quot;DB rate limit exceeded&quot;)<br>            continue<br>        }</pre><pre>        queryDatabase(job)</pre><pre>        if _, err := apiLimiter.Take(); err != nil {<br>            log.Println(&quot;API rate limit exceeded&quot;)<br>            continue<br>        }</pre><pre>        callExternalAPI(job)<br>    }<br>}</pre><p>Even with many workers:</p><ul><li>Database load stays controlled</li><li>External API quotas are respected</li><li>Scaling workers does <strong>not</strong> break external contracts</li></ul><h3>Why This Approach Works Well</h3><p>✅ Global limits enforced via Redis<br> ✅ Workers stay stateless and simple<br> ✅ External systems are protected<br> ✅ Easy to tune limits without changing logic<br> ✅ Works across services, pods, and regions</p><p>This pattern fits perfectly for:</p><ul><li>Background job processors</li><li>Data aggregation pipelines</li><li>Crawlers and indexers</li><li>Services using shared API keys</li></ul><h3>Open Source &amp; Contributions</h3><p>The project is open source, and feedback is very welcome.</p><p>👉 GitHub repository:<br> <a href="https://github.com/linhbkhn95/redis-ratelimiter">https://github.com/linhbkhn95/redis-ratelimiter</a></p><p>Feel free to:</p><ul><li>Open issues</li><li>Suggest improvements</li><li>Submit pull requests</li><li>Share real-world use cases</li></ul><h3>Final Thoughts</h3><p>Rate limiting is not just about throttling traffic — it’s about <strong>protecting systems while remaining scalable</strong>.</p><p>Composite rate limiting turns complex policies into <strong>simple, composable building blocks</strong>, and Redis makes them work reliably at scale.</p><p>If you’re building distributed systems in Go, I hope this library helps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d103c21f39e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>