Microservices Architecture — DevOps

Microservices split an application into small services that stand on their own, each with its own code, process and data, talking through REST, gRPC or a…

Someone in the meeting suggests: "To make it easy to scale, let's just split notes-api into microservices." It sounds modern. Before you agree, you need to know what you get and what you pay for it.

Microservices is a way of building an application as several small services that stand on their own. Each service has its own code, process and data, is deployed on its own, and talks to the other services over the network. The opposite is a monolith: one application, one process, one deploy. Picture the difference between a small eatery and a food court. At a small eatery, one seller prepares your whole order from one stock of ingredients. In a food court, the rice, the satay and the iced tea come from three different stalls, each with its own stock, and a waiter takes your order to the right stall. In this picture, the small eatery is the monolith and each stall is one service. The stock is the data, your order is one request, and the waiter is the API gateway (covered below). Your order is only complete when the slowest stall is done, and if the satay stall is closed, part of your order fails. These two properties, waiting for the slowest and failing in part, are what people forget most often when they split an application.

Monolith: notes-api today

notes-api is a monolith. One Express process on web-1 holds every module (part of the code): login, notes, attachments, PDF export. They all use one Postgres database, notes. The page for one note is built inside that same process. Three functions are called and one JOIN query (it combines several tables) goes to Postgres. It is all done in about 12 ms.

Split it: one request becomes many calls

The microservices version of notes-api has an API gateway in front: a single entry point that forwards each request to the right service. Behind it are four services. The page GET /notes/12 now needs four network calls, one after another:

Order Call Latency + network Done at
1 auth-svc checks the token 15 ms 2 ms 17 ms
2 notes-svc gets note 12 30 ms 2 ms 49 ms
3 attachments-svc lists the attachments 40 ms 2 ms 91 ms
4 profile-svc the author's profile 20 ms 2 ms 113 ms

113 ms, where the monolith took 12 ms. And each call can fail on its own: a timeout (the wait took too long), a service in the middle of a deploy, or a network drop.

One slow service, one slow request

With calls in a row, the total time is the sum of every call. If attachments-svc slows down to 1,200 ms, the page becomes 17 + 32 + 1,202 + 22 = 1,273 ms (each number already includes the 2 ms of network). One slow service makes the whole request slow.

After auth, the three other calls don't need each other's results, so all three can run in parallel (at the same time) with Promise.all. The total becomes auth plus the slowest call:

attachments-svc In a row In parallel
40 ms (normal) 113 ms 17 + 42 = 59 ms
1,200 ms (slow) 1,273 ms 17 + 1,202 = 1,219 ms

Parallel helps while everything is normal, but it doesn't help when one service is slow: the request still waits for the slowest. The more services one request touches, the more often one of them is slow. Suppose each service is slow 1% of the time, and their slow moments are not connected. A request that touches 4 services is only fast when all four are fast (0.99⁴ ≈ 96.1%), so it is slow 1 - 0.99⁴ ≈ 3.9% of the time.

Timeouts, fallbacks and retries

The cure: never wait forever. Give every call a timeout (a limit on how long to wait), and prepare a fallback (a stand-in answer once the limit passes). This is the core of the code in the Lab. The services are fake (made with setTimeout), but their latencies match the table:

const LATENCY = { auth: 15, notes: 30, attachments: 1200, profile: 20 };
const HOP = 2;

function call(service) {
  // In production: fetch to http://attachments-svc/... with AbortSignal.timeout
  return new Promise((resolve) => setTimeout(() => resolve({ service }), LATENCY[service] + HOP));
}

function withTimeout(service, ms, fallbackValue) {
  const limit = new Promise((resolve) => setTimeout(() => resolve(fallbackValue), ms));
  return Promise.race([call(service), limit]);
}

async function notePage() {
  const user = await call("auth");
  const [notes, attachments, profile] = await Promise.all([
    call("notes"),
    withTimeout("attachments", 300, { fallback: [] }),
    call("profile"),
  ]);
  return { user, notes, attachments, profile };
}

A retry (trying again) sounds safe, but be careful. With 2 retries there are 3 attempts, and each one ends at the 300 ms limit. So every page waits 17 + 3 × 300 = 917 ms, and attachments-svc, already overloaded, receives three times the requests. Retry only requests that are safe to repeat (GET, or idempotent ones), a limited number of times, with a growing pause between attempts (backoff).

Each service owns its data

In microservices, each service owns its data. The attachments table may only be read and written by attachments-svc. When notes-svc needs attachment data, notes-svc has to ask through the API; it may not read the attachments database directly.

How services talk

API gateway: one way in

The gateway keeps a list of path prefixes (the start of a path), each with its service address. For each request it picks the longest prefix that matches, then forwards the rest of the path:

Request Matching prefix Forwarded to
/api/orders/123 /api/orders (not /api, because /api/orders is longer) http://orders:3002 with path /123
/auth/login /auth http://auth:3001 with path /login
/unknown none error no route
/authors none: /auth only matches up to a segment boundary error no route

A prefix matches whole path segments (the parts between two / signs). /auth takes /auth and /auth/login, but not /authors. So the gateway's code checks path === prefix or path.startsWith(prefix + "/"), not startsWith(prefix) alone. In the exercise, you write exactly this in routeRequest(gateway, path): return { service, path } from the longest prefix, or { error: "no route" }. If nothing is left of the path, path is "/".

When to split, and when not to

Almost every successful microservices system started as a monolith (monolith first). The boundaries between services only become visible once the application is in use, and moving a wrong boundary costs far more in microservices. Split when there is a real reason: many teams waiting for each other to deploy, one part needing very different scaling, or a domain (an area of the business, such as payments) that is clearly separate.

For notes-api, with a small team and one server, the decision is: don't split. Tidy the monolith into modules. If PDF export gets heavy, move it to a background worker (a separate process that works in the background) through a queue. The design discussion goes deeper in the System Design track, in the Microservices vs Monolith lesson.

⚠️ Split the app, get new problems

TL;DR: Microservices split an application into small services that stand on their own, each with its own code, process and data, talking through REST, gRPC or a message queue. The opposite is a monolith: one application, one process, one deploy. The price: one request becomes many network calls, and a request always waits for the slowest service. In the Lab, attachments-svc (the attachments service) is made slow: 1,200 ms. The note page that calls it then takes 1,273 ms with calls in a row and 1,219 ms with calls in parallel. With a 300 ms timeout (a limit on how long to wait) plus a fallback (a stand-in answer), it takes 317 ms. Retries (trying again) multiply the load, so limit them and add backoff (a growing pause). Each service owns its data, and sharing a database means a distributed monolith (the hassle of microservices without their freedom). An API gateway (the single entry point) picks the longest path prefix that matches and forwards the rest of the path. Start with a monolith, and split only when teams, scaling or the domain (an area of the business) truly demand it. notes-api doesn't need to be split yet.

⚡ Monolith and microservices

Meaning: A monolith is one application, one process, one deploy. Microservices split an application into small services that stand on their own, each with its own code, process and data, talking to each other over the network.

⚡ One request becomes many calls

Write:

const [notes, attachments, profile] = await Promise.all([
  call("notes"),
  call("attachments"),
  call("profile"),
]);
Guess first: the page for one note first waits for auth-svc (17 ms), then makes the three calls above, each to another service. attachments-svc slows down to 1,200 ms (1,202 ms with the network). With Promise.all (parallel), how long does the page take?

1,219 ms: auth's 17 ms plus the slowest call (1,202 ms). Parallel helps while everything is normal, but the request still waits for the slowest service.

Meaning: Each call is one network call to another service, and Promise.all runs all three at the same time. The monolith builds the same page in about 12 ms. In microservices, each call can also fail on its own.

⚡ Timeouts and fallbacks

Write:

function withTimeout(service, ms, fallbackValue) {
  const limit = new Promise((resolve) => setTimeout(() => resolve(fallbackValue), ms));
  return Promise.race([call(service), limit]);
}

Meaning: withTimeout gives one call a timeout (a limit on how long to wait) of ms. Promise.race returns whichever finishes first: the service's answer, or fallbackValue once the limit passes. With a 300 ms limit on the slow attachments-svc call from the step before, the page takes 17 + 300 = 317 ms and appears without the attachment list. This is called graceful degradation: part of a feature is missing, the page still works. Retries (trying again) add load, so limit them and add a backoff (a growing pause).

⚡ Each service owns its data

Meaning: The attachments table may only be touched by attachments-svc, and the other services ask through the API. If every service still shares one database, the result is a distributed monolith: the hassle of microservices without their freedom. Start with a monolith, and split only when teams, scaling or the domain (an area of the business) truly demand it.

What you will learn