# Idempotent Operations Pattern

> Idempotent operations with request IDs: a runnable model of retried requests and deduplication, checked so a retried request is never applied twice.

Canonical: https://fizzbee.io/design/patterns/idempotent-operations/
Source: https://github.com/fizzbee-io/fizzbee/tree/main/examples/references/13-05-02-idempotent-operations


Operations that can be safely retried

**Demonstrates:** idempotency, deduplication, request IDs

```fizz
# Example 0067: Idempotent Operations Pattern
# Operations that can be safely retried
# Demonstrates: idempotency, deduplication, request IDs

action Init:
    processed_ids = set()
    counter = 0
    requests = 0

atomic action SendRequest:
    require requests < 3

    request_id = requests
    requests = requests + 1

    # Process if not already processed (idempotent)
    if request_id not in processed_ids:
        processed_ids.add(request_id)
        counter = counter + 1

atomic action RetryRequest:
    require requests > 0

    # Retry with existing request ID
    request_id = oneof range(requests)

    # Idempotent: no effect if already processed
    if request_id not in processed_ids:
        processed_ids.add(request_id)
        counter = counter + 1

# Safety: counter never exceeds unique requests
always assertion IdempotencyInvariant:
    return counter <= requests

# Safety: processed IDs match counter
always assertion ProcessedCorrectly:
    return counter == len(processed_ids)

atomic action NoOp:
    if requests >= 3:
        pass
```
Run it in the FizzBee playground: https://fizzbee.io/play


**Expected result** (from `fizz`):

```
StateSpaceOptions: options:{max_actions:100 max_concurrent_actions:2}
Nodes: 7, queued: 0
Valid Nodes: 4 Unique states: 4
IsLive: true
PASSED: Model checker completed successfully
```

[Source on GitHub](https://github.com/fizzbee-io/fizzbee/tree/main/examples/references/13-05-02-idempotent-operations)

## Language features used

[Assertions](/reference/language/assertions/), [Flow Modifiers](/reference/language/flow-modifiers/)

