Add notification-service module: Kafka fan-out by user preference, per-channel templates, rate-limited workers, non-blocking retries with DLT, idempotent delivery (10 tests on embedded Kafka, 9 transcripts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7rAEgiNi4Fo94nVoB
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
# notification-service
|
||||
|
||||
Runnable companion code for ankurm.com's **System Design: Notification Service with Spring Boot and Kafka (Email, SMS, Push)**.
|
||||
One HTTP call becomes a Kafka message; a fan-out step applies the user's preferences and renders a template per channel; one
|
||||
worker per channel delivers it with a rate limit, non-blocking retries and a dead-letter topic. Providers are simulated, and Kafka
|
||||
runs **inside the test JVM** (KRaft via `spring-kafka-test`), so there is nothing to install except JDK 25 and Maven.
|
||||
|
||||
## Versions
|
||||
|
||||
| Component | Version | Verified against |
|
||||
|---|---|---|
|
||||
| JDK | **25.0.5.1+2** (Temurin, LTS) | `java -version` |
|
||||
| Spring Boot | **4.2.0** | `maven-metadata.xml` on Maven Central (GA, no `-M`/`-RC`) |
|
||||
| Spring for Apache Kafka | **4.2.0** | `mvn dependency:tree` |
|
||||
| Apache Kafka clients / embedded broker | **4.4.0** | `mvn dependency:tree` (managed by Boot 4.2.0) |
|
||||
| Jackson | **3.1.8** (`tools.jackson`) | `mvn dependency:tree` |
|
||||
| Maven | 3.9.11 | `mvn -v` |
|
||||
|
||||
## Quickstart
|
||||
|
||||
```bash
|
||||
export JAVA_HOME=/path/to/jdk-25 && export PATH="$JAVA_HOME/bin:$PATH"
|
||||
cd notification-service
|
||||
./scripts/run-all.sh # runs all 10 tests and regenerates every file in output/
|
||||
```
|
||||
|
||||
The first Maven run must be online so the plugins are cached; `-o` works afterwards. The test run prints a lot of
|
||||
embedded-broker shutdown noise (`KafkaStorageException ... NoSuchFileException` while the broker's temp directory is deleted);
|
||||
it is harmless and happens after the tests have passed.
|
||||
|
||||
To run the service against a real broker, start Kafka on `localhost:9092` and `mvn spring-boot:run`, then
|
||||
`POST /api/notifications` with `{"requestId":"r-1","userId":"u-1","templateId":"welcome","params":{"name":"Asha"}}`.
|
||||
The in-memory `UserDirectory` starts empty, so save a `UserProfile` first (the tests show how), otherwise the request is
|
||||
logged as `SKIPPED: unknown user`.
|
||||
|
||||
## How the pieces fit
|
||||
|
||||
| Class | Job |
|
||||
|---|---|
|
||||
| `NotificationController` | `POST /api/notifications` → publish to `notification.requests`, answer `202` |
|
||||
| `FanOutService` | read a request, check preferences and template, publish one rendered `ChannelMessage` per allowed channel |
|
||||
| `TemplateRegistry`, `TemplateEngine` | one template id, one body per channel, `{{placeholder}}` substitution |
|
||||
| `UserDirectory`, `UserProfile` | preferences and contact points (in-memory stand-in for a table) |
|
||||
| `EmailWorker`, `SmsWorker`, `PushWorker` | one `@RetryableTopic` listener per channel, plus the `@DltHandler` |
|
||||
| `ChannelWorker` | shared delivery logic: dedupe, wait for a rate-limit token, call the provider, log |
|
||||
| `TokenBucket`, `RateLimiters` | per-channel limit with a testable clock |
|
||||
| `IdempotencyStore` | remembers delivered `(requestId, channel)` pairs |
|
||||
| `FaultInjector`, `SimulatedProvider` | scripted provider failures, transient or permanent |
|
||||
| `DeliveryLog` | append-only event list that every transcript is built from |
|
||||
|
||||
## Captured output (`output/`)
|
||||
|
||||
Every file is written by a JUnit test that asserts the same facts it prints.
|
||||
|
||||
| File | What it shows |
|
||||
|---|---|
|
||||
| `01-templates.txt` | one template, three channel bodies, missing-parameter error |
|
||||
| `02-token-bucket.txt` | burst of 5 then 20/s, on a fake clock |
|
||||
| `03-fan-out.txt` | preferences, template coverage, unknown user, bad parameters |
|
||||
| `04-retries-and-dlt.txt` | transient failures retried, permanent ones not, outage ends in the DLT |
|
||||
| `05-idempotency.txt` | same `requestId` twice, one delivery |
|
||||
| `06-rate-limit.txt` | 25 SMS against a 20/s limit (timing is indicative) |
|
||||
| `07-retry-reorders.txt` | a retried message is overtaken by a later one |
|
||||
| `08-http-end-to-end.txt` | HTTP `202`, then the deliveries |
|
||||
| `09-topics.txt` | the retry and dead-letter topics Spring Kafka created |
|
||||
|
||||
## What this module deliberately is not
|
||||
|
||||
In-memory preferences, in-memory idempotency and a single JVM: the code shows the shape, not the storage. See the article for what
|
||||
changes in production (a shared idempotency store, a preference service, per-partition limits across instances).
|
||||
Reference in New Issue
Block a user