API Gateways: Roles, Decisions, and a Traefik Example
Choose where an API gateway fits, keep edge policy separate from service authorization, and route a request with a version-pinned Traefik example.
An API gateway gives client traffic one managed route to backend services. This guide helps decide when a gateway, a backend-for-frontend, or a service mesh fits, and separates shared edge policy from service-owned authorization. A small Traefik 3.7.17 lab shows how to route a request through a pinned Docker configuration.
API Gateways: Roles, Decisions, and a Traefik Example
An API gateway is a client-facing entry point that routes requests to backend services and can apply shared edge policies. It can simplify how clients find services, but it also adds a component to deploy, configure, and monitor.
For example, a product page may need catalog details, inventory, and price information. A gateway can route each API request to the right service and apply shared transport or identity checks. The services still decide what a caller may do with the data they own.
Decide whether you need a gateway
Use this decision path before adding another hop:
- One application or one service, with little shared edge policy? Keep the route simple. A separate gateway may add operational work without solving a real client problem.
- Several client-facing services need shared routing or policy? Put an API gateway at the edge so clients use a stable entry point.
- Do mobile, web, and partner clients need different response shapes or aggregation? Keep the gateway as the entry point and add a backend for frontend (BFF) only for the clients whose needs differ.
- Is the main problem service-to-service traffic inside the platform? Look at a service mesh or the platform’s service networking features. A north-south API gateway does not replace east-west service identity and telemetry.
- Managed or self-hosted? A managed gateway shifts more of the gateway infrastructure operation to a provider. A self-hosted gateway gives the team control over deployment and configuration, with the matching on-call and upgrade work.
| Need | Likely fit | Keep in mind |
|---|---|---|
| Shared entry point, route selection, and edge policies | API gateway | Treat configuration changes as production changes |
| Different payloads or composition for different clients | BFF behind the gateway | Keep domain decisions in their owning services |
| Internal service identity and service-to-service policy | Service mesh or platform networking | It solves a different traffic path from public API routing |
| One service with no cross-cutting edge requirement | Direct endpoint | Revisit the decision when client or routing needs change |
What belongs at the edge
A gateway is useful for policy that applies across routes: host and path routing, TLS termination, request-size limits, coarse identity checks, and traffic policy. Keep rules visible and narrow so a route change does not turn into hidden application behavior.
The service that owns a resource remains responsible for authorization. A gateway may confirm that a token is present and valid, but a catalog service must still decide whether that caller can see a product, and an orders service must authorize an order operation against the tenant and user. A request can reach a service through a queue, internal client, or another gateway path; the resource owner still needs its own checks.
| Concern | Gateway role | Service role |
|---|---|---|
| Route selection | Match host, method, and path to an upstream | Expose a stable contract for the route |
| Caller identity | Apply shared credential checks when configured | Validate trusted caller context where required |
| Resource access | Apply broad route-level policy | Authorize tenant, object, and operation access |
| Domain behavior | Pass the request to the right service | Own business rules and data changes |
| Rate limits | Enforce edge quotas when configured | Protect service-specific resources if needed |
For deeper guidance, see API authentication versus authorization, API rate limits and quotas, and network security boundaries. Use those guides for implementation details.
Follow a request across the boundary
A gateway routes traffic; it does not make every downstream operation succeed. For a catalog request, the gateway accepts the client connection, chooses an upstream from the route configuration, and forwards the request. The catalog service then applies its resource checks and returns the result.
sequenceDiagram
participant Client
participant Gateway
participant Catalog as Catalog service
Client->>Gateway: GET /v1/catalog/items/42
Gateway->>Gateway: Match route and apply configured edge policy
Gateway->>Catalog: Forward request
Catalog->>Catalog: Authorize access to item 42
Catalog-->>Gateway: Item response or service error
Gateway-->>Client: Return response
Keep the failure response tied to the failing boundary. A missing or invalid credential may be rejected at the gateway; an authenticated caller who lacks access should be denied by the owning service. A gateway can return a rate-limit response when an edge quota is exceeded. Upstream connection failures and timeouts should be distinguishable from application errors so a client and on-call engineer can tell whether to retry, correct a request, or investigate a dependency. Retrying a state-changing request needs an idempotency guarantee.
The gateway is also part of the availability path. If its route table is wrong or all gateway instances are unavailable, clients may be unable to reach otherwise healthy services. Keep gateway health separate from integration and workflow health; the API gateway and integration monitoring guide follows those signals across a request.
Run a small Traefik gateway
The example uses Traefik Proxy v3.7.17, the latest stable release listed on the official releases page when checked on 2026-11-04; v3.8.0-rc.1 is listed as a pre-release. It also pins the whoami backend to v1.12.0. The Compose configuration follows the Traefik v3.7 Docker quick start and Docker provider reference. It routes a local request under /catalog to the sample service; it does not configure production TLS, authentication, or rate limits.
With Docker Engine and Docker Compose installed, save this as compose.yaml:
services:
traefik:
image: traefik:v3.7.17
command:
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "127.0.0.1:8080:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
catalog:
image: traefik/whoami:v1.12.0
labels:
- "traefik.enable=true"
- "traefik.http.routers.catalog.rule=PathPrefix(`/catalog`)"
- "traefik.http.routers.catalog.entrypoints=web"
- "traefik.http.services.catalog.loadbalancer.server.port=80"
Start the containers and call the route:
docker compose up -d
curl -i http://127.0.0.1:8080/catalog
docker compose down
The response should be HTTP 200 with the whoami service’s request and network details. The port label tells Traefik which container port to use. The Docker provider normally exposes discovered containers by default; this example disables that behavior and opts the catalog service in with traefik.enable=true.
This socket mount is for a local lab only. Traefik warns that unrestricted Docker API access can expose the host if the gateway is compromised. For a deployment, use a restricted Docker API proxy or another provider and follow the Traefik Docker provider security guidance. Keep the dashboard private and authenticated; do not enable the quick-start’s insecure dashboard option on a public interface.
This configuration only routes HTTP traffic. Add TLS, identity middleware, quotas, or config validation according to your environment. Keep workflow decisions in the service that owns them instead of building a second application layer in the gateway.
Configuration, failure, and operations
A route definition is production configuration. Review changes, validate them before rollout, and keep a rollback path. A wrong path match can send traffic to the wrong service; an upstream that is slow or unavailable can occupy connections and affect requests that share the gateway. Set timeouts and connection limits based on the service contract and measured workload. Avoid automatic retries for non-idempotent operations.
A few checks help keep the design understandable:
- Keep route ownership clear. A platform team can own shared gateway infrastructure while service teams own their route contracts and upstream health.
- Limit the blast radius. Avoid putting unrelated workflows behind one policy or one deployment unit if teams need independent changes.
- Measure both sides of the route. Gateway request status and duration show the edge result; dependency and workflow measures show whether the backend operation completed.
- Protect the management path. Restrict access to dashboards, configuration APIs, and the Docker or orchestration control plane.
- Keep gateways stateless where possible. Session or business state in the gateway complicates scaling, failover, and rollback.
For route latency, provider failures, and workflow completion, continue with the monitoring guide. For managing environment-specific settings and dependencies, see backend configuration across environments.
Further reading
- API rate limits, quotas, and consumer fairness covers quota design and client fairness.
- API authentication versus authorization works through identity and object-level access decisions.
- Network security covers trust boundaries between public entry points and private services.
- API gateway and integration monitoring follows route, dependency, and workflow health.
- Service mesh covers service-to-service traffic and policy.
Conclusion
A gateway earns its place when clients need shared routing or edge policy. Let services keep their business rules and resource checks. Treat gateway config like any production change: pin it, review it, and watch both sides of each route.
Category
Related Posts
GraphQL Federation: Subgraphs, Routing, and Trade-offs
Learn how GraphQL federation composes subgraphs, routes operations, and affects service ownership, query planning, latency, failure handling, and authorization.
Amazon Architecture: History and Service Design
Separate documented Amazon and AWS milestones from popular retellings, then apply the lesson about service boundaries to a current request flow.
Asynchronous Communication: From Outbox to Consumer
Choose when to use asynchronous messaging and follow an order event through an outbox, delivery, idempotency, retries, dead-letter handling, and monitoring.