The queue to the feeder narrows when the animal bites and widens when it is calm - and every keeper reads the same counter, so the animal is not fed by five keepers at once.
👋 Hi, I'm Anton - a software engineer. These days I work on a platform: contracts, generated code, data paths and the tooling around them. Running notes live on my GitHub: github.com/brilliant-almazov.
The learning limit was lifted from one service into the shared library; in the library it is built and no service imports it yet, and the counter under it has three backends. The curve in this part is a shape, not a measurement.
This is part 10 of the series on what a shared platform does for every service, told as a zoo. Maybe you already do it better, maybe you'd do it another way - this is how it is going, and where it stops.
In real life
Next door to the zoo lives a cat who does not belong to us. Miso, in this story, is the neighbour, and the neighbour has the thing we need - so we feed the neighbour, and the neighbour gives something back. Biscuit runs the feeder. He is enthusiastic: "More food? More food!"
Miso bites. "HISS."
Pip knows what a bite means: too much, too fast. Narrow the feeder by one step. Another bite, another step. But never below the floor - Pickle asks "narrower still?" and Pip says no: a feeder narrowed to nothing feeds nobody. Then, after a quiet while with no bites, the feeder widens again, one step at a time, until Miso purrs. And Nora adds the rule that makes it work with more than one keeper: there is one counter for all of them, so five keepers do not each think the feeder is theirs.
A limit you cannot know in advance
Outside sources - the ones a service calls over the network and does not own - have limits. Sometimes they are written down, often they are not, and they change: a busy hour, a new plan, a different region. A fixed limit in configuration is either too careful and wastes capacity, or too generous and gets bitten.
The second problem is replicas. A service runs as several copies. A limit kept in each copy's memory is a limit per copy: three replicas, three times the calls the source agreed to.
A window that learns
The library's rate window answers both. It keeps two windows - per second and per minute - and it reacts to the source. When the source answers "limit", the window shrinks by a step, but not below a minimum. When a recovery period passes without any "limit", it grows by a step. And the occupied slots are not kept in memory: they live in the library's distributed counter, so the limit is one for all replicas. The library's description puts it in those words - the limit is common to every replica.
As a form - the names are illustrative:
rate_window:
per_second: <n>
per_minute: <n>
throttle_step: <n> # the source says "limit": narrow by a step
minimum: <n> # never below
recovery: <duration> # no refusals this long: one step wider
counter: redis | nats-kv | postgres # shared by every replica
The shape of the ceiling over time looks like the comic: flat while things are calm, down a step on each bite, resting on the floor, then climbing back after quiet periods. The picture below is that shape, drawn - not a measurement of any source.
| Event | What the window does | Why |
|---|---|---|
| the source answers "limit" | shrinks by a step | it bit: too much, too fast |
| it shrinks to the minimum | stops shrinking | a feeder of zero feeds nobody |
| a recovery period with no "limit" | grows by a step | it is calm: try a little more |
| a call from any replica | takes a slot in the shared counter | one limit for the whole service |
From the calling side, the shape is short - names shortened, a form:
// a call to an outside source - a form
if err := window.Acquire(ctx); err != nil {
return err // no free slot in the shared counter: wait or refuse
}
resp, err := source.Call(ctx, req)
if saysLimit(resp, err) {
window.Throttle(ctx) // it bit: one step narrower, not below the minimum
}
What counts as a bite is the source's own signal - an HTTP 429, a quota error, whatever that source says when it means "too much". Recognising it is the one part each caller writes, because only the caller knows how its source says no.
One counter, three backends
The shared part sits on the library's distributed counter: one interface, three backends - Redis, a NATS JetStream key-value store, and PostgreSQL - with a decorator that adds metrics. A service picks whichever its stand already runs. The window does not care which.
Where it came from
The window was not designed in the library first. It was written in a service that called an outside source and needed exactly this, and then it was lifted into the library so the next service would not write it again. That is how most of the library grows: one resident solves a problem, and the solution moves to the keeper's handbook. In the library itself, no service imports it yet.
What went wrong
The failure this answers is the one every service that calls an outside source meets sooner or later: a fixed limit that is right on a quiet day and wrong on a busy one, multiplied by the number of replicas. Each replica behaves well on its own, and the source sees the sum.
The honest lesson is about where the code lives. A good mechanism inside one service helps one service. Lifting it into the library is half the work; the other half is the second service that uses it, and that has not happened yet. Until it does, this is a tool on a shelf.
What I do not claim
- Not a measured curve. The line is a shape.
- Not that any service imports the library version yet. None does.
- Not the right numbers for step, floor or recovery. They are per source.
- Not the exact settings format. The form above shows the shape.
What of this is method
- Learn the limit from the refusals. Down a step on "limit", up a step on quiet.
- Never below a floor. A limit of zero is an outage you made yourself.
- One limit per service, not per replica. Keep the slots in a shared counter.
Zoo keeping, built in - Part 10. Next: A Notice Board For Every Keeper.
If you call outside sources with limits, is your limit fixed or learned? If you run several replicas, does each keep its own limit? If you lifted a mechanism from a service into a library, who was its second user? How does your feeder react to a bite?








Top comments (0)