A10Intermediate
Idempotency Thinking Training
30 minWhen designing new features
Format: Given an operation, determine whether it's idempotent; if not, figure out how to make it idempotent.
What is Idempotency: Executing the same operation once or ten times should produce the same result.
Exercise: Determine whether the following operations are idempotent:
| Operation | Idempotent? | Why? |
|---|---|---|
| Set username to "Alice" | Yes | Setting it 10 times still results in "Alice" |
| Add $100 to account balance | No | Adding 10 times results in $1000 |
| Send welcome email | ? | You decide |
| Delete post with ID 123 | ? | You decide |
| Create a new order | ? | You decide |
| Update user age to 25 | ? | You decide |
| Like a post | ? | You decide |
Why It Matters: When the network is unstable, requests may be sent twice. If "place order" is not idempotent, the user might be charged twice.
Solution Concepts:
- Idempotency Key: Each request carries a unique ID; the server checks if it's already been processed
- "Set" is naturally idempotent compared to "increment":
balance = 200vsbalance += 100