New age of scaling

2. Polling burns the database budget

ScaleArchitect · Scaling RCA · 12 October 2026 · 2 of 4

75 Role A users · single-instance baseline · polling and SQL RCA

Test window: 11–12 October 2026 · All times UTC · Root causes, proposed fixes and the next scaling gate

Scaling decision

HOLD HIGHER LOAD · Reduce work per user The 75-user journey completes without errors or logouts, but database CPU reaches 74% while app CPU is 7%. Background polling duplicates work, discards completed responses and repeatedly scans the same tables.

Next move: Remove duplicate requests first, stop cancellation loops, then deploy the same-result SQL candidates in small, measurable changes.

Baseline completion
75 / 75
One instance · 0 errors · 0 logouts
Mentions polling cost
36.6%
Share of all server time in this baseline
Page-cancelled calls
2,142
Server work can continue after browser abort

Fix order: remove demand before adding capacity

PriorityChangeMeasured opportunityExpected effect
P1 · firstRemove duplicate mentions timer10.5 polls/user/min; 36.6% of server timeAvoid duplicate requests; gain needs retest
P1 · nextSkip overlapping periodic polls2,142 page-cancelled callsStop paying for discarded answers
P1 · nextRender filter controls only on initial load28.5 KB and ~8 queries discarded per pollRemove unused work from every refresh
P1 · after correctness checksDeploy selective scope-filter/query rewrite60,127 → 149 rows read for one scoped accountUse indexes; consolidate repeated reads

Root causes and proposed fixes

F04

Two timers request the same unbounded mentions list

P1 · High leverage
Owner Frontend + API developerConfidence Duplicate code + timingsEvidence E06

Impact. A mostly empty median response consumes more than a third of total server time. Each user multiplies the same avoidable request stream.

Root cause

The page contains the same 10-second polling block twice. Each request runs a 17-join history query without a row limit.

Evidence

The endpoint receives 10.5 calls per user per minute, with p50 0.79 s and p95 5.26 s. Two ready handlers start identical timers. The source ranks this as a 1–2-hour change, not a guaranteed throughput increase.

Proposed fix

Delete the second ready/timer block in notifications.js; retain the first block and its search binding. Add a one-in-flight guard. Bound the API history only after agreeing unread-count and paging behavior.

Polling and result bound · proposed change

Adapted, redacted excerpt · application names, fields, paths and response labels are illustrative aliases.

- Two timers each call refreshNotifications(true) every 10 seconds
+ One timer calls refreshNotifications(true) every 10 seconds
+ Background refresh skips while its previous request is active

- ORDER BY items.created_date DESC, items.created_time DESC
+ ORDER BY items.created_date DESC, items.created_time DESC LIMIT 200

Timer lines are a behavioral diff; ORDER BY is a query excerpt. The 200-row bound needs product approval and a separate total-unread count if required.

Risk and rollout

A history limit must not hide older unread mentions or silently reduce the unread badge. A stale in-flight flag must reset on errors as well as success.

Retest and acceptance

Verify one poll per interval, working search, all request-outcome resets and correct unread/paging behavior. Repeat the 75-user recording and compare endpoint call count, total server time, DB CPU and completion.

F05

Refresh ticks cancel answers but not the work behind them

P1 · Request amplification
Owner Frontend + backend developerConfidence Browser vs origin + sourceEvidence E06

Impact. The browser drops slow answers while the server continues their work. Completed-response browser timings understate the server tail, and each new tick adds pressure to the same queue.

Root cause

Periodic refresh always aborts its predecessor. An aborted callback can also clear state belonging to the replacement request. Separately, every info response builds filter controls the browser throws away.

Evidence

The baseline contains 1,219 cancelled info polls and 923 cancelled main polls. Cloud Run p95 is about 8 s versus about 4 s in browser figures. The unused filter include adds 28.5 KB and about 8 queries per poll. It also produces 3,951 PHP warnings across the run.

Proposed fix

Separate periodic refresh from deliberate user actions: periodic ticks skip an active request; a user action may replace it. Detach the old request before aborting and let cleanup clear state only when it still owns the current request.

Refresh ownership · proposed control flow

Adapted, redacted excerpt · application names, fields, paths and response labels are illustrative aliases.

- Periodic tick aborts the previous request and starts another
+ Periodic tick returns when the previous request is active
+ User action detaches the old request, then aborts it
+ Completion resets state only if this is still the current request

Control-flow specification from E06. Keep existing response rendering and intentional action semantics.

Filter rendering · proposed PHP excerpts

Adapted, redacted excerpt · application names, fields, paths and response labels are illustrative aliases.

+ $RENDER_INITIAL_FILTERS = true;
  include 'summary_refresh.php';

+ <?php if (!empty($RENDER_INITIAL_FILTERS)): ?>
  <div id="initial-filter-panel">
      <?php include 'initial_filters.php'; ?>
  </div>
+ <?php endif; ?>

First excerpt belongs to initial page rendering; second guards the existing filter panel in the info endpoint. Preserve its existing attributes.

Risk and rollout

Do not delay intentional filter changes behind stale replies. Test aborted-callback ordering. Keep the initial filter panel and all its selectors intact.

Retest and acceptance

Periodic polling produces no self-cancellation loop; failures cannot freeze refresh state. The first page still renders filters, polling responses omit the unused panel, and origin and browser latency converge without losing UI correctness.

SQL candidate: less scanning, the same answer

The allowed-scope lookup reads a fraction of the rows

Same data and parameters · one account limited to 17 data scopes · MySQL Handler-based rows read, not rows_examined. E07.

CandidateRows read: original → candidateTimed comparisonResult equivalence
Filter panel60,127 → 149438 → 150 msEvery returned row compared: identical
Status bar102,594 → 591751 → 482 msEvery returned row compared: identical
Measure the real timing gain, not just the row reduction

Workstation/proxy timings include roughly 100 ms network overhead. These are query comparisons, not end-to-end p95 or a post-deployment load retest. E07.

F06

Scope filters hide indexed columns; blanket rewrites break NULL semantics

P1 · Candidate measured
Owner Backend + database engineerConfidence Result sets + query comparisonEvidence E07

Impact. Each polling request repeats full-table reads to return a permitted subset. A fast rewrite is unacceptable if it changes which rows a Role A user may see.

Root cause

IFNULL(column, 0) wraps indexed scope columns. Positive allowed-scope comparisons can avoid that wrapper, but blocked scopes sit inside NOT: replacing their predicates indiscriminately excludes NULL-scope rows that the current policy includes. The filter panel also reads the same work-item set six times.

Evidence

For the account limited to 17 scopes, filter reads drop 403.5× and timings improve 438 → 150 ms. Status-count reads drop 173.6×, with 751 → 482 ms. The unrestricted status-count case improves only 648 → 627 ms: the effect depends on scope. Four dev account scopes have equal result sets; blocked-scope coverage uses synthetic local assigned resources because none of the dev assigned resources has blocked scopes.

Proposed fix

Use index-friendly predicates only where their NULL semantics are proven. Keep blocked-scope behavior, read the permitted work-item set once for all six lists, and merge the total and weight-sum reads.

Allowed-scope predicate · proposed semantic change

Adapted, redacted excerpt · application names, fields, paths and response labels are illustrative aliases.

- IFNULL(work_item.scope_id, 0) = :allowed_scope
+ work_item.scope_id = :allowed_scope

  NOT (IFNULL(work_item.scope_id, 0) = :excluded_scope)

Parameterized SQL illustration, not a drop-in query. Allowed IDs must be positive; retain the full existing permission logic and blocked-scope semantics.

Shared count read · proposed SQL shape

Adapted, redacted excerpt · application names, fields, paths and response labels are illustrative aliases.

- SELECT COUNT(*) FROM work_item WHERE <same permission predicate>
- SELECT SUM(item_weight) FROM work_item WHERE <same permission predicate>
+ SELECT COUNT(*) AS total, SUM(item_weight) AS weight_sum
+ FROM work_item WHERE <same permission predicate>

Query-shape illustration. Keep permission guards, NULL/empty-result handling and existing output contract.

Risk and rollout

Check ordinary, mirrored, blocked, unrestricted and group-scoped scopes. Do not copy the earlier blanket IFNULL removal. New index proposals remain unmeasured and are not prerequisites to claim these measured candidate results.

Retest and acceptance

Compare complete result sets, order, counts and NULL behavior before rollout. Deploy one reviewed change, repeat the same 75-user journey and measure DB CPU, polling latency and correctness. Do not claim full-system speedup from the query benchmark alone.

Scaling gate

Keep the successful baseline fixed while removing wasted work. Advance only after the full recording still completes, request counts fall as intended, permission results match and the database has measurable headroom.