Skip to content
Visual explainer 5h to learn 5 steps

Google Cloud capstone: design the event-driven order service

Produce a project and identity map, request and event diagrams, a failure plan, a budget and an RPO and RTO for the order service.

By Invicly Tech, , in Google Cloud Engineering and Services. About 9 minutes to read.

Topic 30 of 32

In short

Before you build, you design. For the order service you decide which projects exist, which service account does what, how a request becomes an event, and what happens when something fails. You also write down the budget, the teardown list and the recovery targets.

Think of it like this: It is the floor plan and fire plan for a building: you draw the rooms, the doors and the exits before anyone lays a brick.

Explained simply

The capstone is a small order service. A customer sends an order to a web service. That service does not do all the work itself. It drops a note, called an event, into a message system. A second service picks up the note and saves it.

Why split it like this? If the saving part is slow or broken, orders are not lost. The notes wait in line. This is the main idea of an event-driven app: the parts talk through messages, not through direct calls.

This first topic is paper work. You decide who is allowed to do what, draw how a request flows, and plan for things going wrong. Planning first is cheaper than fixing later, and it gives you a checklist for the build.

You also write numbers down. How much money may this cost? How much data can we lose? How long can we be down? These numbers are called budget, RPO and RTO, and they guide every design choice.

Finally you list everything you will create, so you can delete it all at the end. In the cloud, forgotten resources keep costing money.

Press Play on a diagram to walk through it one step at a time.

SequenceRequest and event flow for one order
Client
Producer (Cloud Run)
Pub/Sub
Consumer (Cloud Run)
  1. 1POST /orders with an ID token
    Client to Producer (Cloud Run): POST /orders with an ID token
  2. 2Publish order-created event
    Producer (Cloud Run) to Pub/Sub: Publish order-created event
  3. 3202 Accepted with an order ID
    Producer (Cloud Run) to Client: 202 Accepted with an order ID
  4. 4Push subscription delivers the event
    Pub/Sub to Consumer (Cloud Run): Push subscription delivers the event
  5. 5Store event, skip if the ID was seen
    Consumer (Cloud Run) to Consumer (Cloud Run): Store event, skip if the ID was seen
  6. 6Return 2xx to acknowledge
    Consumer (Cloud Run) to Pub/Sub: Return 2xx to acknowledge
MapProject and identity map
Project: orders-dev (one per environment)
  • Service accounts
    • producer-sa: publish to one topic
    • consumer-sa: write to storage
    • deployer-sa: used by Terraform
  • Messaging
    • Topic: order-events
    • Push subscription to consumer
    • Dead-letter topic: order-events-dlq
  • Storage
    • Cloud Storage bucket or BigQuery dataset
  • Guardrails
    • Billing budget with alerts
    • Labels on every resource
LayersLayers of the order service
  1. 1
    Clients

    Browser, mobile app or test script.

  2. 2
    Producer service

    Cloud Run, authenticated, publishes events.

  3. 3
    Messaging

    Pub/Sub topic, push subscription, dead-letter topic.

  4. 4
    Consumer service

    Cloud Run, handles duplicates, writes data.

  5. 5
    Storage and analysis

    Cloud Storage or BigQuery.

  6. 6
    Operations

    Cloud Logging, Monitoring, billing budget.

In depth

Projects and service identities

A Google Cloud project is the boundary for resources, billing and IAM. Use one project per environment, such as orders-dev and orders-prod, so a mistake in dev cannot touch production. Inside a project, every workload runs as a service account. The producer needs to publish to one topic. The consumer needs to write to one bucket or dataset. A deployer identity is used by Terraform. Give each only the role it needs, on the smallest resource that works. Avoid the basic Owner and Editor roles for workloads. Turn on audit logs for IAM changes so you can see who granted what.

  • One service account per service
  • Predefined or custom roles, not basic roles
  • Grant on the resource, not the whole project, where possible
  • Label every resource with app and env

Retries, dead letters and replay

Pub/Sub delivers each message at least once. If the consumer returns an error, or does not answer in time, Pub/Sub tries again with a delay you can set as a retry policy with minimum and maximum backoff. After a set number of attempts a dead-letter policy moves the message to another topic, so one bad message does not block the rest. To replay later, either keep acknowledged messages on the subscription and use seek, or republish from the dead-letter topic once the bug is fixed. Exactly-once delivery exists, but only for pull subscriptions in one region, so a push design still needs idempotent handling.

FlowLife of a failing message
  1. 1
    Delivered

    Push to the consumer.

  2. 2
    Fails

    Consumer returns 500.

  3. 3
    Backoff

    Pub/Sub waits, then retries.

  4. 4
    Attempts used

    Delivery attempts reach the limit.

  5. 5
    Dead letter

    Message moves to the DLQ topic.

  6. 6
    Replay

    Fix the bug, then republish.

Budget, teardown and targets

A Cloud Billing budget sends alerts at thresholds such as 50, 90 and 100 percent of an amount. It does not stop spending by itself. For a short exercise that is fine, because you also own the teardown list. For recovery, pick numbers you can test. An RPO of zero for stored orders is natural, because Pub/Sub keeps messages until they are acknowledged or retention ends. An RTO of fifteen minutes means you must be able to redeploy from Terraform in that time. Turn those into an SLO, such as 99.5 percent of orders stored within one minute over thirty days.

  • Budgets alert, they do not cap
  • RPO and RTO must be testable, not hopeful
  • An SLO needs a measurement and a window

Learn it in 5 short steps

About 1h each. Track them with a timer in your plan.

  1. 1
    Map projects and service identities~1h
  2. 2
    Draw request and event flows~1h
  3. 3
    Plan retries, dead letters and replay~1h
  4. 4
    Set budget and teardown inventory~1h
  5. 5
    Define RPO, RTO and an SLO~1h

Key points

  • Give each environment its own project, and give each service its own service account with one narrow role.
  • Pub/Sub delivers at least once, so the consumer must cope with duplicates and the design must say how.
  • Plan failure first: retry with backoff, then a dead-letter topic, then replay once the bug is fixed.
  • Set recovery targets in numbers: RPO is how much data you can lose, RTO is how long you can be down.
  • Write the teardown inventory now, so nothing is left running and billing when the exercise ends.

Key terms

Tap a card to flip it. 0/6 known

Examples

A project and identity map in plain text

Write the map as a small table before any code. Each row says who, may do what, on which resource. If a row needs the word all, look for a narrower grant. This map becomes the Terraform IAM resources in the next topic.

text
project: orders-dev
producer-sa  -> roles/pubsub.publisher on topic order-events
consumer-sa  -> roles/storage.objectCreator on bucket orders-dev-events
push-sa      -> roles/run.invoker on service consumer
deployer-sa  -> project deploy roles (used by Terraform only)
users        -> roles/run.invoker on service producer

Setting the targets for the order service

Suppose orders are low volume and a short delay is fine. Choose an SLO of 99.5 percent of orders stored within 60 seconds over 30 days. That allows about 3.6 hours of bad time in 30 days if measured as time, or 1 in 200 orders if measured as events. Choose an RTO of 30 minutes, because a Terraform redeploy plus a replay fits in that time, and an RPO near zero, because unacknowledged messages stay in Pub/Sub. Write the numbers and how you will measure them.

Quick reference

  • One project per environment; one service account per service.
  • Grant roles on the smallest resource that works.
  • Pub/Sub is at-least-once; design consumers to be idempotent.
  • Retry policy then dead-letter policy, then replay by seek or republish.
  • Billing budgets send alerts; they do not stop spending.
  • RPO is data you can lose; RTO is time you can be down.
  • Write the teardown list before you create anything.

Common mistakes

  • Using one broad service account for everything, so a bug in one service can touch all resources.
  • Assuming a budget stops spending. A budget only sends alerts unless you build automation on top of it.
  • Drawing the happy path only, with no retry, dead-letter or replay plan.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Why use one service account per service?

Question 2

2What does a dead-letter topic do?

Question 3

3A billing budget set to 100 dollars will:

Question 4

4An RTO of 30 minutes means:

Question 5

5Why must the consumer handle duplicates?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    List five resources for the order service and give each a label app=orders and env=dev.

  2. Exercise 2medium

    Write the identity map for three service accounts with one role each.

  3. Exercise 3hard

    Write a failure plan: retry settings, dead-letter limit, replay steps and one SLO.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need a real Google Cloud project for the design?
No. The design is paper work. You need a project, and a billing account, only when you deploy in the next topic.
Why Pub/Sub instead of calling the consumer directly?
Messages wait in line if the consumer is down or slow, so orders are not lost and the two services can scale separately.
Is exactly-once delivery available?
Pub/Sub offers it for pull subscriptions under some conditions, not for push. Idempotent handling is the safer design.

Go deeper

ShareXLinkedInWhatsApp