Senior
If the same event arrives twice (a duplicate), what do you do?
sobes.tech AI
Answer from AI
When processing duplicate events, it is important to ensure idempotency — so that reprocessing does not lead to errors or incorrect states.
Approaches:
- Store a unique event identifier (event ID) and check if it has already been processed.
- Use databases or caches (e.g., Redis) to store processed IDs with TTL.
- In the processing logic, ignore events with IDs that have already been encountered.
Example in Go:
var processedEvents = make(map[string]bool)
func handleEvent(eventID string) {
if processedEvents[eventID] {
fmt.Println("Duplicate event, skipping")
return
}
// Process the event
processedEvents[eventID] = true
fmt.Println("Processing event", eventID)
}
In a real application, it is better to use thread-safe structures and external storage for fault tolerance.