Distilling Kafka’s Binary Protocol into Elixir

Breakout Session

Kafka’s wire protocol evolves fast: dozens of APIs, versioned schemas, and “flexible versions” (KIP-482) with compact encodings and tagged fields. For most client ecosystems, keeping up means either dragging in a large Java stack or hand-maintaining a sprawling protocol layer.

In the BEAM world, kafka_ex takes a different route. It relies on Kayrock, which treats the Kafka protocol as data, not handwritten code. At compile time, Kayrock.Generate loads the upstream Erlang :kpro_schema and expands it into pure Elixir modules: typed request/response structs plus serializers/deserializers for every supported API version. Flexible versions and tagged fields are handled via AST generation and a small set of pattern-matching helpers, so the “weird bits” live in one place.

The payoff is operational: adding support for a new Kafka release is mostly update :kpro_schema → run a Mix task → commit generated .ex files. No JNI. No JVM dependency. No rewriting protocol logic by hand. Elixir applications still work with normal structs, while the protocol stays correct and current.

I’ll walk through the generator pipeline end-to-end, show a concrete example of one Kafka API across versions, and share the design tradeoffs (what’s generated vs handwritten) so you can apply the same pattern to other fast-changing binary protocols.


Anton Borisov

Fresha