Sobes.tech
Middle+

Scenario For example, creating an order. There is a request to the service and a response, and between these two actions, we log the products ordered (for example, to count product popularity). The analytics service periodically runs slowly or times out, and we fail to respond, losing orders. What should be done to stop losing orders and, consequently, money?

sobes.tech AI

Answer from AI

To avoid losing orders due to slow or timeout analytics, it is necessary to separate order processing from sending data to analytics. Main approaches:

  1. Asynchronous analytics processing

    • Instead of waiting for a response from the analytics service in the main order processing flow, send data to a message queue (e.g., Kafka, RabbitMQ).
    • A separate worker or service will read from the queue and send data to analytics.
  2. Buffering and retries

    • If the analytics service is unavailable, data remains in the queue and retries are attempted.
  3. Error handling and monitoring

    • Log errors when sending data to analytics.
    • Set up alerts for long delays or failures.
  4. Service decomposition

    • Separate the order service and analytics service so that failures in analytics do not affect order processing.

Example in Go using a channel for asynchronous sending:

 type Order struct {
     ID    string
     Items []string
 }

 var analyticsChan = make(chan Order, 100)

 func createOrder(order Order) {
     // Order processing
     // ...

     // Send to analytics asynchronously
     select {
     case analyticsChan <- order:
         // successfully sent to channel
     default:
         // channel is full, can log or save locally
     }
 }

 func analyticsWorker() {
     for order := range analyticsChan {
         // Send data to analytics service
         // If error occurs, retries can be implemented
     }
 }

 func main() {
     go analyticsWorker()
     // ...
 }

This approach ensures that order creation does not depend on the analytics service state, and orders are not lost.

Scenario For example, creating an order. There is a… - sobes.tech