AI generated
This project implements a simple 2-service microservice architecture using Python and Flask.
- API Service: Acts as a gateway for clients.
- Worker Service: Handles background job processing asynchronously.
The system demonstrates:
- HTTP communication between services
- Asynchronous job execution using threads
- Basic job state management
- Docker Compose orchestration
Client → API Service → Worker Service → (background processing) ↓ Job State Storage (in-memory) - Receives client requests
- Forwards jobs to worker service
- Retrieves job status and results
- Creates and manages jobs
- Processes jobs asynchronously using threads
- Stores job state in memory
-
Asynchronous job processing (non-blocking API)
-
Job lifecycle states:
pendingrunningdonefailed
-
Simulated workload:
- Fibonacci computation
- Artificial delay (
sleep) - Random failure injection
-
RESTful endpoints
-
Dockerized services
-
Client sends a request to API:
POST /job/{number} -
API forwards request to Worker:
POST /process -
Worker:
- Creates a
Job - Starts a background thread
- Returns
job_idimmediately
- Creates a
-
Client polls:
GET /job/{job_id}
POST /job/{number} Response:
{ "status": "taken", "job_id": 1 }GET /job/{job_id} Response:
{ "job_id": 1, "status": "running", "input": 100, "output": null }GET /list-all POST /process Body: { "n": 100 } GET /info Body: { "id": 1 } GET /all Each job contains:
job_idstatusinputoutputinit_timefinished_timemod(for Fibonacci calculation)
docker-compose up --buildcurl -X POST http://localhost:5000/job/100curl http://localhost:5000/job/1curl http://localhost:5000/list-all-
Services communicate using Docker internal DNS:
http://worker:5001 -
Do NOT use
localhostbetween containers -
Worker uses in-memory storage (data is lost on restart)
-
Threading is used for async execution (not production-grade)
- No persistence (no database)
- Not thread-safe under heavy load
- No retry or queue system
- No authentication or validation
- Uses Flask development server
- Add Redis or database for persistence
- Replace threading with a real job queue (Celery, RabbitMQ)
- Add retry and timeout handling
- Implement pagination and filtering
- Add structured logging
- Add health check endpoints
This project demonstrates a minimal but functional microservice system with:
- Service-to-service communication
- Asynchronous processing
- API gateway pattern
- Docker-based deployment
It serves as a foundation for building more advanced distributed systems.