<?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 Neuro on Medium]]></title>
        <description><![CDATA[Stories by Neuro on Medium]]></description>
        <link>https://medium.com/@neuroquarkk?source=rss-ec02dbe9ccfd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ixTwm2oYxIA4gKjaCE3x4g.jpeg</url>
            <title>Stories by Neuro on Medium</title>
            <link>https://medium.com/@neuroquarkk?source=rss-ec02dbe9ccfd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 11:44:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@neuroquarkk/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[A multi-instance URL shortener built around a Key Generation Service]]></title>
            <link>https://medium.com/@neuroquarkk/a-multi-instance-url-shortener-built-around-a-key-generation-service-75e65f5d9e3e?source=rss-ec02dbe9ccfd------2</link>
            <guid isPermaLink="false">https://medium.com/p/75e65f5d9e3e</guid>
            <dc:creator><![CDATA[Neuro]]></dc:creator>
            <pubDate>Mon, 20 Jul 2026 11:52:32 GMT</pubDate>
            <atom:updated>2026-07-20T11:52:32.686Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>R</strong>ecently I built a URL shortener as an experiment. I wanted to see whether I could run it as a multi-instance setup without a shared, synchronized counter to generate base62 short codes. A shared counter means every instance has to coordinate on every single id generated. There were a few options around this and I went with a Key Generation Service (KGS)</p><h4><strong>Key Generation Service (KGS)</strong></h4><p>KGS is a small, standalone service which generates random base62 strings and then stores them in a Redis SET. It also monitors that set and if the pool drops below a threshold, it generates more and fill it up to the capacity</p><p>No server instance knows or cares how ids get made, they just pull from a pool that KGS keeps full. This separation means KGS can change how it generates ids, a different algorithm or a different batch size without changing a single line of the server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*3l1KLLnh-iIvwHw4OxGU0g.png" /><figcaption>KGS’s monitor and refill loop</figcaption></figure><h4>Why a SET and where it falls short</h4><p>A Redis SET gives deduplication meaning it can’t hold two identical members. That covers a case where KGS happens to generate the same code twice while first copy is present in the pool. The odds of that happening is small but the set make it a non-issue</p><p>But there can be a scenario where the code is popped, written to Postgres and then later generated again. As the set only knows what’s currently inside, a code that already left is invisible to it</p><p>That case is rare too. The place that handles it is the shorten endpoint itself. On insert, if Postgres rejects the row with a unique-constraint violation, the handler uses a new code and retries capped at few attempts</p><p>The set and retry loop is solving the same problem where one keep the pool safe and other against what’s already left to prevent collision</p><h4>Keeping the pool full: from snapshot to rate</h4><p>KGS&#39;s job is to keep the Redis pool full but when to check and refill turned out to be most interesting part</p><h4>version one: deciding from snapshot</h4><p>The first version checked the pool&#39;s current size on some interval. If it was above a threshold percentage, sleep a while and check again. If it dropped below, refill immediately</p><p>The problem was that the snapshot couldn&#39;t tell how the pool got to where it is. A pool sitting at 55% could mean a slow, steady trickle that&#39;ll take a long time to drop or it could also mean a burst during the last few seconds and it&#39;ll most probably drain the pool empty before the next check but this version couldn&#39;t tell that apart</p><h4>version two: deciding from rate</h4><p>The fix was simple, to estimate before how fast is it draining. Each check calculate how many ids disappeared since last check divided by how much time passed, a depletion rate</p><p>The rate is tracked over a small sliding window of recent checks and the max rate during that decide the sleep duration. We could have used average, noise reduction, weighted average, etc but I went with this choice of being over cautious</p><p>The sleep duration comes from ids left above threshold divided by the max recent rate. That estimate is clamped between a minimum and maximum</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*hwpYI5FJKmFpWcKjakh5hQ.png" /><figcaption>Rate based sleep estimation</figcaption></figure><p>This approach is also not perfect as it doesn’t completely remove that blind spot. A better approach can be exposing an endpoint that the server can call whenever they need a batch, the KGS then in-turn can keep a better track of the depletion and refill when it drop below the threshold</p><h4>The in-memory buffer (idstore)</h4><p>KGS doesn’t solve the problem of how fast a server instance can hand out an id. If every /shorten request had to pop directly from Redis, it will put a network round-trip on every request across the instances</p><p>For that, each server instance has its own local buffer. On startup, it pulls a batch from Redis pool into memory and when a request needs a code, it pops one from the local buffer. When the local buffer runs low, it refills itself in the background</p><p>The buffer’s fill on startup is still synchronous because if a server instance started accepting requests before its local buffer was populated, the first requests could fail with no available ids. The background refill goroutine only takes over after that first batch is confirmed</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*wnQr6A6zfeQr-Ct6KufaOg.png" /><figcaption>idstore’s startup fill and background fill</figcaption></figure><h4><strong>Tracing Request</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*721lA0JBH9_4EGmSLyBmaA.png" /><figcaption>Tracing a shorten request</figcaption></figure><h4>Where it landed</h4><p>The finished shape consisted of four server instance behind Nginx each running its own idstore buffer, a single Redis instance keeping the shared pool, a single Postgres instance as the source of truth for short -&gt; url mapping and KGS monitoring and refilling the pool with no direct coupling to anything else</p><p>None of these pieces are complicated on their own, a pool, a local buffer, a rate estimate. What made this worth doing was the constraint that started it: no shared counter, no coordination between instances. This was meant to be a good excuse to think carefully about a small problem and that part worked out better than I expected</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75e65f5d9e3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>