Multi-Cloud Deployment
Run AuthHero across multiple cloud providers for high availability, geographic distribution, and resilience.
Why Multi-Cloud?
- High Availability - Survive provider outages
- Geographic Distribution - Serve users from nearest location
- Cost Optimization - Use best pricing for each workload
- Vendor Independence - Avoid lock-in
Architecture Patterns
Active-Active (Global Distribution)
Deploy to multiple regions/providers simultaneously with traffic distributed globally.
Use Cases:
- Global user base
- < 100ms latency requirement
- 24/7 uptime critical
Setup:
Deploy to multiple providers
bash# Cloudflare (edge) cd cloudflare && wrangler deploy # AWS US-EAST-1 cd aws && sam deploy --region us-east-1 # AWS EU-WEST-1 cd aws && sam deploy --region eu-west-1Configure global load balancer
- Cloudflare Load Balancer
- AWS Global Accelerator
- Route 53 with geo-routing
Set up database replication
- DynamoDB global tables
- PlanetScale (MySQL) replication
Active-Passive (Failover)
Primary in one provider, automatic failover to another.
Use Cases:
- Cost-sensitive deployments
- Disaster recovery requirement
- Less critical latency needs
Setup:
Deploy primary and standby
typescript// Primary: Cloudflare Workers, on D1 import { drizzle } from "drizzle-orm/d1"; import createAdapters from "@authhero/drizzle"; import * as schema from "@authhero/drizzle/schema/sqlite"; import { init } from "authhero"; export default { async fetch(request: Request, env: Env, ctx: ExecutionContext) { const { app } = init({ dataAdapter: createAdapters(drizzle(env.AUTH_DB, { schema })), }); return app.fetch(request, env, ctx); }, };typescript// Standby: AWS Lambda, on DynamoDB import { handle } from "hono/aws-lambda"; import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import createAdapters from "@authhero/aws-adapter"; import { init } from "authhero"; const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({})); const { app } = init({ dataAdapter: createAdapters(docClient, { tableName: "authhero" }), }); export const handler = handle(app);The standby only has current data if the primary's writes reach its database — see Database Strategies.
Configure health checks
typescript// Add health endpoint to both app.get("/health", async (c) => { try { await dataAdapter.tenants.get("health-check"); return c.json({ status: "ok" }); } catch { return c.json({ status: "error" }, 503); } });Set up DNS failover
- Monitor
/healthendpoint - Automatic DNS switch on failure
- 60-second TTL for fast failover
- Monitor
Database Strategies
AuthHero keeps all state — users, sessions, refresh tokens, signing keys — in the data adapter. There is no separate session store, so every deployment that serves a tenant must read the same data, either from one shared database or from replicas you keep in sync.
Option 1: One multi-region database
Use a database whose replication you trust, and point every deployment at it:
MySQL on PlanetScale through the Kysely adapter:
typescriptimport { Kysely } from "kysely"; import { PlanetScaleDialect } from "kysely-planetscale"; import createAdapters, { type Database } from "@authhero/kysely-adapter"; const db = new Kysely<Database>({ dialect: new PlanetScaleDialect({ url: process.env.DATABASE_URL }), }); const dataAdapter = createAdapters(db);DynamoDB global tables through the AWS adapter. DynamoDB replicates the table across regions; AuthHero does not coordinate the replication itself:
typescriptimport { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import createAdapters from "@authhero/aws-adapter"; const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({})); const dataAdapter = createAdapters(docClient, { tableName: "authhero" });
Other multi-region SQL databases (CockroachDB, Aurora PostgreSQL) are not supported by the built-in adapters.
Option 2: Independent databases with mirrored writes
Give each provider its own database and mirror writes to the others with createPassthroughAdapter from @authhero/adapter-interfaces. Reads come from the primary; writes go to the primary first and are then copied to each secondary:
import { drizzle } from "drizzle-orm/d1";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { createPassthroughAdapter } from "@authhero/adapter-interfaces";
import createDrizzleAdapters from "@authhero/drizzle";
import * as schema from "@authhero/drizzle/schema/sqlite";
import createAwsAdapters from "@authhero/aws-adapter";
const primary = createDrizzleAdapters(drizzle(env.AUTH_DB, { schema }));
const backup = createAwsAdapters(
DynamoDBDocumentClient.from(new DynamoDBClient({ region: "eu-west-1" })),
{ tableName: "authhero-backup" },
);
function mirror<T extends object>(primaryAdapter: T, backupAdapter: T) {
return createPassthroughAdapter({
primary: primaryAdapter,
secondaries: [
{
adapter: backupAdapter,
onError: (error, method) => console.error("mirror failed", method, error),
},
],
});
}
// A standby can only take over if it holds everything a request reads:
// the tenant and its configuration, users and credentials, and the state of
// sessions in flight. Mirror every adapter your tenants write.
const dataAdapter = {
...primary,
tenants: mirror(primary.tenants, backup.tenants),
users: mirror(primary.users, backup.users),
passwords: mirror(primary.passwords, backup.passwords),
keys: mirror(primary.keys, backup.keys),
clients: mirror(primary.clients, backup.clients),
clientGrants: mirror(primary.clientGrants, backup.clientGrants),
connections: mirror(primary.connections, backup.connections),
resourceServers: mirror(primary.resourceServers, backup.resourceServers),
roles: mirror(primary.roles, backup.roles),
organizations: mirror(primary.organizations, backup.organizations),
sessions: mirror(primary.sessions, backup.sessions),
loginSessions: mirror(primary.loginSessions, backup.loginSessions),
refreshTokens: mirror(primary.refreshTokens, backup.refreshTokens),
codes: mirror(primary.codes, backup.codes),
// ...and the other adapters in DataAdapters that hold data you need
};Mirroring is per entity and best-effort: a secondary write that fails is reported to onError and not retried. See Adapters → Layering adapters.
Mirroring only copies writes made after it is switched on, so the standby also needs the data that existed before. AuthHero does not ship a backfill tool for this; write one against the adapters, following the pattern in the database migration guide, whose scripts cover tenants, users and password hashes:
- Copy and verify before the standby may take traffic: tenants, signing keys, clients and client grants, connections, resource servers, roles and their permissions, organizations, users (including linked accounts) and password hashes. Without these the standby cannot issue or validate tokens, or let users sign in.
- Copy, or accept losing: sessions, login sessions, refresh tokens and codes. If you don't copy them, users who are signed in when you fail over sign in again on the standby.
Repeat the verification regularly while mirroring runs, since failed mirror writes are not retried.
Failing back. Writes the standby accepts during a failover are not mirrored back to D1. Before you route traffic back to the primary, copy those writes from the standby's database to D1 (the same backfill run in the other direction) and verify the two match; until then, keep traffic on the standby.
Session Management
Sessions, login sessions and refresh tokens are stored through the data adapter, and tokens are signed with the tenant's keys from the keys adapter. A deployment can therefore continue a session started on another deployment only if it reads the same data — which the database strategies above provide. No extra session store or shared JWT secret is needed or supported.
Traffic Routing
Geographic Routing
Route users to nearest deployment:
Cloudflare Load Balancer:
{
"pools": [
{
"id": "cloudflare-edge",
"origins": ["workers.cloudflare.com"],
"enabled": true
},
{
"id": "aws-us-east",
"origins": ["auth-us.example.com"],
"enabled": true
},
{
"id": "aws-eu-west",
"origins": ["auth-eu.example.com"],
"enabled": true
}
],
"geo_steering": {
"policy": "geo"
}
}Route 53 Geolocation Routing:
aws route53 change-resource-record-sets \
--hosted-zone-id Z123456 \
--change-batch file://geo-routing.jsonWeighted Routing
Gradually shift traffic:
{
"weights": {
"cloudflare": 50, // 50% of traffic
"aws-us": 30, // 30% of traffic
"aws-eu": 20 // 20% of traffic
}
}Use for:
- Blue-green deployments
- Canary releases
- A/B testing
- Gradual migration
Cost Optimization
Tiered Strategy
Use cheapest provider for each workload:
User Request
│
▼
┌────────────────────┐
│ Cloudflare Workers │ ◀─ Authentication (cheapest, fastest)
│ Edge Layer │
└────────────────────┘
│ Heavy operations
▼
┌────────────────────┐
│ AWS Lambda │ ◀─ Batch jobs, email sending
│ Regional Layer │
└────────────────────┘
│ Database
▼
┌────────────────────┐
│ Primary Database │ ◀─ DynamoDB, PlanetScale or D1
│ Data Layer │
└────────────────────┘Cost Breakdown:
| Workload | Best Provider | Monthly Cost (est.) |
|---|---|---|
| Auth requests (10M) | Cloudflare | $5 |
| Database (100GB) | DynamoDB | $100 |
| Email sending | AWS SES | $10 |
| File storage | R2/S3 | $5 |
| Total | $120 |
Compare to single-provider: ~$200/month
Monitoring
Monitor each deployment separately:
// Add provider tag to logs
app.use("*", async (c, next) => {
c.set("provider", process.env.PROVIDER); // "cloudflare" | "aws"
await next();
});
// Centralized logging
app.use("*", async (c, next) => {
const start = performance.now();
await next();
await analytics.track({
provider: c.get("provider"),
path: c.req.path,
duration: performance.now() - start,
});
});Tools:
- Datadog - Multi-cloud monitoring
- New Relic - Application performance
- Grafana Cloud - Metrics aggregation
- Sentry - Error tracking
Example Configurations
Cloudflare Primary + AWS Backup
The Cloudflare Worker serves traffic from D1 and mirrors writes to DynamoDB with the passthrough adapter shown in Option 2. The AWS deployment reads the DynamoDB copy and takes over through DNS failover — only once the existing data has been backfilled into DynamoDB and verified, and traffic returns to Cloudflare only after the writes made on AWS are copied back to D1, as described there:
// aws/src/index.ts
import { handle } from "hono/aws-lambda";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import createAdapters from "@authhero/aws-adapter";
import { init } from "authhero";
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const { app } = init({
dataAdapter: createAdapters(docClient, { tableName: "authhero-backup" }),
});
export const handler = handle(app);AWS Multi-Region Active-Active
Deploy the same Lambda to each region against a DynamoDB global table:
// src/index.ts — deployed unchanged to us-east-1 and eu-west-1
import { handle } from "hono/aws-lambda";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import createAdapters from "@authhero/aws-adapter";
import { init } from "authhero";
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const { app } = init({
dataAdapter: createAdapters(docClient, { tableName: "authhero" }),
});
export const handler = handle(app);Testing Multi-Cloud
Verify failover works:
# Primary health check
curl https://auth.example.com/health
# Simulate primary failure
# (disable primary in load balancer)
# Verify traffic fails over
curl https://auth.example.com/health
# Should return 200 from backup
# Check latency from different regions
curl -w "@curl-format.txt" https://auth.example.com/healthMigration Guide
Moving from single-cloud to multi-cloud:
Phase 1: Add monitoring
- Set up health checks
- Configure alerting
- Baseline metrics
Phase 2: Deploy standby
- Deploy to second provider
- Set up database replication
- Test failover
Phase 3: Enable routing
- Configure load balancer
- Start with 1% traffic to new provider
- Monitor errors
Phase 4: Scale up
- Gradually increase traffic
- Add more regions
- Optimize costs
Best Practices
Same configuration everywhere
- Use environment variables
- Keep code identical
- Automate deployments
Test failover regularly
- Monthly failover drills
- Automated testing
- Document procedures
Monitor everything
- Centralized logging
- Cross-provider metrics
- Alert on anomalies
Plan for eventual consistency
- Design for it
- Handle conflicts
- User-visible state
Keep it simple
- Start with two providers
- Add complexity as needed
- Don't over-engineer