Ensuring Idempotency in High‑Concurrency Systems: Strategies and Best Practices
To prevent duplicate operations such as repeated inserts, updates, or message sends in high‑traffic applications, this article explains the concept of idempotency and outlines practical techniques—including read‑only APIs, MVCC with optimistic locking, deduplication tables, distributed locks, safe deletes, unique indexes, UUID‑based request tracking, and state‑machine safeguards.
During system development, duplicate inserts, updates, or message retransmissions often occur due to complex business logic and network uncertainty; certain operations, like order creation, must be idempotent to avoid severe consequences.
System's idempotency
Idempotency is a concept where performing an operation N times yields the same result as performing it once.
How to guarantee idempotency in high‑concurrency systems
1. Query
Read‑only APIs are naturally idempotent because querying once or multiple times does not change any data.
2. MVCC solution
Multi‑Version Concurrency Control with conditional updates (optimistic locking) uses a version field or other conditions to ensure safe updates under concurrency.
update table_xxx set name=#name#, version=version+1 where version=#version# update table_xxx set quality=quality-#subQuality# where quality-#subQuality# >= 03. Separate deduplication table
When many business documents need deduplication (e.g., in ERP systems), a dedicated deduplication table can be used; inserting into it leverages the database's unique index to enforce uniqueness.
4. Distributed lock
In distributed systems where a unique key is hard to define, a third‑party distributed lock can be acquired before inserting or updating data, ensuring only one instance performs the operation.
5. Delete operation
Deletion can be made idempotent by treating the first delete as the actual operation; subsequent delete attempts simply return success without affecting data.
6. Unique index on insert
Business‑level primary keys can be constrained with a unique index; for example, three fields together guarantee uniqueness, and the database enforces it.
API‑level idempotency can be achieved by adding a unique identifier (e.g., UUID) to the request payload; the server uses this identifier to detect and ignore duplicate submissions.
7. State‑machine idempotency
When business documents or tasks follow a finite state machine, once a document reaches a later state, attempts to revert to an earlier state are rejected, preserving idempotency.
Source: http://www.cnblogs.com/moonandstar08/p/5334865.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
