Scrivio Interview library
CodingIntermediate7 min read · updated 13 November 2026

A pricing service keeps an in-memory map of 3 million prices loaded from the compacted topic prices (24 partitions) and must not serve traffic until the map is complete.

Sketch the Java bootstrap with the Kafka consumer API, explain how it knows it has caught up, and how it tells Kubernetes it is ready.

Use a consumer without a group: assign() all 24 partitions, seekToBeginning(), and capture endOffsets() for the same partitions at the start as the target. Poll in a loop, putting each value into the map and removing the key for a tombstone, until position() is at or past the target for every partition; with transactional producers, read with isolation.level=read_committed so the target is the last stable offset. Keep Spring Boot's readiness at REFUSING_TRAFFIC until the load finishes, then publish ReadinessState.ACCEPTING_TRAFFIC, which the probe on /actuator/health/readiness follows, and keep the same consumer polling afterwards for live updates.

Why this question is asked

Loading a cache from a compacted topic at startup is a common pattern for services that need fast lookups, such as prices, feature flags or reference data. The two tricky questions are how the service knows it has read everything, and how it keeps Kubernetes from sending traffic to a half-loaded instance. Interviewers ask this to check whether we know the low-level consumer API, the subtleties of end offsets and transactions, and how Spring Boot's availability states connect to Kubernetes probes. In this answer, we will recap why we use assign() instead of a group, write the bootstrap, explain the caught-up check, connect readiness to Kubernetes, and look at what happens after the load.

1Why a Consumer Without a Group

A consumer group shares partitions between instances: with three pricing pods in one group, each pod would get 8 of the 24 partitions and load only a third of the prices. Every pod needs all 3 million prices, so a group is the wrong tool. A group would also commit offsets, and we do not want any: every start must read from the beginning to rebuild the map.

So we use manual assignment. consumer.assign(partitions) gives this consumer exactly the partitions we list, with no group coordinator, no rebalancing and no committed offsets. We leave group.id unset and enable.auto.commit false.

2The Bootstrap Code

The loader runs on its own thread, so the same consumer can carry on with live updates after the initial load. An ApplicationRunner waits for the load to finish, which keeps the application's startup, and therefore its readiness, on hold until the map is complete.

Java
@Component
class PriceLoader implements ApplicationRunner {
private final Map<String, BigDecimal> prices = new ConcurrentHashMap<>(4_000_000);
private final CountDownLatch loaded = new CountDownLatch(1);
private final ApplicationContext context;
private volatile boolean running = true;
PriceLoader(ApplicationContext context) { this.context = context; }
@Override
public void run(ApplicationArguments args) throws InterruptedException {
Thread.ofPlatform().name("price-loader").start(this::consume);
loaded.await(); // readiness is still REFUSING_TRAFFIC here
AvailabilityChangeEvent.publish(context, ReadinessState.ACCEPTING_TRAFFIC);
}
private void consume() {
Properties p = new Properties();
p.put("bootstrap.servers", "kafka:9092");
p.put("key.deserializer", StringDeserializer.class.getName());
p.put("value.deserializer", StringDeserializer.class.getName());
p.put("enable.auto.commit", "false"); // no group.id: nothing is committed
p.put("isolation.level", "read_committed");
p.put("max.poll.records", "5000");
try (var consumer = new KafkaConsumer<String, String>(p)) {
List<TopicPartition> parts = consumer.partitionsFor("prices").stream()
.map(i -> new TopicPartition(i.topic(), i.partition())).toList(); // 24 partitions
consumer.assign(parts);
consumer.seekToBeginning(parts);
Map<TopicPartition, Long> target = consumer.endOffsets(parts); // captured once, at the start
while (!caughtUp(consumer, target)) {
apply(consumer.poll(Duration.ofMillis(500)));
}
loaded.countDown(); // 3,000,000 prices in the map
while (running) {
apply(consumer.poll(Duration.ofSeconds(1))); // live updates from now on
}
}
}
private boolean caughtUp(KafkaConsumer<String, String> c, Map<TopicPartition, Long> target) {
return target.entrySet().stream().allMatch(e -> c.position(e.getKey()) >= e.getValue());
}
private void apply(ConsumerRecords<String, String> records) {
for (var r : records) {
if (r.value() == null) prices.remove(r.key()); // tombstone: product deleted
else prices.put(r.key(), new BigDecimal(r.value()));
}
}
}

In real code, the loader thread should also catch exceptions and release the runner with a failure, so a broken load fails the startup instead of hanging forever, and a @PreDestroy method should set running to false and call consumer.wakeup().

3How the Loader Knows It Has Caught Up

endOffsets() returns, for each partition, the offset that the next record written will get. If partition 5 has records at offsets 0 to 124,999, its end offset is 125,000. We capture these numbers once, at the start, and treat them as the finish line. Records written after that moment are live updates; we will apply them too, but we do not wait for them, otherwise a busy topic would keep moving the finish line.

The check uses position(), the offset of the next record this consumer will read, rather than the offset of the last record we saw. The reason is that a partition's last offsets may not be records at all. Compaction leaves gaps, and transactional producers write commit markers, control records the consumer never hands to us. If the last offset before 125,000 is a commit marker, we will never see a record with offset 124,999, but position() will still move to 125,000 once the consumer has passed it. An empty poll is not a safe signal either; it can happen while data remains, for example during a slow fetch.

If the prices topic is written by transactional producers, we read with isolation.level=read_committed. Then endOffsets() returns the last stable offset, the point before the first still-open transaction, so the target never includes records that may yet be aborted, and the loader does not wait for a transaction that may stay open for a while.

Step throughThe bootstrap loop
  1. assign() all 24 partitions
  2. seekToBeginning()
  3. Capture endOffsets() as target
  4. poll() and apply recordsnull value removes the key
  5. position >= target on every partition?
    YesMap complete: signal readyNoPoll again
  6. Keep polling for live updates

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

4Telling Kubernetes the Service Is Ready

Spring Boot tracks two availability states. Liveness says whether the application is working at all; readiness says whether it should receive traffic. During startup, readiness is REFUSING_TRAFFIC, and Spring Boot publishes ACCEPTING_TRAFFIC only after the application has started and every ApplicationRunner and CommandLineRunner has returned. Our runner waits on the latch, so readiness stays refused until the map is complete. Publishing AvailabilityChangeEvent with ReadinessState.ACCEPTING_TRAFFIC ourselves makes that moment explicit.

With the actuator on the classpath and the health probes enabled (they are switched on automatically on Kubernetes, or with management.endpoint.health.probes.enabled=true), the readiness state is exposed at /actuator/health/readiness, and the pod's readinessProbe points there. Kubernetes adds the pod to the Service's endpoints only when that returns 200. Loading 3 million prices may take tens of seconds, so the startupProbe or the liveness probe's initial delay must allow for it, or Kubernetes will restart the pod mid-load.

If we preferred to load in the background without holding the runner, Boot would mark the application ready as soon as the runners return, so we would need a custom HealthIndicator in the readiness group that stays DOWN until the latch is released. Check your Spring Boot version's documentation for the probe group settings.

5After the Bootstrap

Once ready, the same consumer keeps polling and applies live price changes, so the map stays current with a delay of milliseconds. Readers use the ConcurrentHashMap without locks.

For faster restarts, a pod can keep a local snapshot, for example a RocksDB store on a persistent volume, together with the offsets it reflects. On restart, it loads the snapshot and seeks to those offsets instead of the beginning, reading only the changes since. Kafka Streams does exactly this with its state stores and checkpoint files, which is worth considering if the pattern spreads to more services.

6Follow-up questions

Why not use a @KafkaListener for this?

A listener container is built around consumer groups and continuous consumption, and it does not tell us when we have caught up to a target. It can be configured with explicit partition assignment and an initial offset, but the caught-up check and readiness gating are easier to see and control with the plain consumer API.

What if the topic is written by transactional producers but we use read_uncommitted?

endOffsets() then returns the high watermark, which can include records from open transactions, and the map may include prices from transactions that later abort. With read_committed, both the target and the records we apply reflect committed data only.

How much memory do 3 million prices need?

With String keys, BigDecimal values and ConcurrentHashMap overhead, roughly 100 to 200 bytes per entry, so about 300 to 600 MB. Measuring with a heap histogram is better than guessing, and compact types such as a long key and a long amount in cents cut it considerably.

7Conclusion

In this answer, we bootstrapped a 3-million-entry price map from a compacted topic with a group-less consumer: assign() all partitions, seekToBeginning(), capture endOffsets() as the target, and poll until position() reaches the target on every partition, removing keys on tombstones. We used read_committed so the target is the last stable offset, kept Spring Boot's readiness at REFUSING_TRAFFIC by holding an ApplicationRunner until the load finished, then published ACCEPTING_TRAFFIC for the probe on /actuator/health/readiness. After that, the same consumer applies live updates, and a local snapshot with offsets can make restarts faster.

What to say in the interviewAbout 90 seconds aloud

Open with

Every pod needs all 3 million prices, so I would not use a consumer group, which would split the partitions. I would create a consumer with no group id and auto commit off, call assign with all 24 partitions of prices, seekToBeginning, and then take endOffsets for the same partitions once, at the start, as my finish line.

Then, if they want more

Then I poll in a loop: for each record I put the value in the map, or remove the key if the value is null, because that is a tombstone. I stop the bootstrap when position is at or past the captured end offset for every partition.

I use position rather than the last record's offset because compaction gaps and transaction markers mean the last offsets may never be delivered as records, and an empty poll is not proof of being caught up.

If producers are transactional, I read with isolation level read_committed, so endOffsets returns the last stable offset and I never wait for or include open transactions. For Kubernetes, the load runs while an ApplicationRunner waits, so Spring Boot keeps readiness at REFUSING_TRAFFIC; when it finishes, I publish AvailabilityChangeEvent with ACCEPTING_TRAFFIC, and the readiness probe on /actuator/health/readiness starts returning 200.

After that the same consumer keeps polling for live updates, and for faster restarts I could keep a local RocksDB snapshot with its offsets.

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 "Sketch the Java bootstrap with the Kafka consumer API, explain how it knows it has...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/945e901cef/