This example shows how to use Durable Objects and Hyperdrive to build a unique counter for multiple users.
- It assigns one Python Durable Object to each room.
- Reactions that increase the count are kept in an in-memory dictionary for fast aggregation first, and persisted to Durable Object storage until an alarm flushes them to PostgreSQL through Hyperdrive every five seconds.
- Docker Compose - for local PostgreSQL
- uv
Start PostgreSQL, install the Python dependencies, then start the Worker:
docker compose up --build -d --wait
uv run pywrangler devOpen http://localhost:8787 to join a room, send reactions, and watch each
five-second batch move from the Durable Object to PostgreSQL.
curl -X POST http://localhost:8787/rooms/concert/reactions/heart
curl -X POST http://localhost:8787/rooms/concert/reactions/laugh
curl http://localhost:8787/rooms/concert/statsThe stats response separates accepted totals, PostgreSQL persisted totals, and the
currently buffered pending counts.
Apply schema.sql to the production PostgreSQL database first. Then create a Hyperdrive
configuration for that database, replace the placeholder id in wrangler.jsonc with
its Hyperdrive ID, and run:
uv run pywrangler deploy