Skip to main content

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 TypeSource ServicePurpose
USER_REGISTEREDAuth ServiceWelcome notification
USER_LOGINAuth ServiceLogin alert
ORDER_PLACEDOrder ServiceOrder confirmation
PAYMENT_SUCCESSPayment ServicePayment confirmation
PRODUCT_CREATEDProduct ServiceAdmin notification

๐Ÿ—„๏ธ Data Modelโ€‹

Notification Documentโ€‹

  • id
  • eventType
  • userEmail
  • message
  • timestamp
  • read (boolean)

MongoDB is used for:

  • Flexible schema
  • Fast writes
  • Read-heavy workloads

๐Ÿ” Security Modelโ€‹

  • APIs accessed through API Gateway

  • Identity provided via:

    • X-User-Email
    • X-User-Role

Authorization Rules:โ€‹

  • Users โ†’ fetch only their notifications
  • Admins โ†’ fetch all notifications

๐ŸŒ APIsโ€‹

User APIsโ€‹

MethodEndpointDescription
GET/api/notificationsGet user notifications
PUT/api/notifications/{id}/readMark as read

Admin APIsโ€‹

MethodEndpointRoleDescription
GET/api/notifications/allADMINGet 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 listener
  • NotificationService โ€“ Core logic
  • NotificationRepository โ€“ MongoDB persistence
  • NotificationController โ€“ Fetch APIs
  • GatewayHeaderAuthenticationFilter โ€“ Security
  • GlobalExceptionHandler โ€“ 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.