I am doing kafka producers and consumers. there are three ways to do Serialization and de-serialization.
1, custom object -> byte[] -> object (example)
2, custom object -> String -> object (I tried. working)
3, custom object -> JsonNode -> object (example)
Which one is better? Thanks
You can try benchmarking but I would imagine all three are about the same.
- Object -> byte[]: In this case three things are happening.
- Object is converted into a tree of Json objects.
- Tree is converted to a string.
- String is converted to bytes.
- Object -> String: In this case the same three things are happening.
- I assume you are using the object mapper, which builds a tree of Json objects internally and converts the tree to a string.
- I also assume you are using the StringSerializer which lets kafka convert the string to bytes.
- Object -> JsonNode: Again same three things are happening.
- The object mapper in producing a tree of Json objects.
- The JsonSerializer converts the tree to a string and the string to bytes.
If you're interested in maximizing performance you might want to avoid using json as a serialization mechanism and explore protobuf.
A kafka protobuf example is here. Some numbers comparing protobuf performance vs json serialization are here.