Build a simple backend service for handling product orders using:
-
Python + FastAPI
-
PostgreSQL
-
Redis
-
RabbitMQ
The focus is on backend architecture, correct usage of async processing, caching, and database design. No frontend is required.
Design and implement these tables:
-
id
-
name
-
price
-
stock
-
created_at
-
id
-
user_id
-
status (PENDING, PAID, FAILED)
-
total_price
-
created_at
-
id
-
order_id (FK → orders.id)
-
product_id (FK → products.id)
-
quantity
-
unit_price
Implement the following endpoints:
GET /products
POST /orders
Request body:
{
"user_id": 1,
"items": [
{"product_id": 3, "quantity": 2},
{"product_id": 5, "quantity": 1}
]
}
Response:
-
order_id
-
initial status =
PENDING
GET /orders/{id}
Returns:
-
order details
-
status
-
total_price
-
list of order items
POST /orders/{id}/pay
This endpoint should simulate a payment process and push a message to RabbitMQ for background order processing.
Use Redis for one or more of the following purposes:
-
Cache product list or product stock levels
-
Temporary shopping cart storage (optional)
-
Fast lookup for frequently accessed data
When payment is triggered (POST /orders/{id}/pay):
-
Publish a message to RabbitMQ with the
order_id -
A background worker must consume messages and:
-
Fetch the order from PostgreSQL
-
Validate product stock
-
Deduct stock from products table
-
Calculate total price
-
Update order status:
-
PAIDif successful -
FAILEDif stock is insufficient
-
-
Save final result in database
-
-
Handle race conditions properly when updating stock.
-
Use database transactions and locking where needed (e.g.,
SELECT FOR UPDATE). -
Ensure data consistency even with concurrent order processing.
Create a docker-compose.yml with:
-
FastAPI app
-
PostgreSQL
-
Redis
-
RabbitMQ
-
Worker service
One command should start the whole system.
Provide at least:
-
2–3 unit or integration tests
-
Basic validation tests for error cases (out of stock, invalid product ID)
Include a README.md that explains:
-
How to run the project
-
Example API requests
-
Architecture overview
-
Where Redis and RabbitMQ are used
The candidate will be evaluated based on:
-
Code organization and structure
-
Correct use of Redis (not as a primary DB)
-
Correct use of RabbitMQ for async processing
-
Database schema design
-
Handling concurrency and race conditions
-
Error handling and stability
-
Clean documentation
-
Docker setup quality