Relay API Design: Building Email Infrastructure on Cloudflare Workers

1. Why We Didn't Use a Major Provider's Email API

When building an app that sends memos via email, a major mail provider's sending API is the first thing that comes to mind. However, for indie developers, the Google OAuth verification process presents a formidable barrier.

Apps that haven't passed Google OAuth verification display a warning screen: "This app isn't verified by Google." For a memo app, this warning is devastating. Users send their personal thoughts and private information as memos. If "unverified" appears during their very first experience, who would trust this app?

The Google OAuth verification process is a steep climb for solo developers. Preparing a privacy policy, justifying scope requirements, undergoing security assessments -- for a team at a large company, this is routine. But for a solo-developed app, the cost of going through all this just for a single email-sending feature is disproportionately high.

What we needed was a solution that could send emails without compromising user trust and without the complexity of OAuth verification. The answer was building our own Relay API.

2. Cloudflare Workers + Resend API Architecture

The Relay API architecture is simple yet robust. It receives requests from the iOS app and relays them to an email delivery service -- a design focused on doing exactly one thing well.

iOS App Relay API (Cloudflare Workers) Resend API Email Delivery

Cloudflare Workers is a serverless platform that executes TypeScript code at edge locations around the world. Unlike traditional serverless platforms that suffer from cold starts, Workers starts processing requests almost instantly with near-zero cold start time.

Resend API is a modern email delivery service. It makes SPF/DKIM/DMARC configuration straightforward and achieves high delivery rates. Its developer-friendly API can be called simply from TypeScript.

The cost advantage is significant. For a memo app's traffic volume, Cloudflare Workers' free tier (100,000 requests per day) is more than sufficient. Resend API is also free for up to 100 emails per month. For an indie-developed memo app, this means near-zero operational cost.

3. Email Address Verification (6-Digit Code)

The Relay API only sends memos to verified email addresses. This is the most fundamental layer of abuse prevention.

Before any memos can be sent, the destination email address must go through a verification flow:

  • User enters their email address in the app
  • Relay API sends a 6-digit verification code to that address
  • User enters the received 6-digit code back into the app
  • Verification complete. Memo sending to that email address is now permitted

This mechanism prevents unauthorized sending to other people's email addresses. Since only the email address owner can receive the verification code, impersonation is impossible.

Verification is a one-time process per email address. Once verified, users can continue sending memos without re-entering any code.

4. Multi-Layer Rate Limiting

To prevent abuse of the email sending API, we implement rate limiting across multiple layers.

const RATE_LIMITS = {
  devicePerMinute: 30,
  devicePerDay: 200,
  ipPerHour: 120,
  globalPerDay: 300,
};
Device Level
Per-Device Limiting
Prevents excessive sending from a single device. Limits set at 30 per minute and 200 per day. These thresholds are unreachable during normal memo usage but reliably block automated mass sending.
IP Level
Per-IP Address Limiting
Limits mass requests from the same network. 120 per hour. Protects infrastructure even against attacks using multiple devices on the same network.
Global
Global Limit
Protects the entire infrastructure. A global cap of 300 per day serves as the final line of defense against any attack pattern.

Counters are stored in Cloudflare KV and reset per time window. Thanks to edge computing, rate limit checks complete in tens of milliseconds. This minimizes latency for legitimate requests while reliably blocking unauthorized access.

5. Idempotency: Ensuring Memos Never Arrive Twice

Network communication is inherently unreliable. Between tapping send and receiving a response, the connection can drop. The app detects a timeout and retries the send, but the first request had actually reached the server -- a common scenario.

Idempotency guarantees solve this problem:

  • Each memo is assigned a unique UUID (message ID)
  • The Relay API records every received message ID
  • If a request with the same message ID arrives again, the email is not sent a second time
  • A success response is returned to the client (notifying that it was already sent)

With this design, even if retries occur due to network instability, the same memo will never be delivered twice. This is a critical design decision for maintaining user trust.

6. The Edge Computing Advantage

The primary reason for choosing Cloudflare Workers as our email infrastructure is the fundamental advantages of edge computing.

Latency
Global Edge Locations = Low Latency
Cloudflare operates edge servers in over 300 cities worldwide. Requests are processed at the location closest to the user, delivering API responses in tens of milliseconds from any country.
Cold Start
Near-Zero Cold Start
Traditional serverless platforms (like AWS Lambda) can take hundreds of milliseconds to several seconds for cold starts. Cloudflare Workers essentially eliminates this problem. Processing begins the instant the send button is tapped.
Operations
Auto-Scaling with No Infrastructure Management
Server management, capacity planning, scaling configuration -- none of these are needed. The system scales automatically as requests increase, and incurs no cost when idle.

For a memo app, this combination is ideal. Fast, reliable, cheap. No matter where in the world the user is, processing completes the moment they send a memo. That's the strength of a Relay API built on edge computing.

Try Simple Memo - for Obsidian, built on this technical foundation.

Download on the App Store

References