In-sync replicas (ISR) in Kafka - Time & Space Complexity
When working with Kafka's In-sync replicas (ISR), it's important to understand how the system keeps track of replicas that are up-to-date.
We want to know how the time to check and update ISR changes as the number of replicas grows.
Analyze the time complexity of the following Kafka ISR update logic.
// Pseudocode for ISR update
for each replica in replicasList {
if (replica.lastOffset == leader.lastOffset) {
add replica to ISR
} else {
remove replica from ISR
}
}
This code checks each replica's offset against the leader's offset to update the ISR list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all replicas to compare offsets.
- How many times: Once per replica in the replicas list.
The time to update ISR grows directly with the number of replicas.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: Doubling replicas roughly doubles the work needed.
Time Complexity: O(n)
This means the time to update ISR grows linearly with the number of replicas.
[X] Wrong: "Checking ISR is constant time no matter how many replicas there are."
[OK] Correct: Each replica must be checked individually, so more replicas mean more work.
Understanding how ISR updates scale helps you explain Kafka's reliability and performance in real systems.
"What if we only checked replicas that recently changed offset? How would the time complexity change?"
Practice
In-sync replicas (ISR) mean in Kafka?Solution
Step 1: Understand the role of ISR
ISR are replicas that keep up-to-date with the leader's data.Step 2: Compare options
Only Replicas that have the latest data from the leader correctly describes ISR as replicas with the latest data.Final Answer:
Replicas that have the latest data from the leader -> Option CQuick Check:
ISR = Latest data replicas [OK]
- Thinking ISR includes offline replicas
- Confusing ISR with replicas that lag behind
- Assuming ISR means replicas with old data
Solution
Step 1: Identify configuration related to ISR acknowledgments
The settingmin.insync.replicasdefines the minimum ISR count for write success.Step 2: Eliminate unrelated options
Other options control fetch size or retention, not ISR acknowledgments.Final Answer:
min.insync.replicas -> Option BQuick Check:
Minimum ISR replicas = min.insync.replicas [OK]
- Confusing min.insync.replicas with max.insync.replicas
- Choosing unrelated configs like offsets.retention.minutes
- Assuming replica.fetch.max.bytes controls ISR count
Solution
Step 1: Understand ISR behavior when a replica falls behind
If a replica lags, it is removed from ISR, leaving others in ISR.Step 2: Analyze leader and ISR state
The leader continues with remaining ISR replicas; it does not stop or change leader immediately.Final Answer:
Only replica1 remains in ISR; leader continues with one ISR -> Option AQuick Check:
Lagging replica leaves ISR, leader continues [OK]
- Thinking leader stops producing when ISR shrinks
- Assuming all replicas leave ISR together
- Believing lagging replica becomes leader immediately
Solution
Step 1: Interpret the error message
The error means the number of replicas in ISR is less than the configured minimum.Step 2: Match cause with options
Too few replicas are currently in ISR compared to min.insync.replicas directly matches the error cause; other options are unrelated to ISR count.Final Answer:
Too few replicas are currently in ISR compared to min.insync.replicas -> Option DQuick Check:
ISR count below min.insync.replicas causes error [OK]
- Blaming leader offline instead of ISR count
- Confusing consumer group commit with ISR errors
- Thinking zero partitions cause this error
Solution
Step 1: Understand min.insync.replicas meaning
This setting defines the minimum ISR count required for leader failover and write success.Step 2: Match configuration to requirement
To require at least 2 ISR replicas, setmin.insync.replicas=2and have at least 3 replicas to allow failover.Final Answer:
Set min.insync.replicas=2 and have at least 3 replicas per partition -> Option AQuick Check:
High availability needs min.insync.replicas=2 with 3 replicas [OK]
- Setting min.insync.replicas higher than replica count
- Using too few replicas for desired availability
- Setting min.insync.replicas to zero disables safety
Ready to prove it?
Pass the test and this pattern moves unknown
