Request and response mapping in AWS - Time & Space Complexity
When using request and response mapping in AWS, it is important to understand how the number of mapping operations affects performance.
We want to know how the time to process mappings grows as the number of requests or mapping rules increases.
Analyze the time complexity of the following mapping operations.
// Example of request and response mapping in API Gateway
{
"requestTemplates": {
"application/json": "mapping logic for request"
},
"responseTemplates": {
"application/json": "mapping logic for response"
}
}
// Each request triggers these mappings before forwarding or returning data
This sequence shows how each incoming request and outgoing response is transformed by mapping templates.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Applying request and response mapping templates for each API call.
- How many times: Once per request and once per response, repeating for every API call.
Each API call requires processing the mapping templates once for the request and once for the response.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 20 mapping operations (10 requests + 10 responses) |
| 100 | 200 mapping operations |
| 1000 | 2000 mapping operations |
Pattern observation: The number of mapping operations grows directly with the number of API calls.
Time Complexity: O(n)
This means the time to process mappings grows linearly with the number of API calls.
[X] Wrong: "Mapping templates run once and then apply to all requests without extra cost."
[OK] Correct: Each request and response is processed separately, so mapping happens every time, adding cost per call.
Understanding how request and response mappings scale helps you design APIs that perform well as traffic grows.
"What if we added caching to reduce mapping operations? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of mapping templates
Mapping templates in API Gateway transform data formats between client requests and backend responses.Step 2: Identify the main purpose
This transformation allows different systems to communicate smoothly by renaming, adding, or removing fields.Final Answer:
To change data formats between client and backend -> Option BQuick Check:
Mapping templates = data format change [OK]
- Confusing mapping with data storage
- Thinking mapping encrypts data
- Assuming mapping monitors activity
userId in a request mapping template?Solution
Step 1: Recall JSON path syntax in mapping templates
The correct way to access a JSON field is using$input.path('$.fieldName').Step 2: Match the syntax to the field
UsinguserId$input.path('$.userId')correctly accesses theuserIdfield.Final Answer:
$input.path('$.userId') -> Option AQuick Check:
JSON field access = $input.path('$.field') [OK]
- Using incorrect method names like json or get
- Missing the '$.' prefix in JSON path
- Confusing body access with path access
{
"user": "$input.path('$.username')",
"id": "$input.path('$.userId')"
}What will be the transformed request if the client sends:
{"username": "alice", "userId": "123"}Solution
Step 1: Understand the mapping template fields
The template mapsuserto$.usernameandidto$.userId.Step 2: Apply the client JSON values
Client sends username as "alice" and userId as "123", so these replace the template placeholders.Final Answer:
{"user": "alice", "id": "123"} -> Option CQuick Check:
Template fields replaced by client values [OK]
- Confusing template keys with client keys
- Leaving placeholders as literal strings
- Assuming null values when fields exist
{
"message": "$input.path('$.msg')",
"status": "$input.path('$.code')"
}But the backend returns:
{"message": "Success", "code": 200}What is the main issue causing the mapping to fail?
Solution
Step 1: Compare template paths with backend keys
The template expectsmsgandcode, but backend returnsmessageandcode.Step 2: Identify mismatch causing failure
Sincemsgdoes not exist in backend response, mapping fails for that field.Final Answer:
The template uses wrong JSON paths not matching backend keys -> Option AQuick Check:
Template paths must match backend keys exactly [OK]
- Assuming backend keys are case-insensitive
- Thinking syntax errors cause this issue
- Ignoring key name mismatches
requestTime with the current timestamp to every request sent to your backend using request mapping. Which mapping template snippet correctly adds this field?Solution
Step 1: Identify how to get current request time
In API Gateway mapping templates,$context.requestTimeprovides the current request timestamp.Step 2: Check each option for correct usage
{ "user": "$input.path('$.user')", "requestTime": "$context.requestTime" } uses$context.requestTimecorrectly; others use invalid or unrelated variables.Final Answer:
{ "user": "$input.path('$.user')", "requestTime": "$context.requestTime" } -> Option DQuick Check:
Use $context.requestTime for current timestamp [OK]
- Using $input.requestTime which doesn't exist
- Confusing stage variables with request time
- Using unrelated context variables
Ready to prove it?
Pass the test and this pattern moves unknown
