Is key required as part of sending messages to Kafka?

前端 未结 3 1708
有刺的猬
有刺的猬 2020-12-22 16:39
KeyedMessage keyedMessage = new KeyedMessage(request.getRequestTopicName(), Seri         


        
3条回答
  •  失恋的感觉
    2020-12-22 17:13

    The key with a message is basically sent to get the message ordering for a specific field.

    • If key=null, data is sent round-robin (to a different partition and to a different broker in a distributed env. and of course to the same topic.).
    • If a key is sent, then all messages for that key will always go to the same partition.

    Explain and example

    • key can be any string or integer, etc.. take an example of an integer employee_id as key.
    • So emplyee_id 123 will always go to partition 0, employee_id 345 will always go to partition 1. This is decided by the key hashing algorithm which depends on the number of partitions.
    • if you don't send any key then the message can go to any partition using a round-robin technique.

提交回复
热议问题