> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# The Two Generals Problem

> Explains the Two Generals Problem and why unreliable networks prevent absolute agreement, motivating retries, idempotency, and durable deduplication in distributed systems.

You'll be surprised to learn a seemingly simple fact: perfectly reliable communication across an unreliable channel is mathematically impossible. This idea is best understood through a classic puzzle.

Imagine two generals. Their armies sit on two hills, and the enemy is camped in the valley between them. To win, both generals must attack at the exact same dawn. If only one attacks, that army is wiped out.

The only way the generals can communicate is by sending a messenger down through the valley, right past the enemy — and that messenger might be captured along the way.

So the first general sends a messenger: "Attack at dawn."

But here's the problem: he has no way of knowing whether the messenger reached the second general. He cannot attack yet, because the other general might never have gotten the message.

The second general, having received the message, sends a messenger back: "Confirmed — attack at dawn." Now the second general has the same doubt: did that confirmation reach the first general? If the confirmation didn't arrive, the first general won't attack.

To resolve that uncertainty, the first general could send yet another messenger: "Got your confirmation." But then the first general is unsure whether that acknowledgement reached the second general. The cycle continues — no finite number of acknowledgements guarantees both sides have the same knowledge.

Take a moment to consider the consequence: over a channel that can drop messages, two parties can never reach absolute certainty that they both agree.

![A stylized diagram titled "The Two Generals" showing two stick-figure generals on opposite hills with flags and enemy icons between them. A message log on the right illustrates a back-and-forth confirmation sequence ("Attack at dawn" → "Confirmed" → "Got it — you got that?" … "It never ends").](https://mintcdn.com/kodekloud-c4ac6d9a/2h0o-_DtLcLu6HM7/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/System-Design/The-Two-Generals-Problem/two-generals-problem-diagram.jpg?fit=max\&auto=format\&n=2h0o-_DtLcLu6HM7\&q=85\&s=7952e2464b35b9869f91089b626bacfb)

Why this matters for real systems

* The valley = the network.
* The messengers = packets or requests.
* The enemy = anything that can drop or delay messages (network congestion, router failures, service crashes, or deliberate rate-limiting).

This is not just a thought experiment — it describes the shape of every network call you've ever made. Because the network can be unreliable, you cannot assume both parties achieve absolute common knowledge about any one message.

Practical implications:

* Retries are necessary because you can't tell whether the recipient never saw the message or processed it but their acknowledgement was lost.
* Duplicates are normal, not exceptional.
* Systems must be designed to tolerate repeated deliveries safely.

Consider a payment webhook. When you POST an event to a user's endpoint and you don't receive a response, two distinct cases are indistinguishable from your side:

1. The event never arrived.
2. The event arrived and was processed, but the reply was lost on the return path.

From the sender's perspective both look identical — so the safe policy is to retry. Retries make duplicate deliveries likely.

![A dark-themed presentation slide titled "So what do we do?" showing a payment webhook diagram: a "You" box sending events to a "User" box with red X's indicating lost events/replies and a retry arrow. Text notes explain two failure cases ("the event never arrives" and "the reply is lost after processing") and that from your side both look identical.](https://mintcdn.com/kodekloud-c4ac6d9a/2h0o-_DtLcLu6HM7/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/System-Design/The-Two-Generals-Problem/payment-webhook-lost-events-replies.jpg?fit=max\&auto=format\&n=2h0o-_DtLcLu6HM7\&q=85\&s=b141d59963525e7e8e18067881c41ccb)

Idempotency: the practical defense against duplicates
Design systems assuming duplicates will occur. Make operations idempotent: when an event arrives, check whether you've already processed that event ID. If yes, do nothing (or return the same success); if no, process it and record that ID as seen.

Example pseudocode (transactionally safe):

```python theme={null}
def handle_event(event_id, payload):
    # atomically check and insert the seen-event marker
    with db.transaction():
        if db.exists_seen_event(event_id):
            return 200  # already-processed: idempotent response
        process_payload(payload)
        db.insert_seen_event(event_id)
    return 201  # processed successfully
```

Implementation notes:

* Store seen-event IDs in durable storage (database, distributed KV store).
* Use a retention window appropriate for your traffic and storage budget.
* Prefer transactional semantics or optimistic concurrency to avoid race conditions.
* If your operation is expensive, consider storing a precomputed response or a marker to short-circuit duplicate processing.

![A dark-themed slide titled "Duplicates? Expect them." showing a simple flow: when an event arrives, check its ID against a seen-events list — if yes, do nothing; if no, process the event and save the ID.](https://mintcdn.com/kodekloud-c4ac6d9a/2h0o-_DtLcLu6HM7/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/System-Design/The-Two-Generals-Problem/duplicate-event-id-check-save.jpg?fit=max\&auto=format\&n=2h0o-_DtLcLu6HM7\&q=85\&s=536b9fc4fbe2540b31cb5341ff5beaa8)

In practice, use durable storage for seen-event IDs, choose an appropriate retention window, and design operations to be idempotent (or detect and ignore duplicates). Retries, acknowledgements, and timeouts are practical tools — but they do not change the fundamental impossibility of achieving absolute common knowledge over an unreliable channel.

Delivery semantics at a glance

| Guarantee | What it means | Practical example |
| - | - | - |
| At-most-once | Each message is delivered zero or one times. Risk: message loss. | `fire-and-forget` HTTP without retries |
| At-least-once | Each message is delivered one or more times. Risk: duplicates. | Retries until acknowledgement (common for webhooks) |
| Exactly-once | Delivered once and only once from the application's observable perspective. Hard to achieve without careful coordination; usually implemented with idempotency and deduplication. | Idempotent handlers with durable seen-event markers |

Conclusion
You stop chasing impossible certainty and instead make your system correct and safe in the presence of uncertain delivery — by retrying and by ensuring repeated deliveries are harmless. The Two Generals Problem gives a compact, formal intuition for why distribution systems adopt idempotency, retries, timeouts, and durable deduplication as standard patterns.

Links and references

* [Two Generals' Problem — Wikipedia](https://en.wikipedia.org/wiki/Two_Generals%27_Problem)
* [Idempotency in distributed systems — Concepts and patterns](https://martin.kleppmann.com/notes/compensation.html) (concepts by Martin Kleppmann)
* [At-least-once vs exactly-once delivery — Discussion and trade-offs](https://aws.amazon.com/blogs/compute/exactly-once-processing-aws-lambda/)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/ef53ec43-96e9-4d1b-8a6e-e6eb97b0d0dc/lesson/f248e511-bfc6-41dd-804f-0b22c276eabf)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.