Skip to contentমূল লেখায় যান

Topic 25 · Courseটপিক 25 · Course

Distributed Transactions

When you pay for a trip, two things must change together: the shop must keep your order, and the bank must take the money. If only one change happens, someone loses. A distributed transaction is a way to keep such changes together when they live in different places.

6 min read6 মিনিটে পড়া যায় · 7 sections7টি সেকশন · 6 sources6টি সোর্স · updated Nov 2026আপডেট: Nov 2026

Two-phase commit versus sagaTwo-phase commit বনাম saga

Make one step fail and see 2PC roll back while a saga compensates.একটা ধাপ ব্যর্থ করান আর দেখুন 2PC রোলব্যাক করে, আর saga আন্ডু করে।

2 min2 মিনিট7 steps7টি ধাপ
  • Locks heldধরা লক0
  • Committed, to undoকমিট, আন্ডু বাকি0
Managerম্যানেজারone business actionএকটা বিজনেস কাজ1 · Order1 · অর্ডার2 · Payment2 · পেমেন্ট3 · Stock3 · স্টক

You’ll learnআপনি শিখবেন

  • How 2PC prepares, then commits or rolls back2PC কীভাবে প্রিপেয়ার করে, তারপর কমিট বা রোলব্যাক
  • Why a prepared transaction keeps locksপ্রিপেয়ার করা ট্রানজেকশন কেন লক ধরে রাখে
  • How a saga commits step by stepSaga কীভাবে ধাপে ধাপে কমিট করে
  • What a compensating transaction undoescompensating transaction কী আন্ডু করে

In plain words

A distributed transaction is one business action that changes more than one database or service. Many systems give each microservice its own database, so it is more complex to keep ACID guarantees across these separate stores Source 5সোর্স 5.

Two main ways exist: two-phase commit (2PC), where all databases get ready first and then commit or roll back together, and the saga, where each service commits a small step and later steps undo it if needed.

সহজ English

A distributed transaction is one business action that changes more than one database or service. Many systems give each microservice its own database. So it is more complex to keep ACID guarantees across these separate stores Source 5সোর্স 5.

There are two main ways. One is two-phase commit (2PC). All databases get ready first. Then they commit or roll back together. The other is the saga. Each service commits a small step. Later steps undo it if needed.

বাংলা ব্যাখ্যা

ডিস্ট্রিবিউটেড ট্রানজেকশন (distributed transaction) হলো এমন একটা বিজনেস অ্যাকশন যা একাধিক ডেটাবেস বা সার্ভিস বদলায়। অনেক সিস্টেম প্রতিটা মাইক্রোসার্ভিসকে তার নিজের ডেটাবেস দেয়। তাই এই আলাদা স্টোরগুলোর মধ্যে ACID গ্যারান্টি ধরে রাখা আরও জটিল Source 5সোর্স 5।

প্রধান দুটো উপায় আছে। একটা two-phase commit (2PC)। এতে সব ডেটাবেস আগে প্রস্তুত হয়, তারপর একসঙ্গে কমিট (commit) বা রোলব্যাক করে। অন্যটা saga। এতে প্রতিটা সার্ভিস একটা ছোট ধাপ কমিট করে। দরকার হলে পরের ধাপগুলো সেটা আন্ডু (undo) করে।

When you need it

You need this topic when one user action touches more than one database or service, such as an order and a payment.

সহজ English

You need this topic when one user action touches more than one database or service, such as an order and a payment.

বাংলা ব্যাখ্যা

যখন একটা ইউজার অ্যাকশন একাধিক ডেটাবেস বা সার্ভিস ছোঁয়, যেমন একটা অর্ডার আর একটা পেমেন্ট, তখন এই বিষয়টা দরকার।

How it works

Two-phase commit

PostgreSQL gives the building blocks of 2PC, but not the whole protocol. The docs say the purpose of these commands is to let an external transaction manager run atomic global transactions Source 1সোর্স 1. These can run across several databases or other transactional resources Source 1সোর্স 1. The docs say that, unless you write a transaction manager, you probably should not use PREPARE TRANSACTION Source 1সোর্স 1.

For each database, the steps look like this:

  1. Inside a transaction block, the manager sends the work. Then it runs PREPARE TRANSACTION 'some-id'. This command needs a transaction id, which is a string of fewer than 200 bytes Source 1সোর্স 1.
  2. After the prepare, the transaction is no longer tied to the session. Its state is fully stored on disk Source 1সোর্স 1.
  3. When the manager has checked that the other databases are also prepared to commit, it commits the transaction with COMMIT PREPARED Source 1সোর্স 1. If not, it uses ROLLBACK PREPARED Source 1সোর্স 1.
manager --> database A: PREPARE TRANSACTION 'tx1'
manager --> database B: PREPARE TRANSACTION 'tx1'
   both prepared?
     yes --> COMMIT PREPARED 'tx1' on A and on B
     no  --> ROLLBACK PREPARED 'tx1' on each database that prepared
             (a failed PREPARE is already a rollback)

COMMIT PREPARED and ROLLBACK PREPARED can run in a different session from the one that did the prepare Source 1সোর্স 1. Both commit or roll back at once Source 2সোর্স 2 Source 3সোর্স 3.

What 2PC gives you, as the docs state it. After a prepare, there is a "very high probability" that the commit can succeed, even if the database crashes before the commit Source 1সোর্স 1. The docs say probability, so do not read it as a guarantee.

What 2PC costs, as the docs state it. A prepared transaction keeps the locks that it held Source 1সোর্স 1. If it stays prepared for a long time, it interferes with VACUUM Source 1সোর্স 1. In extreme cases, it can make the database shut down to prevent transaction ID wraparound Source 1সোর্স 1. The docs call it unwise to leave a transaction prepared for long Source 1সোর্স 1.

Default setting. The feature is off by default: max_prepared_transactions is 0, and it can only be set at server start Source 4সোর্স 4. Without a manager that closes prepared transactions promptly, the docs advise to keep it at 0 Source 1সোর্স 1. The view pg_prepared_xacts lists them Source 1সোর্স 1.

Saga

Local transactions. A saga breaks the work into a sequence of local transactions. Each one completes in one service, updates that service's database, and starts the next step with an event or a message Source 5সোর্স 5. If a step fails, the saga runs compensating transactions to reverse the changes of the earlier steps Source 5সোর্স 5.

Compensation. A compensating transaction is not a simple rollback. A plain restore of the old state could overwrite changes from other users, so the compensation must be application specific Source 6সোর্স 6. The system records how to undo each step Source 6সোর্স 6.

Point of no return. A saga has a pivot transaction: the point of no return. After it succeeds, all later steps must finish. These later steps can be retried, and they are idempotent Source 5সোর্স 5.

There are two ways to run a saga Source 5সোর্স 5:

WayHowGoodBad
ChoreographyServices exchange events. There is no central controller.Fits simple workflows Source 5সোর্স 5Gets confusing when steps grow, and integration tests need all services running Source 5সোর্স 5
OrchestrationA central orchestrator tells each service what to do and handles failure recoverySuits complex workflows Source 5সোর্স 5The orchestrator is a point of failure Source 5সোর্স 5

সহজ English

Two-phase commit

PostgreSQL gives the building blocks of 2PC, but not the whole protocol. The docs say these commands let an external transaction manager run atomic global transactions across several databases or other transactional resources Source 1সোর্স 1. The docs say that you probably should not use PREPARE TRANSACTION, unless you write a transaction manager Source 1সোর্স 1.

For each database, the steps are:

  1. Inside a transaction block, the manager sends the work. Then it runs PREPARE TRANSACTION 'some-id'. This command needs a transaction id. The id is a string of fewer than 200 bytes Source 1সোর্স 1.
  2. After the prepare, the transaction is no longer tied to the session. Its state is fully stored on disk Source 1সোর্স 1.
  3. The manager checks that the other databases are also prepared to commit. Then it commits the transaction with COMMIT PREPARED Source 1সোর্স 1. If not, it uses ROLLBACK PREPARED Source 1সোর্স 1.

COMMIT PREPARED and ROLLBACK PREPARED can run in a different session from the one that did the prepare Source 1সোর্স 1. Both commit or roll back at once Source 2সোর্স 2 Source 3সোর্স 3.

What 2PC gives you, as the docs state it. After a prepare, there is a "very high probability" that the commit can succeed, even if the database crashes before the commit Source 1সোর্স 1. The docs say probability. Do not read it as a guarantee.

What 2PC costs, as the docs state it. A prepared transaction keeps the locks that it held Source 1সোর্স 1. If it stays prepared for a long time, it interferes with VACUUM. In extreme cases, it can make the database shut down to prevent transaction ID wraparound Source 1সোর্স 1. The docs call it unwise to leave a transaction prepared for long Source 1সোর্স 1.

The feature is off by default. max_prepared_transactions is 0, and you can set it only at server start Source 4সোর্স 4. Without a manager that closes prepared transactions promptly, the docs advise to keep it at 0 Source 1সোর্স 1. The view pg_prepared_xacts lists prepared transactions Source 1সোর্স 1.

Saga

A saga breaks the work into a sequence of local transactions. Each one completes in one service and updates the database of that service. It starts the next step with an event or a message Source 5সোর্স 5. If a step fails, the saga runs compensating transactions. They reverse the changes of the earlier steps Source 5সোর্স 5.

A compensating transaction is not a simple rollback. A plain restore of the old state could overwrite changes from other users. So the compensation must be application specific Source 6সোর্স 6. The system records how to undo each step Source 6সোর্স 6.

A saga has a pivot transaction. It is the point of no return. After it succeeds, all later steps must finish. These later steps can be retried, and they are idempotent Source 5সোর্স 5.

There are two ways to run a saga Source 5সোর্স 5:

বাংলা ব্যাখ্যা

Two-phase commit

PostgreSQL 2PC-র বিল্ডিং ব্লক দেয়, কিন্তু পুরো প্রোটোকল দেয় না। ডকুমেন্টেশন বলে এই কমান্ডগুলোর উদ্দেশ্য হলো একটা এক্সটারনাল ট্রানজেকশন ম্যানেজার-কে (external transaction manager) একাধিক ডেটাবেস বা অন্য ট্রানজেকশনাল রিসোর্স জুড়ে অ্যাটমিক (atomic) গ্লোবাল ট্রানজেকশন চালাতে দেওয়া Source 1সোর্স 1। ডকুমেন্টেশন বলে, নিজে ট্রানজেকশন ম্যানেজার না লিখলে আপনার সম্ভবত PREPARE TRANSACTION ব্যবহার করা উচিত নয় Source 1সোর্স 1।

প্রতিটা ডেটাবেসের জন্য ধাপগুলো এই রকম:

  1. ট্রানজেকশন ব্লকের ভেতরে ম্যানেজার কাজ পাঠায়। তারপর সে PREPARE TRANSACTION 'some-id' চালায়। এই কমান্ডে একটা ট্রানজেকশন আইডি লাগে। আইডি হলো 200 বাইটের কম দৈর্ঘ্যের একটা স্ট্রিং Source 1সোর্স 1।
  2. প্রিপেয়ারের (prepare) পর ট্রানজেকশন আর সেশনের সঙ্গে বাঁধা থাকে না। তার অবস্থা (state) পুরোপুরি ডিস্কে রাখা থাকে Source 1সোর্স 1।
  3. ম্যানেজার যাচাই করে যে অন্য ডেটাবেসগুলোও কমিট করার জন্য প্রস্তুত। তখন সে COMMIT PREPARED দিয়ে ট্রানজেকশন কমিট করে Source 1সোর্স 1। না হলে সে ROLLBACK PREPARED ব্যবহার করে Source 1সোর্স 1।

COMMIT PREPARED ও ROLLBACK PREPARED প্রিপেয়ার করা সেশনের চেয়ে আলাদা সেশনে চলতে পারে Source 1সোর্স 1। দুটোই সঙ্গে সঙ্গে কমিট বা রোলব্যাক করে Source 2সোর্স 2 Source 3সোর্স 3।

2PC আপনাকে কী দেয়, ডকুমেন্টেশন যেভাবে বলে। প্রিপেয়ারের পর কমিট সফল হওয়ার "very high probability" থাকে, কমিটের আগে ডেটাবেস ক্র্যাশ করলেও Source 1সোর্স 1। ডকুমেন্টেশন বলে probability (সম্ভাবনা)। একে গ্যারান্টি হিসেবে পড়বেন না।

2PC-র খরচ কী, ডকুমেন্টেশন যেভাবে বলে। একটা প্রিপেয়ার করা ট্রানজেকশন যে লক (lock) ধরে রেখেছিল তা ধরে রাখে Source 1সোর্স 1। এটা অনেকক্ষণ প্রিপেয়ার করা থাকলে VACUUM-এর কাজে বাধা দেয়। চরম ক্ষেত্রে এটা transaction ID wraparound ঠেকাতে ডেটাবেস শাটডাউন করিয়ে দিতে পারে Source 1সোর্স 1। ডকুমেন্টেশন বলে একটা ট্রানজেকশন অনেকক্ষণ প্রিপেয়ার করা রাখা বুদ্ধিমানের কাজ নয় Source 1সোর্স 1।

ফিচারটা ডিফল্টে বন্ধ। max_prepared_transactions হলো 0, এবং এটা শুধু সার্ভার স্টার্টের সময় সেট করা যায় Source 4সোর্স 4। যে ম্যানেজার প্রিপেয়ার করা ট্রানজেকশন দ্রুত বন্ধ করে, এমন ম্যানেজার না থাকলে ডকুমেন্টেশন একে 0-তে রাখার পরামর্শ দেয় Source 1সোর্স 1। pg_prepared_xacts ভিউ (view) প্রিপেয়ার করা ট্রানজেকশনগুলোর তালিকা দেখায় Source 1সোর্স 1।

Saga

একটা saga কাজকে local transaction-এর একটা ক্রমে ভাঙে। প্রতিটা একটা সার্ভিসে সম্পূর্ণ হয় এবং সেই সার্ভিসের ডেটাবেস আপডেট করে। এটা একটা ইভেন্ট বা মেসেজ দিয়ে পরের ধাপ শুরু করে Source 5সোর্স 5। কোনো ধাপ ফেইল করলে saga compensating transaction চালায়। এগুলো আগের ধাপগুলোর পরিবর্তন উল্টে দেয় Source 5সোর্স 5।

Compensating ট্রানজেকশন সাধারণ রোলব্যাক নয়। পুরোনো অবস্থা সোজাসুজি রিস্টোর করলে অন্য ইউজারদের করা পরিবর্তন মুছে যেতে পারে। তাই compensation অবশ্যই অ্যাপ্লিকেশন-নির্দিষ্ট হতে হবে Source 6সোর্স 6। সিস্টেম প্রতিটা ধাপ কীভাবে আন্ডু করতে হয় তা রেকর্ড করে রাখে Source 6সোর্স 6।

একটা saga-তে থাকে একটা pivot transaction। এটা ফেরার পথ বন্ধ হওয়ার বিন্দু (point of no return)। এটা সফল হলে পরের সব ধাপ শেষ করতে হবে। এই পরের ধাপগুলো আবার চেষ্টা (retry) করা যায়, এবং সেগুলো idempotent Source 5সোর্স 5।

Saga চালানোর দুটো উপায় আছে Source 5সোর্স 5:

  • Choreography। সার্ভিসগুলো ইভেন্ট আদান-প্রদান করে। কোনো কেন্দ্রীয় কন্ট্রোলার নেই। এটা সহজ ওয়ার্কফ্লোর জন্য মানানসই Source 5সোর্স 5। ধাপ বাড়লে এটা গোলমেলে হয়ে যায়, এবং ইন্টিগ্রেশন টেস্টের জন্য সব সার্ভিস চালু রাখতে হয় Source 5সোর্স 5।
  • Orchestration। একটা কেন্দ্রীয় orchestrator প্রতিটা সার্ভিসকে কী করতে হবে বলে দেয় এবং ফেইলিউর রিকভারি সামলায়। এটা জটিল ওয়ার্কফ্লোর জন্য উপযোগী Source 5সোর্স 5। Orchestrator হলো ফেইলিউরের একটা বিন্দু (point of failure) Source 5সোর্স 5।

A real-life example

A shop has an order service and a payment service, each with its own database. This example is the tutorial's own.

With a saga: the order service saves the order as "pending". The payment service takes the payment. The order service then marks the order "confirmed". If the payment fails, the saga runs a compensating step: it cancels the pending order. Between the steps, the data can be inconsistent. It should become consistent when all steps finish Source 6সোর্স 6. Each service commits its own step, and a compensating step can also fail Source 5সোর্স 5.

সহজ English

A shop has an order service and a payment service. Each has its own database. This example is the tutorial's own.

With a saga, the steps are these. The order service saves the order as "pending". The payment service takes the payment. The order service then marks the order "confirmed". If the payment fails, the saga runs a compensating step. It cancels the pending order. Between the steps, the data can be inconsistent. It should become consistent when all steps finish Source 6সোর্স 6. Each service commits its own step. A compensating step can also fail Source 5সোর্স 5.

বাংলা ব্যাখ্যা

একটা শপের একটা অর্ডার সার্ভিস ও একটা পেমেন্ট সার্ভিস আছে। প্রতিটার নিজের ডেটাবেস। এই উদাহরণ টিউটোরিয়ালের নিজের।

Saga দিয়ে ধাপগুলো এই রকম। অর্ডার সার্ভিস অর্ডারটা "pending" হিসেবে সেভ করে। পেমেন্ট সার্ভিস পেমেন্ট নেয়। তারপর অর্ডার সার্ভিস অর্ডারটা "confirmed" চিহ্নিত করে। পেমেন্ট ফেইল করলে saga একটা compensating ধাপ চালায়। সে pending অর্ডার বাতিল করে। ধাপগুলোর মাঝখানে ডেটা অসঙ্গত (inconsistent) থাকতে পারে। সব ধাপ শেষ হলে এটা কনসিসটেন্ট হওয়ার কথা Source 6সোর্স 6। প্রতিটা সার্ভিস নিজের ধাপ কমিট করে। একটা compensating ধাপও ফেইল করতে পারে Source 5সোর্স 5।

Common mistakes

সহজ English

বাংলা ব্যাখ্যা

  • Saga-কে একটা ট্রানজেকশন ভাবা। সার্ভিসগুলোর মধ্যে কোনো বিল্ট-ইন (built-in) আইসোলেশন নেই। Lost update, dirty read এবং fuzzy (nonrepeatable) read ঘটতে পারে Source 5সোর্স 5।
  • "Prepared"-কে "কমিট হবেই" পড়া। ডকুমেন্টেশন বলে "very high probability" Source 1সোর্স 1।
  • একটা প্রিপেয়ার করা ট্রানজেকশন ভুলে যাওয়া। এটা তার লক ধরে রাখে। এটা VACUUM-কে স্টোরেজ খালি করতে বাধা দিতে পারে Source 1সোর্স 1।
  • যে ধাপ ফেরানো যায় না তা আগে রাখা। সেগুলো শুধু ক্রিটিক্যাল চেক পাশ করার পর করুন Source 6সোর্স 6।

What a tester can check

Tutorial advice

These test ideas are the tutorial's own advice. They are not from a source.

  • Fail every step. Make each step fail in turn, and check that the compensating steps leave the data in a valid state.
  • Repeat a step. Send the same message twice. Nothing should change.
  • Look for stuck work. Look for orders that stay "pending". A step can be blocked and not failed.
  • If PostgreSQL 2PC is on, query pg_prepared_xacts after a failed test run, and look for old prepared transactions.

সহজ English

These test ideas are the tutorial's own advice. They are not from a source.

  • Fail every step. Make each step fail in turn. Check that the compensating steps leave the data in a valid state.
  • Repeat a step. Send the same message twice. Nothing should change.
  • Look for stuck work. Look for orders that stay "pending". A step can be blocked and not failed.
  • If PostgreSQL 2PC is on, query pg_prepared_xacts after a failed test run. Look for old prepared transactions.

বাংলা ব্যাখ্যা

এই টেস্টের ধারণাগুলো টিউটোরিয়ালের নিজের পরামর্শ। এগুলো কোনো সোর্স থেকে নয়।

  • প্রতিটা ধাপ ফেইল করান। পালা করে প্রতিটা ধাপ ফেইল করান। দেখুন compensating ধাপগুলো ডেটাকে একটা বৈধ অবস্থায় রাখে।
  • একটা ধাপ আবার চালান। একই মেসেজ দুবার পাঠান। কিছুই বদলানো উচিত নয়।
  • আটকে থাকা কাজ খুঁজুন। যেসব অর্ডার "pending" থেকে যায় সেগুলো খুঁজুন। একটা ধাপ ফেইল না হয়ে ব্লকড (blocked) থাকতে পারে।
  • PostgreSQL 2PC চালু থাকলে, একটা ব্যর্থ টেস্ট রানের পর pg_prepared_xacts কুয়েরি করুন। পুরোনো প্রিপেয়ার করা ট্রানজেকশন খুঁজুন।

Key words

WordMeaning
Distributed transactionOne business action that changes more than one database or service
Two-phase commitA way to prepare all databases first, and then commit or roll back all Source 1সোর্স 1
Prepared transactionA transaction with its state stored on disk, which waits for a commit or a rollback Source 1সোর্স 1
SagaA sequence of local transactions with compensating steps Source 5সোর্স 5
Compensating transactionA step that undoes the effect of a finished step Source 6সোর্স 6
Pivot transactionThe point of no return of a saga Source 5সোর্স 5
IdempotentRepeating the same operation does not change the outcome Source 5সোর্স 5
  1. [1]
    PREPARE TRANSACTION

    PostgreSQL Documentation · accessed 2026-10-22দেখা হয়েছে: 2026-10-22

  2. [2]
    COMMIT PREPARED

    PostgreSQL Documentation · accessed 2026-10-22দেখা হয়েছে: 2026-10-22

  3. [3]
    ROLLBACK PREPARED

    PostgreSQL Documentation · accessed 2026-10-22দেখা হয়েছে: 2026-10-22

  4. [4]
    Resource Consumption (max_prepared_transactions)

    PostgreSQL Documentation · accessed 2026-10-22দেখা হয়েছে: 2026-10-22

  5. [5]
    Saga distributed transactions pattern

    Azure Architecture Center (Microsoft Learn) · accessed 2026-10-22দেখা হয়েছে: 2026-10-22

  6. [6]
    Compensating Transaction pattern

    Azure Architecture Center (Microsoft Learn) · accessed 2026-10-22দেখা হয়েছে: 2026-10-22