# Two-phase commit (2pc) Pattern

Motivation

Coordinates all the services that participate in a distributed atomic transaction on whether to commit or rollback the transaction.

### Solution

Steps:

* **commit-request phase** - the coordinator process attempts to prepare all the transaction's participating processes to take the necessary steps for either committing or aborting the transaction and to vote
  * sends a query to commit message to all participants and waits until it has received a reply from all participants.
  * participants execute the transaction up to the point where they will be asked to commit.
  * each participant replies with an agreement message
* **commit phase** - based on voting of the participants, the coordinator decides whether to commit/abort
  * success:
    1. The coordinator sends a commit message to all the participants.
    2. Each participant completes the operation, and releases all the locks and resources held during the transaction.
    3. Each participant sends an acknowledgement to the coordinator.
    4. The coordinator completes the transaction when all acknowledgements have been received.
  * failure:
    1. The coordinator sends a rollback message to all the participants.
    2. Each participant undoes the transaction using the undo log, and releases the resources and locks held during the transaction.
    3. Each participant sends an acknowledgement to the coordinator.
    4. The coordinator undoes the transaction when all acknowledgements have been received.

<figure><img src="/files/dV2FpYs2H23GTBsjkPqS" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/5108DkzkoIUqYTiQ9x7K" alt=""><figcaption></figcaption></figure>

### Cons

The coordinator is a single point of failure.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dianadarie.gitbook.io/system-design-notes/data-synchronisation-patterns/cqrs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
