Design a Reliable Poller for a Lagging Upstream Database
System DesignModel answerLast updated
Determine whether variable record lag allows a fixed polling interval to guarantee one new record per poll, then define a production-ready ingestion strategy.
Design a Reliable Poller for a Lagging Upstream Database
A upstream service emits one event every 30 seconds. You cannot inspect or modify this service. Each event eventually lands in database B — a read-only black box — with a per-record propagation delay anywhere from zero to five minutes. No event is ever dropped.
You control database C and a poller process that reads from B and writes into C. A downstream consumer reads from C and expects to process exactly one new record per request, never receiving duplicates or missing any record.
Before going live you are allowed an observation window in which you can watch real arrivals and calibrate your design. Once live, the business requirement is the shortest possible polling interval that still satisfies the one-record-per-request contract with zero failures.
Clarifying questions worth raising
- Does B expose a stable cursor, offset, or sequence number you can query against?
- Can B be queried for "give me at most one record I have not yet seen"?
- Is it possible for events to appear in B out of the order they were generated?
- Does a poll that returns zero results count as a contract failure?
- Is C allowed to buffer multiple ingested records ahead of the one-at-a-time consumer?
Part 1 — Analyze the core requirement
Using worst-case reasoning, decide whether the known generation cadence (one per 30 s) and the known maximum lag (five minutes) are enough to choose a fixed polling interval that guarantees exactly one new arrival per poll.
Your answer should address:
- A worst-case arrival argument, not an average-rate argument
- The distinction between generation cadence and observed arrival cadence
- Whether the guarantee as literally stated is achievable, and why or why not
Part 2 — Use the observation window
Describe what you would measure or prove during the boot/test phase before going live, and be honest about what a finite observation window can and cannot tell you.
Your answer should address:
- How to establish stable record identity, arrival ordering, and a lag distribution
- How to validate cursor semantics and idempotency
- A warm-up or backlog-drain policy before the consumer starts
- Why empirical observation is evidence, not proof, of a production bound
Part 3 — Define the live polling strategy
Propose a concrete design, including a polling rate, under clearly stated assumptions. The downstream consumer's one-at-a-time requirement must be preserved without pretending the source delivers exactly one record per interval.
Your answer should address:
- How to decouple ingestion from one-at-a-time consumption
- Durable cursors, deduplication, and retry behavior
- Buffer sizing derived from the five-minute lag bound
- Monitoring signals and the response plan if the lag bound is violated
Hints
Hint 1
Start with the worst case: two records generated 30 s apart could both propagate to B at the maximum five-minute lag and arrive at almost the same moment. A fixed interval calibrated to average behavior will fail in that scenario.
Hint 2
A buffer in C is the key architectural move. Separate the question 'how fast should I poll B?' from the question 'how does the consumer get exactly one record at a time?' — these do not have to be the same rate.
Hint 3
Think about what a cursor buys you: idempotent re-reads on retry, safe resume after a crash, and the ability to detect out-of-order arrivals. Commit the cursor to C only after successfully writing the record, not before.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
What a Strong Answer Looks Like
Part 1 — Why a fixed interval cannot guarantee exactly one arrival
The literal requirement — every poll returns exactly one previously unseen record, never zero, never two — is not satisfiable with a fixed interval given the constraints. Here is the worst-case argument.
Two consecutive records are generated 30 seconds apart. Each individually experiences the maximum five-minute lag. Record 1 arrives at t = 5:00, record 2 arrives at t = 5:30. A poller set to 30-second intervals will see zero records for nine consecutive polls (t = 0:30 through t = 4:30), then one record, then one more 30 s later. That is the best case.
Now flip it: record 1 experiences nearly the full five-minute lag, record 2 experiences near-zero lag. Both appear in B within a few seconds of each other. Any poll interval longer than that gap returns two records at once; any interval shorter returns zero on the preceding poll. There is no fixed interval that threads this needle deterministically because arrival timing is not under your control.
The deeper issue is that generation cadence and arrival cadence are different things. You know one record is produced every 30 seconds; you do not know when it surfaces in B. Variable lag turns a uniform production stream into a bursty, irregular arrival stream. A guarantee that depends on uniform arrivals cannot be built on top of variable lag without an intermediate buffer.
Common mistake: using average lag (say, 2.5 minutes) to set the interval. Averages hide the variance that breaks the contract.
Part 2 — What to do in the observation window
The observation window lets you gather evidence, not proofs. Treat it that way.
Measure:
- Record identity and ordering — confirm B provides a stable, monotonically increasing cursor or sequence number. If records can arrive out of generation order (sequence 7 before sequence 6), your cursor logic must handle gaps.
- Lag distribution — record wall-clock insertion time in C alongside the generation timestamp embedded in each record. Collect p50, p95, p99, and the observed maximum. If your observed max is 3 minutes over a 24-hour window, that is weak evidence the stated 5-minute bound is correct. It is not proof.
- Idempotency — deliberately re-poll an already-consumed cursor offset and confirm B returns the same record. Confirm that writing it a second time to C is a safe no-op.
Warm-up policy: Before the consumer starts, drain any backlog in B. A practical rule: wait until at least one full maximum-lag window (5 minutes) has elapsed with no new arrivals, or until you have caught up to a known recent cursor. This prevents the consumer from seeing a burst of queued records on first start.
Be honest about limits: A finite test cannot prove an unbounded production property. If you observed a maximum lag of 4:45 in testing, the production system could still exceed 5 minutes. Design with margin — treat the 5-minute figure as a soft bound you monitor, not a hard guarantee you rely on.
Part 3 — The live strategy
The key architectural move is decoupling ingestion rate from consumption rate. C acts as a buffer; the poller fills it as fast as records arrive, and the consumer drains it one record at a time.
Poller design:
- Poll B on a short interval — 10 to 15 seconds is reasonable given 30-second generation cadence. This ensures you pick up near-zero-lag arrivals promptly and reduces the gap between generation and availability in C.
- Issue a query of the form "give me all records with cursor > last_committed_cursor, ordered by cursor, limit N" where N is a small batch size (e.g., 10). Write each record into C's buffer table, then advance and durably commit the cursor in C only after the write succeeds. Commit cursor after write, never before — otherwise a crash between the two leaves a gap.
- If the poll returns zero records, that is expected behavior, not a failure. Log it, increment a counter, continue.
Deduplication: Use the record's stable identity (cursor offset or a natural key embedded in the record) as a unique key in C's buffer table. An upsert or insert-if-not-exists on that key makes every write idempotent, so retries are safe.
Out-of-order arrivals: If B can surface record 7 before record 6, hold record 7 in C with a "pending" status until record 6 also arrives (or until a configurable timeout), then release them in order. Buffer sizing should accommodate at least one full lag window: at 30 s/record and 5 min max lag, that is a maximum of 10 records in flight simultaneously. Size the buffer for at least 2x that — 20 slots — with an alert threshold.
Consumer interface: Expose a simple "pop one record" endpoint or queue from C's buffer. It returns the oldest unprocessed record and marks it in-progress. The consumer acknowledges completion; only then is the record marked done. This satisfies the one-at-a-time requirement without placing that constraint on the poller.
Monitoring and response:
- Alert if observed lag exceeds 4 minutes (80% of the stated bound). Investigate before it becomes a violation.
- Alert if the buffer depth exceeds the burst threshold (e.g., more than 15 records queued). This signals either a burst arrival or a slow consumer.
- If the lag bound is actually violated in production (a record takes more than 5 minutes), the system continues to function correctly — it just holds a slightly longer queue. The violation is a data contract issue to escalate to the B team, not a reason to crash the poller.
Summary of the contract: The downstream consumer receives exactly one record per request because C buffers and serializes delivery. The poller makes no such promise — it ingests as fast as B delivers, which is variable. The business requirement is preserved by the buffer layer, not by the polling interval.