Repository navigation
Import botocore and gssapi lazily in SASL mechanisms - #3171
Merged
Merged
Conversation
dogukancagatay
force-pushed
the
lazy-import-botocore-gssapi
branch
from
October 24, 2026 21:25
84fd386 to
902549e
Compare
Importing kafka loaded botocore whenever it was installed, even in processes that never use AWS_MSK_IAM. That adds about 15 MiB of memory per process. Load botocore only when an AWS_MSK_IAM mechanism is created. A missing package still raises the same error at that point.
Importing kafka loaded the gssapi library whenever it was installed, even in processes that never use Kerberos. That costs about 4 MiB of memory per process. Load gssapi only when a GSSAPI mechanism is created. A missing library still raises the same error at that point.
dogukancagatay
force-pushed
the
lazy-import-botocore-gssapi
branch
from
October 24, 2026 21:49
902549e to
17c4cbf
Compare
This was referenced Oct 25, 2026
Owner
|
Thanks for the improvement! |
dpkp
pushed a commit
that referenced
this pull request
Oct 26, 2026
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.
import kafkaloads every SASL mechanism module. Two of them, AWS_MSK_IAM and GSSAPI, pull in their optional dependencies right away whenever those packages are installed. Every process pays for that memory, even if it only uses PLAIN or SCRAM, or no SASL at all.The cost is real. On Python 3.13, import kafka takes about 50 MiB of RSS with botocore and gssapi installed. botocore accounts for roughly 15 MiB and gssapi for 4 MiB.
This change loads each library only when its mechanism is created. With both changes, import kafka drops to about 30 MiB in the same environment.
User-facing behaviour stays the same. If the library is missing, creating the mechanism still raises the same error with the same message. Tests now fake these modules instead of patching module attributes, and each mechanism gets a test for the missing-library case.
The full test suite passes against a Kafka 4.3.0 broker with both the selector and
asyncionetwork backends.