Fundamentals 8 min read

What Starbucks Can Teach Us About Two-Phase Commit and Asynchronous Messaging

The article uses Starbucks' coffee‑ordering process to illustrate asynchronous order handling, correlation identifiers, exception‑handling strategies, two‑phase commit, and conversation patterns, showing how real‑world practices inform the design of reliable distributed messaging systems.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
What Starbucks Can Teach Us About Two-Phase Commit and Asynchronous Messaging

Translator's Preface

This translation is based on the 2004 article "Starbucks Does Not Use Two-Phase Commit" and may contain omissions or errors.

1. Hot Chocolate Request (Hotto Cocoa o Kudasai)

Observing Starbucks in Japan, the author notes that the company maximizes orders by processing them asynchronously: the cashier marks the cup, places it in a queue of cups on the coffee machine, decoupling the cashier from the barista.

This queue allows multiple baristas to work in a Competing Consumer scenario when demand spikes.

2. Correlation

Because coffee completion order may differ from order placement, Starbucks uses explicit correlation identifiers (e.g., the customer's name) to match each finished drink to the correct customer.

In the US, the customer's name is written on the cup.

In other regions, the drink type may serve as the identifier.

3. Exception Handling

Starbucks demonstrates several error‑handling strategies that map to messaging systems:

Write‑off: discard the product or remove the cup from the queue.

Retry: remake the drink if it was prepared incorrectly.

Compensating Action: refund the customer if the machine fails.

3.1 Write‑off

The simplest strategy—do nothing or discard the work—can be acceptable when the loss is negligible.

3.2 Retry

When a subset of operations fails, either undo completed work or retry the failed steps; idempotent receivers allow blind retries.

3.3 Compensating Action

Undo all completed operations to return the system to a consistent state, e.g., refunding a failed financial transaction.

4. Two‑Phase Commit

Unlike the previous strategies, two‑phase commit involves a prepare phase (order placed, receipt printed, cash held) and an execute phase (cash, receipt, and coffee exchanged simultaneously). This would dramatically reduce throughput in a coffee shop, illustrating why two‑phase commit can hinder scalability.

5. Conversation Pattern

The Starbucks interaction exemplifies a short synchronous request (order & payment) followed by a longer asynchronous process (drink preparation and delivery). Similar patterns appear in e‑commerce, where order IDs are assigned synchronously and subsequent steps occur asynchronously, often requiring compensation or retry mechanisms.

Observing everyday asynchronous workflows helps design effective messaging solutions.

Thank you for reading!

Distributed SystemsError Handlingcorrelationasynchronous messagingTwo-Phase Commit
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.