performance domain
owns the cost of answering a question: round trips per screen, query cost, cache identity.
why
- screens are judged by what they cost to answer, not what they compute.
- round trips, query cost, cache identity fail together: unbounded query breaks the screen, cache without one identity owner breaks the data.
- grouping them keeps every read path designed for bounded, measurable cost.
when to use
this domain owns a decision when the question is what an answer costs to produce โ round trips per screen, query cost and indexing, pagination bounds, cache identity and invalidation. correctness of stored facts belongs to the persistence domain.
good and bad
bad
// route handler fetches ALL runs for the channelexport const getDashboard = async (c: Context) => {const channelId = c.req.param("channelId");// No WHERE clause, no LIMIT โ unbounded resultconst allRuns = await db.query.runs.findMany({where: { channelId },});// Browser filters and countsconst activeCount = allRuns.filter((r) => r.status === "running" || r.status === "queued").length;return c.json({ activeCount });};
- query has no bounds: 100 hours of video = 10,000+ runs streamed to the app and loaded into memory.
- browser parses JSON, iterates, counts. 10-hour livestream (50k runs) = 8+ MB payload. mobile browsers choke.
- after 30 seconds page still shows "loading". user refreshes, queries pile up, connection exhausts the database, other pages time out.
- no metrics for query cost, result count, or what the page needed โ cannot tell database vs network vs browser.
good
// route handler reads one pre-aggregated factexport const getDashboard = async (c: Context) => {const channelId = c.req.param("channelId");// Indexed lookup by channelId only (constant-time)const dashboard = await db.query.channelDashboardProjection.findUnique({where: { channelId },});return c.json({ activeCount: dashboard.activeCount });};// Separate service maintains the projection// (reads all runs once, updates only the aggregated columns)async function updateChannelDashboardProjection(channelId: string) {const activeCount = await db.query.runs.findMany({where: {channelId,status: { in: ["running", "queued"] }, // indexed},select: { id: true }, // count only}).then((rs) => rs.length);await db.query.channelDashboardProjection.update({where: { channelId },data: { activeCount, updatedAt: new Date() },});}
- answer is a single indexed row: constant-time even at 100k runs.
- response is 100 bytes, not 8 MB. page loads in 5ms.
- cost predictable whether the channel has 10 or 1M runs.
- measurable: track query latency and result size, alert on deviation.