Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Show how a Spring Boot application declares a topic named "orders" with 12 partitions and replication factor 3, and explain what KafkaAdmin does at startup. What happens if you later change the bean to 6 partitions?

We declare the topic as a NewTopic bean built with TopicBuilder, for example TopicBuilder.name("orders").partitions(12).replicas(3).build(), and Spring Boot's auto-configured KafkaAdmin uses the Kafka Admin client at startup to create every declared topic that does not exist yet. If we later change the bean to 6 partitions, nothing shrinks: Kafka cannot reduce a topic's partition count, so KafkaAdmin only reports the difference. Increasing partitions is possible, but it changes which partition each key goes to.

Why this question is asked

Topics are the tables of a Kafka system: once data and consumers depend on them, their shape is hard to change. Spring Kafka makes it very easy to declare a topic in code, which is great for development and small services, and interviewers ask about it to check that we also know the limits: what KafkaAdmin does and does not do, why partitions only grow, and why production clusters often refuse to let applications create topics at all. In this answer, we will recap partitions and replicas, declare the topic in code, follow KafkaAdmin at startup, see what happens when the partition count changes, and finish with how production setups usually handle this.

1A Quick Recap of Partitions and Replicas

A Kafka topic is split into partitions, ordered logs that are the unit of parallelism: a consumer group can have at most one active consumer per partition, so 12 partitions allow up to 12 consumers in a group. Records with the same key always go to the same partition, which is what gives per-key ordering.

The replication factor says how many copies of each partition exist on different brokers. With replication factor 3, one copy is the leader that serves reads and writes, and two followers keep copies, so the topic survives the loss of a broker. A replication factor of 3 needs at least 3 brokers in the cluster.

2Declaring the Topic as a Bean

Spring Kafka offers TopicBuilder, a fluent helper that produces an Admin client NewTopic object. We declare it as a bean in any configuration class.

Java
@Configuration
class KafkaTopicsConfig {
@Bean
NewTopic orders() {
return TopicBuilder.name("orders")
.partitions(12)
.replicas(3)
.config(TopicConfig.RETENTION_MS_CONFIG, "604800000") // 7 days
.config(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "2")
.build();
}
}

Notice the two configs. The Kafka class org.apache.kafka.common.config.TopicConfig holds constants for topic-level settings, so we avoid typing "retention.ms" by hand. Setting retention explicitly means the topic does not silently inherit whatever the broker default happens to be, and min.insync.replicas of 2 together with acks=all on the producer means a write is only acknowledged once two copies have it.

3What KafkaAdmin Does at Startup

We did not create a KafkaAdmin ourselves. Spring Boot auto-configures one from the spring.kafka properties, using the same bootstrap servers as the producers and consumers. When the application context starts, KafkaAdmin collects every NewTopic bean, asks the cluster which of those topics already exist, and creates the missing ones in one batch through the Admin client.

Step throughKafkaAdmin at startup
  1. Collect NewTopic beansorders: 12 partitions, RF 3
  2. Describe topics on the clusterAdmin client call
  3. Topic exists?
    YesCompare partition countNoCreate it with all settings
  4. Bean asks for more partitions?
    YesAdd partitionsNoLeave it, log any difference

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

For topics that already exist, KafkaAdmin compares the partition count. If the bean asks for more partitions than the topic has, it adds partitions; if the bean asks for fewer, it cannot do anything and only logs the difference. Newer versions can also update topic configs such as retention when modifyTopicConfigs is enabled; check your version for the details. By default, a cluster that cannot be reached at startup is logged, not fatal; setting fatalIfBrokerNotAvailable makes it stop the application instead.

4Changing the Bean to 6 Partitions

Suppose the topic was created with 12 partitions and someone later edits the bean to 6. On the next startup KafkaAdmin sees 12 existing partitions, 6 requested, and cannot reduce them, because Kafka has no operation that removes partitions. Removing one would mean deleting a log full of records and moving its keys elsewhere. The topic keeps 12 partitions, and the only trace is a log message, which is easy to miss. The code now lies about the real topic, so it is worth keeping the bean and the cluster in step.

The reverse change, from 12 to 24, does work, and that is the more dangerous one. The default partitioner chooses a partition from a hash of the key modulo the partition count, so changing the count moves most keys to new partitions.

Java
// Simplified: what the default partitioner does for a keyed record
int partition(String key, int partitions) {
return Utils.toPositive(Utils.murmur2(key.getBytes(UTF_8))) % partitions;
}
partition("customer-17", 12); // say 5
partition("customer-17", 24); // 5 or 17: depends on the hash

If customer-17 moves from partition 5 to 17, new events for it are in partition 17 while older ones may still be unread in partition 5, and a consumer of partition 17 can process the new event first. Per-key ordering is broken for every key that moved, so increasing partitions on a keyed topic needs a plan, not a bean edit.

5How Production Clusters Usually Handle Topics

In production, brokers usually run with auto.create.topics.enable=false, so a typo in a topic name does not create a stray topic with default settings. Many organisations go further and give applications no permission to create topics: the platform team, or a topic definition in Git applied by Terraform or an operator, owns them. Then NewTopic beans are useful in development and tests, and in production KafkaAdmin either finds the topic already there or fails for lack of rights, which is why spring.kafka.admin.auto-create can be turned off per profile.

6Follow-up questions

Which topic configs would you always set explicitly?

I would always set the partition count and replication factor, min.insync.replicas (usually 2 with replication factor 3), retention.ms or retention.bytes, and cleanup.policy, which is delete for event streams and compact for topics that hold the latest value per key. Depending on the data I would also set max.message.bytes if records can be large, and sometimes compression.type. The point is that broker defaults differ between clusters, and a topic should not change behaviour because it was created on a different cluster.

Can KafkaAdmin create topics with different settings per environment?

Yes. The NewTopic bean is ordinary code, so it can read values from properties, for example 1 partition and replication factor 1 in a local profile and 12 and 3 in staging. In production the topic is usually created outside the application, and the bean is disabled or only verifies that the topic exists.

7Conclusion

In this answer, we declared the orders topic as a NewTopic bean with TopicBuilder, with 12 partitions, replication factor 3 and explicit retention and min.insync.replicas. Spring Boot's KafkaAdmin creates missing topics at startup through the Admin client and can add partitions to existing ones, but Kafka never reduces partitions, so changing the bean to 6 only produces a log message. Increasing partitions remaps keys and breaks per-key ordering, which is one reason production clusters disable auto topic creation and often keep topic creation out of applications.

What to say in the interviewAbout 80 seconds aloud

Open with

I would declare a bean of type NewTopic using Spring Kafka's TopicBuilder: name orders, partitions 12, replicas 3, and I would set configs explicitly, for example retention of seven days and min.insync.replicas of 2, so the topic does not depend on broker defaults.

Then, if they want more

Spring Boot auto-configures a KafkaAdmin from the spring.kafka properties. At startup it collects all NewTopic beans, describes the existing topics with the Admin client and creates the ones that are missing.

For an existing topic, if the bean asks for more partitions, it can add them, but Kafka has no way to reduce the number of partitions, so if someone changes the bean to 6, the topic stays at 12 and KafkaAdmin just logs the mismatch. That means the code no longer describes the real topic, which I would fix.

Going up is possible but risky: the default partitioner uses a hash of the key modulo the partition count, so most keys move to a different partition and per-key ordering breaks during the transition.

In production I would expect auto.create.topics.enable to be off and often applications have no create rights at all; topics are managed by the platform, and NewTopic beans are mainly for development and tests.

About this answer

Last updated
12 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.
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 "Show how a Spring Boot application declares a topic named "orders" with 12 partitions...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/36ea1a5301/