Repository navigation
Description
Analysis baseline: main @ 3ec3455 — 9 November 2026.
Priority: P2
Context / Current Behavior
Notifications already have a PostgreSQL-backed durable queue, collection and delivery leases, fenced acknowledgements, incident deduplication, stable webhook event/idempotency identifiers, exponential retries and restart recovery. Configuration and runtime queue state live in two locked system_settings JSONB rows; this is not an in-memory-only queue.
The worker collects and delivers independently within a pass, but attempts at most four deliveries sequentially, then waits 15 seconds after the pass finishes. Limits include a 90-second lease, eight attempts, 30-second to one-hour retry backoff, 24-hour delivery lifetime, 256 queued deliveries, 1,024 incidents and 4 MiB state. A slow destination can delay unrelated deliveries in the same burst.
Problem Statement
Simultaneous host incidents can saturate admission and produce growing scheduling delay. Even instant transports have a theoretical per-process ceiling of four attempts per 15 seconds before collection/storage cost. Full-state JSONB locking/validation/rewrites compound this bottleneck. Existing caps and deferred admission protect memory, but do not establish fair delivery latency or sustainable storm throughput.
Proposed Architecture / Solution
Evolve the current durable PostgreSQL design with bounded concurrent delivery, fair per-channel/per-owner admission, due-work wakeups and a recovery poll. Wakeups are hints; durable state remains authoritative after a missed event, Valkey loss or process crash. Keep collection and delivery failure domains independent.
First benchmark the existing JSONB queue. Use narrower mutations or a normalized PostgreSQL queue only if measured contention warrants the compatibility cost; no BullMQ, new broker or replacement database is justified by default.
Implementation Scope
- Define collection freshness, queue-admission and delivery-start/drain SLOs for a reproducible burst, including more potential deliveries than the current 256-entry bound.
- Implement bounded global/per-channel delivery and fair selection; prevent one stalled SMTP server, webhook, owner or category from consuming all slots.
- Evaluate timer-to-next-due and local/post-commit wakeups with bounded coalescing, while retaining periodic replay/reconciliation.
- Add bounded retry jitter and explicit SMTP/webhook retry classifications; define treatment of Retry-After, permanent rejection, expiry and exhausted attempts.
- Preserve durable claim-before-send and acknowledgement-after-result. Expose active, deferred, exhausted and terminal states plus oldest pending age.
- Review transaction cost with perf(workers): measure remaining SQL, lock and history costs #399; keep network I/O outside database locks and revalidate channel revision/owner access before decrypting credentials.
- Define cancellation and drain behavior for in-flight transports and collection calls rather than merely clearing the timer.
Security and Reliability Considerations
Preserve channel-bound encryption, fresh authorization and category/private-destination permissions, DNS address validation/pinning, verified TLS hostname, redirect refusal and allowlisted bounded payloads. Keep audit and backup-outcome receipt/cursor commits reliable.
A receiver may accept a message just before the sender crashes prior to acknowledgement. Preserve a stable webhook idempotency key and test receiver-side duplicate suppression; do not claim exactly-once SMTP delivery. Explicitly document which duplicates the sender can prevent and which require receiver support.
Acceptance Criteria
- Record baseline and candidate burst admission/drain times, healthy-channel p95/p99 delay, retries, queue age and database lock/write cost under the same resource budget.
- Numeric global/per-channel/owner concurrency and rate limits are enforced, including stalled destinations and mixed SMTP/webhook workloads.
- A healthy destination makes bounded progress behind failing destinations; admission beyond queue capacity is deferred visibly without losing eventual recovery notifications.
- Lost wakeups, Valkey failure and process restart recover from PostgreSQL without stranded work or duplicate incident creation.
- Lease/token/expiry fences reject stale acknowledgements. Channel/configuration/permission changes prevent fresh admission and unauthorized retries; already-admitted transports may finish and their actual results remain recordable. Crash-after-acceptance behavior and remaining duplicate risk are verified.
- Retry jitter stays inside declared bounds; permanent/exhausted/expired outcomes stop retrying and remain inspectable without a busy loop.
- Define and test the authorization-to-network-admission boundary: revocation/channel edits prevent new unauthorized admission, while an already-admitted send or receiver acceptance cannot be retracted. Shutdown/restore stop further admission and preserve durable queue state and truthful acknowledgements.
- Existing queue bounds, audit behavior, credential isolation and backup/restore compatibility remain effective.
Testing Requirements
Extend worker, incident-engine, collection, PostgreSQL repository and webhook transport regressions. Include concurrent claim/ack, queue saturation and history eviction, delayed collection, slow SMTP, HTTP 429/5xx/permanent failures, DNS/TLS errors, restart before/after send and ack, revoked owners, cancellation and restore. Add a seeded incident-storm fixture in #397, with a deduplicating webhook receiver and independently measured transport attempts/accepted IDs. Keep normal CI short.
Dependencies / Related Issues
#237/#238/#289 already delivered the queue and incident behavior and remain closed. Use #397 for repeatable storms; use the notification-specific database review in #399 and resource-policy design in #400. #395 owns shared lifecycle conventions. #392 increases the possible event rate but is not a prerequisite for testing delivery improvements. Notification authorization, incident policy and application persistence remain in Bun unless #401 later establishes a narrower measured reason to change ownership.
Relevant Source Files
- web/src/config/notifications.config.ts
- web/src/server/Notifications/notification-worker.ts
- web/src/server/Notifications/collector.ts
- web/src/server/Notifications/incident-engine.ts
- web/src/server/Notifications/notification-storage.ts
- web/src/server/Notifications/notification-repository.ts
- web/src/server/Notifications/notification-delivery.server.ts
- web/src/server/Notifications/webhook-delivery.ts
- web/src/tests/server/Notifications/notification-worker.test.ts
- web/src/tests/server/Notifications/notification-repository.db.test.ts
- web/src/tests/server/Notifications/incident-engine.test.ts
- web/src/tests/server/Notifications/webhook-delivery.test.ts
Activity