Ritenta
Back to roadmap

Lesson 6 of 7 · 3 min read

Measure and budget notification performance

In our course notification service, an instructor publishes an update and learners receive it through a delivery worker. At the start of term, instructors report a slow publish button and learners report late reminders. Those symptoms can come from different parts of the system. The team needs separate measurements before choosing what to change.

Measure what each user waits for

Measure publication from the time the service receives a valid request to the time it confirms that the update and delivery job have been saved durably. Measure dispatch from that durable commit to acceptance by the email provider. Label the second metric clearly because provider acceptance does not establish inbox delivery. For a scheduled reminder, start the dispatch clock when the reminder becomes due. Count timeouts and failed sends alongside latency so fast failures cannot make a change look successful.

Turn expectations into budgets

Choose an initial service-level objective such as 95% of valid publish requests completing successfully within 300 ms over a rolling seven-day window. Give dispatch its own objective, for example 99% of due notifications accepted by the provider within 30 seconds. These are example product decisions. To design the synchronous path, allocate 40 ms for validation, 180 ms for the transaction that saves the announcement and delivery job, and 80 ms for overhead and headroom. Those allocations guide design. Verify the target with the measured end-to-end distribution, since adding the 95th percentiles of individual stages does not give the end-to-end 95th percentile.

Find the expensive stage under load

Record timings for the database transaction, time spent waiting in the queue, and the provider call. Preserve a correlation ID across the request and job. Compare a steady workload to a burst of large courses publishing at once, and inspect the p50 and p95 for both publish and dispatch. Suppose publish p95 stays at 180 ms and provider p95 stays at 400 ms, while p95 queue wait climbs from 1 second to 22 seconds. Investigate why workers fall behind before tuning the publish handler. If provider capacity allows it, try limited worker concurrency and check queue wait, provider rate limits and retry volume together.

Verify the change and protect delivery

Replay the same workload before and after the change with the same course sizes and job mix. Compare completion within the two objectives, queue depth, duplicate sends and accepted notifications per worker-minute. A provider timeout can happen after it has already accepted the message. Keep retries bounded, back them off with jitter, and reuse the same delivery identifier when the provider supports idempotency. Verify the timeout case with a fake provider that accepts the send and drops the acknowledgement.

Give every user-visible wait its own measurement and budget. Use stage timings to choose a change and end-to-end results under comparable load to judge it.

Long code lines scroll sideways →
import { performance } from 'node:perf_hooks';

async function measurePublish(publish, observeDurationMs) {
  const start = performance.now();
  try {
    return await publish();
  } finally {
    observeDurationMs(performance.now() - start);
  }
}

Sources