Producing

A producer is a Resource: acquiring it creates the producer, and releasing it delivers what is still pending and closes it (see Releasing a producer). Records are produced in non-empty batches.

// Write one record to the topic "events", and wait until it is delivered
import cats.data.NonEmptyList
import cats.effect.IO

import xkafka.*

val utf8 = Serializer.utf8[IO]

val program =
  for
    client   <- ClientSettings.from(NonEmptyList.one("localhost:9092")).liftTo[IO]
    settings <- ProducerSettings.from(client, utf8, utf8).liftTo[IO]
    topic    <- Topic.from("events").liftTo[IO]
    // Acquire a producer, send the record, and release the producer
    _        <- KafkaClient[IO].producer(settings).use(_.produceAndAwait(NonEmptyList.one(ProducerRecord(topic, "key", "value"))))
  yield ()

Sending without waiting for delivery

produce returns F[F[NonEmptyList[Produced]]]. The outer effect completes once the client has accepted the records for delivery, and the inner one once it reports their delivery outcome. Unless acks is NoAcknowledgement, that includes the broker's acknowledgement. Holding the inner effect lets several batches be in flight at once:

// Send two batches without waiting between them, then wait for both to be delivered
for
  first  <- producer.produce(firstBatch)   // Accepted, not yet delivered
  second <- producer.produce(secondBatch)  // Accepted while the first is still being delivered
  _      <- first                          // Wait for the first's outcome
  _      <- second                         // Wait for the second's outcome
yield ()

produceAndAwait runs both stages together, for callers that do not need the pipelining.

For a stream of batches, use pipe:

// Send a stream of batches, with at most 256 awaiting delivery at once
batches.through(producer.pipe(maxInFlight = 256))

pipe enqueues later batches while earlier ones still await their delivery outcome, and emits results in the order the batches arrived. maxInFlight sets how many batches may await delivery at once. Mapping produceAndAwait over a stream instead would send one batch at a time.

To write records and consumer offsets atomically, use a transactional producer. See Transactions.

Reading delivery results

Each Produced holds a record and the metadata reported for it, in the order the records were given:

// Send a batch, and read the offset each record was written at, if Kafka reported one
producer.produceAndAwait(batch).map(_.map(produced => produced.metadata.offset))

RecordMetadata carries the topic-partition, and the offset and timestamp, each an Option that is set if Kafka reports it.

If any record is not delivered, the inner effect fails with the failure of the first such record. If the client refuses a record as it is enqueued, the outer effect fails instead.

Two kinds of bad record are only caught after they are enqueued, so they fail the inner effect like a record that is not delivered:

While the client's buffer is full, the outer effect waits for room, then enqueues the rest of the records. If no room frees up within the enqueue timeout (one minute unless you set withEnqueueTimeout), that record fails with ErrorCode.RequestTimedOut, and so does every record after it in the batch. Cancelling the outer effect stops the wait.

Failures are KafkaException values; see Errors for how to tell them apart.

When some records fail

produceAttemptEach reports on every record, so you can tell which records were delivered when others were not. Each Attempted holds a record and either its metadata or why it was not delivered:

// Send a batch, then act on each record's own outcome
producer.produceAttemptEachAndAwait(batch).flatMap: attempts =>
  attempts.toList.traverse_ :
    case Attempted(record, Right(metadata)) => recordDelivered(record, metadata)  // Delivered
    case Attempted(record, Left(failure))   => retryLater(record, failure)        // Not delivered, so retry this record later

produceAttemptEach has the same two stages as produce, and produceAttemptEachAndAwait and pipeAttemptEach match produceAndAwait and pipe. pipeAttemptEach carries on past records that fail.

Records

A ProducerRecord names its topic, key, and value, and optionally a partition, a timestamp, and headers. A timestamp before 1970, or of 1970-01-01T00:00:00Z itself, raises IllegalArgumentException: Kafka holds none before, and librdkafka writes that time as the time it sends the record at, so on Scala.js and Scala Native the record would carry another. A key or value of None, through the .option codecs, represents a Kafka null. Tombstones are produced this way; see Nulls and tombstones.

Partitions

A record with a key and no partition goes to the partition given by the murmur2 hash of its serialized key, modulo the topic's partition count. Every platform uses this hash, the same one the Java producer uses, so a key always lands on the same partition whichever platform produced it. A record with neither a key nor a partition goes to a partition the platform's client chooses.

partitionsFor returns the partitions a topic currently has. Use it to pick a partition to produce to. Asking about a topic the cluster does not have creates it if the broker creates topics on request, which Kafka brokers do by default. It waits for a topic still being created up to the client's metadataRefreshInterval or 30 seconds, whichever is shorter, and then fails. A consumer's partitionsFor does not create a topic.

// List the partitions the topic "events" has now
import cats.data.NonEmptyList
import cats.effect.IO

import xkafka.*

val utf8 = Serializer.utf8[IO]

val program =
  for
    client   <- ClientSettings.from(NonEmptyList.one("localhost:9092")).liftTo[IO]
    settings <- ProducerSettings.from(client, utf8, utf8).liftTo[IO]
    topic    <- Topic.from("events").liftTo[IO]
    found    <- KafkaClient[IO].producer(settings).use(_.partitionsFor(topic))  // Creates the topic if the broker creates topics on request
  yield found

Acknowledgements

acks sets how many replicas must hold a record before the broker acknowledges it. It defaults to Acks.AllReplicas. To use any other value, turn idempotence off first:

// Have only the partition leader acknowledge a record, which needs idempotence off
settings.withoutIdempotence.withAcks(Acks.Leader)
value when delivery can complete a record is lost if
Acks.NoAcknowledgement the producer sends it without waiting for a broker response delivery fails without the caller knowing
Acks.Leader the partition leader has it the leader fails before a follower copies it
Acks.AllReplicas every in-sync replica has it every in-sync replica fails

Idempotence

Producers are idempotent by default: xkafka turns on Kafka's enable.idempotence on every platform, so a send the client retries does not write the record twice.

Idempotence requires Acks.AllReplicas. Settings that combine it with another acks value are rejected with SettingsError.IdempotenceRequiresAllReplicas, which is why withAcks and withIdempotence return a ValidatedNel. Call withoutIdempotence to turn it off.

Transactional producers are always idempotent.

Batching and compression

A producer sends records to the broker in batches. linger sets how long a record waits for others to join its batch before the batch is sent. It defaults to five milliseconds, and zero sends each batch as soon as it can. A longer linger makes larger batches, at the cost of that much delay per record.

compression sets how each batch is compressed: Compression.Uncompressed, which is the default, Gzip, Snappy, Lz4 or Zstd. Every platform supports all five, and consumers read them all without being told which one was used.

// Compress batches with zstd, and let a record wait 20ms for others to join its batch
import scala.concurrent.duration.*

import cats.effect.IO

import xkafka.*

def batched[F[_], K, V](settings: ProducerSettings[F, K, V]): IO[ProducerSettings[F, K, V]] =
  settings.withCompression(Compression.Zstd).withLinger(20.millis).liftTo[IO]

withLinger returns a ValidatedNel, because a negative linger, or one of ProducerSettings.LingerLimit or longer, is rejected, and so is one not less than a message.timeout.ms or delivery.timeout.ms the properties set, or one that with the request timeout passes a delivery.timeout.ms the properties set. See Validation for every rule.

A batch is sent as soon as it is full or its linger has passed, whichever comes first. batchSize sets how many bytes of records make a batch full. Unless it is set, the default differs by platform: 16384 bytes on the JVM, and 1000000 bytes on Scala.js and Scala Native. withBatchSize sets the same size on every platform, and withoutBatchSize goes back to each one's default.

Flushing

flush sends every record enqueued before it without waiting out the linger, and completes once each of them has been delivered or has failed:

// Send the batch now, without waiting out the linger time, then read the results
for
  delivered <- producer.produce(batch)  // Enqueue the batch
  _         <- producer.flush           // Done once each record is delivered or has failed
  results   <- delivered                // Each record's own outcome
yield results

Releasing a producer

Releasing the resource first waits for records the producer has already accepted, then closes the producer. closeTimeout bounds that delivery wait and defaults to sixty seconds:

// Wait at most ten seconds for undelivered records when a producer is released
import scala.concurrent.duration.*

import cats.effect.IO

import xkafka.*

def withCloseTimeout[F[_], K, V](settings: ProducerSettings[F, K, V]): IO[ProducerSettings[F, K, V]] =
  settings.withCloseTimeout(10.seconds).liftTo[IO]

Like every method that sets a duration, it returns a ValidatedNel; see Changing settings.

Records still undelivered when the timeout expires are dropped. Closing the producer comes after that wait, so releasing can take somewhat longer than closeTimeout. A shorter timeout releases sooner but can drop more records.

closeTimeout applies to producers. Consumers have no equivalent setting.