Resource expansion (embed related data) in Rest API - Time & Space Complexity
When a REST API expands resources to include related data, it fetches more information in one go.
We want to know how this extra fetching affects the time it takes to get the data.
Analyze the time complexity of the following code snippet.
GET /orders?expand=customer,items
// The API fetches orders, and for each order, it also fetches the related customer and items data.
This code fetches a list of orders and expands each order with its related customer and items details.
- Primary operation: For each order, fetching related customer and items data.
- How many times: Once per order in the list.
As the number of orders grows, the API does more work fetching related data for each order.
| Input Size (n orders) | Approx. Operations |
|---|---|
| 10 | Fetch 10 orders + 10 customers + 10 items sets |
| 100 | Fetch 100 orders + 100 customers + 100 items sets |
| 1000 | Fetch 1000 orders + 1000 customers + 1000 items sets |
Pattern observation: The work grows roughly in direct proportion to the number of orders.
Time Complexity: O(n)
This means the time to get all data grows linearly with the number of orders requested.
[X] Wrong: "Expanding related data only adds a small fixed cost regardless of how many orders there are."
[OK] Correct: Each order adds extra fetches for its related data, so the cost grows with the number of orders, not fixed.
Understanding how expanding related data affects performance helps you design APIs that stay fast as data grows.
What if the API also expanded nested related data, like each item's supplier? How would the time complexity change?
Practice
expand parameter in a REST API request?Solution
Step 1: Understand the
Theexpandparameter roleexpandparameter is used to include related or nested resource data inside the main resource response.Step 2: Compare with other options
Filtering, pagination, and deletion are different API features and not related to embedding data.Final Answer:
To include related resource data within the main resource response -> Option BQuick Check:
Expand = embed related data [OK]
- Confusing expand with filtering or pagination
- Thinking expand deletes data
- Assuming expand changes data instead of embedding
Solution
Step 1: Identify the standard parameter for resource expansion
The common parameter to embed related data isexpand.Step 2: Check the URL syntax
The correct syntax uses?expand=authorto embed the author data inside posts.Final Answer:
/posts?expand=author -> Option AQuick Check:
Use expand=field to embed [OK]
- Using embed or include instead of expand
- Using fields which filters fields, not expands
- Missing the question mark before parameters
/orders?expand=customer, what will the response include?Solution
Step 1: Analyze the expand parameter usage
The parameterexpand=customertells the API to include customer data inside each order.Step 2: Understand the response content
The response will contain order details plus the related customer information embedded.Final Answer:
Order details with embedded customer information -> Option DQuick Check:
Expand=customer means embed customer data [OK]
- Expecting only orders without customer info
- Thinking expand replaces main resource
- Confusing expand with filtering or unrelated data
/posts?expand=comments but the API returns an error. What is the most likely cause?Solution
Step 1: Check if 'comments' is expandable
Not all fields can be expanded; the API must support expanding 'comments'.Step 2: Verify HTTP method and parameter correctness
Expansion usually works with GET and uses 'expand', so POST or 'include' are unlikely causes.Step 3: URL format check
Missing trailing slash usually does not cause expansion errors.Final Answer:
The 'comments' field is not a valid expandable resource -> Option AQuick Check:
Expandable fields must be supported by API [OK]
- Using wrong HTTP method for expansion
- Confusing expand with include parameter
- Assuming all fields are expandable
Solution
Step 1: Understand how to expand multiple resources
Multiple expansions are usually comma-separated in a singleexpandparameter.Step 2: Evaluate each option's syntax
/invoices?expand=customer,payment uses comma-separated values correctly. /invoices?expand=customer&expand=payment repeats the parameter which is often unsupported. /invoices?embed=customer,payment uses 'embed' which is incorrect. /invoices?expand=customer;payment uses semicolon which is not standard.Final Answer:
/invoices?expand=customer,payment -> Option CQuick Check:
Multiple expands separated by commas [OK]
- Repeating expand parameter multiple times
- Using embed instead of expand
- Separating resources with semicolons
Ready to prove it?
Pass the test and this pattern moves unknown
