Urgent.News

What's breaking now, across thousands of outlets.

Tech

Idempotency: Protecting User Intent Beyond the Buy Button

User bought some stock but the network is slow. There is no immediate feedback, so out of frustration, they tap the Buy button several times. A few seconds later, multiple orders are matched. Have you ever got into this situation? Disabling the button in the UI helps, but it isn't guarantee. UI state changes happen on main thread and depend on when the run loops get a chance to process them. If…

Idempotency is a crucial concept for protecting user intent, especially when online transactions may be affected by network issues or server delays. When a user attempts to buy stock, they may unintentionally tap the "Buy" button multiple times due to slow network speeds or frustration, leading to multiple orders being placed. Disabling the button can mitigate this issue, but it is not foolproof.

UI state changes are dependent on the main thread, and if the thread is busy, multiple tap events may be processed before the UI indicates that it is disabled.

Idempotency is the solution to this problem. An idempotent operation can be performed multiple times without changing the result beyond the initial application. In this context, the user's intent to buy stock once should not be altered by repeated taps, retries, or network issues. While the backend can handle this, the client also plays a vital role.

It needs to communicate to the server that these requests represent the same intent. This is achieved through an idempotent key, which is created on the client side and remains unchanged until a new intent is formed.

The idempotent key is typically a UUID (Universally Unique Identifier) version 4, which is randomly generated. However, for a more orderly approach, libraries can be used to create UUID version 7, which includes a timestamp. When the client submits an order, it includes the idempotency key in the request header. The server then checks if it has already processed an order with this key.

If it has, the server returns the existing order's status instead of creating a new one. This ensures that the user's intent is executed only once, regardless of how many times the "Buy" button is tapped.

The key's lifespan is determined by the user's actions. Once an order is submitted, the key is used to track the order's status. If the user submits another order with the same intent, the same key is reused. This approach prevents duplicate orders and ensures that the server treats all requests with the same key as a single, unified intent. The client and server work together to uphold this end-to-end contract, guaranteeing that user intent is protected and processed accurately.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Ho scritto Ratiform per smettere di scrivere i form in Ratatui

Il problema: due campi vanno bene, tre iniziano a far male Chi ha scritto anche solo una TUI con Ratatui conosce la progressione.

  • Ratiform crate simplifies form management in Rust by handling focus, validation, and rendering.
  • Unique field identifiers as Rust generic types prevent confusion and compilation errors.

Gitea SSL with Apache as reverse proxy

We want to host a nice and secure container registry - to push docker images there and our kubernetes cluster would pull them from this registry. So came the idea to use the gitea over ssl.

  • Gitea container registry feature enables HTTPS access
  • Apache reverse proxy configured to point to Gitea on port 3000
  • SSL setup and virtual host creation for Gitea registry

More from Friday 11 September →