Node.js
Summary - Microservices
Combine HTTP communication, gateway and circuit breaker to write a service mesh.
Summary - Microservice
In this group you learned the core patterns of microservice architecture: services communicating over HTTP, a single entry point with an API gateway, dynamic address lookup with service discovery, and failing-service isolation with a circuit breaker.
Now you will combine them into a small gateway + breaker skeleton. The skeleton will serve a /api/users path like an API gateway and hold a CircuitBreaker instance. This mini task shows how the microservice patterns combine.
Gateway + breaker design
The skeleton has two parts: gateway routing with app.use("/api/users", ...) and a breaker instance with new CircuitBreaker(). The gateway can check the breaker state on each request.
Flow: const breaker = new CircuitBreaker(); app.use("/api/users", (req, res) => { if (breaker.state === "open") return res.status(503).json({ error: "Service unavailable" }); res.json({ kaynak: "user-service" }); });. This makes the gateway check the breaker before making a call. If the breaker is open, the request is rejected immediately; no call is made. This pattern is common in real production systems. Logging, retry and metrics can be added on top.
Exercise Build the gateway + breaker skeleton
Import express, create app. Define a simple breaker instance with const breaker = { state: "closed", failures: 0 }. Define app.use("/api/users", (req, res) => res.json({ kaynak: "user-service" })). Print "System ready" with console.log.
Show example solution
const express = require("express");
const app = express();
const breaker = { state: "closed", failures: 0 };
app.use("/api/users", (req, res) => res.json({ kaynak: "user-service" }));
console.log("System ready");
Test yourself
-
What does the gateway + breaker skeleton combine?
app.use("/api/users", ...) + new CircuitBreaker()
- Only require
- API gateway and circuit breaker
- Only console.log
- Only a registry
Show answer
API gateway and circuit breaker — Gateway + breaker combine.
-
Match each tool to its role
- app.use
- Defines gateway routing
- CircuitBreaker
- Isolates a failing service
- Map
- Used as a registry
- fetch
- Sends a service call
Next lesson: What are message queues?