When multiple services need to consume the same Kafka messages with different processing logic, Kafka's topic and consumer group model lets each service process the full stream of messages independently.
Key Points: • Publishing messages to a single topic makes them available to every consumer that subscribes, regardless of how many different services need them. • Each service subscribes as its own consumer group, and Kafka guarantees every consumer group receives a full copy of every message on the topic. • Within a single consumer group, partitions are divided among the group's instances, which is how a service scales its own processing horizontally without duplicate work inside that group. • Each service applies its own business logic to the messages it reads, so one service's processing failure doesn't affect another service's independent consumer group. • Consumer offsets are tracked per group, so each service progresses through the topic at its own pace without blocking the others.
Example: An OrderPlaced topic might be consumed by a Billing service's consumer group, which charges the customer, and separately by an Analytics service's consumer group, which updates dashboards; both read every message independently and can fall behind or catch up without affecting each other.
Code Example:
@KafkaListener(topics = "order-placed", groupId = "billing-service")
public void handleForBilling(OrderPlacedEvent event) {
billingService.chargeCustomer(event);
}
@KafkaListener(topics = "order-placed", groupId = "analytics-service")
public void handleForAnalytics(OrderPlacedEvent event) {
analyticsService.record(event);
}Interview Tip: A concise interview answer is:
"I'd publish once to a shared topic and let each service consume it under its own consumer group, since Kafka guarantees every consumer group gets a full copy of the stream. Each service then applies its own logic independently, scales its own consumption by adding instances within its group, and tracks its own offsets without interfering with the others."