A notification service spends most of its time waiting on someone else: Firebase, Brevo, Twilio, DynamoDB, Kafka. Heavy computation isn't the risk. The risk is a slow or failing provider letting work pile up. This post covers what the service uses to stay stable.
Who does the work
There's no separate job queue or worker fleet. The Kafka listeners are the workers: each one consumes an event, stores it and calls the providers. Kafka is the queue, and adding consumer replicas is how you add capacity (Part 3).
The REST API is a different thing. It serves the timeline, preferences and template management, and it doesn't send event notifications.
Virtual threads
The service runs on virtual threads, for web requests and for async tasks. They're cheap, so a call waiting on a slow provider doesn't hold an operating-system thread. A thread per task is affordable again, and the code can stay simple and blocking in style.
Capped async executors
Some work runs off the main listener path, like delivery status updates and publishing Kafka events. It runs on async executors that create one virtual thread per task, with a semaphore capping how many tasks are in flight:
- The notification executor allows 50 concurrent tasks and handles delivery status updates.
- The general executor allows 100 and handles other async work, such as publishing events.
When a cap is reached, new tasks wait for a free slot. Without the cap, a provider slowdown would let outbound work grow without limit against connection pools that are finite. The caps are configuration values, so they can be tuned per environment.
The executors also carry the security context over to the new thread, so work started on behalf of a user keeps that user's identity.
Circuit breakers
SMS and push each have a circuit breaker, and both use the same settings. The breaker looks at the last 20 calls and needs at least 10 before it judges. It opens if 50% or more fail, or if 80% or more are slower than 5 seconds. Once open, calls fail instantly for 30 seconds. Then it lets 5 test calls through and closes again if they succeed.
The slow-call rule works like a soft timeout. A provider that answers, but slowly, gets treated as failing before it can use up the pool.
Two details matter in practice. The breaker wraps only the provider call, so unrelated failures don't count against it. And an open breaker doesn't turn the service's health check unhealthy, so a provider outage doesn't cause restarts that wouldn't fix anything.
Failure isolation
Provider errors are caught where they happen. A push failure becomes a FAILED status and a metric, not an exception that kills the listener. One failing channel doesn't stop the others, and one bad event doesn't block the partition.
Retries
The service has no custom retry or backoff for push, email or SMS. A failed send is recorded and left alone. A database write that throws goes back to Kafka's standard consumer error handling, and the idempotent store (Part 4) makes a redelivery safe.
Retries for transient provider failures are the obvious next improvement. They'd need care about duplicates, though, since external sends aren't deduplicated.
Rate limiting
There's no per-user or per-service rate limiter today. The concurrency caps and circuit breakers contain the load. If spam protection ever becomes a requirement, it would be a new layer in front of intake.
Where this leaves us
Stability here comes from putting limits on things: capped async work, providers that fail fast, and failure handling that stays local to one send. None of it is fancy. Together it stops one slow dependency from taking the whole service down.
Top comments (0)