This project implements a notification service as part of a microservices architecture. It leverages Kafka for asynchronous messaging, Redis for caching templates, and Spring Boot for service orchestration. The service ensures efficient delivery of notifications, scalability, and reliability.
- Template Management: Cache notification templates in Redis to reduce database load.
- Contact Management: Consume contact data through Kafka and deliver notifications.
- Rebalancer Service: Ensures all contacts are processed, even in case of failures.
- Asynchronous Messaging: Utilizes Kafka for at-least-once delivery of messages.
- Microservices Design: Decoupled architecture with separate services for templates, contacts, and rebalancing.
- Java 17
- Spring Boot 3.0
- Apache Kafka
- Redis
- PostgreSQL
- Docker & Docker Compose
- Java 17+
- Docker and Docker Compose
- Kafka CLI Tools (optional for manual operations)
- Postman or cURL (for testing APIs)
git clone https://github.com/your-repo/notification-service.git
cd notification-serviceCreate a .env file in the root directory and configure the following:
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/notificationdb
SPRING_DATASOURCE_USERNAME=your_db_username
SPRING_DATASOURCE_PASSWORD=your_db_password
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
REDIS_HOST=localhost
REDIS_PORT=6379Use Docker Compose to start the services, including Kafka, Redis, and PostgreSQL:
docker-compose up --buildRun the following commands to create required topics:
docker exec -it kafka /bin/bash
# Inside Kafka container
kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic templates-topic
kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic contacts-topic
kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic notifications-topicRun the application locally (if needed):
./mvnw spring-boot:run-
Cache Template
POST /templates- Request Body:
{ "id": 1, "name": "Welcome Template", "content": "Hello {{name}}, welcome to our service!" }
-
Fetch Template by ID
GET /templates/{id}
-
Delete Template
DELETE /templates/{id}
-
Consume Contacts
- Contacts are consumed from the Kafka
contacts-topic.
- Contacts are consumed from the Kafka
-
Send Notifications
- Notifications are processed and delivered based on the template and contact data.
The rebalancer ensures reliable processing of contacts by:
- Identifying unprocessed contacts in Kafka or Redis.
- Retrying delivery or marking contacts as failed for manual intervention.
src/main/java/com/azimsh3r/notificationservice/
├── config/ # Kafka and Redis configuration files
├── controller/ # REST controllers for APIs
├── dto/ # Data Transfer Objects (DTOs)
├── service/ # Business logic for templates, contacts, and notifications
├── repository/ # Data access layer
└── utils/ # Utility classes for common operations
- API Service:
- Publishes templates and contacts to Kafka topics (
templates-topicandcontacts-topic).
- Publishes templates and contacts to Kafka topics (
- Notification Service:
- Consumes messages from Kafka.
- Caches templates in Redis for fast access.
- Processes contacts and delivers notifications.
- Rebalancer Service:
- Verifies all contacts were processed and reattempts delivery when needed.
- Kafka: Add more partitions for topics to scale horizontally.
- Redis: Use Redis clusters for distributed caching.
- Kubernetes: Deploy services in Kubernetes for auto-scaling and fault tolerance.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
feature/your-feature). - Commit your changes.
- Push to your branch and create a pull request.
This project is licensed under the MIT License. See LICENSE for more details.
For queries or issues, reach out to:
- Author: azimsh3r
- Email: azimjon.works@gmail.com