Notification Service is an asynchronous microservice built with FastAPI for managing and delivering notifications across multiple channels (Email, Telegram, In-App). Designed with scalability and high performance in mind, it leverages background task processing and real-time WebSocket communication.
- Multi-Channel Delivery: Seamlessly send messages via Email (SMTP), Telegram (Bot API), and In-App (WebSockets).
- Asynchronous Architecture: Fully async implementation using
FastAPI,asyncpg, andSQLAlchemy 2.0for maximum throughput. - Background Processing: Time-consuming tasks (like reaching out to 3rd-party APIs or SMTP servers) are offloaded to Celery workers using Redis as a message broker.
- Real-Time Updates: Clients instantly receive notification statuses via WebSockets powered by Redis Pub/Sub for cross-worker event broadcasting.
- Secure Authentication: JWT-based authentication (access tokens) and secure password hashing (
bcrypt). - Templating Engine: Create and manage dynamic notification templates using Jinja-like variables (
{{ username }},{{ user_id }}). - Containerized: Ready for deployment with Docker and Docker Compose, wrapping the API, Celery worker, PostgreSQL, and Redis in a single isolated environment.
- Backend Framework: FastAPI, Pydantic
- Database & ORM: PostgreSQL, SQLAlchemy 2.0 (async), Alembic (Migrations)
- Task Queue & Cache: Celery, Redis
- Real-time: WebSockets, Redis Pub/Sub
- Testing: Pytest, pytest-asyncio, HTTPX
- Deployment: Docker, Docker Compose
graph TB
Client["Client (HTTP)"] -->|POST /notifications/send| API["FastAPI"]
WS_Client["Client (WS)"] <-->|WebSocket| API
API -->|"Save to DB (status=pending)"| DB[(PostgreSQL)]
API -->|"Publish Task"| Broker["Redis (Broker)"]
Broker --> Worker["Celery Worker"]
Worker -->|Email| SMTP["SMTP Server"]
Worker -->|Telegram| TG["Telegram Bot API"]
Worker -->|In-App| RedisPub["Redis Pub/Sub"]
RedisPub -->|"Push Event"| API
API -->|"WebSocket Push"| WS_Client
Worker -->|"Update Status"| DB
-
Clone the repository
git clone https://github.com/your-username/notification-service.git cd notification-service -
Set up Environment Variables Create a
.envfile based on the template (provide your DB credentials, SMTP, and Telegram tokens). -
Run with Docker Compose
docker-compose up --build -d
The API will be available at
http://localhost:8000. -
Apply Database Migrations (if not run automatically)
docker exec -it notify-api alembic upgrade head
Interactive documentation available at http://localhost:8000/docs (Swagger UI).
- Auth:
POST /auth/register,POST /auth/login - Templates:
GET /templates/,POST /templates/,GET /templates/{id},PATCH /templates/{id},DELETE /templates/{id} - Notifications:
POST /notifications/send- Schedule a notification (background task)GET /notifications/- History & PaginationGET /notifications/stats- Delivery statisticsGET /notifications/{id}- Specific notification status
- WebSocket:
ws://localhost:8000/ws/notifications- Connect to receive live updates.
The project includes a comprehensive test suite using pytest and an in-memory SQLite database to mock services and test business logic without external dependencies.
pytest tests/ -v