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

ComponentRole
Load balancerPublic HTTPS entry with managed TLS; Cloud Armor rate-limits and filters abusive traffic.
Intake serviceValidates the order, writes it to Firestore with the idempotency key, publishes OrderPlaced.
Orders storeStores orders and their status; the idempotency key as document ID prevents duplicates.
Order eventsTopic with one subscription per consumer, retries, and a dead-letter topic.
Payment workerCharges the customer idempotently and updates the order status.
Notification functionSends the confirmation email when the order is placed.
AnalyticsA BigQuery subscription streams every event into a table for reporting.

How the pieces connect

  1. Load balancer → Intake service: forwards the request
  2. Intake service → Orders store: creates the order (idempotency key as ID)
  3. Intake service → Order events: publishes OrderPlaced
  4. Order events → Payment worker: push subscription
  5. Order events → Notification function: Eventarc trigger
  6. 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

Services, comparisons and patterns

Sources

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake