Development with AWS Services · Topic 1 of 7

Decoupling, Sync or Async, and API Gateway

Decouple components with SQS, SNS and EventBridge, choose synchronous or asynchronous calls, and shape REST APIs with mappings and validation.

  • 5 min
In a nutshell
  • Loose coupling means a queue, topic or event bus sits between components.
  • SQS buffers for one consumer group, SNS fans out, EventBridge routes by rules.
  • Synchronous callers wait and retry on error. Asynchronous callers get an acknowledgment and the service retries.

What it is

Two components are tightly coupled when one calls the other directly and waits for the answer. If the callee is slow, down or changed, the caller breaks, and both must scale together.

Loosely coupled components talk through a buffer or a broker. The sender hands off a message and moves on, so each side can fail, scale and deploy on its own.

The second decision is synchronous or asynchronous. A synchronous caller blocks until it gets a result. An asynchronous caller gets an acknowledgment and the work finishes later.

Clientbrowser or appHTTPSAPI Gatewayfront doorsync callLambdabusiness logicAPI Gatewayfront doorasync hand offSQS queuebufferpollsWorkerLambda consumer

How it works

Choosing the decoupling service

NeedServiceHow the developer uses it
Buffer work for one consumer groupAmazon SQSProducer calls SendMessage, consumer polls and deletes
Push one message to many subscribersAmazon SNSPublish to a topic, subscribers get copies
Route events by content to many targetsAmazon EventBridgePutEvents, rules match event patterns
Orchestrate steps with stateAWS Step FunctionsStart an execution, states call services

A queue absorbs spikes. If a consumer is slow, messages wait instead of failing the producer. Because SQS can deliver a message more than once, consumers must be idempotent, meaning that processing the same message twice gives the same result.

Synchronous or asynchronous

SynchronousAsynchronous
CallerWaits for the responseGets an acknowledgment only
Typical servicesAPI Gateway to Lambda, ALB, direct SDK callSQS, SNS, EventBridge, Lambda Event invocation
ErrorsReturned to the caller, who retriesRetried by the service, then sent to a DLQ or destination
Good forA user waiting for a resultLong work, spikes, fan out

When a request needs more time than a caller will wait, return an HTTP 202 (accepted) with a job ID, do the work through a queue, and let the client poll or receive a notification. In a REST API, the Lambda integration can be made asynchronous by setting the integration request header X-Amz-Invocation-Type to 'Event'. API Gateway can also call SQS directly as an AWS service integration, so no function is needed in the middle.

Shaping a REST API in API Gateway

A REST API method has four stages: method request, integration request, integration response and method response. You change data at the two integration steps.

  • Lambda proxy integration passes the whole request to the function as one event. The function must return statusCode, headers and body, and the body must be a string. No mapping templates are used.
  • Non proxy integration lets you transform. A mapping template, written in Velocity Template Language (VTL), rewrites the request before the backend and the response before the client.
  • HTTP APIs are the simpler, cheaper type. They have no VTL templates and use parameter mapping instead.
{
  "orderId": "$input.params('id')",
  "quantity": $input.json('$.qty')
}

This template builds a backend payload from a path parameter and a body field.

Enforcing validation rules

Attach a model (a JSON Schema) to the method request and choose a request validator. The validator can check the body, and required query string parameters and headers. An invalid call gets a 400 response from API Gateway, and your function is never invoked. Use gateway responses to customize those error bodies.

Maintaining the API

Changes to a REST API do not go live until you create a deployment to a stage. Stages such as dev and prod have their own URLs. Stage variables let one API point at different Lambda aliases or backends per stage. The default integration timeout for a REST API is 29 seconds.

How the exam asks about this

  • "A web app calls an API that takes several minutes to finish. Users must not wait." -> Accept the request, put the work on an SQS queue (or invoke Lambda asynchronously), and return 202.
  • "Reject requests with a missing field before they reach Lambda, with the least code." -> A model plus a request validator on the method.
  • "A legacy backend expects a different JSON shape, and you cannot change it." -> A mapping template on a non proxy integration.

What to remember for the exam

  • Loose coupling means a queue, topic or event bus sits between components.
  • SQS buffers for one consumer group, SNS fans out, EventBridge routes by rules.
  • Synchronous callers wait and retry on error. Asynchronous callers get an acknowledgment and the service retries.
  • Lambda proxy integration returns statusCode, headers and a string body.
  • Mapping templates (VTL) work only in non proxy REST API integrations.
  • Request validators with a model return 400 before the backend runs.
  • A REST API change needs a new deployment to a stage before it takes effect.
Watch out

A Lambda proxy response whose body is a JSON object instead of a string makes API Gateway return a 502 error. Convert it with JSON.stringify or json.dumps.

Common mistakes

Calling a slow backend synchronously. The REST API integration timeout defaults to 29 seconds, so long jobs fail at the gateway. Hand the work off asynchronously instead.

Expecting mapping templates with proxy integration. Proxy mode passes data through unchanged. Switch to a non proxy integration when you must transform.

Forgetting to redeploy. Editing a validator or model in the editor changes the working copy only. Create a deployment so the stage picks it up.

Check yourself

  • Why must a consumer of a standard SQS queue be idempotent?

    Standard queues deliver at least once, so the same message can arrive twice and must not cause a double effect.

  • How do you reject a request with a missing body field before it reaches Lambda?

    Attach a JSON Schema model to the method and enable a request validator, which returns 400.

  • What must a Lambda proxy integration return to API Gateway?

    An object with statusCode, headers and a body that is a string.

Topic 1 of 7 in Development with AWS Services

Test yourself on Development with AWS Services

The full AWS DVA-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.