Skip to content
Communication Between Services

Lesson 2 of 10 · 22 min

x
2/10

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Synchronous REST APIs

REST over HTTP is the default inter-service protocol. One service sends an HTTP request to another's endpoint and waits for a JSON response. It is human-readable, well-understood, and works through firewalls and load balancers without special tooling.

The trade-offs are real. HTTP/1.1 carries overhead per request. JSON serialization is slower than binary formats. The caller blocks until the response arrives — if the downstream service is slow or down, the caller's thread (or connection) is tied up. For internal service-to-service calls where you control both ends, REST is a solid starting point. Add timeouts, retries with backoff, and circuit breakers before you hit production traffic.

Before
No timeout — hangs forever on failure
1const response = await fetch(2  'http://inventory-service/api/stock/42'3);4const stock = await response.json();
After
Production-ready HTTP client
1const controller = new AbortController();2const timeout = setTimeout(() => controller.abort(), 3000);3 4try {5  const response = await fetch(6    'http://inventory-service/api/stock/42',7    { signal: controller.signal }8  );9  if (!response.ok) throw new Error(`HTTP ${response.status}`);10  return await response.json();11} finally {12  clearTimeout(timeout);13}

Exercise

Add a 2–3s AbortController timeout to an outbound fetch to another service. Return a clear error on abort vs HTTP non-2xx. Note what happens to the caller if you omit the timeout under load.

Previous

Progress is saved in this browser.

Next Lesson