Skip to Content
Course content

190: API Gateway Pattern in Scala Microservices

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I remember a colleague of mine, Sarah, who spent three days debugging a "flaky" frontend dashboard. The UI was intermittently failing to load user profiles, but the backend logs for the individual services looked pristine. When we finally sat down to trace the network traffic, we found the culprit: the frontend was making twelve separate HTTP calls to twelve different microservices just to render a single page. One slow response from the "User Preferences" service was triggering a timeout that cascaded through the browser's connection limit, making the whole app feel broken. Sarah wasn't fighting a bug in the code; she was fighting the physics of network latency.

This is exactly why we use the API Gateway pattern. Instead of forcing your client—whether it's a React app or a mobile client—to keep a map of fifteen different service URLs and handle the orchestration of multiple requests, you introduce a single entry point. The Gateway acts as the "front door," routing requests to the appropriate backend services and, more importantly, aggregating data so the client only has to ask once.

The Cost of a Chatty Frontend

When you let the client talk directly to your microservices, you're leaking your internal architecture to the outside world. If you decide to split your "Orders" service into "OrderHistory" and "CurrentOrders," you've just broken every client version that expects a single endpoint. Beyond the versioning nightmare, there's the "chattiness" problem I mentioned with Sarah. Every round-trip over the public internet is expensive.

In Scala, we can build a Gateway that doesn't just route, but composes. Instead of the client calling /users/{id}, /orders/{id}, and /preferences/{id}, the client calls /profile/{id}. The Gateway then hits those three services internally—where latency is negligible—and stitches the results together into a single JSON payload. It's a massive win for the user experience.

Aggregating Responses with Cats Effect

The real magic happens when we leverage Scala's concurrency primitives. If you call three backend services sequentially, your Gateway is only as fast as the sum of those three calls. That's a waste of resources. Using Cats Effect and Http4s, we can fire these requests in parallel. I prefer using parTraverse or Parallel` blocks to ensure we aren't blocking threads while waiting for the network.

import cats.effect._
import cats.implicits._
import org.http4s._
import org.http4s.client.Client

case class UserProfile(user: User, orders: List[Order], prefs: Prefs)

def getFullProfile(userId: String, client: Client[IO]): IO[UserProfile] = {
  val userCall = client.expect[IO, User](uri"/users/$userId")
  val ordersCall = client.expect[IO, List[Order]](uri"/orders/$userId")
  val prefsCall = client.expect[IO, Prefs](uri"/prefs/$userId")

  // We run these concurrently. If one fails, the whole composition fails fast.
  (userCall, ordersCall, prefsCall).parTupled.map { 
    case (user, orders, prefs) => UserProfile(user, orders, prefs) 
  }
}

Notice how clean that is. We aren't managing threads or callbacks manually; we're describing a concurrent data flow. If the "Orders" service is lagging, the "User" and "Prefs" calls are already happening. The Gateway becomes a high-performance orchestrator rather than a bottleneck.

Centralizing Cross-Cutting Concerns

Beyond data aggregation, the Gateway is the perfect place to stop repeating yourself. I've seen too many projects where every single microservice implements its own JWT validation logic, rate limiting, and CORS configuration. It's a maintenance nightmare. When the security team decides to rotate the signing key or change the token format, you have to redeploy twenty services.

By moving these concerns to the Gateway, your internal services can trust that any request reaching them has already been scrubbed and authenticated. The Gateway validates the token and then passes a simple X-User-Id header to the backend. This simplifies your internal service logic immensely—they can focus on business rules rather than the plumbing of HTTP security. Just be careful not to let the Gateway become a "God Service" where you start putting actual business logic; if you find yourself writing complex if/else statements about business rules in the Gateway, it's time to move that logic back into a dedicated microservice.




📋 Practical Task

Build a Concurrent User Dashboard Aggregator

You are tasked with creating a simplified API Gateway endpoint for a User Dashboard. You have three existing mock services: UserService, AccountService, and NotificationService. Currently, the frontend is making three separate calls to these services.

Your Goal: Implement a single Gateway endpoint GET /dashboard/{userId} that:

  • Calls all three backend services concurrently using Cats Effect.
  • Aggregates the results into a single DashboardResponse case class.
  • Handles a partial failure: if the NotificationService fails (returns a 500 or timeouts), the Gateway should still return the User and Account data, providing an empty list for notifications rather than failing the entire request.

Requirements:
1. Use parTupled or Parallel to ensure the backend calls happen simultaneously.
2. Use .handleErrorWith` or `.attempt` specifically on the notification call to implement the fallback logic.
3. Ensure the final response is returned as a JSON object containing the combined data.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.