Bird
Raised Fist0
Kafkadevops~20 mins

Batch size and compression tuning in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kafka Batch & Compression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Effect of batch.size on message sending

Consider a Kafka producer configured with batch.size=16384 bytes and linger.ms=0. The producer sends messages of 1000 bytes each. How many messages will be sent in one batch if the producer is continuously sending messages?

A16384 messages
B1 message
C16 messages
D0 messages
Attempts:
2 left
💡 Hint

Batch size limits the total bytes in a batch. Divide batch size by message size.

❓ Predict Output
intermediate
2:00remaining
Compression type impact on message size

A Kafka producer sends a batch of 100 messages, each 1KB uncompressed. The producer uses compression.type=gzip. After compression, the batch size is 50KB. What is the approximate compression ratio?

A2:1
B0.5:1
C50:100
D1:2
Attempts:
2 left
💡 Hint

Compression ratio = uncompressed size / compressed size.

❓ Predict Output
advanced
2:00remaining
Producer batch sending timing with linger.ms

A Kafka producer has batch.size=32768 bytes and linger.ms=100. It sends messages of 5000 bytes each. If the producer sends 3 messages quickly, what happens?

AThe producer sends the batch immediately after the first message.
BThe producer waits indefinitely until batch is full.
CThe producer sends each message immediately without waiting.
DThe producer waits 100ms before sending the batch of 3 messages.
Attempts:
2 left
💡 Hint

Consider how linger.ms affects batching when batch is not full.

❓ Predict Output
advanced
2:00remaining
Effect of compression.type=snappy on CPU usage

Which of the following is true about using compression.type=snappy in Kafka producers compared to gzip?

ASnappy uses less CPU and compresses faster but with lower compression ratio than gzip.
BSnappy uses more CPU and compresses slower but with higher compression ratio than gzip.
CSnappy and gzip have identical CPU usage and compression ratio.
DSnappy does not compress data, only encrypts it.
Attempts:
2 left
💡 Hint

Think about speed vs compression trade-offs.

🧠 Conceptual
expert
3:00remaining
Choosing optimal batch size and compression for high throughput

You want to maximize Kafka producer throughput for large messages (~1MB each) on a network with moderate latency. Which combination is best?

ASet batch.size to 1MB and disable compression to reduce CPU usage.
BSet batch.size to 5MB and use compression.type=lz4 for fast compression and decompression.
CSet batch.size to 512KB and use compression.type=gzip for maximum compression ratio.
DSet batch.size to 100KB and use compression.type=snappy for minimal latency.
Attempts:
2 left
💡 Hint

Consider message size, network latency, and compression speed.

Practice

(1/5)
1. What does increasing the batch.size setting in Kafka producer configuration primarily affect?
easy
A. The number of bytes sent in one request to the broker
B. The number of partitions a topic has
C. The compression algorithm used for messages
D. The timeout for message delivery

Solution

  1. Step 1: Understand batch.size meaning

    The batch.size controls how many bytes the producer will try to batch together before sending to the broker.
  2. Step 2: Identify what batch.size affects

    Increasing batch.size means more data is sent in one request, improving throughput but possibly increasing latency.
  3. Final Answer:

    The number of bytes sent in one request to the broker -> Option A
  4. Quick Check:

    batch.size = bytes per request [OK]
Hint: Batch size controls bytes per request, not partitions or compression [OK]
Common Mistakes:
  • Confusing batch size with number of partitions
  • Thinking batch size sets compression type
  • Assuming batch size controls timeout
2. Which of the following is the correct way to enable compression with gzip in Kafka producer configuration?
easy
A. "compression": "gzip"
B. "compress.type": "gzip"
C. "compression.type": "gzip"
D. "compressionType": "gzip"

Solution

  1. Step 1: Recall Kafka producer config keys

    The correct key for compression is compression.type.
  2. Step 2: Match correct syntax

    The value should be a string like "gzip". So the correct config is "compression.type": "gzip".
  3. Final Answer:

    "compression.type": "gzip" -> Option C
  4. Quick Check:

    compression.type = gzip [OK]
Hint: Use exact key 'compression.type' to enable compression [OK]
Common Mistakes:
  • Using wrong key names like 'compression' or 'compress.type'
  • Incorrect camelCase key 'compressionType'
  • Missing quotes around 'gzip'
3. Given the following Kafka producer config snippet:
{
  "batch.size": 16384,
  "compression.type": "snappy"
}

What is the expected effect on message sending?
medium
A. Messages are sent in batches up to 16 KB and compressed with Snappy
B. Messages are sent one by one without compression
C. Messages are compressed with gzip and batch size is ignored
D. Batch size is 16 bytes and compression is disabled

Solution

  1. Step 1: Interpret batch.size value

    The batch size is set to 16384 bytes, which means messages will be grouped up to 16 KB before sending.
  2. Step 2: Interpret compression.type value

    The compression type is set to "snappy", so messages will be compressed using Snappy algorithm.
  3. Final Answer:

    Messages are sent in batches up to 16 KB and compressed with Snappy -> Option A
  4. Quick Check:

    batch.size=16384 + compression.type=snappy [OK]
Hint: Check batch size bytes and compression.type value carefully [OK]
Common Mistakes:
  • Confusing batch size units (bytes vs KB)
  • Assuming gzip compression instead of snappy
  • Thinking batch size disables compression
4. You have set batch.size to 0 in your Kafka producer config. What issue will this cause?
medium
A. Producer will throw a configuration error and fail to start
B. Producer will send messages immediately without batching
C. Producer will batch messages indefinitely causing delays
D. Producer will compress messages with default gzip

Solution

  1. Step 1: Understand batch.size valid values

    Batch size can be set to 0, which disables batching. Messages are sent immediately without waiting to fill a batch.
  2. Step 2: Identify Kafka producer behavior on batch.size=0

    The producer will not throw an error; it will work but send each message in its own request, potentially reducing throughput.
  3. Final Answer:

    Producer will send messages immediately without batching -> Option B
  4. Quick Check:

    batch.size=0 disables batching [OK]
Hint: batch.size=0 disables batching safely, sends immediately [OK]
Common Mistakes:
  • Thinking batch.size=0 causes a configuration error
  • Believing it causes indefinite batching and delays
  • Confusing batch size with compression settings
5. You want to optimize Kafka producer throughput for large messages but keep latency low. Which combination of settings is best?
hard
A. Use default batch.size and compression.type = "none"
B. Decrease batch.size and disable compression
C. Set batch.size to 0 and use compression.type = "gzip"
D. Increase batch.size and use compression.type = "lz4"

Solution

  1. Step 1: Consider batch.size impact on throughput and latency

    Larger batch sizes improve throughput by sending more data at once but can increase latency slightly.
  2. Step 2: Choose compression type for speed and size

    LZ4 compression is fast and efficient, reducing message size without high CPU cost, helping throughput and latency.
  3. Step 3: Evaluate other options

    Decreasing batch size reduces throughput; disabling compression wastes bandwidth; batch.size=0 causes no batching.
  4. Final Answer:

    Increase batch.size and use compression.type = "lz4" -> Option D
  5. Quick Check:

    Large batch + fast compression = best throughput & low latency [OK]
Hint: Big batch + fast compression (lz4) balances speed and size [OK]
Common Mistakes:
  • Choosing gzip which is slower than lz4
  • Setting batch.size to 0 reducing throughput
  • Disabling compression reduces efficiency
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard