Skip to content

Import botocore and gssapi lazily in SASL mechanisms - #3171

Merged
dpkp merged 2 commits into
dpkp:masterfrom
dogukancagatay:lazy-import-botocore-gssapi
Oct 26, 2026
Merged

dpkp merged 2 commits into
dpkp:masterfrom
dogukancagatay:lazy-import-botocore-gssapi

Conversation

@dogukancagatay

Copy link
Copy Markdown
Contributor

import kafka loads 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 asyncio network backends.

@dogukancagatay
dogukancagatay force-pushed the lazy-import-botocore-gssapi branch from 84fd386 to 902549e Compare October 24, 2026 21:25
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.
@dpkp

dpkp commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thanks for the improvement!

@dpkp
dpkp merged commit 32e2dbb into dpkp:master Oct 26, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants