Why federation scales GraphQL - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When using GraphQL with federation, we want to understand how the time to get data changes as the number of services grows.
We ask: How does combining multiple GraphQL services affect the speed of queries?
Analyze the time complexity of this federated GraphQL query resolver snippet.
type Query {
product(id: ID!): Product
}
type Product @key(fields: "id") {
id: ID!
name: String
price: Float
}
extend type Review @key(fields: "id") {
id: ID! @external
product: Product @requires(fields: "id")
}
This code shows how a federated GraphQL schema splits data across services and links them by keys.
Look for repeated data fetching or combining steps.
- Primary operation: Fetching data from each subservice for parts of the query.
- How many times: Once per service involved in the query, plus merging results.
As the number of federated services (n) increases, the query engine calls each service once to get its data.
| Number of Services (n) | Approx. Operations |
|---|---|
| 2 | 2 service calls + 1 merge |
| 5 | 5 service calls + 1 merge |
| 10 | 10 service calls + 1 merge |
Pattern observation: Operations grow roughly linearly with the number of services.
Time Complexity: O(n)
This means the time to resolve a federated query grows linearly with the number of services involved.
[X] Wrong: "Federation makes queries slower exponentially because it combines many services."
[OK] Correct: Each service is called once per query, so the growth is linear, not exponential.
Understanding how federation scales helps you design systems that stay fast as they grow, a key skill in real projects.
What if the query requested nested data requiring multiple calls per service? How would that affect the time complexity?
Practice
Solution
Step 1: Understand federation purpose
Federation breaks a large GraphQL API into smaller, manageable services.Step 2: Identify the benefit
This splitting helps teams work independently and manage parts easily.Final Answer:
It splits a big API into smaller parts for easier management. -> Option CQuick Check:
Federation = splits API for management [OK]
- Thinking federation slows down the API
- Believing federation removes backend services
- Assuming all teams share one codebase
Solution
Step 1: Recall federation SDL syntax
Federated services useextend typeto add fields to shared types.Step 2: Match correct syntax
extend type Query { product(id: ID!): Product } usesextend type Query, which is correct for federation.Final Answer:
extend type Query { product(id: ID!): Product } -> Option BQuick Check:
Federation uses 'extend type' syntax [OK]
- Using 'type' instead of 'extend type' in federated services
- Using non-existent 'service' or 'federation' keywords
- Confusing base schema with extended schema
Product service defines type Product { id: ID!, name: String } and Review service extends it with extend type Product { reviews: [Review] }. What will a query for { product(id: "1") { name reviews { body } } } return?Solution
Step 1: Understand federation field extension
The Review service extends Product with reviews, so combined schema includes reviews.Step 2: Query result combines data
The query asks for product name and reviews body, which federation resolves from both services.Final Answer:
Product name and list of reviews with their body fields. -> Option AQuick Check:
Federation merges fields, query returns combined data [OK]
- Assuming extended fields are unavailable
- Expecting errors due to field extension
- Thinking federated services cannot combine data
type User { id: ID!, name: String }. The Order service tries to extend User with extend type User { orders: [Order] } but the gateway returns an error. What is the likely cause?Solution
Step 1: Check federation key requirement
Federation requires types extended across services to have a @key directive for identification.Step 2: Identify missing @key
User type lacks @key in User service, so gateway cannot resolve extensions.Final Answer:
User type is not marked with @key directive in User service. -> Option AQuick Check:
@key missing causes federation errors [OK]
- Thinking extended types must be fully redefined
- Blaming gateway instead of schema directives
- Assuming fields must be in base service only
Solution
Step 1: Understand federation team ownership
Federation scales by letting teams own services with clear boundaries and keys.Step 2: Identify best practice
Clear @key types and minimal overlap avoid conflicts and enable smooth composition.Final Answer:
Each team owns a service with clear @key types and minimal overlap. -> Option DQuick Check:
Team ownership + @key = scalable federation [OK]
- Thinking one schema file for all teams scales well
- Avoiding @key directives breaks federation
- Centralizing all services under one team limits scaling
