Understanding Idempotence and Common Backend Solutions
The article explains the concept of idempotence in computing, illustrates typical duplicate‑request scenarios, and presents three practical backend solutions—unique database indexes, optimistic locking with version control, and a Redis‑based token mechanism—along with their advantages, trade‑offs, and implementation details.
Idempotence is a computing concept where multiple identical requests produce the same result as a single request, ensuring that repeated operations do not cause unintended side effects. The HTTP specification defines an idempotent request method as one whose effect on the server is identical whether the request is sent once or many times.
Typical duplicate‑request scenarios include users clicking a submit button multiple times, network‑induced retries, page back‑navigation resubmissions, and message re‑consumption.
In CRUD operations, the idempotence of each database action varies. The article provides a visual overview of which operations are naturally idempotent.
Solution 1: Add a Unique Index
By adding a unique constraint to a relevant column (e.g., order number) in the database, duplicate inserts trigger an exception, preventing repeated data creation. While safe, this approach pushes the responsibility entirely to the database layer and may affect performance or user experience if duplicate attempts are reported as successful.
Solution 2: Database Optimistic Lock
Optimistic locking adds a version field to a table; each update increments this version and includes a version check in the WHERE clause. This ensures that only the first successful request updates the row, while subsequent duplicate requests fail the version check.
Example order table and update statement:
UPDATE orders SET amount=102, version=version+1 WHERE order_id=1003 AND version=5
The WHERE clause guarantees that only one request can succeed because the version changes after the first successful update.
Solution 3: Redis + Token
In a Redis‑based token approach, the frontend obtains a unique token from the backend before making the actual operation request. The backend checks Redis for the token's existence; if present, it proceeds with the business logic, otherwise it treats the request as a duplicate.
In high‑concurrency scenarios, the token should be deleted after the business operation succeeds; deleting it beforehand could cause lost retries if the operation fails, while deleting it later could allow a second request to execute before the token is removed.
Conclusion
Idempotence is a crucial concept in concurrent programming and interview questions. The article summarizes common solutions, highlighting Redis‑based token as a widely applicable and effective method, while also mentioning other techniques such as deduplication tables and state machines.
For further reading, see the original source: https://blog.csdn.net/A_art_xiang/article/details/132106309
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.