##Architecture diagram:
By Noble Mutuwa Mulaudzi - DevOps Engineer
Introduction
Microservices architecture has become the go-to approach for building scalable, resilient, and maintainable applications. In this article, I'll walk you through how I designed and implemented a microservices system using:
- Keycloak for authentication
- Apache Kafka as the message bus
- Django for the backend services
- React for the frontend
- Docker to containerize everything and run it locally
The system consists of four microservices:
- User Management - Handles authentication and user profiles
- Appointment - Manages appointment scheduling
- Analytics - Collects system usage data
- Notification - Sends notifications based on events
Why Microservices?
Traditional monolithic applications often struggle with scalability and maintainability. Microservices, on the other hand, offer:
- Scalability – Independent services can scale separately.
- Resilience – Failure in one service doesn’t bring down the entire system.
- Flexibility – Services can be developed, deployed, and maintained independently.
The Tech Stack
- Django – Backend framework for each microservice
- React – Frontend application
- Keycloak – Centralized authentication and authorization
- Apache Kafka – Message broker for event-driven communication
- PostgreSQL – Primary database
- Docker – Every service, including Keycloak, Kafka, and the frontend, is containerized
Microservices Breakdown
1. User Management Microservice
- Handles user profiles and authentication
- Uses Keycloak for issuing JWT tokens
2. Appointment Microservice
- Manages user appointments
- Emits
appointment-events
when an appointment is created
3. Analytics Microservice
- Collects system-wide usage data
- Consumes
appointment-events
from Kafka - Stores aggregated data for reporting
4. Notification Microservice
- Listens for
appointment-events
- Sends email to users
Event-Driven Communication with Kafka
Kafka enables asynchronous communication between services. The architecture follows a producer-consumer pattern:
- Producers – Each microservice publishes domain events.
- Consumers – Other services listen to these events.
-
Topics – Organize event streams (
appointment-events
,).
Example: Appointment Creation Event Flow
- A user books an appointment through the Appointment microservice.
- The Appointment service emits an
appointment.created
event to Kafka. - The Analytics service listens to this event and logs the new appointment for reporting.
- The Notification service listens to the same event and sends a confirmation email.
Authentication with Keycloak
Keycloak provides:
- User Management – Self-registration, social login, password resets
- Role-Based Access Control (RBAC) – Services enforce authorization based on user roles
- Token-based Authentication – JWT tokens for secure API communication
Each microservice validates JWT tokens before processing incoming requests.
Running the System Locally
1. Clone the Repository
The full project is available on GitHub. Clone it using:
git clone https://github.com/Mutuwa99/microservice-kafka.git
cd microservice-kafka
2. Run Keycloak
Navigate to the stack/
directory and start Keycloak using:
cd stack
docker-compose -f keycloak-docker-compose.yml up -d
- Navigate to localhost:7080/
- Use 'admin' as the username and passowrd to login
- Create a realm called 'myrealm' and enable it
- Navigate to the 'myrealm' and click clients and create
- Fill in client id and name as 'user-management'
- Set Client authentication and Authorization to on
- Copy the client id and name for future use in the microservices
3. Run Apache Kafka
Still in the stack/
directory, start Kafka by running:
docker-compose -f kafka-docker-compose.yml up -d
- Navigate to localhost:8080/ to see the kafka site
4. Run All Microservices
NB: before running this ensure :
- You navigate to notification service and replace :
EMAIL_HOST_USER = '' # Replace with your Gmail address
EMAIL_HOST_PASSWORD = '' # Replace with the app password you generated
- Navigate to all the microservices folders , open their settings.py and replace the 'KEYCLOAK_CLIENT_SECRET' with the secret you got while setting up your client in keycloak
To start all services, run:
docker-compose -f services-docker-compose.yml up -d
Our backend django service are running on these urls:
Usermanagement:
localhost:8000/admin/
Appointment service:
localhost:10000/admin/
Notification service:
localhost:9000/admin/
Analytics service:
localhost:8001/admin/
our frontend app is running on: localhost:3000/
- Login with the user you created on keycloak
Conclusion
Building microservices with Django, Keycloak, and Kafka provides a scalable and maintainable solution. By using Docker to containerize everything, you can run the entire system locally with ease.
What are your thoughts on microservices? Have you used Django, Keycloak, or Kafka in your stack? Let’s discuss in the comments! 🚀
This version includes the GitHub repository reference and the exact commands to start each component. Let me know if you want any additional changes! 🚀🔥
What are your thoughts on microservices? Have you used Django, Keycloak, or Kafka in your stack? Let’s discuss in the comments! 🚀
Top comments (0)