As your application grows, a single backend often turns into a tangled mess — harder to scale, test, and deploy.
That’s where microservice architecture comes in.
Instead of one large codebase, you split your app into independent, loosely coupled services — each handling one specific business function (like users, products, or orders).
In this blog, we’ll build a simple microservice-based system using:
-
π’ Node.js + Express — for APIs
-
π MongoDB — as individual service databases
-
π³ Docker Compose — to run them together
π§ What Are Microservices?
Microservices are small, autonomous services that communicate over APIs.
Each service:
-
Has its own database
-
Runs independently
-
Can be scaled or deployed separately
Example: A shopping app might have:
-
π€ User Service → handles signup/login
-
π¦ Product Service → manages items
-
π Order Service → processes purchases
Instead of one app handling all three, you split them like this:
⚙️ Step 1 — Project Setup
Let’s create a folder structure:
Each service is its own Express + MongoDB app.
π€ Step 2 — Creating the User Service
Inside user-service/index.js:
✅ What’s happening:
-
A small API that creates users in a separate database.
-
Runs on port 4001.
π¦ Step 3 — Product Service
product-service/index.js:
Each service has its own database and API — completely independent.
π Step 4 — Order Service & Service Communication
In order-service/index.js:
✅ Key concept:
Services communicate via HTTP. Later, you could upgrade this to message queues (RabbitMQ, Kafka) for better scalability.
π Step 5 — Adding an API Gateway (Optional but Recommended)
Instead of calling each service directly, create an API Gateway.
gateway/index.js:
Now, your frontend can just call:
π³ Step 6 — Dockerize Everything
Create a Dockerfile for each service:
Then a docker-compose.yml at the root:
Run it all with:
π Step 7 — Authentication & Security (Next Steps)
To make this production-ready:
-
Add JWT authentication via a dedicated Auth service.
-
Use service-to-service tokens for internal requests.
-
Secure your databases and API Gateway.
π Step 8 — Testing the System
Use Postman or Thunder Client:
-
Create a user →
POST /users -
Create a product →
POST /products -
Create an order →
POST /orders(include userId + productId) -
Verify responses and linked data
You’ll see independent services working together like a small distributed system.
π§ Conclusion
Congratulations — you just built a microservice architecture using Node.js and MongoDB! π
You learned:
-
How to separate services by domain
-
Communicate using REST APIs
-
Deploy with Docker Compose
-
Use an API Gateway for unified routing
π‘ Tip: For larger projects, explore message queues (RabbitMQ, Kafka), Kubernetes, and service discovery for even more scalable setups.
✍️ Final Thoughts
Microservices aren’t always necessary — for small apps, monoliths are faster to build.
But if you’re planning for growth, scalability, or team collaboration, microservices give you that flexibility.
“Build small, deploy often, scale independently.”
Comments
Post a Comment