Skip to content

AxonOps — AI-Native Control Plane for Open Source Data Platforms

Kafka Topic Configuration

Topic configuration controls per-topic behavior for retention, compaction, compression, and storage. Topic-level settings override broker defaults.


Topic configuration follows a precedence hierarchy:

Topic configuration precedence over broker defaults

Terminal window
kafka-topics.sh --bootstrap-server kafka:9092 \
--create \
--topic events \
--partitions 12 \
--replication-factor 3 \
--config retention.ms=86400000 \
--config cleanup.policy=delete \
--config compression.type=lz4 \
--config min.insync.replicas=2
Terminal window
# Add or update configuration
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config retention.ms=172800000,max.message.bytes=10485760
# Remove configuration (revert to default)
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--delete-config retention.ms
Terminal window
# Show all topic configurations
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--describe
# Show topic details including configuration
kafka-topics.sh --bootstrap-server kafka:9092 \
--describe \
--topic events

Terminal window
# Retain messages for 24 hours
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config retention.ms=86400000
SettingDefaultDescription
retention.ms604800000 (7 days)Time to retain messages
retention.bytes-1 (unlimited)Size limit per partition
segment.ms604800000 (7 days)Time before rolling segment
Terminal window
# Retain 10GB per partition
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config retention.bytes=10737418240

Size Retention Behavior

retention.bytes applies per partition, not per topic. A topic with 12 partitions and retention.bytes=10GB can retain up to 120GB total.

When both time and size limits are set, messages are deleted when either limit is exceeded:

Terminal window
# Delete when older than 7 days OR when partition exceeds 50GB
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config retention.ms=604800000,retention.bytes=53687091200

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config cleanup.policy=delete

Segments are deleted when:

  • Segment age exceeds retention.ms
  • Partition size exceeds retention.bytes
  • Segment is closed (newer segment exists)

Log compaction retains the latest value for each key:

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name user-profiles \
--alter \
--add-config cleanup.policy=compact

Log compaction retaining the latest value for each key

SettingDefaultDescription
min.cleanable.dirty.ratio0.5Minimum dirty/total ratio to trigger
min.compaction.lag.ms0Minimum age before compacting
max.compaction.lag.msMAX_LONGMaximum age before forced compaction
delete.retention.ms86400000Time to retain tombstones
segment.ms604800000Time before rolling active segment
Terminal window
# Configure compaction behavior
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name user-profiles \
--alter \
--add-config min.cleanable.dirty.ratio=0.3,\
min.compaction.lag.ms=3600000,\
delete.retention.ms=86400000

Combines compaction with time-based deletion:

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name changelog \
--alter \
--add-config cleanup.policy=compact,delete

With this policy:

  • Records are compacted (latest value per key retained)
  • Compacted segments are deleted after retention.ms

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config compression.type=zstd
ValueDescription
producerUse producer's compression (default)
uncompressedStore uncompressed
gzipGZIP compression
snappySnappy compression
lz4LZ4 compression
zstdZstandard compression
compression.typeProducer CompressionStored As
producerAnyProducer's choice
lz4noneLZ4 (broker compresses)
lz4gzipLZ4 (broker recompresses)
uncompressedlz4Uncompressed (broker decompresses)

Performance Consideration

Setting topic compression different from producer compression causes broker CPU overhead for recompression. Let producer control compression when possible (compression.type=producer).


Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name large-events \
--alter \
--add-config max.message.bytes=10485760
SettingDefaultDescription
max.message.bytes1048588 (~1MB)Maximum record batch size
max.decompressed.message.bytesKafka maximum allocatable arrayMaximum decompressed record size
message.timestamp.typeCreateTimeCreateTime or LogAppendTime
message.timestamp.before.max.msMAX_LONGMaximum accepted age for a producer timestamp
message.timestamp.after.max.ms3600000 (1 hour)Maximum accepted future producer timestamp

Coordinated Size Limits

Increasing max.message.bytes requires coordinating with:

  • Producer: max.request.size
  • Consumer: max.partition.fetch.bytes
  • Broker: message.max.bytes, replica.fetch.max.bytes

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name critical-events \
--alter \
--add-config min.insync.replicas=2
SettingDefaultDescription
min.insync.replicas1Minimum replicas for acks=all
unclean.leader.election.enablefalseAllow out-of-sync leader
Replication Factormin.insync.replicasTolerates
312 broker failures (data risk)
321 broker failure (safe)
532 broker failures (safe)

min.insync.replicas=1

With min.insync.replicas=1, a single replica can acknowledge writes. If that replica fails before replication, data is lost even with acks=all.


Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config segment.bytes=536870912,segment.ms=3600000
SettingDefaultDescription
segment.bytes1073741824 (1GB)Segment file size
segment.ms604800000 (7 days)Time before rolling
segment.index.bytes10485760Index file size
segment.jitter.ms0Random jitter for segment rolling
SettingDefaultDescription
index.interval.bytes4096Bytes between index entries
flush.messagesMAX_LONGMessages before fsync
flush.msMAX_LONGTime before fsync

Segment Rolling

Active segments are not eligible for deletion or compaction. Smaller segments enable faster cleanup but increase file handle usage.


Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config message.timestamp.type=LogAppendTime
ValueDescription
CreateTimeProducer-set timestamp (default)
LogAppendTimeBroker-set timestamp on append
Terminal window
# Reject records more than one day old or one hour in the future
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config message.timestamp.before.max.ms=86400000,message.timestamp.after.max.ms=3600000

message.timestamp.difference.max.ms was removed in Kafka 4.0. Use the independent before/after limits above. They apply when message.timestamp.type=CreateTime.


Kafka 4.4+

These settings require Kafka 4.4 brokers. Do not add them to topic templates used with older clusters.

remote.copy.lag.ms and remote.copy.lag.bytes delay upload of closed segments. A segment becomes eligible when either configured threshold is satisfied.

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config 'remote.storage.enable=true,remote.copy.lag.ms=3600000,remote.copy.lag.bytes=10737418240'

The effective lag must not exceed the corresponding local retention limit. A value of 0 makes segments immediately eligible. A value of -1 derives the threshold from effective local retention.

Kafka 4.4 adds compression.gzip.level, compression.lz4.level, and compression.zstd.level for topics whose compression.type selects the corresponding codec. It also adds max.decompressed.message.bytes, which limits an individual decompressed record where the broker must decompress data. Lowering that limit below existing record sizes can make compaction and timestamp-based offset lookups fail for affected partitions.

When share-group DLQ auto-creation is disabled, mark the explicitly created DLQ topic:

Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name dlq.payments-workers \
--alter \
--add-config errors.deadletterqueue.group.enable=true

See Group Configuration for group and cluster settings.


Terminal window
kafka-configs.sh --bootstrap-server kafka:9092 \
--entity-type topics \
--entity-name events \
--alter \
--add-config follower.replication.throttled.replicas=0:1,0:2,1:1,1:2
SettingDescription
follower.replication.throttled.replicasReplicas subject to replication throttle
leader.replication.throttled.replicasLeaders subject to replication throttle

Throttling is used during partition reassignment to limit network impact.


This table highlights commonly operated settings. Use kafka-configs.sh --describe to see the effective values for a topic.

SettingDefaultDescription
cleanup.policydeletedelete, compact, or both
compression.typeproducerCompression algorithm
delete.retention.ms86400000Tombstone retention
file.delete.delay.ms60000Delay before file deletion
flush.messagesMAX_LONGMessages before fsync
flush.msMAX_LONGTime before fsync
index.interval.bytes4096Index interval
max.compaction.lag.msMAX_LONGMax time before compaction
max.message.bytes1048588Maximum message size
max.decompressed.message.bytesKafka maximum allocatable arrayMaximum decompressed record size
message.timestamp.typeCreateTimeTimestamp type
message.timestamp.before.max.msMAX_LONGMaximum accepted timestamp age
message.timestamp.after.max.ms3600000Maximum accepted future timestamp
min.cleanable.dirty.ratio0.5Compaction trigger ratio
min.compaction.lag.ms0Min time before compaction
min.insync.replicas1Minimum ISR
preallocatefalsePreallocate segment files
retention.bytes-1Size retention per partition
retention.ms604800000Time retention
remote.storage.enablefalseEnable tiered storage for the topic
local.retention.ms-2Local time retention; inherit total retention
local.retention.bytes-2Local size retention; inherit total retention
remote.copy.lag.ms0Kafka 4.4+: time threshold before remote copy
remote.copy.lag.bytes-1Kafka 4.4+: size threshold before remote copy
errors.deadletterqueue.group.enablefalseKafka 4.4+: permit use as a share-group DLQ
segment.bytes1073741824Segment size
segment.index.bytes10485760Index file size
segment.jitter.ms0Segment roll jitter
segment.ms604800000Segment roll time
unclean.leader.election.enablefalseAllow unclean election