Skip to content
Dispatch Dispatch Dispatch
Python - Durable webhooks

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.

pip install "dispatch-py[fastapi]" requests

Here’s the full code of the server that reliably sends webhooks:

from fastapi import FastAPI
from dispatch.fastapi import Dispatch
import requests
app = FastAPI()
dispatch = Dispatch(app)
@dispatch.function
def publish(url):
response = requests.post(url, data={"message": "Hello world"})
response.raise_for_status()
@app.get("/")
def root():
publish.dispatch("https://httpstat.us/500")
return "OK"

Save this code in a main.py file and run it with the Dispatch CLI:

dispatch run -- uvicorn main:app

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.

dispatch | 2024-05-02 06:57:59.165 WARN function call failed function=publish status=Temporary error
dispatch | 2024-05-02 06:57:59.396 INFO calling function function=publish

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.