Relay API設計:Cloudflare Workersでメール送信基盤を構築 Relay API Design: Building Email Infrastructure on Cloudflare Workers
1. なぜGmail APIを使わなかったのか
メモをメールで送信するアプリを作るとき、最初に思い浮かぶのはGmail APIです。しかし、個人開発者にとってGoogle OAuth審査のハードルは極めて高いものでした。
Google OAuth審査を通過していないアプリには、「このアプリはGoogleで確認されていません」という警告画面が表示されます。メモアプリにとって、この警告は致命的です。ユーザーは自分の考えや個人的な情報をメモとして送信します。その最初の体験で「検証されていません」と表示されたら、誰がこのアプリを信頼するでしょうか。
Google OAuth審査の申請プロセスは、個人開発者にとって険しい道のりです。プライバシーポリシーの準備、スコープの正当性説明、セキュリティ評価——大企業のチームならともかく、一人で開発しているアプリにとっては、メール送信という単一機能のために払うコストとしてはあまりにも大きすぎます。
必要だったのは、OAuth審査の複雑さなしに、ユーザーの信頼を損なわずメールを送信できるソリューションでした。その答えが、自前のRelay APIです。
2. Cloudflare Workers + Resend APIアーキテクチャ
Relay APIのアーキテクチャは、シンプルかつ堅牢です。iOSアプリからのリクエストを受け取り、メール送信サービスに中継する——それだけに特化した設計です。
Cloudflare Workersは、世界中のエッジロケーションでTypeScriptコードを実行するサーバーレスプラットフォームです。従来のサーバーレスで問題になるコールドスタートがほぼゼロで、リクエストを受けた瞬間から処理が始まります。
Resend APIは、モダンなメール送信サービスです。SPF/DKIM/DMARCの設定が容易で、高い到達率を実現します。開発者フレンドリーなAPIで、TypeScriptからシンプルに呼び出せます。
コスト面でも優れています。メモアプリのトラフィック量であれば、Cloudflare Workersの無料枠(1日10万リクエスト)で十分にまかなえます。Resend APIも月100通まで無料。個人開発のメモアプリにとって、ほぼゼロコストで運用できるインフラです。
3. メールアドレス検証(6桁コード方式)
Relay APIは、検証済みのメールアドレスにのみメモを送信します。これは悪用防止の最も基本的な防御層です。
メモ送信を開始する前に、送信先のメールアドレスを検証するフローが必要です。
- ユーザーがアプリにメールアドレスを入力
- Relay APIが6桁の検証コードをそのアドレスに送信
- ユーザーが受信した6桁コードをアプリに入力
- 検証完了。以降、そのメールアドレスへのメモ送信が許可される
この仕組みにより、他人のメールアドレスに無断でメモが送信されることを防ぎます。メールアドレスの所有者本人だけが検証コードを受信できるため、なりすまし送信は不可能です。
検証はメールアドレスごとに一度だけ。一度検証が完了すれば、以降はコード入力なしでメモを送信し続けられます。
4. 多層レート制限の実装
メール送信APIの悪用を防ぐため、複数のレイヤーでレート制限を実装しています。
const RATE_LIMITS = {
devicePerMinute: 30,
devicePerDay: 200,
ipPerHour: 120,
globalPerDay: 300,
};
カウンターはCloudflare KVに保存され、各時間ウィンドウごとにリセットされます。エッジコンピューティングの利点により、レート制限チェックも数十ミリ秒で完了。正当なリクエストの遅延を最小限に抑えつつ、不正アクセスを確実に遮断します。
5. 冪等性の保証:メモが2通届かない設計
ネットワーク通信には不確実性がつきものです。送信ボタンを押してから応答が返るまでの間に、通信が途切れることがあります。アプリはタイムアウトと判断して再送信を試みますが、実は最初のリクエストはサーバーに届いていた——よくあるシナリオです。
この問題を解決するのが冪等性(idempotency)の保証です。
- 各メモに一意のUUID(メッセージID)を付与
- Relay APIは受信したメッセージIDを記録
- 同一のメッセージIDを持つリクエストが再度届いた場合、メールの重複送信は行わない
- クライアントには成功レスポンスを返す(すでに送信済みであることを通知)
この設計により、ネットワーク不安定な環境でリトライが発生しても、同じメモが2通届くことは絶対にありません。ユーザーの信頼を守るための重要な設計判断です。
6. エッジコンピューティングの利点
Cloudflare Workersをメール送信基盤に採用した最大の理由は、エッジコンピューティングの本質的な利点にあります。
メモアプリにとって、この組み合わせは理想的です。速い、信頼性が高い、安い。ユーザーが世界中どこにいても、メモを送信した瞬間に処理が完了する。それがエッジコンピューティングで構築したRelay APIの強みです。
この技術基盤の上に構築された「Captio式シンプルメモ」を、ぜひお試しください。
App Storeでダウンロード1. Why We Didn't Use Gmail API
When building an app that sends memos via email, Gmail 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.
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,
};
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.
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 - Captio-style, built on this technical foundation.
Download on the App Storeよくある質問
Q. なぜCloudflare Workersを選んだのですか?
コールドスタートがほぼゼロ、世界中のエッジから配信、月額ほぼ無料。メモアプリに必要な速さと信頼性の両方を実現できるプラットフォームだからです。
Q. メールの到達率は?
Resend APIは高い到達率を実現しています。SPF/DKIM/DMARC設定済みで、迷惑メール判定を最小化しています。
Q. サーバーにメモの内容は保存されますか?
Relay APIは通過型です。メモ内容の恒常的な保存は行いません。送信処理に必要な範囲でのみ一時的に扱います。
Q. レート制限に引っかかったらどうなりますか?
アプリ側でOutboxにキューイングされ、制限解除後に自動リトライします。メモは失われません。
Frequently Asked Questions
Q. Why Cloudflare Workers?
Near-zero cold start, global edge delivery, and nearly free monthly cost. It's a platform that achieves both the speed and reliability a memo app demands.
Q. What's the email delivery rate?
Resend API achieves high delivery rates. SPF/DKIM/DMARC are pre-configured to minimize spam classification.
Q. Is memo content stored on the server?
The Relay API is pass-through. Memo content is not persistently stored. It is handled temporarily only as needed for the send operation.
Q. What happens when rate limited?
The app queues memos in the Outbox and automatically retries after the rate limit resets. No memos are lost.
関連記事
Related Articles
参考文献 References
- Cloudflare Workers Documentation — リレーAPIのサーバーレス実行基盤。エッジでのリクエスト処理に使用Serverless execution platform for the relay API, processing requests at the edge
- RFC 5321 — Simple Mail Transfer Protocol (SMTP) — メール送信の基本プロトコル仕様。リレーAPIが準拠するSMTP標準Core email transmission protocol specification that the relay API adheres to
- RFC 6376 — DomainKeys Identified Mail (DKIM) — 送信メールの改ざん防止と送信元認証に使用する電子署名仕様Digital signature specification for preventing email tampering and authenticating senders
- RFC 7208 — Sender Policy Framework (SPF) — 送信ドメイン認証。許可されたIPアドレスからのメール送信を検証Sender domain authentication verifying email is sent from authorized IP addresses
- Resend API Documentation — メール送信APIプロバイダー。Cloudflare Workersから呼び出すメール配信サービスEmail sending API provider called from Cloudflare Workers for mail delivery