GraphQL Federation Rollout Guide
This guide outlines a production-ready rollout strategy for GraphQL Federation (using Apollo Federation v2 as the reference implementation). It focuses on composition , gateway performance , caching , and errors . The rollout is phased: Planning , Staging , Production Canary , and Full Rollout . Use tools like Apollo Rover, Studio, and Router for supergraph management.
Assumptions:
Subgraphs are built with @apollo/subgraph (Node.js/TS example).
Gateway: Apollo Router (recommended for perf over Gateway).
Schema registry: Apollo Studio or self-hosted.
Phase 1: Planning & Composition
Supergraph Composition
Composition builds a single "supergraph" schema from independent subgraphs. Each subgraph declares entities (shareable types) with @key directives.
Subgraph Schemas (example):
graphql Copy code
# accounts subgraph (schema.graphql)
type User @key(fields: "id") {
id: ID!
email: String!
name: String!
}
extend type Query {
me: User
}
extend type Mutation {
createUser(email: String!, name: String!): User!
}
# products subgraph (schema.graphql)
type Product @key(fields: "id") {
id: ID!
name: String!
ownerUserId: ID! @external(resolver: "resolveOwnerUser")
}
extend type User @key(fields: "id") {
id: ID! @external
ownedProducts: [Product!]!
}
Compose Supergraph :
bash Copy code
# Install Rover: https://go.apollo.dev/rover
rover supergraph compose \
--config supergraph.yaml \
--output supergraph-schema.graphql
supergraph.yaml:
yaml Copy code
subgraph_url:
accounts: https://accounts.example.com/graphql
products: https://products.example.com/graphql
Validation :
Lint with rover subgraph check accounts --schema ./supergraph-schema.graphql.
Publish to registry: rover subgraph publish studio-org/studio-api --name accounts --schema ./schema.graphql --routing-url https://....
Rollout Checkpoint : 100% composition success in staging. Use schema checks in CI/CD.
Phase 2: Gateway Performance
Deploy Apollo Router as the gateway for native Rust performance (10x faster than JS Gateway).
Optimization Strategies
Query Planning :
Router auto-generates query plans (parallel subgraph fetches).
Enable query planning cache : Persist plans in Redis.
toml Copy code
# router.yaml
[supergraph]
query_planning.cache = "redis://localhost:6379"
Batching & Concurrency :
Default batching: Router batches identical subgraph queries.
Tune concurrency: [subgraph.default] concurrency_limit = 100.
Metrics & Monitoring :
Enable Prometheus: otlp_exporter.otlp_endpoint = "http://otel:4317".
Key metrics: apollo_router_subgraph_requests_total, query_planning_duration_seconds.
Target: <50ms p95 latency, <1% error rate.
Deployment :
yaml Copy code
# docker-compose for staging
services:
router:
image: ghcr.io/apollographql/router:v1.20.0
volumes: ['./router.yaml:/etc/router.yaml']
ports: ['4000:4000']
Rollout Checkpoint : Load test with Artillery (artillery run graphql-load.yml) targeting 10k req/min. Ensure <100ms p99.
Phase 3: Caching
Federation caching is multi-layered: client → gateway → subgraphs → DB.
Strategies
Gateway-Level Caching (Apollo Router):
toml Copy code
# router.yaml
[query_planning.cache]
in_memory = { limit = "1GiB", ttl = "5m" }
[[http.caches]]
name = "public"
sources = [{ mode = "embedded", path = "public.smithy" }]
Persist queries: Use Apollo Persisted Queries (APQ) for client-side hashing.
Subgraph Caching :
Dataloader for N+1 (automatic in @apollo/subgraph).
Redis for entity resolution:
ts Copy code
// accounts subgraph resolver
const cache = new Redis({ url: 'redis://localhost:6379' });
const user = await cache.get(`user:${id}`);
if (!user) {
user = await db.user(id);
await cache.setex(`user:${id}`, 300, JSON.stringify(user));
}
CDN Edge Caching :
Cloudflare/ Fastly with Cache-Control: public, max-age=60.
Normalize queries via introspection-disabled endpoint.
Invalidation :
Use webhooks: Subgraph mutation → invalidate via rover supergraph introspect.
Entity cache TTL: 1-5min for volatile data.
Rollout Checkpoint : Cache hit rate >70% under load (monitor http_cache_hits_total).
Phase 4: Errors
Federation supports partial responses and error masking for resilience.
Handling & Propagation
Subgraph Errors :
ts Copy code
// Custom error in subgraph
throw new ApolloError('User not found', 'NOT_FOUND');
Router propagates as errors array in response.
Gateway Configuration :
toml Copy code
# router.yaml - mask internal errors
[supergraph.include_subgraph_errors]
all = false # Only buffer public errors
subgraph_exceptions = ["accounts"] # Propagate for critical subgraphs
Partial Errors :
json Copy code
// Client receives
{
"data": { "me": null, "products": [...] },
"errors": [{ "message": "User service down", "path": ["me"], "extensions": { "code": "SUBGRAPH_ERROR" } }]
}
Tracing & Logging :
OpenTelemetry: Auto-traces subgraph spans.
Sentry integration: console.error(error, { subgraph: 'accounts' }).
Circuit breakers: Use tower middleware in Router for subgraph health.
Rollback Plan :
Feature flag subgraphs via routing URLs.
Blue-green: Deploy new supergraph version to /v2, migrate clients.
Rollout Checkpoint : Chaos test (e.g., kill subgraph pod) → <5% query failure, graceful degradation.
Full Rollout Checklist
Step Action Success Criteria 1 Compose & validate 0 composition errors 2 Deploy Router staging p95 <100ms, 99.9% uptime 3 Enable caching >60% hit rate 4 Error injection test Partial responses work 5 Canary (10% traffic) Metrics stable 6 Full prod Monitor 24h, auto-rollback if >1% error
Tools & Resources
Rover : npm i -g @apollo/rover
Apollo Studio : Schema registry & query analytics.
Monitoring : Grafana + Prometheus.
CI/CD : GitHub Actions template here .
This rollout ensures zero-downtime, high-performance federation. For custom schemas, provide yours for tailored advice! 🕵️♂️