package kafka

  1. Overview
  2. Docs


Kafka is a high-throughput distributed messaging system.

Published: 03 Feb 2018


OCaml bindings for Kafka



MIT License


$ opam install kafka

From source:

$ make            # use jbuilder
$ make test       # assuming kakfa is running at localhost:9092 with a 'test' topic.
$ make install    # use opam


#use "topfind";;
#require "kafka";;

(* Prepare a producer handler. *)
let producer = Kafka.new_producer ["","localhost:9092"];;
let producer_topic = Kafka.new_topic producer "test" ["","10000"];;

(* Prepare a consumer handler *)
let consumer = Kafka.new_consumer ["","localhost:9092"];;
let consumer_topic = Kafka.new_topic consumer "test" ["auto.commit.enable","false"];;
let partition = 1;;
let timeout_ms = 1000;;

(* Start collecting messages *)
(* Here we start from offset_end, i.e. we will consume only messages produced from now. *)
Kafka.consume_start consumer_topic partition Kafka.offset_end;;

(* Produce some messages *)
Kafka.produce producer_topic partition "message 0";;
Kafka.produce producer_topic partition "message 1";;
Kafka.produce producer_topic partition "message 2";;

(* Consume messages *)
let rec consume t p = match Kafka.consume ~timeout_ms t p with
  | Kafka.Message(_,_,_,msg,_) -> msg
  | Kafka.PartitionEnd(_,_,_) -> consume t p
  | exception Kafka.Error(Kafka.TIMED_OUT,_) ->
    (Printf.fprintf stderr "Timeout after: %d ms\n%!" timeout_ms; consume t p)
let msg = consume consumer_topic partition in assert (msg = "message 0");
let msg = consume consumer_topic partition in assert (msg = "message 1");
let msg = consume consumer_topic partition in assert (msg = "message 2");

(* Stop collecting messages. *)
Kafka.consume_stop consumer_topic partition;;

(* Topics, consumers and producers must be released. *)
Kafka.destroy_topic producer_topic;;
Kafka.destroy_handler producer;;
Kafka.destroy_topic consumer_topic;;
Kafka.destroy_handler consumer;;


The API is documented in lib/kafka.mli, and the Lwt extension is documented in lib_lwt/kafka_lwt.mli.

See bin/ for an example consumer using queues, batches and lwt. See bin/ for an example producer.

Configuration options of producers, consumers and topics are inherited from librdkafka/CONFIGURATION.

Dependencies (5)

  1. jbuilder >= "1.0+beta7"
  2. cmdliner
  3. lwt
  4. base-unix
  5. ocaml >= "4.02.3"

Dev Dependencies


Used by