Can someone provide a low level example of 2 phase commit (2pc)? Having a hard time understanding it.

Samurai 1 Reputation point

Orders and Payment service example is the most popular example on the internet.

Let's say we have Orders service and Payments service. When an order is placed, the Order service writes it to its database but the payment service must also write it to its own database in order for the transaction to be complete. Here is my understanding:

  1. User sends a place order request to Orchestrator
  2. Orchestrator invokes Orders service to as well as Payment service at the same time. Now according to what I have read, Order and Payment services are supposed to respond to Orchestrator by telling it whether or not they are ready. What does that mean? What does it mean to be ready here?
  3. Order and Payment service respond back, telling Orchestrator that they are "ready" (whatever that means).
  4. Orchestrator sends another request to both the services (commit request).
  5. Order writes the record to its database. The Payment service writes the record to its own database. They both respond back with status 200 to Orchestrator.
  6. Orchestrator checks if both of the participants have returned status code 200. If yes, then it does nothing. If no then it asks them to ABORT?? How? One of the participants already wrote the transaction to its database.
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,793 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,139 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tom Phillips 17,721 Reputation points

    I suggest you read this:

    Issues and considerations
    Consider the following points when implementing the saga pattern:
    Data can't be rolled back, because saga participants commit changes to their local databases.

    0 comments No comments