Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A Kafka Streams app counts mobile app events in 1-minute tumbling windows defined with TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(1)), using a TimestampExtractor that reads the event time from the payload.

Counts are 3 to 8 percent lower than a nightly batch job over the same data, and the metric dropped-records-total keeps rising. Phones buffer events offline and send them minutes to hours late. Explain the cause and your options.

With no grace, a window stops accepting records as soon as stream time, the highest timestamp the task has seen, passes the window end. Phones that send events late deliver them after newer events have already moved stream time forward, so they are dropped and counted in dropped-records-total. We set a grace period from the measured lateness, accept that it costs state and delays final results, send events older than the grace to a separate topic for batch correction, and clamp timestamps from clocks set far in the future.

Why this question is asked

Counts that are a few percent too low with no errors anywhere are the kind of bug that erodes trust in a streaming system. The metric dropped-records-total is the clue, and the mobile clients are the reason. Interviewers ask this to see whether we understand event time, stream time and grace, and whether we can make a sensible trade-off rather than just raising a number. In this answer, we will see why records are dropped, measure lateness, set a grace period and understand its cost, handle very late events, and protect stream time from bad clocks.

1Why Late Events Are Dropped

The TimestampExtractor gives each event its time from the payload, the moment the phone recorded it. Each task tracks stream time as the highest timestamp it has seen. A 1-minute window from 10:00 to 10:01 accepts records until stream time passes window end plus grace. With ofSizeWithNoGrace, grace is zero, so as soon as any record with a timestamp after 10:01 is processed, the 10:00 window is closed for good.

Now a phone that was offline from 09:58 to 10:20 reconnects and sends its buffered events. By then stream time on that task is around 10:20, set by other phones, so all its events from 09:58 to 10:20 belong to closed windows. Streams drops them, increments dropped-records-total, and the counts come out 3 to 8 percent low compared with the nightly batch job, which sees every event.

Step throughA buffered event arriving after its window closed
Online phonesStreams taskOffline phone
events at 10:00:30, 10:01:10
stream time 10:01:10: window 10:00 closes (no grace)
events up to 10:20
reconnects, sends event from 10:00:45
window 10:00 already closed: drop, dropped-records +1

5 messages in order. Play them, or step through them one at a time.

2Measuring Lateness Before Choosing a Grace

Lateness is how far behind stream time an event is when it arrives. We can measure it in the extractor, whose partitionTime argument is the highest timestamp seen so far on that partition, by subtracting each event's timestamp from it and exporting the result as a histogram. For mobile apps the distribution usually has a big body within a few seconds and a long tail of minutes and hours from phones that were offline.

Suppose the data shows that 97 percent of events are less than 10 seconds late, 99.5 percent less than 10 minutes and the rest up to several hours. A 10-minute grace then recovers almost all the missing counts.

3Setting the Grace Period and Its Cost

With the measurement in hand, we keep windows open for 10 minutes of stream time after they end.

Java
events.groupByKey()
.windowedBy(TimeWindows.ofSizeAndGrace(Duration.ofMinutes(1), Duration.ofMinutes(10)))
.count(Materialized.<String, Long, WindowStore<Bytes, byte[]>>as("events-per-minute")
.withRetention(Duration.ofMinutes(60))); // must be >= size + grace (11 minutes)
// event from 10:00:45 arriving when stream time is 10:08 -> counted
// event from 10:00:45 arriving when stream time is 10:12 -> dropped (10:01 + 10 min passed)
Grace period trade-offs (1-minute windows)
GraceEvents droppedOpen windows per keyFinal result delay
03 to 8 percent1About 1 minute
10 minutesAbout 0.5 percent11About 11 minutes
6 hoursNear zero361Over 6 hours
Example figures from a measured lateness distribution; use your own data.

Grace is not free. Each key keeps 11 open windows instead of 1, so the window store is larger, and the store's retention must be at least size plus grace or Streams rejects the configuration. If results are emitted only when windows close, for example with suppress(untilWindowCloses), every final result now waits 11 minutes instead of 1. Without suppression, each late event produces an updated count for its old window, so downstream must handle updates to past minutes.

4Events That Are Hours Late

Holding windows open for hours to catch the last half percent costs a lot of state and delays everything, so we do not. Instead, before the windowed aggregation, a small processor step compares each event's timestamp with the current stream time (context.currentStreamTimeMs() in the Processor API) and sends events older than the grace to a separate topic, late-events. A batch or reconciliation job then corrects the stored aggregates for those minutes, usually once an hour or nightly. If the business is happy with a known gap, we can also accept it and report the late share openly, which is honest and cheap.

Either way, the dropped events are no longer invisible, which is the real problem today.

5Protecting Stream Time From Bad Clocks

Phone clocks are not reliable. A device whose clock is set a year ahead sends an event that pushes stream time a year forward for its whole task, and every other event on that partition becomes late and is dropped. The extractor should compare the payload time with the record's own timestamp or the current wall-clock time and reject or clamp events more than a few minutes in the future.

Java
public long extract(ConsumerRecord<Object, Object> rec, long partitionTime) {
long eventTime = ((AppEvent) rec.value()).occurredAt().toEpochMilli();
long limit = System.currentTimeMillis() + Duration.ofMinutes(5).toMillis();
return eventTime > limit ? rec.timestamp() : eventTime; // clamp clocks far in the future
}

6Follow-up questions

Which metric and threshold would you alert on?

The task-level dropped-records-rate (and the total) compared with the input rate. After setting the grace, we expect the dropped share to stay around the measured tail, say 0.5 percent, so we alert when it exceeds 1 percent for 15 minutes. A sudden jump usually means a bad clock pushed stream time forward or a client release changed buffering, both worth a look.

Is the nightly batch job then the source of truth?

For exact historical counts, yes, because it sees every event regardless of lateness. The streaming counts serve the live view. A reconciliation that compares the two per minute catches both late-event gaps and bugs.

Could we use the record timestamp instead of the payload time to avoid the problem?

Then no events would be late, but each event would be counted in the minute it arrived rather than the minute it happened, which changes the meaning of the metric. Usually the business wants event time.

7Conclusion

In this answer, we saw that with no grace a window closes as soon as stream time passes its end, so buffered mobile events arrive after their windows have closed and are dropped, which explains the low counts and the rising dropped-records-total. We measured lateness, set a 10-minute grace with enough store retention and accepted its cost in state and delay, sent events older than the grace to a separate topic for batch correction, and clamped timestamps from clocks far in the future.

What to say in the interviewAbout 90 seconds aloud

Open with

Streams tracks stream time per task as the highest record timestamp it has seen, and a window accepts records only until stream time passes the window end plus grace.

Then, if they want more

With ofSizeWithNoGrace the grace is zero, so once other phones' newer events move stream time past a minute, any event for that minute arriving later is dropped and counted in dropped-records. Phones that were offline send exactly those events, which explains the three to eight percent gap with the batch job.

Out-of-order data is normal here, so I would measure the lateness distribution and set the grace from it, for example ofSizeAndGrace with one minute and ten minutes, which keeps windows open for ten minutes of stream time.

That costs something: the window store must retain at least size plus grace, set with Materialized.withRetention, it holds more windows, and if I emit only on window close, results wait longer.

Events hours late cannot all be held, so I would route events older than the grace to a separate topic and correct the aggregates in a reconciliation job, or accept and report the gap.

And I would clamp timestamps far in the future in the extractor, because one device with a bad clock can advance stream time and close windows for everyone on that partition.

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 "Counts are 3 to 8 percent lower than a nightly batch job over the same data, and the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/3cbd3a34ad/