CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

index.js
// Import express, create app and define breaker
// write here

// Define the "/api/users" gateway route
// write here

// Print "System ready"
// write here

Sample solution

index.js
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");

Expected output

System ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the gateway + breaker skeleton combine?

index.js
app.use("/api/users", ...) + new CircuitBreaker()
  • Only require
  • API gateway and circuit breaker Correct answer
  • Only console.log
  • Only a registry

Explanation: Gateway + breaker combine.

2 Match each tool to its role

Term Meaning
app.use Defines gateway routing
CircuitBreaker Isolates a failing service
Map Used as a registry
fetch Sends a service call

All lessons in Advanced