Repository navigation
Perform initial shard sync operation after leader election - #1731
Merged
Merged
Conversation
rattang-dev
temporarily deployed
to
manual-approval
April 16, 2026 16:28 — with
GitHub Actions
Inactive
lucienlu-aws
approved these changes
Apr 21, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
When a KCL application starts fresh (lease table does not exist), all workers simultaneously perform the initial shard sync operation via syncShardsOnce(). Each worker independently calls DescribeStream/
ListShards, paginating through the entire shard history. For large streams with many workers, this creates a burst of shard discovery API calls proportional to the number of workers, which can cause throttling
by the service and prevent the application from starting.
For example, a stream with 100K shards and 1000 workers would generate ~1000 concurrent ListShards paginations at startup.
Root Cause
The initial shard sync runs before the migration state machine and leader decider are initialized. Without leader election, every worker that finds an empty lease table proceeds to call syncShardsOnce(). The
existing shouldInitiateLeaseSync() method uses a random 1-30 second wait to reduce contention, but this is insufficient for large fleets β multiple workers can finish the wait simultaneously and all proceed
with shard sync.
Description of Changes
This change moves the initialization of the migration state machine and leader decider (migrationStateMachine.initialize() and leaderDecider assignment) before the initial shard sync block. This allows the
shard sync to be gated by leaderDecider.isLeader(), ensuring only the elected leader performs the initial shard discovery.
Why this is safe
empty lease table. Only one worker acquires the lock and performs shard sync.
is unaffected.
Testing