Communication Between Services

Lesson 2 of 10 · 22 min

x
20%

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
const response = await fetch(
  'http://inventory-service/api/stock/42'
);
const stock = await response.json();
After
Production-ready HTTP client
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 3000);

try {
  const response = await fetch(
    'http://inventory-service/api/stock/42',
    { signal: controller.signal }
  );
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  return await response.json();
} finally {
  clearTimeout(timeout);
}

Key Takeaway

REST is the lingua franca of service APIs — always pair it with timeouts, error handling, and explicit contracts.

PreviousNext Lesson