Evidence before change
Performance optimization
Removing redundant database work and avoidable client-side JavaScript on the busiest screens, guided by instrumentation and protected by query-count regression tests.
01Problem
The most-visited screens, the manager dashboard and the roster, made more database calls than their data required. The app runs against a remote, pooled PostgreSQL database, so every extra sequential round trip adds real latency.
Some HR pages also shipped server-only code to the browser.
02User Needs
- Store managers check the dashboard many times a day on phones over mobile data.
- HR runs month-end exports across every store and needs them to complete reliably.
03Constraints
- No change to attendance, payroll or permission behaviour was acceptable.
- Authorization had to be re-evaluated on every request against live data, so caching across requests was not an option.
- No speculative optimization: each change needed an observed cause.
04Technical Decision
Instrument first with a request-scoped query counter, change only what the evidence justifies, and lock each improvement in with a test that fails if the query count grows again.
Optimization approach
Each change started from an observed cause and ended with a test that keeps it from regressing.
01
Instrument
Count database calls per request on the busiest screens
02
Find redundant work
Duplicated lookups, sequential waits, per-row queries
03
Change data access
Memoize per request, batch independent reads, bulk queries
04
Trim the client
Keep server-only code out of browser bundles; load heavy libraries lazily
05
Lock it in
Regression tests fail if query counts grow again
06
Weigh trade-offs
Skip changes whose cost outweighs the gain
05Implementation
- Request-scoped memoization of session and access resolution, so a page and its layout no longer resolve the same user twice. Authorization still runs fresh on each request.
- Independent reads in the daily exception sweep were batched into one parallel group, and the sweep is skipped for days that are already closed.
- The HQ overview loads every store's daily snapshot with a constant number of queries instead of one fan-out per store.
- Timesheet generation, reconciliation and payroll export use bulk queries bounded regardless of employee or day count. The export became read-only and rejects unbounded date ranges.
- UI constants moved out of modules that imported the database client, which removed it from browser bundles. Heavy libraries such as QR rendering are loaded lazily.
- Some changes were deliberately left out. The default roster view was not lazy-loaded, because splitting it would delay first paint for no real gain.
06Validation
- Query-count tests assert the bounded number of queries for every optimized path.
- Golden tests compare batched results with the original per-row calculations.
- A build-output check confirmed no database client code remained in browser chunks.
07Outcome
The busiest screens do less redundant work per request: duplicated lookups are resolved once, independent reads run together instead of one after another, and per-location fan-outs became bounded bulk queries. Several admin pages no longer ship server-only code to the browser. Regression tests keep these properties in place. No production latency figures are claimed.
08Lessons Learned
- The biggest wins were duplicated work and sequential waits, not slow individual queries.
- A query-count test is cheap to write and catches regressions that latency monitoring would only reveal later.
- Instrumenting first sometimes showed there was nothing to fix, which saved work.
Based on the project history and release documentation. Identifiers, credentials, configuration values and open findings are intentionally omitted.
Next case studyCross-location attendance