title: Spring Kafka Partitioning
body: <p>What is the difference in the behavior of the below two code snippets to publish a message?</p>
  
  <p><strong>Approach 1</strong></p>
  
  <pre><code>Message&lt;String&gt; message = MessageBuilder.withPayload("testmsg")
          .setHeader(KafkaHeaders.MESSAGE_KEY, "key").setHeader(KafkaHeaders.TOPIC, "test").build();
  
  ListenableFuture&lt;SendResult&lt;String, String&gt;&gt; future = kafkaTemplate.send(message);
  </code></pre>
  
  <p><strong>Approach 2</strong></p>
  
  <pre><code>ListenableFuture&lt;SendResult&lt;String, String&gt;&gt; future = kafkaTemplate.send("test", "testmsg");
  </code></pre>
  
  <p><strong>Topic Config:</strong></p>
  
  <pre><code>$ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
  Topic:test   PartitionCount:3    ReplicationFactor:1 Configs:
  Topic: test  Partition: 0    Leader: 0   Replicas: 0 Isr: 0
  Topic: test  Partition: 1    Leader: 0   Replicas: 0 Isr: 0
  Topic: test  Partition: 2    Leader: 0   Replicas: 0 Isr: 0
  </code></pre>
  
  <p><strong>Observation:</strong></p>
  
  <p>If there are 3 consumers, one per partition; Approach 1 leads to all messages consumed by a single consumer from a single partition. With Approach 2; consumption is equally split between the 3 partitions/consumers.</p>