--- url: "http://www.enterpriseintegrationpatterns.com/CompetingConsumers.html" titulek: "Competing Consumers - Enterprise Integration Patterns" h1: "" popis: "How can a messaging client process multiple messages concurrently?" jazyk: "en" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-15 15:34:40" znaku: 6852 slov: 682 kodovani: "utf-8" sha256: "4e7aeddaaaf41076903f75e647af7990dcdb940fb5c13c1da0793261f33f003d" --- | | Competing Consumers[Messaging Patterns](index.html) » [Messaging Endpoints](MessagingEndpointsIntro.html) | [Contents](toc.html)[⇐ Prev](EventDrivenConsumer.html)[Next ⇒](MessageDispatcher.html) | | --- | --- | --- | An application is using [*Messaging*](Messaging.html). However, it cannot process messages as fast as they’re being added to the channel. How can a messaging client process multiple messages concurrently? Create multiple *Competing Consumers* on a single channel so that the consumers can process multiple messages concurrently. *Competing Consumers* are multiple consumers that are all created to receive messages from a single [*Point-to-Point Channel*](PointToPointChannel.html). When the channel delivers a message, any of the consumers could potentially receive it. The messaging system's implementation determines which consumer actually receives the message, but in effect the consumers compete with each other to be the receiver. Once a consumer receives a message, it can delegate to the rest of its application to help process the message. (This solution only works with [*Point-to-Point Channel*](PointToPointChannel.html)s; multiple consumers on a [*Publish-Subscribe Channel*](PublishSubscribeChannel.html) just create more copies of each message.) *Competing Consumers Sequence* ... Read the entire pattern in the book [Enterprise Integration Patterns](/books1.html) ## Example: Apache Kafka[NEW](/ramblings/eip1_examples_updated.html) [Apache Kafka](http://kafka.apache.org) is a distributed streaming platform that allows clients to consume messages in both [*Publish-Subscribe Channel*](PublishSubscribeChannel.html) and *Competing Consumers* semantics. Kafka is optimized for high throughout and horizontal scalability and therefore tries to avoid the overhead that can be inherent in coordinating across multiple *Competing Consumers*. So while the client interface allows multiple consumers to consume messages off a topic in a competing fashion, guaranteeing that each message is only consumed by one of the consumers, the implementation actually relies on multiple *partitions* implemented as [*Point-to-Point Channel*](PointToPointChannel.html)s: In the example above, a Kafka topic consists of three partitions divided across two consumers. A [*Message Router*](MessageRouter.html) distributes the messages across the three [*Point-to-Point Channel*](PointToPointChannel.html)s. This router is generally a [*Content-Based Router*](ContentBasedRouter.html) that works off the message key. This allows the sender to control the distribution of messages, e.g. to assure related messages go onto the same partition. This can be necessary to preserve order because in-order delivery is not guaranteed across partitions. This design implies that the decision which consumer receives the message is made *a priori*, meaning consumers don't actually compete but rather read from independent channels. This limits the ability to load balance between consumers, e.g. if one channel runs empty, the corresponding consumer won't be able to aid other consumers whose channels still have messages. Also, as the number of partitions is set independently, not all consumers will service the same number of partitions, as illustrated in the example above. However, as Kafka partitions are relatively inexpensive, there are usually lots of them, meaning that each consumer is in reality likely to read of many partitions and therefore channels. This highlights another trade-off: there can't be more competing consumers in a *consumer group* than there are partitions on that topic. **Related patterns:** [*Content-Based Router*](ContentBasedRouter.html), [*Event-Driven Consumer*](EventDrivenConsumer.html), [*Message*](Message.html), [*Message Channel*](MessageChannel.html), [*Message Dispatcher*](MessageDispatcher.html), [*Message Router*](MessageRouter.html), [*Messaging*](Messaging.html), [*Point-to-Point Channel*](PointToPointChannel.html), [*Polling Consumer*](PollingConsumer.html), [*Publish-Subscribe Channel*](PublishSubscribeChannel.html), [*Transactional Client*](TransactionalClient.html) --- ## Integration Pattern Language [Message Construct.](MessageConstructionIntro.html) [Message](Message.html) [Command Message](CommandMessage.html) [Document Message](DocumentMessage.html) [Event Message](EventMessage.html) [Request-Reply](RequestReply.html) [Return Address](ReturnAddress.html) [Correlation Identifier](CorrelationIdentifier.html) [Message Sequence](MessageSequence.html) [Message Expiration](MessageExpiration.html) [Format Indicator](FormatIndicator.html) [Message Routing](MessageRoutingIntro.html) [Pipes-and-Filters](PipesAndFilters.html) [Message Router](MessageRouter.html) [Content-based Router](ContentBasedRouter.html) [Message Filter](Filter.html) [Dynamic Router](DynamicRouter.html) [Recipient List](RecipientList.html) [Splitter](Sequencer.html) [Aggregator](Aggregator.html) [Resequencer](Resequencer.html) [Composed Msg. Processor](DistributionAggregate.html) [Scatter-Gather](BroadcastAggregate.html) [Routing Slip](RoutingTable.html) [Process Manager](ProcessManager.html) [Message Broker](MessageBroker.html) [Message Transformation](MessageTransformationIntro.html) [Message Translator](MessageTranslator.html) [Envelope Wrapper](EnvelopeWrapper.html) [Content Enricher](DataEnricher.html) [Content Filter](ContentFilter.html) [Claim Check](StoreInLibrary.html) [Normalizer](Normalizer.html) [Canonical Data Model](CanonicalDataModel.html) [Messaging Endpoints](MessagingEndpointsIntro.html) [Message Endpoint](MessageEndpoint.html) [Messaging Gateway](MessagingGateway.html) [Messaging Mapper](MessagingMapper.html) [Transactional Client](TransactionalClient.html) [Polling Consumer](PollingConsumer.html) [Event-driven Consumer](EventDrivenConsumer.html) [Competing Consumers](CompetingConsumers.html) [Message Dispatcher](MessageDispatcher.html) [Selective Consumer](MessageSelector.html) [Durable Subscriber](DurableSubscription.html) [Idempotent Receiver](IdempotentReceiver.html) [Service Activator](MessagingAdapter.html) [Messaging Channels](MessagingChannelsIntro.html) [Message Channel](MessageChannel.html) [Point-to-Point Channel](PointToPointChannel.html) [Publish-Subscr. Channel](PublishSubscribeChannel.html) [Datatype Channel](DatatypeChannel.html) [Invalid Message Channel](InvalidMessageChannel.html) [Dead Letter Channel](DeadLetterChannel.html) [Guaranteed Delivery](GuaranteedMessaging.html) [Channel Adapter](ChannelAdapter.html) [Messaging Bridge](MessagingBridge.html) [Message Bus](MessageBus.html) [Systems Mgmt.](SystemManagementIntro.html) [Control Bus](ControlBus.html) [Detour](Detour.html) [Wire Tap](WireTap.html) [Message History](MessageHistory.html) [Message Store](MessageStore.html) [Smart Proxy](SmartProxy.html) [Test Message](TestMessage.html) [Channel Purger](ChannelPurger.html)