Sobes.tech
Senior

What happens if the service crashes while you are collecting a batch for writing? How did you handle consumer lag?

sobes.tech AI

Answer from AI

If the service crashes during batch collection for writing, data loss may occur if reliable error handling and state saving are not implemented. To minimize risks, the following approaches are usually used:

  • Transactional writing: the batch is collected and written atomically, either completely or not at all.
  • Write-ahead logging: data is logged before writing the batch, so that after a restart, unfinished operations can be restored.
  • Idempotent operations: to prevent duplication upon re-writing.

To handle consumer lag in systems with queues or streams (e.g., Kafka), the following are applied:

  • Monitoring lag and alerts.
  • Automatic scaling of consumers.
  • Optimization of message processing (parallelism, batching).
  • Temporary state saving to continue from the last processed position after a crash.

Example in Go for batch writing with error handling:

func writeBatch(batch []Data) error {
    // Attempt to write the batch
    err := db.Write(batch)
    if err != nil {
        // Log the error, possibly save the batch for retry
        log.Printf("Failed to write batch: %v", err)
        return err
    }
    return nil
}

In a real system, retries, transactions, and state saving are added for robustness.

What happens if the service crashes while you are… - sobes.tech