Sobes.tech
Middle

Practical case: a food ordering app, when creating an order, the internet flickered, the user clicked twice, and two orders were created. What is the reason and how to fix it?

sobes.tech AI

Answer from AI

The reason for creating two orders is a user's double click during a temporary internet outage, which caused the application not to receive confirmation of the first order and to resend the request.

How to fix:

  1. Debouncing or button blocking: After the first click, the "Create Order" button is blocked until a response is received from the server.

  2. Idempotency on the server: The server should be able to recognize repeated requests and not create duplicates. For example, the client generates a unique order identifier (UUID) and sends it along with the request. If the server sees an existing order with that ID, it does not create a new one.

  3. Handling network errors: When the connection is lost, the application can show a message to the user and not resend the request without explicit confirmation.

Example of an idempotent request with UUID:

String orderId = UUID.randomUUID().toString();
// Send orderId along with order data
// Server checks if an order with such orderId exists
// If it exists — returns the existing order, otherwise creates a new one

Thus, a combination of client-side blocking and server-side idempotency will solve the problem of double order creation.