GOAT EngineerPreparing your learning space…

System Design · Practice problem · Medium

Save an Order and Publish Its Event

A transactional outbox keeps both together: write the order and its event to one database in the same transaction, then let a separate relay publish saved events until they land. Readers must treat a repeat as harmless.

Medium means two needs that pull against each other. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.

What is the situation?

Your shop keeps every order in its Orders database. Billing, which another team runs, charges for an order once it hears about it, and other teams want to hear about orders later too. Customers place 500 orders a second and give up after 2 seconds. Billing can write 1,000 records a second, and it records every write it is given as a new charge unless you give it a unique key. Customers sometimes press pay twice, and both presses carry the same order ID. Design the path from POST /orders to Billing, so that customers are answered quickly, trouble behind the save does not cost you a customer, and Billing ends up with every order exactly once. We count every order a customer tries to place, so an order your design refuses counts as one Billing never saw.

What must your design do?

  • Customers are answered within 100 ms at the 99th percentile.
  • At least 99.9% of orders are accepted, including while anything behind your Orders database is down.
  • Every order a customer tries to place reaches Billing, and none reaches it twice.
  • The slowest order reaches Billing within 10 seconds normally, and within 30 seconds after a 15 second outage.
  • Every order is saved in your Orders database.
  • The system costs $800 a month or less.

What does Run test?

Run works without an account. It checks your design against 2 tests you can read before you start:

  • A normal lunchtime. 500 orders a second for 30 seconds, and then 10 quiet seconds for the last orders to arrive.
  • Customers press pay twice. 5% of customers pressed pay a second time, one second later, with the same order ID.

What does Submit add?

Submit needs an account. It runs 3 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.

Which parts can you use?

Already on your canvas: Orders, Billing.

  • Load Balancer: about 50,003 a second, $18 a month each
  • App Server: about 2,000 a second, $45 a month each
  • Queue: effectively unlimited, $25 a month each
  • Worker: about 200 a second, $45 a month each
  • Database: about 5,000 a second, $120 a month each
  • Outbox Relay: about 5,000 a second, $40 a month each

Every connection takes 1 ms, and the budget is $800 a month.

What does this problem leave out?

  • Your Orders database going down. Every design refuses customers then, and our count cannot yet tell a refused order Billing rightly never saw from one it wrongly did, so no test crashes Orders.
  • Whether a row in Orders and a row at Billing come from one write. We check that orders reach Orders and we count what reaches Billing, so a second database that feeds the relay passes here, though it is still two writes.
  • What a transaction costs. The order and its outbox row count as one write.
  • A second reader of the event. Only Billing is counted, so a relay that calls Billing directly also passes.
  • How fast a relay recovers. Rows a dead relay was holding come back when its lease runs out, five minutes unless you set it, even if the relay restarts sooner. No bar grades that wait.
  • A server that reads "already saved" and stops. Here a server makes every call it is connected for, so a repeat that Orders refused still goes on to a queue the server also calls.
  • Cleaning old rows out of the outbox table, and how large it grows.
  • The order in which Billing hears about events.

Where does this problem fit?

Its category is Money & transactions: Correctness comes before speed. It practises consistency, queues & async, idempotency and reliability & ops.

See every practice problem.

By GOAT Engineer. Explore System Design · Privacy Policy · Contact.