Skip to content

4Developers Katowice 2024: Dominik Guzy

CommunityPROIDEA Events34 minOpen on YouTube ↗

Idempotentność API details 4Developers Katowice 2024: Dominik Guzy.

Summary

### Overview In this talk presented at 4Developers Katowice 2024, Dominik Guzy explores the concept of idempotency in API design and distributed systems. He explains how unexpected network retries or message redelivery can cause unintended duplicate operations (such as double financial transactions or corrupted sports stats) and demonstrates practical ways to implement idempotency using both C#/.NET and TypeScript.

### Key Topics - **Theoretical & Mathematical Foundation**: Introduced historically by Benjamin Peirce in 1870, idempotency defines operations where applying them multiple times yields the same result as applying them once. Real-world physical analogies include pressing an elevator floor button or a pedestrian crossing button multiple times. - **HTTP Semantics & Distributed Systems**: While HTTP safe methods (`GET`, `HEAD`, `OPTIONS`, `TRACE`) and certain unsafe methods (`PUT`, `DELETE`) are inherently idempotent with respect to server state, methods like `POST` and `PATCH` are not by default. Distributed systems like AWS SQS and Apache Kafka rely on "at-least-once delivery", where failures or timeouts can cause duplicate message processing, necessitating idempotent consumer design. Load-store CPU architectures (e.g., ARM) also handle page faults via idempotent instruction replay. - **Strategies for Implementation**: 1. *Server-calculated payload hashing* (e.g., AWS Lambda Powertools pattern with DynamoDB persistence). 2. *Client-provided idempotency keys* passed via headers (e.g., the pattern popularized by Stripe).

### Technical Details - **C# / AWS Lambda Powertools Demo**: Implemented via AWS SAM (Serverless Application Model) with a DynamoDB table containing a hash key and a TTL (`expiration`). Instead of hashing the entire request payload (which is brittle), Guzy extracts a specific property (`name`) as the idempotency key with a 10-second TTL. The server checks DynamoDB, sets initial state, processes the handler, records the response, and serves subsequent matching calls directly from the cache. - **TypeScript / Hono Framework Demo**: Using the Hono framework, an endpoint enforces an `x-idempotency-key` header via a validator. An in-memory cache (JavaScript `Map`) stores responses indexed by the client-provided key. If present, the cached response is returned immediately (reducing response time from ~7 ms to ~3–4 ms), preventing duplicate executions of `POST` actions.

### Takeaways Idempotency is simple to implement yet vital for preventing severe bugs in systems that handle financial transactions, real-world value changes, event sourcing, or CQRS architectures. Engineers can delegate key generation to the client or compute targeted payload hashes on the server to make endpoints resilient against retries.

Related talks