Durable webhooks
A typical web application scenario is sending a notification request to a different server. Also known as a webhook, this technique often requires thinking through various failure modes and handling them.
What if the server is down? What if it’s slow? As developers, we usually start by implementing a retry mechanism. What if the server is overloaded? Let’s add exponential back-off. What if our program is a web server that needs to respond quickly? Let’s add a queue. The complexity of the code and infrastructure to handle these seemingly simple tasks grows quickly.
However, Dispatch can take care of all of that with a few lines of code.
Setup
If you haven’t already, make sure you have a Dispatch account and CLI installed.
Example code
Install FastAPI to set up an HTTP server and requests to make HTTP requests.
Here’s the full code of the server that reliably sends webhooks:
Save this code in a main.py file and run it with the Dispatch CLI:
When a GET /
request arrives, publish
function is dispatched to send a webhook. Note that we are publishing to a URL that will always return a 500. Since internal server errors are interpreted as temporary failures, Dispatch retries the operation.
This short program shows how easy it is to create reliable applications with just a few lines of code with Dispatch. We haven’t had to add extra infrastructure like queues, databases, or worker pools. Integration with Dispatch is entirely driven from the application code.