Stripe Webhook Idempotency
Implementing idempotent webhook processing to handle duplicate events and ensure credit balance accuracy.
Stripe webhooks can be delivered multiple times. Processing them without idempotency can lead to duplicate credit additions.
The Problem: - Stripe may retry webhook delivery - Network issues can cause duplicate events - Processing same event twice = double credit addition
The Solution: - Store processed event IDs in database - Check event ID before processing - If event already processed, return success (idempotent) - Use database unique constraint on event ID
Implementation: ``` 1. Receive webhook 2. Verify Stripe signature 3. Check if event_id exists in processed_events table 4. If exists, return 200 (already processed) 5. If not, process event and insert event_id 6. Return 200 ```
Result: - Zero duplicate credit additions - Handles webhook retries gracefully - Simple and reliable
What I'd change: Nothing. This pattern works well and is standard for webhook processing. The key is checking idempotency before any side effects.