I built a poller that doesn't know when to back off
Last week our backend slowed down for about ten minutes, and the web client I’m responsible for made it worse. Not through a crash or a bug in the usual sense: it did exactly what I wrote it to do. That’s the problem. I want to walk through what I got wrong, because the mistake was less about code and more about what I never bothered to specify.
What the scheduler does
The client has a loader scheduler that keeps data on screen fresh. Each piece of data a view depends on is a “reading,” and the scheduler re-reads it at an interval as long as something on screen is subscribed to it. It keeps one request in flight per reading, so if a view asks for data while a request is already out, it just joins that request instead of starting another. A loader can declare its own interval or a rule for picking one; if it declares nothing, it gets polled every five seconds.
On paper that sounds reasonable. In practice, during the slowdown, every open window kept every interval to the second. A list view that polls every five seconds polled every five seconds, right through the whole window, while the backend’s response times climbed and user-initiated actions behind those polls started timing out.
The two things I got wrong in the code
Nothing lengthens an interval when the backend is struggling. The scheduler has no notion of “the server is answering slowly, ease up.” It can’t tell the difference between a healthy backend and one that’s drowning, and so it treats both the same. A server already late on its responses received exactly the request rate it would get at rest, from every open client. We added a steady stream of load to the very overload that was slowing it down.
Intervals are measured from the start of a request, not from its response. If a reading is on a five-second interval and a request takes a second and a half to come back, the next request fires three and a half seconds after that. The “interval” is really a ceiling on the gap between requests, not a floor. Under normal conditions nobody notices. Under slow conditions, it means the effective polling rate goes up precisely when it should go down. Combined with the one-in-flight rule, the scheduler ends up firing the next request essentially the moment the previous one returns.
Individually each of these is a small design decision. Together they produce a client that behaves like a load test against a server that’s already asking for mercy.
The thing I got wrong before the code
Here’s what I think the actual root cause was. The written requirements for this behavior said, in effect, that a reading is “read again at an interval.” That’s it. Nothing about how an interval is measured. Nothing about what happens when responses are slow. Nothing about there being only one request in flight per reading, or about subscriber counting deciding whether a reading is polled at all.
The one-in-flight rule and the subscriber registry are real, and they’re good, but they exist only in the implementation. No requirement names them. So when someone reviewed this code, there was nothing to hold it against. A reviewer could read it, see that it polled at an interval, confirm that matched the one sentence we’d written down, and sign off. Which is exactly what happened.
I treated the scheduler as an implementation detail of “keep the UI fresh” when it’s actually a piece of traffic policy that affects every service we talk to. Traffic policy deserves to be written down.
What I should have specified
If I were writing the requirements today, they’d say:
- One request per reading is in flight at a time. A read that comes due while one is already out waits for that answer rather than stacking.
- An interval is measured from the last response, not the last request. A five-second interval means five seconds of quiet after the data arrives.
- Intervals back off when the server is slow. When response times exceed an expected bound, every interval doubles, up to a cap, and returns to its declared length once responses come back within bound. The client should be the first thing to get out of the way, not the last.
- No reading is polled by default. Every reading that’s polled declares its own interval explicitly. A silent five-second default means things get polled that nobody ever decided should be, and nobody sees the cost until the server does.
And the two behaviors that already exist, one-in-flight and subscriber-gated polling, should have been written down as requirements too, so that a future change can’t quietly remove them and a review can actually check for them.
What I’m changing
The code fixes are straightforward and I’ve started on them: measure from the response, add backoff keyed on response latency, remove the implicit default interval and make each loader declare one.
The habit I’m trying to change is harder. When I write anything that decides when the client talks to a server, I’m going to write down the policy first, in plain language, including what happens when the server is unhappy, and get that reviewed before the code. “It polls at an interval” is not a specification. It’s a hint, and I spent a ten-minute outage learning the difference between the two.
If you’ve got a poller somewhere that you’ve never seen misbehave, it might be worth asking what it does when the thing on the other end slows down. Mine had a very confident answer, and it was the wrong one.