You are here: Home / Topics / What do you mean by method can be called twice?

What do you mean by method can be called twice?

Filed under: Solid System Design on 2026-01-02 10:23:21

This is a very important question.
Most production bugs exist because developers don’t truly understand this.

Let’s break it down slowly, clearly, and with real-world examples.

1️⃣ What “Method Can Be Called Twice” REALLY Means

It does NOT mean:

  • You wrote method() twice in code ❌

It DOES mean:

The same business action is executed more than once — intentionally or unintentionally.

Your system may receive duplicate execution requests.

2️⃣ How Can the Same Method Be Called Twice?

? Case 1: User Action (Very Common)

  • User clicks Pay button
  • UI freezes
  • User clicks again

? Result:

payOrder()  ← first call
payOrder()  ← second call

Both reach backend.

? Case 2: Network Retry (Invisible but Deadly)

  • Backend processes request
  • Response is delayed
  • Load balancer / client retries

? Backend sees:

payOrder()
payOrder()  // retry

Even though user clicked once.

? Case 3: Timeout + Retry Logic

retryTemplate.execute(() -> payOrder());

If timeout happens after DB update but before response → retry happens.

? Case 4: Message Queue Redelivery

  • Kafka / RabbitMQ
  • Consumer crashes after processing
  • Message is delivered again

? This is by design.

? Case 5: Microservice Communication

Service A → Service B

  • Service B processes
  • Service A times out
  • Service A retries

Service B sees duplicate calls.

3️⃣ Why This Is Dangerous

Imagine this method:

public void debit(Long accountId, int amount) {
    balance = balance - amount;
    save(balance);
}

Single call ✅

Balance: 100 → 70

Called twice ❌

Balance: 100 → 70 → 40
? Money lost

4️⃣ This Is NOT a Bug — It’s Reality

Duplicate calls are:

  • Normal
  • Expected
  • Unavoidable

? Unbreakable systems assume duplicates will happen.

5️⃣ The Key Concept: Idempotency

Calling a method once or multiple times produces the same final result

❌ Non-idempotent

balance = balance - amount;

✅ Idempotent

if (alreadyProcessed(requestId)) return;

6️⃣ Real-World Example (Banking)

Banks assign:

transaction_id = UUID

DB constraint:

UNIQUE (transaction_id)

If same request arrives twice:

  • First succeeds
  • Second is rejected safely

No corruption.

7️⃣ Method Called Twice ≠ Bug

It becomes a bug only if:

  • You didn’t design for it
  • State changes twice

8️⃣ How to Mentally Detect Risk

Whenever you write a method, ask:

❓ Can this be retried?
❓ Can this be called twice?
❓ Can client timeout and retry?
❓ Can message be redelivered?

If yes → method must be idempotent.

9️⃣ One-Sentence Rule (Memorize This)

Every write operation must survive duplicate execution.

? What You’ll Learn Next

Tomorrow (Day 2):

  • Why transactions alone are not enough
  • How idempotency + transactions work together
  • Real Spring Boot patterns

If you want, reply:

“Continue Day 2”

This question tells me you’re thinking like a senior engineer already ?


About Author:
N
Neha Sharma     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.