2

In plain old Java, if I implement a class with Serializable interface, Java takes care of serializing an instance of the class and vice versa. Why do we need to supply a separate Serializer and Deserializer class if we use Kafka? Why can't Kafka use the same mechanism as Java Runtime Engine for serializing and deserializing an instance if the class implements Serializable? Can you please explain this?

5
  • 7
    because Java serialization is bad Commented Aug 11, 2019 at 19:17
  • For refernce to @Michael's comment: Oracle plans to dump risky Java serialization Commented Aug 11, 2019 at 19:20
  • Java serialisation is slow, bloatedand insecure. It lacks backwards and forwards compatibility features, and has minimal versioning support. It's implementation as reflection on named private methods is questionable at best. Why would anyone building a highly scalable high throughput messaging system use such a system?? Commented Aug 11, 2019 at 19:22
  • 5
    Keep in mind that Kakfa is not limited to java. It can be used with any other programming language, which will not understand java's serialization. However, you can use java's serialization for your Serializer and Deserializer if you want. Commented Aug 11, 2019 at 19:22
  • In addition to the above: I usually send JSON over Kafka, which is not supported by Java serialization. Commented Aug 12, 2019 at 13:42

1 Answer 1

3

To answer your questions, Kafka utilizes a separate Serializer and Deserializer class so that applications can choose which format of serialization to use. It is a pluggable method of adding flexibility.

Kafka does "come with" two default serializers, a byte and string deserializer, as you note neither of these utilizes Java serialization. As other comments have mentioned java serialization is often seen when the writer and reader applications are tightly coupled, use the same versions of java etc. it's issues become more apparent in environments where these assumptions can't be made. Environments like those Kafka is used in. There is also the matter of Kafka supporting other programming languages in its API's.

If this was a hard requirement you could use the default byte deserializer to read the messages from java as bytes, and post process these. The messages you'd write would be a byte string corresponding to your java serialized object. This basically uses Kafka as just a byte pass through.

Sign up to request clarification or add additional context in comments.

1 Comment

Kafka comes with more than byte and string. All the primitives and java.util.UUID have serializers as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.