Skip to content

A comprehensive Spring Boot-based notification system demonstrating enterprise-grade architecture with async processing, multi-channel delivery, and real-time monitoring. Features Kafka message queuing, Redis rate limiting, PostgreSQL persistence, supporting email, SMS, push, and in-app notifications with template processing and deduplication.

Notifications You must be signed in to change notification settings

cipheraxat/notification-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ฌ Notification System

A multi-channel notification system built with Spring Boot, featuring email, SMS, push, and in-app notifications.

๐ŸŒŸ Features

  • Multi-Channel Support: Email, SMS, Push, and In-App notifications
  • Rate Limiting: Token bucket algorithm using Redis
  • Template System: Reusable message templates with variable substitution
  • Async Processing: Kafka-based message queue for reliable delivery
  • Retry Mechanism: Exponential backoff for failed notifications
  • Priority Queue: HIGH, MEDIUM, LOW priority processing
  • Event Deduplication: Prevents duplicate notifications using Redis-backed event ID tracking
  • RESTful API: Well-documented endpoints with Swagger UI

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   API Layer     โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  Service Layer  โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚   Repository    โ”‚
โ”‚  (Controllers)  โ”‚     โ”‚ (Business Logic)โ”‚     โ”‚   (Database)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ–ผ            โ–ผ            โ–ผ
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚  Kafka   โ”‚ โ”‚  Redis   โ”‚ โ”‚ Channels โ”‚
              โ”‚ (Queue)  โ”‚ โ”‚ (Cache)  โ”‚ โ”‚(Handlers)โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Tech Stack

  • Java 17 - Modern LTS version
  • Spring Boot 3.2 - Application framework
  • PostgreSQL 15 - Primary database
  • Redis 7 - Rate limiting & caching
  • Apache Kafka - Message queue
  • Docker Compose - Local development

๐Ÿš€ Quick Start

Prerequisites

  • Java 17+
  • Docker & Docker Compose
  • Maven 3.8+

1. Start Infrastructure

# Start PostgreSQL, Redis, Kafka
docker-compose up -d

2. Run the Application

# Using Maven
./mvnw spring-boot:run

# Or build and run JAR
./mvnw clean package
java -jar target/notification-system-1.0.0.jar

3. Access the API

๐Ÿ“š API Endpoints

Notifications

Method Endpoint Description
POST /api/v1/notifications Send a notification
POST /api/v1/notifications/bulk Send bulk notifications
GET /api/v1/notifications/{id} Get notification by ID
GET /api/v1/notifications/user/{userId} Get user's notifications
GET /api/v1/notifications/user/{userId}/unread-count Get unread count
PATCH /api/v1/notifications/{id}/read Mark as read
PATCH /api/v1/notifications/user/{userId}/read-all Mark all as read

๐Ÿ›ก๏ธ Event Deduplication

The notification system implements a robust event deduplication mechanism to prevent duplicate notifications from being sent, even in distributed environments where network issues or retries might cause the same event to be processed multiple times.

How It Works

  1. Event ID Tracking: Clients can provide a unique eventId in notification requests
  2. Redis-Based Storage: Event IDs are stored in Redis with a configurable TTL (24 hours by default)
  3. Duplicate Detection: Before processing any notification, the system checks if the event ID has been seen recently
  4. Graceful Handling: If a duplicate is detected, the request is discarded with a clear error response

API Usage

Include an eventId in your notification request:

curl -X POST http://localhost:8080/api/v1/notifications \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "550e8400-e29b-41d4-a716-446655440001",
    "channel": "EMAIL",
    "eventId": "order-confirmation-12345",
    "subject": "Order Confirmed",
    "content": "Your order has been confirmed!"
  }'

Benefits

  • Idempotent Operations: Same event ID sent multiple times results in only one notification
  • Distributed Safety: Works across multiple application instances
  • Configurable TTL: Event IDs automatically expire to prevent memory bloat
  • Backward Compatible: eventId is optional - existing clients continue to work
  • Fast Lookups: Redis provides O(1) lookup performance

Configuration

notification:
  dedupe:
    ttl-seconds: 86400  # 24 hours

Implementation Details

  • Service: DeduplicationService handles event tracking
  • Storage: Redis keys follow pattern event:{eventId}
  • TTL: Configurable expiration prevents infinite growth
  • Thread-Safe: Redis operations are atomic
  • Monitoring: Logs duplicate detection for observability

Templates

Method Endpoint Description
POST /api/v1/templates Create template
GET /api/v1/templates Get all templates
GET /api/v1/templates/{id} Get template by ID
GET /api/v1/templates/name/{name} Get template by name
PUT /api/v1/templates/{id} Update template
DELETE /api/v1/templates/{id} Delete template

๐Ÿ“ Example Requests

Send Email Notification

curl -X POST http://localhost:8080/api/v1/notifications \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "550e8400-e29b-41d4-a716-446655440001",
    "channel": "EMAIL",
    "priority": "HIGH",
    "subject": "Welcome!",
    "content": "Hello! Welcome to our platform."
  }'

Send Using Template

curl -X POST http://localhost:8080/api/v1/notifications \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "550e8400-e29b-41d4-a716-446655440001",
    "channel": "EMAIL",
    "templateName": "welcome-email",
    "templateVariables": {
      "userName": "John"
    }
  }'

Create Template

curl -X POST http://localhost:8080/api/v1/templates \
  -H "Content-Type: application/json" \
  -d '{
    "name": "order-shipped",
    "channel": "EMAIL",
    "subjectTemplate": "Your order #{{orderId}} has shipped!",
    "bodyTemplate": "Hi {{userName}}, your order is on the way!"
  }'

๐Ÿ“Š Database Schema

-- Core tables
users                    -- User information
user_preferences         -- Per-channel preferences
notification_templates   -- Reusable templates
notifications           -- Main notification table

โš™๏ธ Configuration

Key settings in application.yml:

notification:
  rate-limit:
    email: 10     # per hour
    sms: 5        # per hour
    push: 20      # per hour
    in-app: 100   # per hour
  dedupe:
    ttl-seconds: 86400  # 24 hours for event ID tracking
  retry:
    max-attempts: 3
    initial-delay: 60s
    multiplier: 5

๐Ÿงช Testing

# Run all tests
./mvnw test

# Run with coverage
./mvnw test jacoco:report

๐Ÿ“ˆ Back-of-Envelope Calculations

See plan.md for detailed capacity planning including:

  • 10M notifications/day capacity
  • ~116 notifications/second peak
  • Storage and scaling estimates

๐Ÿ—‚๏ธ Project Structure

src/main/java/com/notification/
โ”œโ”€โ”€ config/          # Configuration classes
โ”œโ”€โ”€ controller/      # REST controllers
โ”œโ”€โ”€ dto/             # Request/Response DTOs
โ”œโ”€โ”€ exception/       # Custom exceptions
โ”œโ”€โ”€ kafka/           # Kafka consumer
โ”œโ”€โ”€ model/           # Entity classes
โ”œโ”€โ”€ repository/      # JPA repositories
โ”œโ”€โ”€ scheduler/       # Scheduled jobs
โ””โ”€โ”€ service/         # Business logic
    โ””โ”€โ”€ channel/     # Channel handlers

๐Ÿ”ฎ Future Enhancements

  • Add authentication (OAuth2/JWT)
  • Implement webhooks for delivery status
  • Add support for message scheduling
  • Implement multi-tenancy
  • Add metrics with Prometheus/Grafana
  • Support for attachments (email)
  • A/B testing for templates

๐Ÿ“„ License

MIT License - feel free to use for your projects!

About

A comprehensive Spring Boot-based notification system demonstrating enterprise-grade architecture with async processing, multi-channel delivery, and real-time monitoring. Features Kafka message queuing, Redis rate limiting, PostgreSQL persistence, supporting email, SMS, push, and in-app notifications with template processing and deduplication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •