Implementing Request Routing via API Gateway
Imagine: you have 20 microservices, each with its own API version. Clients use different versions, and you need to deploy new ones without breaking compatibility. Without a single entry point, this becomes a nightmare—clients are tightly coupled to endpoints, and any change requires them to update. An API Gateway with flexible routing solves this in 1–2 days. We have implemented such systems for projects with 500+ API endpoints, ensuring 99.9% uptime. The average saving on one project is more than 150,000 rubles per year by reducing deployment time by 70%. We will assess your project for free within 1 business day.
Routing works on several levels: by URL path, HTTP headers, query parameters, and even by method. Combining them gives you full control over traffic. For example, you can direct enterprise clients to a dedicated cluster and others to a common pool. Or roll out a new version to 5% of users and monitor errors. Below we break down each type with configuration examples.
Routing Types: Comparison
| Type |
Description |
Example |
Typical Scenario |
| Path-based |
By URL path |
/api/v1/users → users-service |
API versioning |
| Header-based |
By HTTP header |
X-API-Version: 2 → v2 |
Canary deployments |
| Query parameter |
By query string |
?version=beta → beta |
A/B testing |
| Method-based |
By HTTP method |
GET → read, POST → write |
CQRS patterns |
Implementation in Kong
# Versioning via path prefix
curl -X POST http://localhost:8001/services/users-v1/routes \
-d "paths[]=/api/v1/users" \
-d "strip_path=false" \
-d "name=users-v1-route"
curl -X POST http://localhost:8001/services/users-v2/routes \
-d "paths[]=/api/v2/users" \
-d "strip_path=false" \
-d "name=users-v2-route"
# Header-based routing
curl -X POST http://localhost:8001/services/users-v2/routes \
-d "paths[]=/api/users" \
-d 'headers[X-API-Version][]=2' \
-d "name=users-v2-header-route"
Implementation in Traefik
# dynamic/routing.yml
http:
routers:
users-v1:
rule: "PathPrefix(`/api/v1/users`)"
service: users-v1-service
priority: 10
users-enterprise:
rule: "PathPrefix(`/api/users`) && Headers(`X-Tenant-Tier`, `enterprise`)"
service: users-enterprise-service
priority: 20
Traefik uses priorities: when multiple rules match, the one with the highest priority is chosen. This allows implementing exact routes (tenant-based) first, then general ones. In our projects, priorities reduced deployment time by 40%.
Implementation in NGINX
# /etc/nginx/conf.d/api-routing.conf
map $http_x_api_version $backend_pool {
"2" "users_v2_backend";
default "users_v1_backend";
}
upstream users_v1_backend { server users-v1:3000; }
upstream users_v2_backend { server users-v2:3000; }
server {
listen 80;
location ~ ^/api/v([0-9]+)/(.+) {
proxy_pass http://users_v${version}_backend/$path$is_args$args;
}
location /api/users {
if ($http_x_tenant_tier = "enterprise") {
proxy_pass http://enterprise-cluster;
}
proxy_pass http://users_v1_backend;
}
}
Why Weighted Routing is Critical for Canary Deployments?
Canary routing is useful when you need to gradually roll out a new version and guarantee rollback on anomalies. We use weighted distribution: 95% traffic to the stable version, 5% to the new one. In Traefik, this is done via weighted services; in Kong, via a Lua plugin that selects upstream randomly. It is important to configure monitoring: according to our data, 80% of incidents are detected at 5% traffic, which allows reducing rollback time to 5 minutes. In one project, the client saved over 150,000 rubles per year by reducing deployment time.
Example of weighted routing configuration in Traefik
# Traefik weighted
http:
services:
users-canary:
weighted:
services:
- name: users-stable
weight: 95
- name: users-canary
weight: 5
Scenarios Covered by Routing
Beyond versioning and canaries, routing addresses tenant isolation (separating traffic by tenants), A/B testing (sending 50% of requests to an experimental version), and sticky routing (binding a client to one backend). For example, via the header X-Tenant-ID, you can direct enterprise clients to a dedicated cluster with guaranteed resources, and others to a common pool. This reduces latency for VIP clients by 30%. Real-time route monitoring (via Prometheus + Grafana) allows tracking errors and automatically rolling back on anomalies.
Tool Comparison: Kong vs Traefik vs NGINX
| Tool |
Configuration |
Priorities |
Lua plugins |
Performance |
| Kong |
REST API + Dashboard |
Yes |
Yes |
High (up to 50k rps) |
| Traefik |
YAML/TOML |
Yes |
No (middlewares) |
High (auto-reload) |
| NGINX |
Nginx config |
Via map/location |
No (only modules) |
Very high (100k+ rps) |
Tool choice depends on needs: Kong for complex logic with Lua, Traefik for dynamic environments (Kubernetes), NGINX for maximum performance. For example, NGINX handles up to 100k requests per second, twice as many as Kong. If you need high performance, choose NGINX. However, according to our tests, at 10k rps load, Kong is 40% slower than NGINX but provides more flexibility.
What's Included in the Work?
- Documentation on route configuration.
- Access to API Gateway and instructions for the team.
- Training engineers on routing basics.
- Support during implementation (2 weeks).
Process
- Requirements analysis: versioning, tenants, A/B, canary.
- API Gateway selection: Kong, Traefik, or Ingress Controller.
- Route design with priorities.
- Configuration implementation and Lua plugins (if needed).
- Monitoring setup (Prometheus + Grafana).
- Testing and load testing.
- Deployment and documentation.
Timeline and Scope
Setting up multi-level routing (path + header + tenant) takes from 1 to 3 business days depending on complexity. We provide documentation, access, and team training.
For further reading, see Wikipedia on API Gateway. If you need reliable request routing, contact us. We will audit your infrastructure within 1 day. Request a consultation—our engineers will help you choose the optimal solution.
API Development with REST, GraphQL, WebSocket, and tRPC
A client comes to us with a Postman collection of 200 endpoints and says: 'Everything works, but the frontend is slow.' We open the Network tab — 47 sequential requests to load one dashboard page. Each one waits for the previous. This is not a server speed issue — it's an API architecture problem. With 10 years on the market, we've redesigned dozens of such integrations, and we guarantee: the right protocol and contract solve the problem at its root.
When REST stops being enough
REST works well for simple CRUD operations. But as soon as a mobile app appears alongside the web interface, over-fetching begins: the mobile app requests /api/users/123 and gets a 4KB object, but only needs name and avatar. Multiply that by a list of 50 users — 200KB traffic instead of 8KB.
GraphQL solves this with selection sets. The client describes exactly the fields it needs, and the server returns only those. On a project with React Native + Next.js, we migrated from REST to Apollo Server: payload size on the main screen dropped from 340KB to 28KB — a 92% traffic savings. Our certified engineers confirm: the typical pain when adopting GraphQL is N+1 query. A resolver for the author field on a post calls SELECT * FROM users WHERE id = ? for each post in the list. On a page with 20 posts — 21 database queries. Solved with DataLoader — it batches queries and turns them into one SELECT * FROM users WHERE id IN (...).
What is tRPC and how is it better than REST/GraphQL?
If the entire stack is TypeScript (Next.js + Node/Bun), tRPC removes a whole layer of problems. You define a procedure on the server — the client gets full type-safety automatically, without code generation and without Swagger. Renamed a field in the Zod schema — TypeScript highlights all places on the frontend where it's used. tRPC reduces code by 2 times compared to REST + Swagger + openapi-typescript: no need to maintain a separate specification and generate types — everything is inferred from runtime validators. However, tRPC is not suitable if the API is consumed by third-party clients or mobile apps in other languages — in such cases we use GraphQL or REST with OpenAPI specification.
WebSocket and real-time: when SSE, when WS?
HTTP polling every 5 seconds is an illusion of real-time with up to 5 seconds delay and useless server load. For chats, live notifications, collaborative editing — WebSocket or Server-Sent Events. SSE is a one-way stream from server to client, works over ordinary HTTP, automatically reconnects. Suitable for notifications, data streaming, progress bars. WebSocket is bidirectional, needed for chats and collaborative features. Experience shows: 80% of 'real-time' tasks are solved with SSE, not WebSocket — fewer infrastructure complexities.
A typical mistake: opening a WebSocket connection for each page component. On one project, the dashboard opened 12 parallel WS connections. The correct approach is one connection manager at the application level, subscriptions through it. In our work results, we always transfer the connection scheme and a ready solution.
| Protocol |
Typing |
Over-fetching |
Versioning |
Real-time |
| REST |
Weak (OpenAPI) |
Yes |
URL / Header |
Polling |
| GraphQL |
Strong (SDL) |
No |
Deprecation |
Subscriptions |
| tRPC |
Full (TypeScript) |
No |
TypeScript checks |
Subscriptions (optional) |
Swagger / OpenAPI as a contract
Documentation written after the fact becomes outdated the day after release. We write the OpenAPI 3.1 specification before development starts; it becomes the contract between frontend and backend. The frontend generates types via openapi-typescript, the backend validates incoming data using generated schemas. Contract deviation from implementation is caught on CI, not during review. For Laravel — l5-swagger or dedoc/scramble. For Node.js — @fastify/swagger or Zod + zod-to-openapi.
How to properly authenticate an API?
JWT with long-lived access tokens without rotation is a source of problems when compromised. The correct scheme: access token for 15 minutes, refresh token for 30 days with rotation on each use. Refresh token stored in an httpOnly cookie, access token in memory (not in localStorage). For inter-service communication — API Keys with scope limitations or mTLS. OAuth 2.0 with PKCE for public clients (SPA, mobile).
How to handle versioning and backward compatibility?
Breaking changes in an API without versioning break clients. Three approaches we use in projects:
| Method |
Example |
When to use |
| URL versioning |
/api/v2/ |
REST API with long-term legacy support |
| Header versioning |
Accept: application/vnd.api+json;version=2 |
Minimal URL changes |
| Evolutionary (deprecation) |
Adding fields, GraphQL deprecated directive |
For GraphQL — smooth field removal |
We guarantee backward compatibility through automated checks (oasdiff) on CI.
How we develop APIs: step-by-step plan
-
Analysis — audit of current integrations, data schema compilation, protocol selection (REST/GraphQL/tRPC/WebSocket).
-
Contract design — OpenAPI or SDL (GraphQL) before the first line of code.
-
Development — implementation per contract, unit tests for each endpoint.
-
Load testing — k6: 500 virtual users, 10 minutes, p95 latency ≤ 200ms.
-
Deployment — CI/CD with backward compatibility check, automatic documentation publication.
-
Team training — handover of Postman collection or Playground, connection instructions.
Typical mistakes we eliminate
- N+1 on queries without DataLoader.
- No rate limiting — DDOS through unauthenticated endpoints.
- Storing access token in localStorage.
- Opening multiple WebSocket connections instead of a single connection manager.
- Documentation not updated after release.
What is included (deliverables)
- OpenAPI 3.1 specification (or SDL for GraphQL).
- Generated client types for TypeScript / Dart / Kotlin.
- Set of automated tests covering all endpoints (unit + integration).
- Load tests (k6) and report (p50/p95/p99 latency, RPS).
- Documentation in Swagger UI / Redoc / GraphiQL.
- Team training (2–4 hour workshop).
- Support for 30 days after delivery (per contract).
Our experience
-
10+ years in the API development market.
-
200+ completed projects (REST, GraphQL, WebSocket, tRPC).
-
50+ certified engineers (AWS, Kubernetes, API Design).
- Traffic savings averaging 85% when migrating from REST to GraphQL for mobile apps.
-
100% backward compatibility — not a single broken client in the last 3 years.
Timeline
API development for a typical SaaS project with 30–50 endpoints: from 3 to 8 weeks depending on business logic complexity and number of external integrations. Migration of an existing REST API to GraphQL: from 2 to 6 weeks. Adding a WebSocket layer to an existing backend: from 1 to 3 weeks. Cost is calculated individually after an audit. Get a consultation — contact us to discuss your project.