Scrivio Interview library
DesignIntermediate7 min read · updated 13 November 2026

An order must be cancelled if it is not paid within 30 minutes, and a reminder email goes out 24 hours after an abandoned cart; about 2 million orders a day. Kafka has no per-message delay.

Design the delayed actions, compare the options, and explain how a timer is cancelled when the order is paid.

Two million orders a day is only about 23 per second, so this is a scheduling problem, not a volume problem. The simplest design is a timer table written in the same transaction as the order and polled by workers with SELECT ... FOR UPDATE SKIP LOCKED; Kafka-only options are a fixed-delay topic per delay tier or a Kafka Streams processor with a wall-clock punctuator, and brokers with native delays or a workflow engine's durable timers also fit within their limits. Timers are not deleted on payment: when one fires, the handler reloads the order and does nothing if it is already paid, which makes it idempotent and safe against races.

Why this question is asked

Delayed work appears in almost every business system: cancel unpaid orders, send reminders, expire reservations, retry a payment tomorrow. Kafka is the event backbone in many of those systems, yet it has no per-message delay, so teams have to choose a design. Interviewers ask this to see whether we size the problem first, know several workable options and their limits, and handle the tricky part, cancelling a timer when the situation changes. In this answer, we will size the load, build the database timer table, look at two Kafka-only designs and at brokers with native delays, compare them, and then see why we never delete timers.

1Sizing the Problem First

Two million orders a day divided by 86,400 seconds is about 23 per second on average, and perhaps 100 to 200 per second at a peak hour. Each order creates at most two timers: the 30-minute payment timeout and, for abandoned carts, the 24-hour reminder. Even at peak that is a few hundred timer inserts and a few hundred firings per second, which any of the options below handles comfortably.

So the hard parts are elsewhere: timers must not be lost when a service crashes, they must fire close to the due time, they must fire once in effect even if they fire twice in practice, and a paid order must never be cancelled. Those are the criteria we compare on.

2Option 1: a Timer Table in the Database

The order service already has a database. When it creates an order, it inserts a timer row in the same transaction, so either both exist or neither does. A small set of workers polls for due timers. SELECT ... FOR UPDATE SKIP LOCKED lets several workers poll at once: each locks the rows it takes and skips rows another worker has locked, so no timer is handled twice concurrently and no worker waits for another.

SQL
CREATE TABLE timer (
id bigserial PRIMARY KEY,
order_id bigint NOT NULL,
action text NOT NULL, -- 'CANCEL_UNPAID' or 'CART_REMINDER'
due_at timestamptz NOT NULL
);
CREATE INDEX timer_due ON timer (due_at);
-- in the order transaction
INSERT INTO timer (order_id, action, due_at) VALUES (981, 'CANCEL_UNPAID', now() + interval '30 minutes');
-- each worker, every second
BEGIN;
SELECT id, order_id, action FROM timer
WHERE due_at <= now() ORDER BY due_at LIMIT 100
FOR UPDATE SKIP LOCKED; -- 0 to 100 rows, never another worker's
-- publish an OrderTimerFired event (or call the handler), then:
DELETE FROM timer WHERE id = ANY(:ids);
COMMIT;

This design is simple, transactional and easy to inspect: a support engineer can query which timers are pending for an order. If the worker publishes to Kafka, it should do so through an outbox or accept that a crash between publishing and committing will fire the timer again, which the idempotent handler below makes harmless. Its limit is volume: millions of firings per second would need partitioning of the table, but 23 per second is nothing.

3Option 2: Kafka-Only Designs

Kafka cannot hold one record back until its own due time, but a topic where every record has the same delay keeps records in due order, because they arrive in time order. So we create one topic per delay tier, for example order-timeouts-30m and cart-reminders-24h. The consumer reads a record, compares the record timestamp plus the tier's delay with the clock, and if the record is not due yet, it pauses the partition and seeks back to that record, resuming when the due time arrives. Since every record behind it is due later, nothing is blocked unfairly. Spring Kafka's non-blocking retries use the same trick for their back-off topics.

The second Kafka-only design uses Kafka Streams. A processor stores each timer in a state store keyed by due time and order id, and a wall-clock punctuator runs every few seconds, scans the store from the earliest key up to now, emits the due actions and deletes them. The state store is backed by a changelog topic, so timers survive restarts.

Both designs keep everything inside Kafka, which some platforms value, but both are more code to get right than a table, and the fixed-tier design only supports the delays we created topics for.

4Option 3: Native Delays and Workflow Engines

Some brokers schedule delivery natively. RabbitMQ has a delayed message exchange plugin, and cloud queues offer a delivery delay, but often with a cap: Amazon SQS allows at most 15 minutes, so the 24-hour reminder would need re-queueing or a different tool. Check each product's limits on delay length and on the number of scheduled messages.

A workflow engine such as Temporal models the whole flow as code with durable timers: wait 30 minutes or until a payment signal arrives, then cancel or finish. This is attractive when the order lifecycle has many such waits, and expensive if this is the only one, because it is a new platform to run.

Comparing the options
OptionStrengthCost or limit
DB timer tableTransactional, inspectablePolling; partition at huge scale
Delay topic per tierAll in Kafka, orderedOnly fixed delays; pause/seek code
Kafka Streams punctuatorAny delay, durable stateStreams app to build and run
Broker native delayNo code for timingCaps, e.g. 15 minutes on SQS
Workflow engine timersWhole flow as codeNew platform to operate
At 23 orders per second, the timer table is usually the simplest correct choice.

5Cancelling a Timer When the Order Is Paid

The natural idea is to delete the timer when the PaymentReceived event arrives. That creates a race: the timer may already be firing, on another worker, at the moment the payment lands. With a fixed-delay Kafka topic, deleting is not even possible, because records cannot be removed.

The robust approach is not to cancel at all. When a timer fires, the handler reloads the order and checks its current state. If the order is paid, it does nothing. If it is still unpaid, it cancels it with a conditional update, so the payment and the cancellation cannot both win.

Java
@Transactional
public void onTimer(long orderId) {
int changed = jdbc.update(
"UPDATE orders SET status = 'CANCELLED' WHERE id = ? AND status = 'AWAITING_PAYMENT'", orderId);
// changed == 0: already paid or cancelled, nothing to do
// changed == 1: we cancelled it; publish OrderCancelled through the outbox
}
Step throughWhen the 30-minute timer fires
  1. Timer for order 981 is due
  2. Reload order 981
  3. Status AWAITING_PAYMENT?
    YesConditional update to CANCELLEDNoDo nothing; already paid or cancelled
  4. Publish OrderCancelled via outbox

4 steps in order. Play them, or step through them one at a time.

The same update makes the handler idempotent: if the timer fires twice, the second run changes nothing. The payment side uses the mirror condition, setting PAID only where the status is AWAITING_PAYMENT, so whichever update commits first wins and the other sees zero rows changed. If deleting timers on payment is still useful to keep the table small, it is an optimisation, never what correctness depends on.

6Follow-up questions

How accurate is the firing time with a polling table?

Within the polling interval plus processing time, so about one or two seconds with a one-second poll. That is fine for a 30-minute timeout and a 24-hour reminder. If a timer must fire within milliseconds of its due time, polling is the wrong tool.

What if the timer worker is down for an hour?

Timers are rows, so nothing is lost; when the workers return, they find every overdue row and process it, oldest first. We should alert on the age of the oldest due timer so a stopped worker is noticed quickly.

Why not use a Kafka record timestamp in the future?

A record's timestamp is only metadata. Kafka delivers records in offset order as soon as they are written, whatever their timestamp says, so a future timestamp does not delay delivery.

7Conclusion

In this answer, we sized the problem at about 23 orders per second and saw that the challenge is reliable scheduling, not volume. A timer table written in the order transaction and polled with FOR UPDATE SKIP LOCKED is simple, transactional and inspectable; fixed-delay Kafka topics and a Kafka Streams punctuator keep everything in Kafka; native broker delays and workflow engines also fit within their limits. Most importantly, we do not rely on deleting timers: the handler reloads the order and acts only if it is still unpaid, which makes it idempotent and safe against races with payment.

What to say in the interviewAbout 90 seconds aloud

Open with

First I would size it: two million orders a day is about 23 a second, maybe a couple of hundred at peak, so this is about reliable scheduling, not throughput.

Then, if they want more

My default would be a timer table in the order database: insert order id, action and due time in the same transaction as the order, and have a few workers poll due rows with SELECT FOR UPDATE SKIP LOCKED, so they share the work without blocking each other. It is transactional and easy to inspect.

If the team wants to stay inside Kafka, there are two options: a topic per delay tier, like order-timeouts-30m, where the consumer pauses until record timestamp plus 30 minutes has passed, which works because one delay per topic keeps records in due order; or a Kafka Streams processor that stores timers by due time and fires them from a wall-clock punctuator.

Brokers with native delays, like RabbitMQ's delayed exchange or SQS delivery delay, which is capped at 15 minutes, or a workflow engine's durable timers also fit, but I would check their limits. For cancellation I would not delete timers on payment, because that races with a timer that is already firing.

Instead, the handler reloads the order and cancels it with a conditional update only if it is still awaiting payment. That makes it idempotent and safe whichever event arrives first.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design the delayed actions, compare the options, and explain how a timer is cancelled...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/fbc550a349/