Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Log Segmentation in Apache Kafka

Tech 1

Apache Kafka employs log semgentation to efficiently organize message data on disk. This mechanism enables optimized write, read, and cleanup operations for persistent logs.

Log Segmentation Principles:

  1. Each Kafka topic partition maintains a collection of persistent log files called segments
  2. Segments contain message ranges determined by either:
    • Time-based policies (e.g., retaining only the last N days of data)
    • Size-based policies (creating new segments when reaching maximum configured size)

Message Retrieval System:

  • Messages are uniquely identified by monotonically increasing offsets
  • Each segment has a accompanying index file for rapid message location
  • Indexes use sparse storage, containing only enough entries for efficient lookup

Segment Maintainance Process:

  • Periodic cleanup tasks evaluate segments against retention policies
  • Eligible segments are marked for deletion and eventually removed from disk (configurable via file.delete.delay.ms)

Configuration examples using Kafka CLI tools:

# Set 24-hour retention policy
bin/kafka-configs.sh --zookeeper localhost:2181 \
  --alter --entity-type topics \
  --entity-name my-topic \
  --add-config retention.ms=86400000

# Inspect segment details
bin/kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe

The describe command outputs comprehensive segment information including partition offsets and storage metrics.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.