Google Cloud · Solution pattern
Event-driven order processing
Accept an order quickly, then process payment, stock and shipping asynchronously, with retries and no lost orders.
Also on: AWS
The business problem
An online shop must accept orders at unpredictable rates, including sale-day spikes, and then charge the card, reserve stock, arrange shipping and tell the customer. These steps are slow, depend on outside systems that sometimes fail, and must not lose or duplicate an order. If the web request had to wait for every step, one slow dependency would make the whole checkout slow or fail.
The answer is to separate accepting the order (fast, durable) from processing it (asynchronous, retried, observable), and to connect the two with events.
Requirements
- Accept an order and answer the customer in well under a second, even when downstream systems are slow.
- Never lose an order, and never charge or ship it twice if a request or step is retried.
- Absorb traffic spikes without capacity planning, and cost close to nothing when idle.
- Add new consumers of order events (analytics, fraud checks) without changing the intake code.
- Handle a failure halfway through, such as payment taken but stock unavailable, by undoing earlier steps.
- Show what happened to every order for support and audit.
The Google Cloud design
A global external Application Load Balancer (with Cloud Armor) sends POST /orders to an intake
service on Cloud Run. The service writes the order to Firestore using the client's idempotency key as
the document ID (a create fails if it already exists, so retries can't duplicate orders) and publishes an
OrderPlaced message to a Pub/Sub topic, then answers the customer. Separate subscriptions deliver the
event to a payment worker (Cloud Run, push subscription with a dead-letter topic), a notification
function (Cloud Run functions) and BigQuery (a BigQuery subscription) for analytics.
Components and their roles
| Component | Role |
|---|---|
| Load balancer | Public HTTPS entry with managed TLS; Cloud Armor rate-limits and filters abusive traffic. |
| Intake service | Validates the order, writes it to Firestore with the idempotency key, publishes OrderPlaced. |
| Orders store | Stores orders and their status; the idempotency key as document ID prevents duplicates. |
| Order events | Topic with one subscription per consumer, retries, and a dead-letter topic. |
| Payment worker | Charges the customer idempotently and updates the order status. |
| Notification function | Sends the confirmation email when the order is placed. |
| Analytics | A BigQuery subscription streams every event into a table for reporting. |
How the pieces connect
- Load balancer → Intake service: forwards the request
- Intake service → Orders store: creates the order (idempotency key as ID)
- Intake service → Order events: publishes OrderPlaced
- Order events → Payment worker: push subscription
- Order events → Notification function: Eventarc trigger
- Order events → Analytics: BigQuery subscription
Security and identity
- Least privilege: each Cloud Run service and function has its own service account (intake can write orders and publish; the worker can read and update orders).
- Push authentication: Pub/Sub push uses an OIDC token, and the worker only accepts its subscription's service account; the worker requires authentication (no public invoker).
- Edge protection: Cloud Armor policies on the load balancer.
Availability and failure handling
Cloud Run, Firestore and Pub/Sub are regional managed services spread across zones. Writing the order before publishing means a later failure can't lose it; Pub/Sub retries delivery and the dead-letter topic holds messages that keep failing. For regional failover, run the intake service in two regions behind the global load balancer with a multi-region Firestore database.
Scaling
Cloud Run, Pub/Sub and Firestore scale with traffic; a spike grows the subscription backlog instead of failing requests. Cap the payment worker's maximum instances so the payment provider isn't overwhelmed.
Cost drivers
Nothing bills for idle capacity: costs follow order volume through Cloud Run requests and compute, Firestore operations, Pub/Sub throughput, BigQuery streaming and storage, and the load balancer's rules and processed data.
Trade-offs
- The customer's confirmation comes before payment completes, so the UI shows a "processing" status.
- Every consumer must be idempotent because Pub/Sub delivers at least once.
- More moving parts than a single synchronous service, in exchange for resilience to slow or failing steps.
Related
Courses and lessons
- Amazon SQS: queues, FIFO, visibility timeout and dead-letter queues · AWS Solutions Architect – Associate (SAA-C03) · Members
- Amazon SNS and the fan-out pattern · AWS Solutions Architect – Associate (SAA-C03) · Members
- Amazon EventBridge: event buses, rules, Scheduler and Pipes · AWS Solutions Architect – Associate (SAA-C03) · Members
- AWS Step Functions: orchestrating workflows · AWS Solutions Architect – Associate (SAA-C03) · Members
- AWS Lambda · AWS Solutions Architect – Associate (SAA-C03) · Members
Services, comparisons and patterns
- Amazon SQS · Service
- Amazon SNS · Service
- Amazon EventBridge · Service
- AWS Step Functions · Service
- AWS Lambda · Service
- Amazon API Gateway · Service
- Amazon DynamoDB · Service
- SQS vs SNS vs EventBridge · Comparison