Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › Apache Kafka

Subscribe to Dozens of Kafka Topics with One Regex Pattern

By Freedom251 Team · · 3 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Java, call KafkaConsumer.subscribe(Pattern) to subscribe dynamically to every Kafka topic whose name matches a regular expression. For example, ^orders\..+$ matches names such as orders.created and orders.cancelled. Kafka checks for matching topics through periodic metadata refresh, so newly created topics are not necessarily discovered immediately.

Subscribe to matching topics in Java

Compile a Java regular expression and pass it to the consumer’s pattern overload:

As an Amazon Associate I earn from qualifying purchases.

Pattern topics = Pattern.compile("^orders\\..+$");
consumer.subscribe(topics);

The anchors make the intended naming rule explicit: ^ marks the start of the name and $ its end. In this example, the escaped dot means a literal period, and .+ requires at least one character after it. The pattern can match orders.created or orders.cancelled, but not a name with a different prefix.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a narrower expression when only specific names should qualify, such as ^orders\.(created|cancelled)$. A broad expression like .* can include topics beyond the intended family. Kafka documents subscribe(Pattern) as subscribing to all topics matching the pattern and dynamically assigning their partitions. Apache Kafka 4.2.0 KafkaConsumer API.

What happens when topics match

Pattern subscription is dynamic, not an instantaneous watch. Kafka periodically refreshes metadata and checks topic names against the expression. In the documented Pattern API, metadata.max.age.ms influences metadata refresh frequency; a lower maximum age means more frequent checks, with correspondingly more refresh activity. Kafka 4.2.0 KafkaConsumer API and Kafka 4.2.0 ConsumerConfig API.

When a new topic matches, its partitions can be added to the consumer group’s assignment through a rebalance. Rebalances can also follow membership or partition changes. A group distributes each partition to one process in that group; separate consumer groups can each consume the topic independently. Apache Kafka 3.9.2 KafkaConsumer API.

Keep the consumer live and manage reassignment

The consumer joins and maintains group membership through calls to poll(Duration). Rebalance work takes place while polling is active, so a consumer that stops polling cannot rely on the normal poll-driven process to complete group activity. Kafka 4.2.0 KafkaConsumer API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If assignment changes affect in-memory processing state, cleanup, or manual offset commits, register an appropriate rebalance listener and handle partition revocation and assignment there. The listener is the place to coordinate work that depends on which partitions this consumer owns. Kafka 4.2.0 ConsumerRebalanceListener API.

Choose between a regex and an explicit topic list

Consideration Regex subscription Explicit topic list
Topics added later New names that match can be discovered through periodic metadata refresh. The list must be updated to include additional topics.
Unintended matches A broad or poorly anchored expression may include unwanted topics. Only the listed names are subscribed to.
Discovery timing Periodic and metadata-driven, not guaranteed to be immediate. The subscription names are specified directly; this comparison does not establish a performance advantage.
Operations A newly matching topic can change group assignments and trigger a rebalance. Assignment changes can still trigger rebalances when membership or partitions change.

Kafka provides both list-based and pattern-based subscription methods. The choice is about how topic membership should be maintained, not a documented performance ranking. Kafka 4.2.0 KafkaConsumer API.

Kafka 4.2: the newer SubscriptionPattern overload

Kafka 4.2 also documents a SubscriptionPattern overload. It is supported only with the CONSUMER group protocol and requires a pattern compatible with Google RE2/J. An incompatible expression can result in InvalidRegularExpression during a subsequent poll. Check the client version and broker capability before choosing this overload; the familiar subscribe(Pattern) API remains separately documented. Kafka 4.2.0 KafkaConsumer API.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Checklist if matching topics are missing or unexpected

  • Verify the name: compare the full topic name with the expression, including capitalization, punctuation, anchors, and escaped characters.
  • Allow for metadata refresh: matching is periodic. For the documented Pattern API, review metadata.max.age.ms if discovery cadence matters.
  • Keep polling: the consumer’s group activity and rebalances are poll-driven.
  • Check access: ensure the consumer has read authorization for topics that may match in the future, as well as access to its configured group ID.
  • Check subscription state: mixing subscribe() with manual assign(), or changing subscription modes without first unsubscribing, can produce a state error.
  • For Kafka 4.2 SubscriptionPattern: verify CONSUMER protocol support and RE2/J compatibility if subscription setup or a later poll reports an error.

Kafka documents authorization failures and invalid subscription-state errors in its consumer API. Kafka 4.2.0 KafkaConsumer API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Apache Kafkadistributed systemsJavaKafka Consumerregular expressions

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.