DEV Community

Max LIAO
Max LIAO

Posted on

How I built a QR code redirect system with Cloudflare Workers for sub-100ms response time

I'm building a one-time payment QR code service, and one of the most critical pieces is the redirect system. When someone scans a QR code, the redirect needs to be instant — any delay and people think the code is broken.

Here's how I architected it.

The Problem

Most QR code services run redirects through their main app server. That means:

  • Cold starts on serverless (500ms+)
  • Single region latency
  • Database queries on every scan

For a QR code that might be printed on a restaurant menu and scanned 100 times a day, that's unacceptable.

The Solution: Cloudflare Workers

Cloudflare Workers run at the edge in 300+ cities worldwide. Every scan hits the nearest edge node.

The flow:

  1. User scans QR → hits ownqrcode.com/r/{slug}
  2. Cloudflare Worker intercepts the request
  3. Worker looks up the destination URL from Supabase
  4. 302 redirect to destination
  5. Async: log scan event (device, location from request.cf)

Key decisions

302 not 301: We use 302 (temporary) redirects because users can change their destination URL anytime. A 301 would get cached by browsers and CDNs, making URL changes invisible to returning visitors.

Geo data for free: Cloudflare's request.cf object gives us city, country, latitude, and longitude on every request — no external geo API needed. This powers our scan analytics dashboard.

Async logging: We don't await the Supabase insert for scan events. The redirect fires immediately, and the analytics data gets logged in the background using ctx.waitUntil(). Users get their redirect in <50ms.

Results

  • Average redirect time: ~40ms
  • Works globally with no cold starts
  • Scan analytics with device + location data at zero extra cost
  • Total infra cost: ~$5/month on Cloudflare Workers

Stack

  • Redirect service: Cloudflare Workers
  • Main app: Next.js 14 (App Router) on Vercel
  • Database: Supabase (PostgreSQL)
  • Payments: Stripe (one-time, no subscriptions)

If you're building anything that needs fast redirects at scale, Cloudflare Workers is hard to beat.


I'm building OwnQR — a $15 one-time QR code generator for small businesses. Happy to answer questions about the architecture.

Top comments (0)