Post 1 of 15 | Phase 1: Foundation
What is a Monolith and Why Does It Become a Problem?
You have built MERN or MEAN applications. In those apps, one Express server handles everything — user registration, login, products, orders, payments, emails. This is called a monolith. One big codebase, one process, one deployment.
This works fine when your app is small. The problem starts when your app grows.
Imagine one person in a restaurant doing every job — taking orders, cooking food, handling billing, washing dishes. Five customers? Fine. Five hundred customers? Everything slows down and breaks.
Your Express monolith has the same problem at scale. If one part of the code crashes or slows down, it affects the entire application.
What is Microservices?
Microservices means splitting that one big server into many small, independent services. Each service does one job only.
Using the restaurant example:
- The waiter only takes orders
- The chef only cooks
- The cashier only handles payment
- Each person works independently
If the cashier is sick, the chef still cooks. No single point of failure.
In code terms, instead of one Express app doing everything, you have:
- user-service, handles only user registration and login
- order-service, handles only orders
- payment-service, handles only payments
- email-service, handles only sending emails
Each service runs as a separate Node.js process. They communicate with each other over a network.
What is Moleculer?
Moleculer is a framework for Node.js that makes building microservices simple and structured.
Think of Moleculer as the walkie-talkie system in that restaurant. Without it, every service has to manually find and call other services using HTTP, handle failures, retry logic, load balancing — all from scratch. Moleculer takes care of all of that for you.
The latest stable version is 0.15.0. You install it with a single command.
npm install moleculer
Why Not Just Use Express with HTTP Calls Between Services?
You could. But you would have to manually build:
- Service discovery (how does service A find service B's address?)
- Load balancing (if service B has 3 instances, which one to call?)
- Retry logic (what if service B is temporarily down?)
- Caching
- Tracing and logging across services
Moleculer gives you all of this out of the box. You focus on business logic.
Five Core Terms You Must Know
Everything in Moleculer is built around these five concepts. Learn these first and the rest of the course will make sense.
ServiceBroker The central manager of your Moleculer application. It starts all services, manages communication between them, and handles discovery. You create one broker per Node.js process.
Service A single unit responsible for one domain. For example, a user-service or an order-service. A service has a name and contains actions and/or events.
Action A callable function inside a service. Similar to a REST endpoint, but internal. For example, user.create or order.list. Other services call actions to get a result back.
Event A broadcast message. One service emits an event, and any other service that is interested can listen and react. Unlike actions, events do not return a response. Think of it like a group notification.
Transporter The communication layer used when services run on different machines. Examples are NATS, Redis, and Kafka. For local development with everything on one machine, no transporter is needed.
What Moleculer Code Looks Like
Do not worry about understanding every detail here. Just read it and get a feel for the structure.
const { ServiceBroker } = require("moleculer");
// Create the broker (the manager) const broker = new ServiceBroker();
// Create a service broker.createService({ name: "math", actions: { add(ctx) { return ctx.params.a + ctx.params.b; } } });
// Start the broker and call the action broker.start() .then(() => broker.call("math.add", { a: 5, b: 3 })) .then(result => console.log("Result:", result)); // Output: Result: 8
Notice there is no req, no res, no route definition. The action is just a function that receives a context object and returns a value. Moleculer handles how it gets called.
Comparing Express vs Moleculer
Express approach in a monolith:
app.get("/math/add", (req, res) => { const result = Number(req.query.a) + Number(req.query.b); res.json({ result }); });
Moleculer approach as a service:
broker.createService({ name: "math", actions: { add(ctx) { return ctx.params.a + ctx.params.b; } } });
The Moleculer action has zero knowledge of HTTP. It is pure business logic. You expose it over HTTP later using the API Gateway module, which is covered in Phase 6 of this course.
What You Will Build by the End of This Course
A mini e-commerce backend with the following services:
- API Gateway, the only service exposed to the internet via HTTP
- user-service, handles registration, login, profile
- order-service, handles creating and listing orders
- payment-service, handles payment processing
All services communicate through Moleculer internally. MongoDB is used for data storage.
Full Course Roadmap
Phase 1 - Foundation Post 1: What is microservices and why Moleculer (this post) Post 2: Installation and your first working service
Phase 2 - Core Concepts Post 3: ServiceBroker in depth Post 4: Services and Actions Post 5: Events
Phase 3 - Communication Post 6: The Context object Post 7: Transporters and multi-node setup
Phase 4 - Fault Tolerance Post 8: Circuit Breaker, Retry, Timeout, Bulkhead
Phase 5 - Caching Post 9: Built-in caching with Memory and Redis
Phase 6 - API Gateway Post 10: Exposing services over HTTP with moleculer-web Post 11: REST API design with Moleculer
Phase 7 - Database Post 12: moleculer-db with MongoDB and Mongoose
Phase 8 - Observability Post 13: Logging, Metrics, and Tracing
Phase 9 - Deployment Post 14: Docker and Docker Compose Post 15: Final project — complete e-commerce backend
Summary
- Monolith means one server doing everything. It becomes a bottleneck at scale.
- Microservices means splitting responsibilities into small independent services.
- Moleculer is the framework that handles communication, discovery, and fault tolerance between those services.
- Five terms to remember: ServiceBroker, Service, Action, Event, Transporter.
- You will build a real e-commerce backend by the end of this course.
Up Next
Post 2 covers installation, project setup, and building your first real Moleculer service with full explanation of every line.
No comments:
Post a Comment