How one quadratic webhook handler pinned our autoscaler at its ceiling
Our Control RPC deployment usually sits near its floor of about 32 pods. One day it was holding steady at its ceiling of 256, and it had been there for hours. External traffic was up about 2x. The deployment had scaled up about 8x.
This is the story of the weeks it took to find out why. Spoiler: the autoscaler was doing exactly what we told it to. The real cause was three separate bugs in our GitLab integration, which together turned a single incoming webhook into potentially millions of internal RPC calls.
Some context: Control and Region silos
Sentry runs as a set of isolated Region silos plus a single Control silo. Control holds data that is shared across organizations or has to be globally unique, like users, organization slugs, and integrations. Regions can’t access Control data directly, so when code running in a region needs that data, it makes a remote procedure call (RPC) to Control. (Mark Story wrote about the design in Removing risk from our multi-region design with simulations.)
That means the Control RPC deployment is a shared resource. Any product code running in any region can generate load on it, and that turns out to matter a lot here.
Suspect #1: the autoscaler
Just before this started, the deployment had been moved to new infrastructure, with a number of small changes along the way, including some autoscaler tweaks made in response to recent incidents. With that much changing at once, it was hard to know where to start. The autoscaler changes looked the most related, so that’s where I looked first.
We scale the Control RPC pods with KEDA against five metrics:
- CPU utilization of pods
- Requests per second to the RPC pods
- The P95 “busyness” of our Granian HTTP servers
- Active connections per pod
- Backpressure on Granian workers
Each of these metrics counts equally, so any one target that is too tight causes a scale-up. Two stood out as dominant: Granian P95 busyness and requests per second.
P95 busyness was very elevated, regularly pinning the deployment at 100%, so it looked like an obvious suspect. RPC latency wasn’t elevated at all and average CPU per pod was very low, so the pods appeared to have plenty of headroom. I raised the busyness target by 15% aaaaaand, nothing. The pods kept scaling.
Why a P95 busyness target can never be satisfied
A teammate asked Claude to explain why this metric was flawed, and the analogy it came up with was too good not to share:
Imagine you run a lemonade stand with a bunch of helpers. You have a rule for hiring more helpers: “If any helper looks busy, hire more.” Every few minutes you look around, find the single busiest helper, and if that one person looks busy, you hire a bunch more people.
Here’s the problem: hiring more helpers doesn’t make the one busiest helper less busy. Maybe they’re stuck squeezing a really tough lemon, or one customer keeps asking them complicated questions. So next time you look, someone still looks busy, and you hire more. And more. Eventually you have 256 helpers standing around doing almost nothing (that’s the low CPU we noticed), but your rule still says “someone’s busy, hire more!” so you’re stuck at the maximum.
The rule should have been: “If helpers on average look busy, hire more.” That way, when you add helpers and spread the work around, the average goes down and you stop hiring. Right now, the rule looks at the wrong thing, so it can never be satisfied.
Making it worse, your hiring rule is fast but your “send people home” rule is slow. You hire 4 people every minute and a half, but you only send 2 home every two and a half minutes, and you wait 5 minutes before even starting. So every little rush ratchets you up and you barely come back down.
I kept adjusting, and settled on P75 busyness with a slightly lower target, plus a higher requests-per-second limit per pod. That helped: pods scaled down more often and latency barely changed. But during our weekly traffic peak, from Tuesday evening to Thursday afternoon, the pod count was still pinned near the top. I’d made the scaling mildly better without fixing the underlying problem.
Suspect #2: Granian worker memory limits
We’d been burned before. In an earlier incident, the memory limit on our old uWSGI workers was set too low, so pods spent most of their time starting workers and then killing them as soon as they hit the ceiling. Granian had since replaced uWSGI but had a similar memory limit setting, so it was worth a closer look.
This time, though, CPU utilization was low, far lower than it had been on the old deployment. The Granian settings turned out to be a red herring.
Letting AI help with the tedium
After hours of staring at dashboards, I handed some of the tedious digging through metrics and deployment diffs to AI tools, including Claude and an internal triage agent. The results were… mixed. Some early findings were good, but there were plenty of dead ends, more red herrings, and a general tendency to overstate how much a given metric had to do with the problem.
They did help me move faster in some areas. Using MCP servers (like Sentry’s), I could summarize metric trends over time and compare the Kubernetes manifests of the old and new deployments. After narrower prompting and some cross-checking, I had six leads:
- Postgres query times were elevated across the board.
- Unschedulable pods were a chronic problem.
- Scale-down was too slow.
- The PgBouncer deployment had changed.
- The Envoy deployment had changed.
- Pods were crashlooping.
Plenty of leads, not enough breadcrumbs
I worked through them one by one. A few things could be tuned, but nothing explained the severe scale-ups:
- Total RPC call counts looked relatively flat, with no significant week-over-week change that we could see.
- Postgres connections were slightly limited, but not enough to affect RPC calls.
- Doubling the scale-down rate helped us come back down from peaks, but any significant load ratcheted us up again.
- The PgBouncer deployment was largely fine.
- The crashloops had no obvious logs or causes.
At this point I was well out of my depth and running out of places to look.
The breakthrough: a latency monitor and a per-method breakdown
While all this was going on, we’d been adding instrumentation to Control and tuning monitors to get better signal on system health. One of those monitors tracked Control RPC latency, which had been flat. Then it started firing sporadically: latency was spiking globally.
Breaking RPC call counts down by method finally showed what the totals had hidden. One semi-expensive RPC method had gone up about 10x within a week:
Not only were the spikes large, they were growing. We finally had a real suspect:
integrations.get_organization_integrations
This RPC method returns the integration installations attached to an integration. It’s usually called in response to a user action that touches an integration, or to an incoming webhook from a provider like GitHub or GitLab.
I shared what I’d found with the engineers who own the integrations code and kicked off an AI triage run on my commute home. By the next morning, another engineer had run their own triage, both runs pointed to the same code path, and a teammate on our source code management team had already shipped a patch. The effect was immediate:
Control scaled almost all the way back to its baseline pod count, and it stayed there through the following week.
Three problems in a trench coat
It turns out this was really three bugs in a trench coat. To see how they combined, you need to know how Sentry models integrations.
How integration installations work
Some providers, like GitHub and Slack, only let a customer install our app once per GitHub or Slack organization. But several Sentry organizations may need to share that one installation. So Sentry stores a top-level Integration record with metadata, configuration, and authentication details. Each Sentry organization that connects to it gets an OrganizationIntegration record, which acts as the “installation” and binds that organization to the shared configuration.
For GitLab, the customer creates an OAuth application in GitLab and enters its details in Sentry:
After that, an OAuth flow gets a token pair, which is stored against that organization’s installation.
To avoid duplicating configuration, each integration has re-entrant install logic that either reuses or extends an existing shared Integration. It finds the existing record with an external ID that is unique to the external resource. For GitLab, that’s the GitLab group ID plus the hostname, since customers can also connect self-hosted GitLab instances. This is where the trouble starts.
Bug 1: a four-year-old grouping bug
About four years ago, we shipped a feature that let GitLab users include all of their groups (GitLab’s equivalent of a GitHub organization) by leaving the group path empty. It turns out this also grouped unrelated installations together when the hostname was https://gitlab.com. Every group-less installation on GitLab’s hosted service ended up attached to a single shared Integration record. Over four years, that record collected more than 2,000 installations.
Bug 2: a webhook backfill that finally started working
Earlier this year, we added issue assignment syncing for GitLab. When someone assigns a GitLab issue, GitLab sends us a webhook, we look up the matching Sentry user if there is one, and we assign the linked Sentry issue to them. To support existing installations, we also added logic to backfill webhook triggers for installations that had sync enabled. That backfill was broken at launch.
In mid-September, a fix to the webhook setup process landed, making sure webhook triggers were configured for every project under the parent integration. This is where things really kicked off: the shared record from Bug 1 now had webhooks set up across all of its installations.
Bug 3: an N² lookup across the silo boundary
Now for the cherry on top. The assignment sync code path had a degenerate case that looked up installations by their parent integration for every single installation. Webhooks are handled in Region silos, but integration data lives in Control, so each of those lookups was an RPC call to Control.
That made it N² lookups for every assignment webhook we received, where N is the number of installations on the integration. For the shared record with more than 2,000 installations, each webhook meant iterating over the installations and making about 2,000 RPC calls, then doing it again for each installation in the results. A single webhook could mean millions of RPC calls.
None of these bugs would have caused much trouble alone. The grouping bug sat quietly for four years. The N² lookup is harmless when N is 1. Together, with the webhook fix as the trigger, they kept the Control RPC deployment pinned at its maximum.
Results
Before the fix, the Control RPC deployment averaged about 150 pods over the week. After it, the average dropped to about 42, roughly 108 fewer pods or about 72% less. More importantly than the savings, it gives the deployment real headroom to scale when it needs to.
What’s still not solved
You might think we can call this done now that the deployment has scaled down, but honestly, we’re not there yet. When we rolled back the P75 busyness change, the deployment went straight back into a scale-up spiral, which means a subset of pods is still consistently running hot. We don’t know why yet. The fix removed the biggest source of load, but it didn’t explain why our scaling is non-linear, and we’re still investigating.
Could this happen to you?
If you run a shared internal service behind an autoscaler, probably. A few things to check:
- Check that your scaling metrics actually drop when you add pods. Tail metrics like P95 are a sensible scaling signal, but they assume more pods means a shorter tail. For us, a few slow, IO-heavy requests kept P95 busyness high no matter how many pods we added, so the autoscaler climbed to its maximum. Sanity-check against CPU: hundreds of pods at near-idle CPU means the signal isn’t tracking load.
- Compare scale-up and scale-down speed. A fast scale-up and a slow scale-down (with a long stabilization window) means every burst ratchets you up and you barely come back down. Kubernetes lets you tune both directions separately through the HPA scaling behavior settings, which KEDA passes through.
- Break internal RPC volume down by method. Our total call counts looked flat while one method went up 10x. Totals hide exactly the kind of regression that causes this.
- Look for N+1 and N² calls that cross a network boundary. A loop that does a lookup per item is cheap against a local database and expensive when each lookup is an RPC call to another service. It’s also cheap when N is small, which is why these bugs survive testing.
- Watch for shared records that grow without limit. Ask what the largest N in production actually is, not what it usually is.
What we learned
- Autoscaling has a lot of knobs, and they interact. Tuning them is tricky and extremely tedious, and a misconfigured autoscaler can hide the real problem as easily as it can cause one.
- One misbehaving product feature can drive the health and cost of a shared system. Shared services need per-caller and per-method visibility, not just totals.
- Tracing a performance problem to its source should take hours, not weeks. For us it took weeks, and the clue that broke it open came from new monitoring, not from the dashboards we already had.
- Bugs can sit for years before they show up. They often stay invisible until new functionality is built on top of them, and several features that each work fine alone can combine into a problem that’s hard to trace.
If you use Sentry’s GitLab integration, the GitLab integration docs cover how to set it up and configure it.