Sobes.tech
Senior

For example, creating an order. There is a request to the service and a response, with inter-service and external goods described (for example, for calculating the popularity of goods). The service repeatedly performs slowly or times out, and we fail to respond, losing the order. What to do to stop losing orders and, accordingly, money?

sobes.tech AI

Answer from AI

To avoid losing orders due to slow service performance or timeouts, it is necessary to shift from synchronous to asynchronous processing and ensure reliable message delivery.

Main approaches:

  1. Using message queues (e.g., Kafka, RabbitMQ). Instead of waiting for responses from external services, the order is placed in a queue, and the service immediately confirms receipt of the order. Order processing occurs asynchronously, which prevents loss due to timeouts.

  2. Following the "Event Sourcing" or "CQRS" pattern — recording the creation event of an order in a log, then processing and updating the state asynchronously.

  3. Implementing retry mechanisms and dead-letter queues for handling failed messages.

  4. Monitoring and alerts — to quickly respond to failures.

Example in Java using Kafka (simplified):

// Sending order to Kafka
producer.send(new ProducerRecord<>("orders", orderId, orderData));
// Service immediately responds to the client that the order is accepted

Thus, the order is not lost, even if external services are slow or unavailable.