HLD: Delayed Trigger Service

Difficulty: Intermediate–Advanced · Prerequisites: Message Queues and Leader Election


TL;DR

A service that fires HTTP callbacks at a future time. Short delays (≤15 min) go directly to a queue. Long delays park in a database and a sweeper migrates them to the queue as their fire time approaches.

flowchart LR
    CALLER["Caller services"]
    API["Trigger API"]
    DB[("Cassandra<br/>durable store")]
    SWEEPER["Sweeper<br/>scans upcoming"]
    SQS["Queue<br/>SQS with delay"]
    DISP["Dispatcher<br/>fires callback"]
    TARGET["Callback URL"]

    CALLER -->|"1. register"| API
    API -->|"2. persist"| DB
    API -->|"3. short delays"| SQS
    SWEEPER -->|"4. scan due"| DB
    SWEEPER -->|"5. enqueue"| SQS
    SQS -->|"6. pick up"| DISP
    DISP -->|"7. POST"| TARGET

In 3 sentences: Services register “call me back in X minutes” requests. The system persists the trigger durably, then fires the HTTP callback at the right time with retries. Two tiers: short delays use a managed queue (SQS); long delays sit in a database until a sweeper promotes them.


1. Understanding the Problem

A delayed trigger service lets internal services register a callback with a future fire time (e.g., “ping me back in 30 minutes”). When the delay elapses, the service POSTs a response to a callback URL the caller provided. It’s the building block behind reminders, payment retries, abandoned-cart nudges, “release the seat hold in 7 minutes,” and “auto-cancel the order if not paid in 15 min.”

2. Naive First Cut

flowchart LR
  Caller["Caller service"] --> API["Trigger API"]
  API --> Mem["In-process map<br/>triggerId to fireAt"]
  Timer["ScheduledExecutorService"] --> Mem
  Timer --> Caller

  class Caller client
  class API,Timer service
  class Mem data

Why this breaks under real constraints:

  • Process crash loses every pending trigger - no durability. If the box reboots, every “fire in 10 min” is gone.
  • One JVM caps throughput - ScheduledExecutorService is fine for thousands of timers, hopeless at millions.
  • No horizontal scale - two replicas would each fire the callback, giving duplicates.
  • No retry on callback failure - if the caller’s endpoint is down at fire time, the trigger is silently lost.
  • No long delays - JVM heap pressure with millions of DelayedTask objects.
  • Hot fire-time spikes - “fire at top of the hour” thundering herd would saturate the executor.
    💡 A thundering herd is a crowd of work arriving in the same instant rather than spread out. Here it happens because humans and cron jobs ask for round delays, so an enormous share of triggers all come due exactly on the minute. Learn more →

The rest of the doc evolves this into a durable, sharded, bucketed scheduler with a timing-wheel front end and at-least-once HTTP callback delivery.

3. Prior Art We’re Drawing From

  • Airbnb Dynein - short-delay jobs (≤15 min) go straight to SQS; long-delay jobs sit in DynamoDB and a sweeper moves them to SQS as fire time approaches. We borrow this two-tier split.
  • Apache Kafka Purgatory + Hierarchical Timing Wheels - O(1) insert/expire for millions of in-memory timers across multiple resolutions. We use this for the hot, near-future tier.
  • AWS SQS delay queues - built-in 0–15 min delay primitive. We treat SQS as the “execution lane” once a trigger is within 15 min.
  • Stripe Idempotency-Key - caller-supplied idempotency key on register so repeated submissions don’t create duplicate triggers.
  • Netflix Maestro - sharded execution layer; we use the same partition-by-triggerId model so each shard is independently leader-led.

When deep dives below cite “borrowing from Dynein” or “borrowing from Kafka Purgatory,” that’s what they mean.

4. Functional Requirements

Core:

  1. registerTrigger(callerId, callbackUrl, payload, delaySeconds, idempotencyKey) returns a triggerId. Persists the intent durably.
  2. At now + delaySeconds, the service POSTs {triggerId, payload} to callbackUrl and considers the trigger fired only after a 2xx ack.
  3. cancelTrigger(triggerId) - best-effort cancel before fire time.

Below the line (out of scope for the interview):

  • Recurring / cron-like triggers.
  • Workflow chaining (trigger A fires trigger B).
  • Per-caller priority / rate limits beyond fairness.
  • Cross-region active-active.

5. Non-Functional Requirements

Core:

  • Durability - once register returns 200, the trigger must fire even if every component crashes. No silent drops.
  • On-time firing - P99 jitter < 1s for short delays, < 5s for long (>15 min) delays.
    💡 Jitter here means “how far off the requested time we actually fire”. P99 is the figure 99% of fires stay inside, so this says the worst 1 in 100 triggers may be up to a second late - not the average. Learn more →
  • Throughput - 10K registers/sec and 10K fires/sec at peak. In steady state those two have to be equal, so one number drives both.
  • At-least-once + idempotent - callback may fire twice; caller must dedupe via triggerId.
    💡 At-least-once delivery = we promise never to drop a callback, and in exchange we may send it twice. The alternative, exactly-once, cannot be built over a network that loses acknowledgements, so the duplicate is pushed onto the caller to recognise. Learn more →

Below the line: strict-once delivery, sub-millisecond jitter, multi-region failover.

6. Scale Estimation (Back-of-Envelope)

  • Pending set: 50M triggers in flight at any given time across all callers
  • Write QPS: 10K registers/sec at peak.
    💡 QPS = queries per second, the rate of requests arriving. Learn more → Cross-check against the pending set: 50M pending at 10K fires/sec implies an average delay of ~5,000s, call it 83 minutes. That matches the workload - seat holds, payment timeouts, nudges - and tells us the long tail is a minority of rows, not the bulk.
  • Read QPS: the sweeper reads a handful of partitions per second (the sub-shards of the bucket currently in range), plus ~5K status queries/sec from callers
  • Storage: the live table is small - 50M rows at ~500B is ~25GB. The volume is in fired rows: 10K/sec is 864M/day, so a 30-day audit retention is ~13TB. That is the number that decides whether audit history lives in Cassandra or gets shipped to object storage.
  • Timing precision: < 1s P99 jitter for short delays, which is the constraint that rules out a plain minute-granularity poll

7. Core Entities