DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Applying Kappa Architecture to Make Data Available Where It Matters
  • Designing High-Volume Systems Using Event-Driven Architectures
  • Data Fabric: What Is It and Why Do You Need It?
  • How To Get Closer to Consistency in Microservice Architecture

Trending

  • Setting Up Data Pipelines With Snowflake Dynamic Tables
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Start Coding With Google Cloud Workstations
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Kafka Architecture: Log Compaction

Kafka Architecture: Log Compaction

This article on Kafka architecture talks goes over Kafka design and log compaction, its structure, and the log compaction process.

By 
Jean-Paul Azar user avatar
Jean-Paul Azar
·
Aug. 31, 17 · Opinion
Likes (11)
Comment
Save
Tweet
Share
47.4K Views

Join the DZone community and get the full member experience.

Join For Free

this post really picks off from our series on kafka architecture which includes kafka topics architecture , kafka producer architecture , kafka consumer architecture , and kafka ecosystem architecture .

this article is heavily inspired by the kafka section on design around log compaction . you can think of it as the cliff notes about kafka design around log compaction .

kafka can delete older records based on time or size of a log. kafka also supports log compaction for record key compaction. log compaction means that kafka will keep the latest version of a record and delete the older versions during a log compaction.

jean-paul azar works at cloudurable . cloudurable provides kafka training , kafka consulting , kafka support and helps setting up kafka clusters in aws .

kafka log compaction

log compaction retains at least the last known value for each record key for a single topic partition. compacted logs are useful for restoring state after a crash or system failure.

they are useful for in-memory services, persistent data stores, reloading a cache, etc. an important use case of data streams is to log changes to keyed, mutable data changes to a database table or changes to object in in-memory microservice.

log compaction is a granular retention mechanism that retains the last update for each key. a log compacted topic log contains a full snapshot of final record values for every record key not just the recently changed keys.

kafka log compaction allows downstream consumers to restore their state from a log compacted topic.

kafka log compaction structure

with a compacted log , the log has head and tail. the head of the compacted log is identical to a traditional kafka log. new records get appended to the end of the head.

all log compaction works at the tail of the log. only the tail gets compacted. records in the tail of the log retain their original offset when written after being rewritten with compaction cleanup .

kafka log compaction structure

log compaction structure

kafka log compaction basics

all compacted log offsets remain valid, even if record at offset has been compacted away as a consumer will get the next highest offset.

kafka log compaction also allows for deletes. a message with a key and a null payload acts like a tombstone, a delete marker for that key. tombstones get cleared after a period. log compaction periodically runs in the background by re-copying log segments. compaction does not block reads and can be throttled to avoid impacting i/o of producers and consumers.

kafka log compaction process

kafka log compaction process

kafka log compaction cleaning

if a kafka consumer stays caught up to head of the log, it sees every record that is written.

topic config min.compaction.lag.ms gets used to guarantee a minimum period that must pass before a message can be compacted. the consumer sees all tombstones as long as the consumer reaches head of a log in a period less than the topic config delete.retention.ms (the default is 24 hours). log compaction will never re-order messages, just remove some. partition offset for a message never changes.

any consumer reading from the start of the log sees at least final state of all records in the order they were written.

kafka log cleaner

recall that a kafka topic has a log. a log is broken up into partitions and partitions are divided into segments which contain records which have keys and values.

the kafka log cleaner does log compaction. the log cleaner has a pool of background compaction threads. these threads recopy log segment files, removing older records whose key reappears recently in the log. each compaction thread chooses topic log that has the highest ratio of log head to log tail. then the compaction thread recopies the log from start to end removing records whose keys occur later in the log.

as the log cleaner cleans log partition segments, the segments get swapped into the log partition immediately replacing the older segments. this way compaction does not require double the space of the entire partition as additional disk space required is just one additional log partition segment - divide and conquer.

topic config for log compaction

to turn on compaction for a topic, use topic config log.cleanup.policy=compact .

to set a delay to start compacting records after they are written, use topic config log.cleaner.min.compaction.lag.ms . records won’t get compacted until after this period. the setting gives consumers time to get every record.

log compaction review

what are three ways kafka can delete records?

kafka can delete older records based on time or size of a log. kafka also supports log compaction for record key compaction.

what is log compaction good for?

since log compaction retains last known value it is a full snapshot of the latest records it is useful for restoring state after a crash or system failure for an in-memory service, a persistent data store, or reloading a cache. it allows downstream consumers to restore their state.

what is the structure of a compacted log? describe the structure.

with a compacted log, the log has head and tail. the head of the compacted log is identical to a traditional kafka log. new records get appended to the end of the head. all log compaction works at the tail of the compacted log.

after compaction, do log record offsets change? no.

what is a partition segment?

recall that a topic has a log. a topic log is broken up into partitions and partitions are divided into segment files which contain records which have keys and values. segment files allow for divide and conquer when it comes to log compaction. a segment file is part of the partition. as the log cleaner cleans log partition segments, the segments get swapped into the log partition immediately replacing the older segment files. this way compaction does not require double the space of the entire partition as additional disk space required is just one additional log partition segment.

jean-paul azar works at cloudurable . cloudurable provides kafka training , kafka consulting , kafka support and helps setting up kafka clusters in aws .

kafka Database Architecture

Opinions expressed by DZone contributors are their own.

Related

  • Applying Kappa Architecture to Make Data Available Where It Matters
  • Designing High-Volume Systems Using Event-Driven Architectures
  • Data Fabric: What Is It and Why Do You Need It?
  • How To Get Closer to Consistency in Microservice Architecture

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!