Notification Service ๐
The Notification Service is responsible for delivering user-facing notifications in ShopVerse based on domain events emitted by other services.
It is a purely event-driven service and does not participate in synchronous business workflows.
๐ฏ Responsibilitiesโ
The Notification Service handles:
- Consuming domain events from Kafka
- Creating user notifications
- Persisting notifications
- Fetching notifications for users and admins
- Marking notifications as read
๐ง Why a Separate Notification Service?โ
Notifications are:
- Asynchronous by nature
- Non-critical to core workflows
- Triggered by multiple services
Separating them ensures:
- No impact on core transactions
- Failure isolation
- Easy extensibility (email, SMS, push notifications)
๐๏ธ High-Level Architectureโ
The Notification Service does not expose public mutation APIs.
๐ก Event Consumption Flowโ
๐ฆ Events Consumedโ
| Event Type | Source Service | Purpose |
|---|---|---|
| USER_REGISTERED | Auth Service | Welcome notification |
| USER_LOGIN | Auth Service | Login alert |
| ORDER_PLACED | Order Service | Order confirmation |
| PAYMENT_SUCCESS | Payment Service | Payment confirmation |
| PRODUCT_CREATED | Product Service | Admin notification |
๐๏ธ Data Modelโ
Notification Documentโ
ideventTypeuserEmailmessagetimestampread(boolean)
MongoDB is used for:
- Flexible schema
- Fast writes
- Read-heavy workloads
๐ Security Modelโ
-
APIs accessed through API Gateway
-
Identity provided via:
X-User-EmailX-User-Role
Authorization Rules:โ
- Users โ fetch only their notifications
- Admins โ fetch all notifications
๐ APIsโ
User APIsโ
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/notifications | Get user notifications |
| PUT | /api/notifications/{id}/read | Mark as read |
Admin APIsโ
| Method | Endpoint | Role | Description |
|---|---|---|---|
| GET | /api/notifications/all | ADMIN | Get all notifications |
๐ก๏ธ Idempotency Strategyโ
Kafka events may be delivered more than once.
To handle this:
- Each event contains a unique identifier
- Duplicate checks prevent multiple notifications
- Consumer logic is idempotent
โ ๏ธ Failure Scenariosโ
โ Kafka Consumer Downโ
- Kafka retains events
- Consumer resumes from last offset
โ MongoDB Downโ
- Events are retried
- Failure logged for investigation
โ Duplicate Eventsโ
- Safely ignored due to idempotency checks
โ๏ธ Key Componentsโ
NotificationConsumerโ Kafka listenerNotificationServiceโ Core logicNotificationRepositoryโ MongoDB persistenceNotificationControllerโ Fetch APIsGatewayHeaderAuthenticationFilterโ SecurityGlobalExceptionHandlerโ Error handling
๐ Scalability Considerationsโ
- Stateless consumer instances
- Kafka consumer groups enable horizontal scaling
- MongoDB handles high write throughput
- Read APIs can be cached if needed
๐งช Testing Strategyโ
- Unit tests for message building
- Kafka consumer tests
- Idempotency tests
- Security tests for role enforcement
๐ Summaryโ
The Notification Service provides:
- Asynchronous, reliable notifications
- Zero coupling with core workflows
- Scalable Kafka-based consumption
- Secure access to user data
It enhances user experience without impacting system stability.