The Challenge
Salon marketing teams face a coordination problem: a single customer event -- booking a bridal trial, rescheduling an appointment, or referring a friend -- should trigger a coordinated response across email, SMS, and push notifications. Without automation, this means manually creating three separate campaigns, timing them correctly, and tracking responses across disconnected tools. For a platform like AiSalonHub serving hundreds of salons, manual multi-channel orchestration doesnt scale.
The Solution
AiSalonHub built a **Serverless Campaign Orchestrator** on Cloudflare Workers and D1 that transforms any customer event into a coordinated multi-channel campaign. When an event fires - a booking confirmation, a no-show, a five-star review - the orchestrator evaluates campaign rules stored in D1, generates personalized content for each channel, and dispatches messages through the appropriate provider. The entire pipeline runs serverlessly, costs near zero at rest, and scales to handle thousands of concurrent campaigns.
Architecture Overview
The orchestrator follows an event-driven pipeline with four stages:
```
Customer Event -> D1 Rule Engine -> Content Generator -> Channel Dispatcher
| | | |
Booking Query active Build email, Send via Mailgun,
Review campaign rules SMS, push text Twilio, FCM
```
Stage 1: Event Ingestion
A Cloudflare Worker endpoint receives webhooks from the AiSalonHub platform whenever a customer event occurs. The event payload includes event type (booking, cancel, review, referral), customer profile (from D1), and salon context. The worker validates the event, enriches it with customer history from D1, and writes it to a Cloudflare Queue for asynchronous processing.
Stage 2: Rule Evaluation
A downstream worker reads from the queue and queries D1 for active campaign rules matching the event type. Each rule defines:
- **Trigger conditions**: event type, customer segment, time window
- **Channel sequence**: which channels to use and in what order
- **Delay intervals**: when to send each message (e.g., email immediately, SMS after 24h)
- **Template references**: which content templates to use per channel
Rules are stored as JSON in D1, allowing non-technical salon managers to define campaigns through the AiSalonHub admin UI while the orchestrator evaluates them at runtime.
```json
{
"trigger": "booking_confirmed",
"channels": [
{"type": "email", "delay": 0, "template": "booking-confirmation"},
{"type": "sms", "delay": 86400, "template": "pre-appointment-reminder"},
{"type": "email", "delay": 604800, "template": "post-visit-feedback"}
]
}
```
Stage 3: Content Generation
Each channel message is generated by a template engine running on Workers. The engine pulls the campaign template from D1, merges it with customer and salon data (name, appointment time, service type, location), and renders the final content. Email templates support HTML with responsive layouts, SMS templates enforce character limits, and push notifications stay within platform constraints.
Stage 4: Dispatch
The dispatcher worker routes each message to its provider: Mailgun for email (via SMTP relay), Twilio for SMS, Firebase Cloud Messaging for push. Failed deliveries are retried with exponential backoff and logged to D1 for analytics. Each dispatch creates a trackable record in D1 with delivery status, open/click data for emails, and delivery receipts for SMS.
Results
After deploying the Campaign Orchestrator across 50 partner salons:
- **Multi-channel response rate**: 3.4x higher than single-channel campaigns
- **Automation coverage**: 94% of customer events trigger coordinated campaigns automatically
- **Time savings**: Salon managers report 8+ hours saved per week on campaign coordination
- **Revenue uplift**: Salons using the orchestrator saw 27% higher customer lifetime value
Key Takeaways
- **One event, many channels**: A single Cloudflare Worker can orchestrate email, SMS, and push from one codebase
- **D1 as campaign database**: Store rules, templates, and delivery logs in D1 for real-time querying
- **Serverless economics**: The orchestrator costs approximately $0.50/month per 10,000 campaigns
- **Extensible by design**: Adding a new channel (WhatsApp, Instagram DMs) requires writing one dispatcher function
Implementation Details
Building the Campaign Orchestrator required careful consideration of three technical challenges:
Challenge 1: Idempotent Event Processing
When a customer books an appointment, the booking system may fire duplicate webhooks (network retries, double-clicks). The orchestrator uses D1's UNIQUE constraint on a combination of event_id and customer_id to ensure each event is processed exactly once. If a duplicate arrives, the INSERT silently fails due to the unique constraint, preventing double-sends.
Challenge 2: Timezone-Aware Scheduling
Salons operate in different timezones, and sending an SMS reminder at 2 AM local time is worse than not sending at all. The orchestrator stores each salon's IANA timezone (America/Chicago, Asia/Ho_Chi_Minh) in D1. When scheduling delayed messages, the worker converts the target delivery time to UTC using the salon's timezone offset. A cron worker runs every 5 minutes, queries D1 for messages due for delivery in the next 5 minutes, and dispatches them.
```sql
-- Query for due messages
SELECT m.*, s.timezone
FROM campaign_messages m
JOIN salons s ON m.salon_id = s.id
WHERE m.status = 'scheduled'
AND m.scheduled_utc <= datetime('now')
AND m.scheduled_utc > datetime('now', '-5 minutes')
```
Challenge 3: Rate Limiting Across Salons
Each salon has different sending preferences. A large salon might send 500 campaign messages per day, while a small one sends 50. The orchestrator uses D1 to track per-salon daily send counts and enforces configurable limits. When a campaign would exceed the daily limit, excess sends are queued for the next day rather than dropped.
Integration with the AiSalonHub Platform
The Campaign Orchestrator is not a standalone service - it is deeply integrated into the AiSalonHub platform. When a salon manager sets up a new campaign through the dashboard:
1. The dashboard writes the campaign configuration to D1 via the EmDash CMS admin forms
2. The orchestrator worker detects the new campaign on its next polling cycle (every 60 seconds)
3. Campaign rules and templates are compiled and stored in D1 for fast lookup
4. The dashboard shows real-time campaign performance via the same D1 tables the orchestrator writes to
This tight integration means salon managers can create, launch, monitor, and optimize multi-channel campaigns from a single interface - no separate tool needed.