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:
Claude
2026-11-24 00:30:04 +05:30
parent ce12a80f58
commit 09f875ddd9
54 changed files with 1516 additions and 0 deletions
@@ -0,0 +1,12 @@
Template rendering: one template id, one body per channel
=========================================================
EMAIL ( 61 chars) Hi Asha, your order A-1042 has shipped and arrives on 27 Nov.
SMS ( 33 chars) Order A-1042 shipped. ETA 27 Nov.
PUSH ( 32 chars) Asha, order A-1042 is on its way
A template with no body for a channel never goes out on it (otp has only SMS):
otp channels: [SMS]
A missing parameter is an error, not an empty string:
MissingParameterException: no value for {{eta}}
@@ -0,0 +1,17 @@
Token bucket: 20 tokens/second, burst 5, fake clock
===================================================
10 sends requested at the same instant (t = 0):
send 1 waits 0 ms
send 2 waits 0 ms
send 3 waits 0 ms
send 4 waits 0 ms
send 5 waits 0 ms
send 6 waits 50 ms
send 7 waits 100 ms
send 8 waits 150 ms
send 9 waits 200 ms
send 10 waits 250 ms
After the queue drains and the clock idles for 10 seconds the bucket refills, but only to the burst size:
next 7 sends wait (ms): [0, 0, 0, 0, 0, 50, 100]
@@ -0,0 +1,31 @@
Fan-out: one request in, one message per allowed channel out
============================================================
1. user opted in to all three, template has all three (order-shipped)
EMAIL FANNED_OUT notification.requests publishing to notify.email
EMAIL SENT notify.email attempt 1
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS SENT notify.sms attempt 1
PUSH FANNED_OUT notification.requests publishing to notify.push
PUSH SENT notify.push attempt 1
2. user has a phone number but has not opted in to SMS
EMAIL FANNED_OUT notification.requests publishing to notify.email
EMAIL SENT notify.email attempt 1
SMS SKIPPED notification.requests user has not opted in
PUSH FANNED_OUT notification.requests publishing to notify.push
PUSH SENT notify.push attempt 1
3. otp template only has an SMS body, so only SMS goes out
EMAIL SKIPPED notification.requests template otp has no EMAIL body
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS SENT notify.sms attempt 1
PUSH SKIPPED notification.requests template otp has no PUSH body
4. unknown user
- SKIPPED notification.requests unknown user u-ghost
5. params missing: every channel skipped, nothing half-rendered is sent
EMAIL SKIPPED notification.requests no value for {{orderId}}
SMS SKIPPED notification.requests no value for {{orderId}}
PUSH SKIPPED notification.requests no value for {{orderId}}
@@ -0,0 +1,21 @@
Retries and the dead-letter topic (SMS channel, 4 attempts, 250 ms doubling)
============================================================================
1. provider times out twice, then works
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS ATTEMPT_FAILED notify.sms attempt 1: TransientProviderException
SMS ATTEMPT_FAILED notify.sms-retry-250 attempt 2: TransientProviderException
SMS SENT notify.sms-retry-500 attempt 3
2. provider says the number is invalid (permanent): no retries
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS ATTEMPT_FAILED notify.sms attempt 1: PermanentProviderException
SMS DEAD_LETTERED notify.sms-dlt needs a human or a replay tool
3. provider is down for the whole window: all attempts used
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS ATTEMPT_FAILED notify.sms attempt 1: TransientProviderException
SMS ATTEMPT_FAILED notify.sms-retry-250 attempt 2: TransientProviderException
SMS ATTEMPT_FAILED notify.sms-retry-500 attempt 3: TransientProviderException
SMS ATTEMPT_FAILED notify.sms-retry-1000 attempt 4: TransientProviderException
SMS DEAD_LETTERED notify.sms-dlt needs a human or a replay tool
@@ -0,0 +1,10 @@
Idempotency: the same requestId published twice
===============================================
one request, published twice, user opted in to EMAIL and PUSH (not SMS)
FANNED_OUT SENT DUPLICATE
EMAIL 2 1 1
PUSH 2 1 1
The provider was called once per channel; the second copy was recognised and dropped.
@@ -0,0 +1,8 @@
Per-channel rate limit: 25 SMS at once, gateway allows 20/s with burst 5
========================================================================
messages sent : 25
rate limit : 20 per second, burst 5
theoretical minimum : (25 - 5) / 20 = 1.0 s
assertion : elapsed >= 900 ms
elapsed (indicative) : about 1.3 s (varies from run to run)
@@ -0,0 +1,14 @@
Ordering: Kafka orders a partition, retry topics do not
=======================================================
published order : first, second (same user, so same key and same partition)
delivered order : second, first
first (one transient failure)
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS ATTEMPT_FAILED notify.sms attempt 1: TransientProviderException
SMS SENT notify.sms-retry-250 attempt 2
second (no failures)
SMS FANNED_OUT notification.requests publishing to notify.sms
SMS SENT notify.sms attempt 1
@@ -0,0 +1,14 @@
HTTP in, Kafka in the middle, providers at the end
==================================================
POST /api/notifications
{"requestId":"<requestId>","userId":"<userId>","templateId":"welcome","params":{"name":"Asha"}}
HTTP 202
{"requestId":"<requestId>"}
what happened afterwards
EMAIL FANNED_OUT notification.requests publishing to notify.email
EMAIL SENT notify.email attempt 1
SMS SKIPPED notification.requests user has not opted in
PUSH FANNED_OUT notification.requests publishing to notify.push
PUSH SENT notify.push attempt 1
+19
View File
@@ -0,0 +1,19 @@
Topics that exist after startup (internal topics hidden)
========================================================
notification.requests
notify.email
notify.email-dlt
notify.email-retry-1000
notify.email-retry-250
notify.email-retry-500
notify.push
notify.push-dlt
notify.push-retry-1000
notify.push-retry-250
notify.push-retry-500
notify.sms
notify.sms-dlt
notify.sms-retry-1000
notify.sms-retry-250
notify.sms-retry-500