Contents
ecommerce-microservice splits a small shop backend into three Flask services, with nginx in front and Docker Compose to run them. The services are user-auth-service, product-management-service and order-processing-service. Each is one routes file, one model and one SQLite database, so the project is exercise-sized.
That is a good size for seeing what a split forces you to decide. A monolith answers three questions without being asked: how a request finds its handler, who the caller is, and where the data lives. Here each one needs an answer in config or code.
One door, three prefixes
server {
listen 80;
location /auth {
proxy_pass http://user-auth-service:5001;
proxy_set_header Host $host;
}
location /products {
proxy_pass http://product-management-service:5002;
}
location /orders {
proxy_pass http://order-processing-service:5003;
}
}I trimmed the repeated proxy_set_header lines. Compose maps host port 8000 to nginx's port 80, and the service names resolve on the Compose network. Because proxy_pass has no URI part, nginx sends the path on exactly as the client wrote it.
That works for products and orders, because the Flask routes are /products and /orders. It does not work for auth. The routes there are /user/register and /user/login, so a request to /auth/... reaches Flask with a prefix nothing handles, and /user/login matches no nginx location at all. As written, login only works on port 5001 directly. A fix is location /auth/ with proxy_pass http://user-auth-service:5001/user/;, which swaps the prefix.
Each service owns one table
| Service | Database file | Table | Token |
|---|---|---|---|
| user-auth-service | users.db | User | issues it |
| product-management-service | products.db | Product | not checked |
| order-processing-service | orders.db | Order | verifies it |
Each config.py has its own SQLALCHEMY_DATABASE_URI, and no service opens another's file. The order model shows what that does to relationships:
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, nullable=False)
product_id = db.Column(db.Integer, nullable=False)
quantity = db.Column(db.Integer, nullable=False)
status = db.Column(db.String(50), default='Pending')user_id and product_id are plain integers. There is no foreign key, and there cannot be one across separate databases. The order route stores whatever product_id it is sent, so nothing checks the product exists or reduces its stock.
No service calls another
No Python file in the repo makes an HTTP request, yet orders still know who the user is. The auth service signs a token at login with create_access_token(identity=user.id). The order service holds the same secret, so it can check the signature itself.
user-auth-service:
environment:
- JWT_SECRET_KEY=myjwtsecretkey
order-processing-service:
environment:
- JWT_SECRET_KEY=myjwtsecretkey@app.route("/orders", methods=["GET", "POST"])
@jwt_required()
def manage_orders():
user_id = get_jwt_identity()
if request.method == "GET":
orders = Order.query.filter_by(user_id=user_id).all()@jwt_required() verifies the token and get_jwt_identity() returns the user id, with no network hop. The single-order lookup filters on id and user_id together, so another user's order comes back as "not found".
What breaks first
- The tables.
db.create_all()appears only in the tests, which use an in-memory database. The README mentionsflask db init, but Flask-Migrate is in norequirements.txt, and each Dockerfile just runsflask run. I find no step that creates the tables in the containers. - The product service. It has no JWT setup, so anyone who can reach it can create, edit and delete products.
- The secrets.
config.pyfalls back tomysecretkeyandmyjwtsecretkeywhen the environment is empty, and Compose sets the same values in plain text. - One SQLite file per service. It suits one container each. A second replica of the order service would need a database server.
- Retries. There are no service-to-service calls, so there are no timeouts or retries either. The first call you add, such as an order asking the product service for stock, brings that whole problem with it.
When this split makes sense
For an app this size, one Flask app with three blueprints would be simpler, and I would start there. As a practice ground it is useful, because the three decisions above are all visible in a few short files. What carries over is prefix routing at the proxy, a token verified locally and references by id. The next changes I would make are the nginx prefix for auth, a token check on product writes and a step that creates the tables.
Found this useful? Share it with your team.