Bird
Raised Fist0
Rest APIprogramming~10 mins

Composite operations (multi-resource) in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Composite operations (multi-resource)
Start Composite Operation
↓
Call API 1: Create Resource A
↓
Check API 1 Success?
No→Rollback if needed
Yes↓
Call API 2: Update Resource B
↓
Check API 2 Success?
No→Rollback API 1
Yes↓
Call API 3: Delete Resource C
↓
Check API 3 Success?
No→Rollback API 1 & 2
Yes↓
All Success
↓
End
The composite operation calls multiple APIs in order, checking success after each. If any fail, it rolls back previous changes to keep data consistent.
Execution Sample
Rest API
POST /resourceA
if success:
  PATCH /resourceB
  if success:
    DELETE /resourceC
    if success:
      return success
    else:
      rollback resourceA and resourceB
  else:
    rollback resourceA
else:
  return error
This code tries to create, update, then delete resources in sequence, rolling back if any step fails.
Execution Table
StepAPI CalledRequestResponseSuccess?Action Taken
1POST /resourceACreate Resource A201 CreatedYesProceed to next API
2PATCH /resourceBUpdate Resource B200 OKYesProceed to next API
3DELETE /resourceCDelete Resource C500 Internal Server ErrorNoRollback resourceA and resourceB
4RollbackDELETE /resourceA, PATCH rollback /resourceB200 OKYesComposite operation ends with error
💡 Step 3 failed, so rollback was triggered to undo previous changes.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
resourceA_statusnonecreatedcreatedrollback startedrolled back
resourceB_statusnonenoneupdatedrollback startedrolled back
resourceC_statusexistsexistsdelete failedexistsexists
operation_statuspendingpendingpendingfailedfailed
Key Moments - 2 Insights
Why do we rollback resourceA and resourceB after resourceC deletion fails?
Because the composite operation must keep data consistent. The execution_table shows step 3 failed, so rollback undoes previous successful steps to avoid partial changes.
What happens if the first API call fails?
The composite operation stops immediately without calling other APIs, as shown in the concept_flow where failure at API 1 leads directly to end with error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response of the PATCH /resourceB call at step 2?
A500 Internal Server Error
B201 Created
C200 OK
D404 Not Found
💡 Hint
Check the Response column in row for step 2 in execution_table.
At which step does the composite operation decide to rollback?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Success? column and Action Taken for step 3 in execution_table.
If the DELETE /resourceC call succeeded, what would be the final operation_status?
Afailed
Bsuccess
Cpending
Drollback started
💡 Hint
Refer to variable_tracker and concept_flow where all success leads to operation end with success.
Concept Snapshot
Composite operations call multiple APIs in order.
Each step checks success before continuing.
If any step fails, rollback undoes previous changes.
This keeps data consistent across resources.
Useful for multi-resource updates in REST APIs.
Full Transcript
Composite operations in REST APIs involve calling multiple API endpoints one after another. After each call, the system checks if it succeeded. If a call fails, the system rolls back any previous successful changes to keep data consistent. For example, creating resource A, updating resource B, and deleting resource C happen in sequence. If deleting resource C fails, the system undoes the creation and update to avoid partial changes. This process ensures all-or-nothing behavior across multiple resources.

Practice

(1/5)
1. What is the main benefit of using composite operations in REST APIs?
easy
A. They combine multiple API calls into a single request to save time and keep data consistent.
B. They allow you to write SQL queries inside REST endpoints.
C. They automatically generate API documentation.
D. They encrypt all API responses for security.

Solution

  1. Step 1: Understand what composite operations do

    Composite operations bundle several API calls into one request to reduce network overhead.
  2. Step 2: Identify the main benefit

    This bundling saves time and ensures related data changes happen together, keeping consistency.
  3. Final Answer:

    They combine multiple API calls into a single request to save time and keep data consistent. -> Option A
  4. Quick Check:

    Composite operations = combine calls + consistency [OK]
Hint: Composite means combining multiple calls into one [OK]
Common Mistakes:
  • Confusing composite operations with documentation tools
  • Thinking composite operations encrypt data
  • Assuming composite operations run SQL queries
2. Which of the following is the correct way to structure a composite operation request in JSON?
easy
A. [{"method": "DELETE", "path": "/items/1"}]
B. {"operation": "GET", "url": "/users"}
C. {"requests": {"method": "POST", "endpoint": "/orders"}}
D. {"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]}

Solution

  1. Step 1: Identify the correct JSON structure for composite operations

    Composite operations usually use an "operations" array with each operation having "method" and "path" keys.
  2. Step 2: Compare options

    {"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} matches this structure exactly, while others use incorrect keys or formats.
  3. Final Answer:

    {"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} -> Option D
  4. Quick Check:

    Composite request = "operations" array with method and path [OK]
Hint: Look for "operations" array with method and path keys [OK]
Common Mistakes:
  • Using singular "operation" instead of plural "operations"
  • Using wrong keys like "url" or "endpoint"
  • Not wrapping operations inside an array
3. Given this composite operation request:
{
  "operations": [
    {"method": "POST", "path": "/users", "body": {"name": "Alice"}},
    {"method": "POST", "path": "/orders", "body": {"userId": "${1.id}", "item": "Book"}}
  ]
}
What is the expected behavior?
medium
A. Create a user named Alice, then create an order linked to that user using the new user ID.
B. Create an order first, then create a user named Alice.
C. Create a user named Alice only; the order creation will fail due to syntax error.
D. The request will fail because referencing previous operation results is not allowed.

Solution

  1. Step 1: Understand operation order and referencing

    The first operation creates a user named Alice. The second operation uses "${1.id}" to reference the ID from the first operation's result.
  2. Step 2: Determine expected behavior

    This means the order is created linked to the new user. This chaining is a key feature of composite operations.
  3. Final Answer:

    Create a user named Alice, then create an order linked to that user using the new user ID. -> Option A
  4. Quick Check:

    Reference previous result with ${1.id} = chained operations [OK]
Hint: Look for ${1.id} to see chaining between operations [OK]
Common Mistakes:
  • Assuming operations run in parallel without order
  • Thinking referencing previous results is invalid
  • Confusing operation indexes starting at 0 instead of 1
4. You have this composite operation request:
{
  "operations": [
    {"method": "POST", "path": "/products", "body": {"name": "Pen"}},
    {"method": "PUT", "path": "/inventory/${1.id}", "body": {"stock": 100}}
  ]
}
But the API returns an error saying "Invalid reference ${1.id}". What is the likely cause?
medium
A. The syntax for referencing previous results should be ${0.id}, not ${1.id}.
B. The first operation did not return an ID, so ${1.id} is invalid.
C. PUT method cannot be used in composite operations.
D. The inventory path should not include dynamic IDs.

Solution

  1. Step 1: Check the reference usage

    The error "Invalid reference ${1.id}" means the first operation's response does not contain an "id" field.
  2. Step 2: Understand why this causes error

    If the first operation's response lacks "id", the reference ${1.id} is invalid and causes failure.
  3. Final Answer:

    The first operation did not return an ID, so ${1.id} is invalid. -> Option B
  4. Quick Check:

    Reference error = missing id in first operation response [OK]
Hint: Check if referenced operation returns the expected field [OK]
Common Mistakes:
  • Assuming index starts at 0 instead of 1
  • Thinking PUT is disallowed in composite operations
  • Ignoring that referenced fields must exist in response
5. You want to create a composite operation that creates a customer, then creates two orders for that customer, and finally updates the customer's status to "active". Which JSON structure correctly represents this multi-resource composite operation?
hard
A. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "1", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "1", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/1", "body": {"status": "active"}} ] }
B. { "operations": [ {"method": "POST", "path": "/orders", "body": {"item": "Laptop"}}, {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "PUT", "path": "/customers/1", "body": {"status": "active"}} ] }
C. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] }
D. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${2.id}", "body": {"status": "active"}} ] }

Solution

  1. Step 1: Identify correct operation order and referencing

    The customer creation is first (operation 1), so its ID is referenced as ${1.id} in subsequent operations.
  2. Step 2: Verify all references use ${1.id} and order is logical

    Orders and update use ${1.id} correctly. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] } matches this perfectly. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${2.id}", "body": {"status": "active"}} ] } incorrectly uses ${2.id} which refers to the first order, not the customer.
  3. Final Answer:

    { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] } -> Option C
  4. Quick Check:

    Use ${1.id} to reference first operation's customer ID [OK]
Hint: Reference first created resource as ${1.id} in later operations [OK]
Common Mistakes:
  • Referencing wrong operation index for customer ID
  • Hardcoding IDs instead of referencing dynamically
  • Incorrect operation order breaking dependencies
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard