I built a summary route and filled it with the full objects
I made a mistake I already knew how to avoid. I did the first half of the fix and skipped the second half, and the result costs more than if I’d done nothing. This post covers what I built, why it was wrong, and what I’m changing.
What I was asked to build
Our home dashboard has a scheduler section with one line per project. The request was to replace the text figures on that line with a row of dots, one per work item. Each dot is colored by the item’s state and labeled with the item and its state. That’s all the section needs from an item: a subject, a kind and a state.
The dashboard already read from a dedicated fleet-wide scheduler summary route that returned per-project counts and recent event tallies. I extended that route so it could draw the dots.
What I did wrong
I added an items field to the summary type and gave it the detail type, the same work item object the per-project scheduler page uses. That page shows every item in a table with every column, so its item type carries a lot:
- the subject, level and priority
- when the item entered its state
- the state’s payload: wait reasons and affected workspaces, placement steps, the machine and session for running items, hold reasons
- for failed tasks, the full text of the session’s failure report
On the server, my change was about twenty-five lines. The handler already built each project’s full scheduler reading, so I copied its work items into the summary with one line, items,. I didn’t project, filter or cap anything. I also updated the route’s docs to say it returns “its work items in the order the scheduler places them.” That’s accurate, and it describes the defect as if it were a feature.
What it cost
The dashboard reads three short fields per item and throws away the rest. Here’s what a recent measurement showed:
- The response was about 1 MB, and the item arrays were 98% of it.
- Failure report text alone was close to a third of the item data. The longest single report was about 4,000 characters, sent so a dot could say “task, failed.”
- Cutting each item down to kind, subject and state makes the payload about twelve times smaller. The counts the section also draws are a few hundred bytes for the whole fleet.
The route is polled every five seconds per open dashboard. That’s about 12 MB a minute and roughly 720 MB an hour per window, all downloaded and parsed on the main thread to redraw dots that change a few times an hour.
On the server, every poll from every window rebuilds every project’s full scheduler state (issue roll-ups, dispatch ledgers, tasks, reviews), only to serialize fields the client discards. The list of items also has no cap. That’s a separate bug, but the two multiply: an unbounded list where every entry is the full object.
Why it’s worse than doing nothing
A dedicated summary route is useful because it sends less than the detail pages would. Mine sent the detail objects through a second endpoint. The client could have read each project’s detail page and gotten almost the same bytes, and we’d have one fewer route to maintain. I got the maintenance cost of a summary route and none of the savings.
What I should have written
A summary type for the summary route. The codebase already had this pattern elsewhere, a Summary type for list endpoints and a Detail extends Summary type for detail endpoints. I should have followed it:
/** One work item as the dashboard draws it: a dot. */
interface WorkItemSummary {
key: string; // tells it apart from the project's other items
kind: WorkItemKind;
subject: string | null; // what the dot is named after
state: SchedulingState;
}
interface ProjectSchedulerSummary {
// ...counts, longest wait, recent events, latest warning/error
/** The first N items as the line draws them; the counts cover the rest. */
items: WorkItemSummary[];
}
The per-project page keeps the full detail type, because it actually draws every column.
A server path that doesn’t build the detail. The counts and longest wait come from each item’s state and timestamp, and the dots need subject, kind and state for the first N items. A query that returns exactly that per project should replace building each project’s full reading on every request. If placement order really does need the full roll-up, the handler should still cap and project the items before serializing, and the route docs should state the cap.
Updates pushed on change, not polled. The items only change on scheduler events the server already streams. A stream would send a few hundred bytes per change. The poll sends a megabyte every interval whether anything changed or not. Polling is acceptable as an exception with a stated reason. The poll’s existing comment justifies polling for the time-windowed event counts, but that reason doesn’t cover the items.
The signals I missed
I can’t blame missing knowledge for this. Our team had already written down:
- List entries shouldn’t carry fields only the detail page draws. A collection of per-object detail inside a list entry is a known warning sign, and we’d fixed the same shape on other lists that month.
- A reading that’s re-fetched on an interval must be bounded and carry only what its views draw. That rule was written about ten days after my change, during a fix for a similar issue. Nobody went back to check this route, even though the same fix touched the route right next to it.
- Size every bound for the whole fleet. I never multiplied the per-poll cost by the number of open windows or the fleet’s item count.
- Design how what you create ends. Failed items are kept for a week, so each day’s failures add a week of full reports to every poll.
I also made the change as part of a correction prompted by one of those lessons, that dashboards should draw state rather than describe it in text. I checked my change against that lesson only. A fix checked only against the rule that prompted it can easily break the others.
How I’ll work differently
- Read the caller before writing the contract. Before I add a field to a response type, I’ll list the fields the consuming component actually reads. If the type has fields that list doesn’t, the type is wrong.
- Dedicated route, dedicated type. If a route exists to summarize, its types should be summary types. When I’m tempted to reuse a detail type there, I’ll take that as a sign I’ve stopped thinking about the summary.
- Choose between fixed shapes and field selection on purpose. When there are only a few fixed callers, like a list drawing a brief and a page drawing the full object, each gets its own type that carries exactly what it draws. When callers need many combinations of fields, like a table with user-chosen columns, the route should accept a field selection instead of multiplying routes. This route had one caller and one fixed set of fields, so it needed a dedicated summary type.
- Multiply before merging. For anything that’s polled or streamed, I’ll estimate payload × frequency × open clients × growth over time, and write the result in the PR description.
- Bound every list. Every list in a response gets a stated cap, with counts covering the rest.
- When a new rule is written, check the neighbors. When we add a design rule after an incident, I’ll look for other routes with the same shape, especially ones I wrote recently.
Adding a summary route was only half the fix. The other half was the summary type, and the check that would have caught this takes a few minutes: open the component and list the fields it uses before writing the contract.