Middle+
How did you configure idempotency to prevent duplicate announcements?
sobes.tech AI
Answer from AI
Idempotency in the context of ad processing is usually achieved by uniquely identifying each ad and checking its existence before processing. In Go, this can be implemented, for example, using a database with a unique index on the field that identifies the ad (such as ID or content hash).
Example approach:
- When receiving an ad, generate a unique key (e.g., UUID or hash).
- Attempt to insert a record with this key into a database with a unique constraint.
- If the insertion is successful — the ad is new, process it.
- If a uniqueness error occurs — the ad has already been processed, ignore it.
Example code in Go using a database:
func processAd(ad Ad) error {
// ad.ID — unique identifier of the ad
err := db.InsertAd(ad)
if err != nil {
if isUniqueViolation(err) {
// Ad has already been processed
return nil
}
return err
}
// Process the ad
return handleAd(ad)
}
You can also use caching (e.g., Redis) with a key set for a certain time to quickly filter out repeated requests.