Video in progress
What do the serializer and deserializer do in Kafka?
Kafka only knows byte arrays. The serializer turns a Java object into bytes on the way out, and the deserializer rebuilds the object from bytes on the consumer side. Key and value are configured separately.
📖 Answer
The broker doesn't look inside a message; to it, it's just a byte[]. The producer and the consumer have to agree on the format between themselves.
Ready-made options:
StringSerializer/StringDeserializerLongSerializer,ByteArraySerializer- Spring's
JsonSerializer/JsonDeserializer - Avro or Protobuf (usually with Schema Registry)
spring:
kafka:
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
If the producer writes one format and the consumer expects another, the deserializer throws an exception. That leads to the "poison pill" problem, so on prod it's standard to wrap it in an ErrorHandlingDeserializer.
Sign up to mark studied questions and save your progress.