Microservices Patterns

Preview — 3 of 10 questions

What is the primary role of an API Gateway in a microservices architecture?

javascript
Without API Gateway:
  Client must know: auth-service:3001, order-service:3002, product-service:3003
  Client handles auth, rate limiting, retries per service
  Exposing internal services directly is a security risk

With API Gateway:
  Client ──▶ api.codejump.io (API Gateway)
               ├── /api/auth/*     → auth-service
               ├── /api/challenges/* → challenge-service
               ├── /api/submissions/* → judge-service
               └── /api/users/*    → user-service

  Gateway handles:
    - Authentication (verify JWT before forwarding)
    - Rate limiting (100 req/min per user)
    - SSL termination
    - Request logging / tracing
    - Protocol translation (REST to gRPC)
    - Response aggregation (BFF pattern)
AThe API Gateway is the single entry point for all client requests — it handles routing, auth, rate limiting, and protocol translation before forwarding to the appropriate microservice.
BThe API Gateway stores all service data in a centralized database to avoid inter-service calls.
CThe API Gateway monitors service health and automatically restarts failed instances.
DThe API Gateway is a load balancer that only operates at the TCP layer, distributing connections by IP.

In a dynamic microservices environment where service instances start and stop frequently, how do services find each other?

javascript
Service Discovery Flow:
  Order Service starts
   Registers: { name: "order-service", host: "10.0.1.42", port: 3002, health: "/healthz" }
    in Service Registry (Consul)

  Payment Service needs to call Order Service:
   Queries registry: "Where is order-service?"
   Registry returns: ["10.0.1.42:3002", "10.0.1.55:3002"] (healthy instances)
   Payment Service picks one (client-side LB) and makes the call

  Order Service instance crashes:
   Registry detects failed health check  removes instance
   Next query only returns healthy instances
AHard-code the IP address of each service in environment variables at deployment time.
BThe API Gateway hard-codes the address of every service and updates its config on every deployment.
CServices broadcast their location on the network using UDP multicast, and callers listen for announcements.
DUse service discovery — a registry (like Consul or etcd) where services register their address on startup and deregister on shutdown. Callers query the registry to find healthy instances.

A payment service calls a fraud-detection service. The fraud service is slow (timing out at 30s). Without protection, all payment requests pile up waiting for timeouts. What does the circuit breaker pattern do?

javascript
Circuit Breaker States:

CLOSED (normal):
  All calls go through to fraud-service
  Failure rate tracked

5 consecutive timeouts  circuit OPENS:

OPEN (tripped):
  Calls to fraud-service immediately return fallback (allow payment with manual review flag)
  No requests sent to fraud-service (prevents thread exhaustion)
  After 30 seconds  enter HALF-OPEN

HALF-OPEN (testing):
  One test request allowed through
  If success  CLOSED (resume normal operation)
  If failure  OPEN again (reset timer)
AIt retries the failed request immediately up to 10 times before returning an error.
BIt monitors failure rate; when failures exceed a threshold, it opens the circuit (stops calling the downstream service) and returns an immediate fallback, preventing cascading failures.
CIt routes requests to a different fraud service in a different region automatically.
DIt caches the last successful fraud-check result and uses it indefinitely while the service is down.

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.