---
created_at: '2013-08-17T13:15:14.000Z'
title: Starbucks Does Not Use Two-Phase Commit (2004)
url: http://eaipatterns.com/ramblings/18_starbucks.html
author: gejjaxxita
points: 112
story_text: ''
comment_text: 
num_comments: 36
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1376745314
_tags:
- story
- author_gejjaxxita
- story_6229004
objectID: '6229004'

---
[Source](http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html "Permalink to Starbucks Does Not Use Two-Phase Commit - Enterprise Integration Patterns")

# Starbucks Does Not Use Two-Phase Commit - Enterprise Integration Patterns


| ----- |
|  ![][1] |  Gregor's Ramblings |  
|  [HOME][2]    [PATTERNS][3]    [RAMBLINGS][4]    [ARTICLES][5]    [TALKS][6]    [DOWNLOAD][7]    [BOOKS][8]    [CONTACT][9] |   |

* * *

| ----- |
| 

# Starbucks Does Not Use Two-Phase Commit

November 19, 2004

 | 

ABOUT ME

![Gregor Hohpe][10]

Hi, I am [Gregor Hohpe][9], co-author of the book [Enterprise Integration Patterns][3]. I like to work on and [write about][4] asynchronous messaging systems, service-oriented architectures, and all sorts of enterprise computing and architecture topics. I am also the Chief Architect at [Allianz SE][11], one of the largest insurance companies in the world. 

TOPICS

[ALL RAMBLINGS][4]

[Architecture (12)][12]  [Cloud (4)][13]  [Conversations (8)][14]  [Design (26)][15]  [Events (27)][16]  [Gregor (4)][17]  [Integration (13)][18]  [Messaging (12)][19]  [Modeling (5)][20]  [Patterns (8)][21]  [Visualization (3)][22]  [WebServices (5)][23]  [Writing (12)][24] 

POPULAR RAMBLINGS

[Starbucks does not use 2-phase commit][25]

[Is this architecture?][26]

[RESTful Conversations][27]

[Hub and Spoke][28]

[Correlation and Conversations][29]

[Diagram-driven Design][30]

RECENT

[Serverless Integration Patterns on Google Cloud Functions][31]

[Modern Examples for Enterprise Integration Patterns][32]

[37 Things or "Where have all my ramblings gone?"][33]

[25 Years of OOP][34]

[How to Scale an Organization? The same way you scale a system!][35]

[The Architect's Penthouse Bookshelf][36]

[If software eats the world, you better use version control!][37]

[If you never kill anything, you will live among zombies. And they will eat your brain.][38]

[Movie Star Architects][39]

 |  
| 

## Hotto Cocoa o Kudasai

I just returned from a 2 week trip to Japan. One of the more familiar sights was the ridiculous number of Starbucks (スターバックス) coffee shops, especially around Shinjuku and Roppongi. While waiting for my "Hotto Cocoa" I started to think about how Starbucks processes drink orders. Starbucks, like most other businesses is primarily interested in maximizing throughput of orders. More orders equals more revenue. As a result they use asynchronous processing. When you place your order the cashier marks a coffee cup with your order and places it into the queue. The queue is quite literally a queue of coffee cups lined up on top of the espresso machine. This queue decouples cashier and barista and allows the cashier to keep taking orders even if the barista is backed up for a moment. It allows them to deploy multiple baristas in a [Competing Consumer][40] scenario if the store gets busy. 

## Correlation

By taking advantage of an asynchronous approach Starbucks also has to deal with the same challenges that asynchrony inherently brings. Take for example, correlation. Drink orders are not necessarily completed in the order they were placed. This can happen for two reasons. First, multiple baristas may be processing orders using different equipment. Blended drinks may take longer than a drip coffee. Second, baristas may make multiple drinks in one batch to optimize processing time. As a result, Starbucks has a correlation problem. Drinks are delivered out of sequence and need to be matched up to the correct customer. Starbucks solves the problem with the same "pattern" we use in messaging architectures -- they use a [Correlation Identifier][41]. In the US, most Starbucks use an explicit correlation identifier by writing your name on the cup and calling it out when the drink is complete. In other countries, you have to correlate by the type of drink. 

## Exception Handling

Exception handling in asynchronous messaging scenarios can be difficult. If the real world writes the best stories maybe we can learn something by watching how Starbucks deals with exceptions. What do they do if you can't pay? They will toss the drink if it has already been made or otherwise pull your cup from the "queue". If they deliver you a drink that is incorrect or nonsatisfactory they will remake it. If the machine breaks down and they cannot make your drink they will refund your money. Each of these scenarios describes a different, but common error handling strategy: 

* **Write-off** \- This error handling strategy is the simplest of all: do nothing. Or discard what you have done. This might seem like a bad plan but in the reality of business this option might be acceptable. If the loss is small it might be more expensive to build an error correction solution than to just let things be. For example, I worked for a number of ISP providers who would chose this approach when there was an error in the billing / provisioning cycle. As a result, a customer might end up with active service but would not get billed. The revenue loss was small enough to allow the business to operate in this way. Periodically, they would run reconciliation reports to detect the "free" accounts and close them. 
* **Retry** \- When some operations of a larger group (i.e. "transaction") fail, we have essentially two choices: undo the ones that are already done or retry the ones that failed. Retry is a plausible option if there is a realistic chance that the retry will actually succeed. For example, if a business rule is violated it is unlikely a retry will succeed. However, if an external system is not available a retry might well be successful. A special case is a retry with [Idempotent Receiver][42]. In this case we can simply retry all operations since the successful receivers will ignore duplicate messages. 
* **Compensating Action** \- The last option is to undo operations that were already completed to put the system back into a consistent state. Such "compensating actions" work well for example if we deal with monetary systems where we can recredit money that has been debited. 

All of these strategies are different than a two-phase commit that relies on separate prepare and execute steps. In the Starbucks example, a two-phase commit would equate to waiting at the cashier with the receipt and the money on the table until the drink is finished. Then, the drink would be added to the mix. Finally the money, receipt and drink would change hands in one swoop. Neither the cashier nor the customer would be able to leave until the "transaction" is completed. Using such a two-phase-commit approach would certainly kill Starbucks' business because the number of customers they can serve within a certain time interval would decrease dramatically. This is a good reminder that a two-phase-commit can make life a lot simpler but it can also hurt the free flow of messages (and therefore the scalability) because it has to maintain stateful transaction resources across the flow of multiple, asynchronous actions. 

## Conversations

The coffee shop interaction is also a good example of a simple, but common [Conversation][43] pattern. The interaction between two parties (customer and coffee shop) consists of a short synchronous interaction (ordering and paying) and a longer, asynchronous interaction (making and receiving the drink). This type of conversation is quite common in purchasing scenarios. For example, when placing an order on Amazon the short synchronous interaction assigns an order number and all subsequent steps (charging credit card, packaging, shipping) are done asynchronously. You are notified via e-mail (asynchronous) when the additional steps complete. If anything goes wrong, Amazon usually compensates (refund to credit card) or retries (resend lost goods). 

In summary we can see that the real world is often asynchronous. Our daily lives consists of many coordinated, but asynchronous interactions (reading and replying to e-mail, buying coffee etc). This means that an asynchronous messaging architecture can often be a natural way to model these types of interactions. It also means that often we can look at daily life to help design successful messaging solutions. Domo arigato gozaimasu! 

## Allianz Does Use Two-phase Commit

Can you explain how to design error handling strategies in loosely coupled systems using a coffee shop as example? Can you explain the difference between PaaS, Docker, and Kubernetes to senior IT management and recommend their role in an on-premise cloud strategy? Or can you argue why test coverage percentages are meaningless but love to implement automated build, test, and deployment mechanisms? Then why not join my growing team at [Allianz][44] in Munich. We are in the midst of one of the largest IT infrastructure and application landscape transformations in the industry to enable Allianz' digital business. That's why we need bright heads who get their hands dirty! Apply via Linkedin as a [Senior Cloud Infrastructure Architect][45] or [Principal Software Architect][45]. We'd love to hear from you. 

 |   | 
| 

_Share:_      [Tweet][46]      

_Follow:_    [Follow @ghohpe][47]   ![Subscribe][48]  [SUBSCRIBE TO FEED][49]

_More On:_  [DESIGN][15]  [CONVERSATIONS][14]     [ALL RAMBLINGS][4]   

![][50]

 | 

* * *

![][51] Gregor is the Chief IT Architect of Allianz SE. He is a frequent speaker on asynchronous messaging and service-oriented architectures and co-authored _Enterprise Integration Patterns_ (Addison-Wesley). His mission is to make integration and distributed system development easier by harvesting common patterns and best practices from many different technologies.  
[www.eaipatterns.com][52]

* * *

| ----- |
|    |  © 2003-2017  • All rights reserved.  | 

* * *

![][53]

[1]: http://www.enterpriseintegrationpatterns.com/img/eip.gif
[2]: http://www.enterpriseintegrationpatterns.com/index.html
[3]: http://www.enterpriseintegrationpatterns.com/eaipatterns.html
[4]: http://www.enterpriseintegrationpatterns.com/ramblings.html
[5]: http://www.enterpriseintegrationpatterns.com/articles.html
[6]: http://www.enterpriseintegrationpatterns.com/talks.html
[7]: http://www.enterpriseintegrationpatterns.com/downloads.html
[8]: http://www.enterpriseintegrationpatterns.com/books1.html
[9]: http://www.enterpriseintegrationpatterns.com/gregor.html
[10]: http://www.enterpriseintegrationpatterns.com/img/gregor_small.jpg
[11]: http://www.allianz.com/en/products_solutions/global_lines/amos/amos.html
[12]: http://www.enterpriseintegrationpatterns.com/ramblings_architecture.html
[13]: http://www.enterpriseintegrationpatterns.com/ramblings_cloud.html
[14]: http://www.enterpriseintegrationpatterns.com/ramblings_conversations.html
[15]: http://www.enterpriseintegrationpatterns.com/ramblings_design.html
[16]: http://www.enterpriseintegrationpatterns.com/ramblings_events.html
[17]: http://www.enterpriseintegrationpatterns.com/ramblings_gregor.html
[18]: http://www.enterpriseintegrationpatterns.com/ramblings_integration.html
[19]: http://www.enterpriseintegrationpatterns.com/ramblings_messaging.html
[20]: http://www.enterpriseintegrationpatterns.com/ramblings_modeling.html
[21]: http://www.enterpriseintegrationpatterns.com/ramblings_patterns.html
[22]: http://www.enterpriseintegrationpatterns.com/ramblings_visualization.html
[23]: http://www.enterpriseintegrationpatterns.com/ramblings_webServices.html
[24]: http://www.enterpriseintegrationpatterns.com/ramblings_writing.html
[25]: http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html
[26]: http://www.enterpriseintegrationpatterns.com/ramblings/86_isthisarchitecture.html
[27]: http://www.enterpriseintegrationpatterns.com/ramblings/81_restconversation.html
[28]: http://www.enterpriseintegrationpatterns.com/ramblings/03_hubandspoke.html
[29]: http://www.enterpriseintegrationpatterns.com/ramblings/09_correlation.html
[30]: http://www.enterpriseintegrationpatterns.com/ramblings/75_ddd.html
[31]: http://www.enterpriseintegrationpatterns.com/google_cloud_functions.html
[32]: http://www.enterpriseintegrationpatterns.com/eip1_examples_updated.html
[33]: http://www.enterpriseintegrationpatterns.com/94_37things.html
[34]: http://www.enterpriseintegrationpatterns.com/93_oop2016.html
[35]: http://www.enterpriseintegrationpatterns.com/92_scaling.html
[36]: http://www.enterpriseintegrationpatterns.com/91_bookshelfpenthouse.html
[37]: http://www.enterpriseintegrationpatterns.com/90_softwareeatstheworld.html
[38]: http://www.enterpriseintegrationpatterns.com/89_zombies.html
[39]: http://www.enterpriseintegrationpatterns.com/88_moviestararchitects.html
[40]: http://www.enterpriseintegrationpatterns.com/CompetingConsumers.html
[41]: http://www.enterpriseintegrationpatterns.com/CorrelationIdentifier.html
[42]: http://www.enterpriseintegrationpatterns.com/IdempotentReceiver.html
[43]: http://www.enterpriseintegrationpatterns.com/09_correlation.html
[44]: http://www.allianz.com
[45]: https://www.linkedin.com/jobs2/view/136775174
[46]: https://twitter.com/share
[47]: https://twitter.com/ghohpe
[48]: http://www.enterpriseintegrationpatterns.com/img/feed_icon_18.png
[49]: http://www.enterpriseintegrationpatterns.com/ramblings.rss
[50]: http://www.enterpriseintegrationpatterns.com/img/37things_468_60.png "37 Things one Architect Knows"
[51]: http://www.enterpriseintegrationpatterns.com/img/Gregor_Portrait_75_90.jpg
[52]: http://www.eaipatterns.com
[53]: http://wms-na.amazon-adsystem.com/20070822/US/img/noscript.gif?tag=enterpriseint-20&linkId=3WNGXJ3FRN6XMQAW

