Idempotency & Transactions Questions with Answers

16 practice questions on Idempotency & Transactions (System Design) with answers and step-by-step solutions. Try 10 samples below, then practise all 16 free in the AptiNest app.

Sample questions

Question 1Easy

Two-phase commit (2PC) has which two phases?

  1. Replicate, then compact
  2. Prepare (vote), then commit or abort
  3. Lock, then unlock
  4. Read, then write

Answer: B. Prepare (vote), then commit or abort

Explanation

The coordinator asks all participants to prepare; only if all vote yes does it tell them to commit.

Step-by-step solution

  1. Two-phase commit is a protocol for one transaction across several participants.
  2. Phase one is prepare: the coordinator asks each participant to prepare and vote yes or no.
  3. Phase two is commit if everyone voted yes, otherwise abort.
  4. So the two phases are prepare (vote), then commit or abort. The answer is option B.

Why the other options are wrong

  • A. Replicate and compact are storage operations, not commit phases.
  • C. Lock and unlock are steps inside the protocol, not its two phases.
  • D. Read and write are data operations, not the phases of a commit protocol.

Tip: 2PC means every participant votes in the prepare phase, and the coordinator commits only if all vote yes.

Question 2Easy

An operation is idempotent if:

  1. It always runs in under one second
  2. It returns a different result each time it is called with the same input
  3. Repeating it has the same effect as doing it once
  4. It cannot fail

Answer: C. Repeating it has the same effect as doing it once

Explanation

Setting a value to 5 is idempotent; incrementing by 1 is not.

Step-by-step solution

  1. An idempotent operation gives the same end state no matter how many times it runs.
  2. Setting x to 5 twice still leaves x as 5, so it is idempotent.
  3. Adding 1 to x twice changes the result, so adding is not idempotent.
  4. Option C says this directly: repeating it has the same effect as doing it once.

Why the other options are wrong

  • A. Running time has nothing to do with idempotency.
  • B. Returning a different result for the same input is the opposite of a stable repeated effect.
  • D. Never failing is not part of the definition; a retried call can still fail.

Tip: Ask: does running it twice change anything after the first run? Setting a value is idempotent; incrementing is not.

Question 3Easy

In an order saga, payment succeeded but shipping fails. The compensating action is:

  1. Lock the customer account
  2. Refund the payment
  3. Retry payment
  4. Delete the order history

Answer: B. Refund the payment

Explanation

Compensations semantically undo completed steps; a refund reverses the charge.

Step-by-step solution

  1. Saga undoes completed steps with compensating transactions.
  2. Payment succeeded, shipping failed.
  3. Refund undoes payment.

Why the other options are wrong

  • A. Locking doesn't undo.
  • C. Retrying payment charges again.
  • D. Deleting history loses audit.

Question 4Easy

What does ACID's 'A' (atomicity) guarantee?

  1. Writes are applied asynchronously to replicas after the commit returns
  2. Data is always available
  3. Each transaction runs on one CPU
  4. All of a transaction's changes happen, or none do

Answer: D. All of a transaction's changes happen, or none do

Explanation

A partial failure rolls back the entire transaction.

Step-by-step solution

  1. ACID stands for Atomicity, Consistency, Isolation and Durability.
  2. Atomicity means a transaction is all or nothing.
  3. If any part of the transaction fails, every change in it is rolled back.
  4. So the answer is option D: all changes happen, or none do.

Why the other options are wrong

  • A. Asynchronous replication is about copies of the data, not about atomicity.
  • B. Being always available is a different property, not atomicity.
  • C. Running on one CPU is not what atomicity means.

Tip: Atomicity means all or nothing; durability means committed data survives a crash.

Question 5Medium

A saga implements a distributed transaction as:

  1. One global lock held across all services until the transaction ends
  2. A nightly batch job
  3. A single SQL transaction spanning databases
  4. Local transactions, each with a compensating action

Answer: D. Local transactions, each with a compensating action

Explanation

If step 3 fails, compensations for steps 2 and 1 run (e.g. refund payment, release inventory).

Step-by-step solution

  1. A saga splits one distributed transaction into a sequence of local transactions.
  2. Each local step commits on its own inside its own service.
  3. If a later step fails, the earlier steps are undone by compensating actions.
  4. So a saga is local transactions, each with a compensating action. The answer is option D.

Why the other options are wrong

  • A. A global lock held across services is a 2PC-style approach, not a saga.
  • B. A nightly batch job is not a transaction pattern.
  • C. A single SQL transaction spanning databases is what sagas avoid.

Tip: A saga is a chain of local steps, each with a compensation that runs in reverse when a later step fails.

Question 6Medium

Pessimistic locking is preferable when:

  1. Reads vastly outnumber writes with no contention
  2. Conflicts are very rare
  3. The data is never updated
  4. Conflicts are frequent and retries are costly

Answer: D. Conflicts are frequent and retries are costly

Explanation

If most transactions would collide, locking up front avoids repeated failed attempts.

Step-by-step solution

  1. Pessimistic locking locks before use.
  2. Good when conflicts are common.
  3. Retrying is expensive otherwise.

Why the other options are wrong

  • A. Optimistic suits no contention.
  • B. Rare conflicts favour optimistic.
  • C. No updates means no locks needed.

Question 7Medium

An idempotency key should be stored for how long?

  1. Long enough to cover the client's retry window
  2. Only until the first byte of the response is sent
  3. Not at all; just log it
  4. Forever, for every request

Answer: A. Long enough to cover the client's retry window

Explanation

Keys only need to outlive possible retries (e.g. 24 hours), after which they can expire.

Step-by-step solution

  1. An idempotency key stops a retried request from running twice.
  2. Clients can keep retrying for as long as their retry window lasts.
  3. So the key must be kept at least that long, for example 24 hours.
  4. The answer is option A, long enough to cover the client's retry window.

Why the other options are wrong

  • B. Expiring the key when the response starts would let a later retry run the request twice.
  • C. Logging the key alone does not let the server recognise a repeat.
  • D. Keeping every key forever wastes storage; keys only need to last the retry window.

Tip: Set the idempotency key expiry longer than the longest realistic retry window.

Question 8Medium

A mobile app retries a 'pay ₹500' request after a timeout. How do we avoid charging twice?

  1. Disable retries in the app
  2. Send an idempotency key; repeats get the stored result
  3. Use GET instead of POST
  4. Charge twice and refund the duplicate later through a nightly reconciliation job

Answer: B. Send an idempotency key; repeats get the stored result

Explanation

The server records the key with the outcome; a retry with the same key gets the original response.

Step-by-step solution

  1. A timeout leaves it unclear whether the payment went through.
  2. The app sends the same idempotency key with every retry of that payment.
  3. On the first request the server stores the key together with the outcome.
  4. A retry with the same key gets the stored result, so the customer is charged once. The answer is option B.

Why the other options are wrong

  • A. Turning off retries loses payments that timed out and does not stop duplicates in other cases.
  • C. Using GET for a charge is wrong and does not by itself prevent a duplicate charge.
  • D. Charging twice and refunding later leaves the customer overcharged in the meantime.

Tip: For any retried write such as a payment or an order, send an idempotency key and store the result against it.

Question 9Hard

What is the classic weakness of 2PC?

  1. It cannot commit more than two participants in one transaction
  2. It needs no network
  3. If the coordinator fails after prepare, participants block
  4. It is eventually consistent

Answer: C. If the coordinator fails after prepare, participants block

Explanation

Prepared participants cannot decide alone and wait for the coordinator, holding locks.

Step-by-step solution

  1. In 2PC, a participant that has voted yes must wait for the coordinator's decision.
  2. If the coordinator fails after the prepare phase, participants cannot decide alone.
  3. They keep their locks and wait, so they are blocked until the coordinator recovers.
  4. So the classic weakness is blocking. The answer is option C.

Why the other options are wrong

  • A. 2PC is not limited to two participants; the problem is the blocking.
  • B. 2PC depends on network messages between the coordinator and the participants.
  • D. 2PC gives atomic commit, not eventual consistency; its weak point is blocking.

Tip: The 2PC weakness to remember is that the coordinator is a single point of blocking after prepare.

Question 10Hard

Orchestrated saga vs choreographed saga:

  1. Orchestrated: a coordinator drives steps; choreographed: services react to events
  2. Orchestrated sagas need no messages
  3. Choreographed sagas use 2PC
  4. They differ only in the database used

Answer: A. Orchestrated: a coordinator drives steps; choreographed: services react to events

Explanation

Orchestration centralises the workflow logic; choreography spreads it across event handlers.

Step-by-step solution

  1. Both are saga styles for multi-step workflows across services.
  2. In orchestration, one coordinator service tells each step what to do.
  3. In choreography, each service reacts to events published by the others, with no central controller.
  4. So orchestrated means a coordinator drives the steps, and choreographed means services react to events. The answer is option A.

Why the other options are wrong

  • B. Orchestrated sagas still need messages or calls from the coordinator to the steps.
  • C. Choreographed sagas do not use 2PC; they use events and compensations.
  • D. The difference is how the workflow is controlled, not which database is used.

Tip: Orchestration means one controller runs the flow; choreography means services react to each other's events.

Practise all 16 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all System Design topics · All topics