DEV Community

Shaikh Al Amin
Shaikh Al Amin

Posted on

Building Resilient, Scalable Apps with Bun, Hono, and Cloudflare

When building modern web applications, handling background jobs, managing state, and ensuring fast database access are critical. Recently, I migrated my Bun + Hono project to Cloudflare’s ecosystem, and the combination of Cloudflare Queues, Durable Objects, KV, R2, D1, and Hyperdrive with Planetscale turned out to be a game-changer—especially for managing repeatable jobs and dead-letter queues (DLQ).

The Stack at a Glance:

Bun + Hono: Lightweight, fast runtime and web framework running on Cloudflare Workers.

Cloudflare Queues: Reliable message passing for background tasks.
Durable Objects + D1: Stateful processing with persistent SQL storage (via D1) for job metadata and retry logic.

KV & R2: Ultra-low-latency key-value cache and object storage for files.
Planetscale + Hyperdrive: MySQL-compatible database with Hyperdrive’s connection pooling and query caching, accessed through Drizzle ORM.

How It All Flows:

Request Handling: An incoming HTTP request hits the Hono app. For simple reads/writes, the app uses Drizzle ORM to query Planetscale through Hyperdrive. Hyperdrive caches frequent queries, dramatically reducing latency and database load.

Job Orchestration: For tasks that need background processing (e.g., sending emails, image processing), the app enqueues a message into Cloudflare Queue. This guarantees delivery and decouples the request from heavy work.

Durable Object Processing: A Durable Object consumes messages from the queue. It maintains job state in its attached D1 database (for persistence across failures) and uses KV for ephemeral data like rate-limit counters. If the job involves files, it streams them to/from R2.

Database Interactions: The Durable Object also updates the Planetscale database via Drizzle + Hyperdrive. Since Hyperdrive pools connections and caches read results, repeated lookups (e.g., checking user quotas) are lightning fast.

Resilience & DLQ Handling: Failed jobs are retried according to logic inside the Durable Object. If all retries fail, the job can be moved to a dead-letter queue (simulated using D1 or KV), allowing manual inspection without losing data.

Why This Rocks:

Performance: Hyperdrive’s cache reduces database round trips; KV gives microsecond access to hot data; R2 handles large files without bloating the DB.

Reliability: Queues and Durable Objects ensure exactly-once processing semantics and state persistence—perfect for financial transactions or idempotent jobs.

Developer Experience: Bun’s speed, Hono’s simplicity, and Drizzle’s type-safe SQL make development a joy. Everything deploys seamlessly with Wrangler.

Top comments (2)

Collapse
 
nathan_flurry profile image
Nathan Flurry

Also highly recommend looking at Rivet Actors for an open-source alternative to Durable Objects + Workflows. It can be self-hosted & supports lower latency to your PlanetScale database.

Collapse
 
shaikhalamin profile image
Shaikh Al Amin

This workflow works really great when you don't want to leave the Cloudflare ecosystem and you want to manage your application serverless way.