Sobes.tech
Senior

Как решить проблему non-repeatable read и дедупликации на стороне консьюмера?

sobes.tech AI

Answer from AI

The non-repeatable read problem occurs when data is read multiple times within a single transaction, and the data changes between reads. To solve this at the database level, stricter isolation levels such as Repeatable Read or Serializable are used. But if it concerns a consumer that reads messages from a queue or stream, then idempotency and deduplication must be ensured.

For deduplication on the consumer side, you can:

  • Use unique message identifiers (e.g., UUID or offset).
  • Store already processed IDs in a fast storage (e.g., Redis or a local cache).
  • Before processing, check whether this ID has already been processed.

Example in Java using a Set to store processed IDs:

Set<String> processedIds = ConcurrentHashMap.newKeySet();

public void consume(Message msg) {
    if (processedIds.contains(msg.getId())) {
        // Message has already been processed, skip
        return;
    }
    // Process the message
    process(msg);
    processedIds.add(msg.getId());
}

For fault tolerance, the state of processedIds can be periodically saved or an external storage can be used.

Thus, by combining database-level transactions and idempotent processing on the consumer side, non-repeatable reads and duplication can be avoided.