Course contentsShow
← System Design
Lesson 1038 of 1,91926. Idempotency and RetriesFree lesson

Side Effect Management

Handling non-idempotent side effects like emails or notifications by separating them from core idempotent operations.

Side Effect Management

You'll learn how to safely handle operations that cannot be made idempotent—like sending emails or push notifications—by isolating them from your core idempotent logic.

The Core Challenge

Some operations are inherently non-idempotent: sending an email, firing a push notification, or charging a credit card. If a client retries a request, you don't want these side effects to execute twice. But in distributed systems, retries are inevitable.

The solution? Separate side effects from your main idempotent operations.

How Side Effect Management Works

Think of it like a two-phase process at a restaurant: first you confirm the order is placed (idempotent), then you ring the kitchen bell (side effect). If the customer accidentally places the order twice, you catch it during confirmation—the bell never rings twice.

Pattern in Practice

  1. Use an idempotency key to guarantee the core operation (like order creation) happens exactly once
  2. Defer side effects until after the core operation completes successfully
  3. Mark side effects as "sent" in your state store alongside the idempotency key
  4. Check the flag before triggering any non-idempotent action
Request arrives with idempotency key → 
  Check if already processed →
    If yes: return cached response (no side effects)
    If no: process core logic →
      Store result with key →
      Check if side effects already sent →
        If not: trigger email/notification + mark as sent

This ensures that even if a retry happens during side effect execution, you'll detect it and skip re-sending.

When to Apply This

Any time your API triggers notifications, external API calls, or financial transactions—operations where "doing it twice" has visible, problematic consequences.

Key Takeaway: Protect non-idempotent side effects by wrapping them with idempotency checks and state tracking—separate the "did the work" phase from the "notify the world" phase.