What is Idempotency?
In the context of webhooks, idempotence is the property of an endpoint to ensure it processes duplicate events exactly once. Most webhook emitters, including Trophy, operate on an “at least once” delivery guarantee. This means you may eventually receive the same webhook multiple times and your code needs to handle those scenarios without unintended consequences. For example, if your webhook handler is subscribed toleaderboard.finished
events and sends users gift cards if they place in the top 10, then your code needs to only send the gift cards once even if your code receives the same event from Trophy multiple times.
Implementing Webhook Idempotency
Making your webhook handlers more robust is trivial in most cases. Let’s take the gift card example above. After sending out a gift card to a user, your code could insert a record into your database that represents that the gift card has been sent to the user.if
statement that checks if the gift card has already been sent to the user.