Asynchronous Microservices with FastAPI: Decoupling Edge API Gateways

2022-01-01 10:00:00+00:00

When migrating from monoliths to microservices, managing service communication and request routing becomes a central challenge. Coupling client applications directly to multiple backend services introduces complexity and security risks. A clean architecture routes all external traffic through a single entry point: an API Gateway.

By leveraging FastAPI's asynchronous handling and HTTP client wrappers, you can design a gateway that proxies requests downstream, buffers traffic spikes, and extracts authorization details efficiently.


1. Designing the Gateway Routing Layer

In our microservices architecture, clients interact solely with api-gateway. This service acts as a reverse proxy, translating request paths into downstream endpoints (such as forwarding /crm to crm-service or /notify to notification-service):

# Proxying request to backend microservices in FastAPI
import httpx
from fastapi import FastAPI, Request, Response

app = FastAPI()
client = httpx.AsyncClient()

@app.api_route("/api/crm/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def crm_proxy(request: Request, path: str):
    url = f"http://crm-service:8000/gq/{path}"
    headers = dict(request.headers)
    # Forward the query or mutation payload
    body = await request.body()
    response = await client.request(
        method=request.method,
        url=url,
        headers=headers,
        content=body,
        timeout=10.0
    )
    return Response(
        content=response.content,
        status_code=response.status_code,
        headers=dict(response.headers)
    )

2. The Benefits of Decoupling

Decoupling downstream microservices behind an API Gateway allows us to standardise rate-limiting, CORS policies, and request tracing in one location, protecting the core business endpoints from unnecessary exposures.

Propagating Verified Tenant Claims from FastAPI Gateways to Internal Services

Once the FastAPI edge gateway validates the external Bearer JWT, forward a signed internal context header or mTLS identity so downstream microservices never repeat expensive JWKS network lookups. See Freelance FastAPI & Pydantic Programmer Guide.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs