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

Samurai 1 Reputation point
2021-07-10T04:40:58.83+00:00

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,604 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.
12,700 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tom Phillips 17,716 Reputation points
    2021-08-19T14:10:25.117+00:00

    I suggest you read this:

    https://learn.microsoft.com/en-us/azure/architecture/reference-architectures/saga/saga

    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