SASL authentication in Kafka - Time & Space Complexity
When Kafka uses SASL authentication, it checks user credentials to allow access. Understanding how long this check takes helps us see how it affects performance.
We want to know how the time to authenticate grows as more users or requests happen.
Analyze the time complexity of the following Kafka SASL authentication process.
// Pseudocode for SASL authentication in Kafka
for each incoming connection {
receive authentication request;
lookup user credentials in user store;
verify credentials;
respond with success or failure;
}
This code checks each connection's credentials one by one to decide if access is allowed.
Look at what repeats as more connections come in.
- Primary operation: Checking credentials for each connection.
- How many times: Once per connection, repeated for every new connection.
As more connections arrive, the number of credential checks grows directly with them.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 credential checks |
| 100 | 100 credential checks |
| 1000 | 1000 credential checks |
Pattern observation: The work grows evenly as the number of connections grows.
Time Complexity: O(n)
This means the time to authenticate grows in a straight line with the number of connections.
[X] Wrong: "Authentication time stays the same no matter how many connections happen."
[OK] Correct: Each connection needs its own check, so more connections mean more work and more time overall.
Understanding how authentication time grows helps you explain system behavior clearly. This skill shows you can think about real-world system limits and user experience.
"What if the user credentials were cached after the first check? How would the time complexity change?"
Practice
Solution
Step 1: Understand SASL role in Kafka
SASL is a protocol used to verify who is connecting to Kafka, ensuring only authorized users can access it.Step 2: Differentiate from other Kafka features
Compression, load balancing, and disk encryption are unrelated to SASL authentication.Final Answer:
To verify user identity before allowing access -> Option BQuick Check:
SASL = user verification [OK]
- Confusing SASL with encryption
- Thinking SASL manages data compression
- Mixing SASL with load balancing
Solution
Step 1: Identify correct property for SASL mechanism
The Kafka client usessasl.mechanismto specify the SASL mechanism like PLAIN, SCRAM, etc.Step 2: Check other options for correctness
security.protocolsets protocol type, not mechanism;sasl.configis invalid;security.mechanismis not a valid property.Final Answer:
sasl.mechanism = PLAIN -> Option CQuick Check:
sasl.mechanism sets SASL type [OK]
- Using security.protocol instead of sasl.mechanism
- Writing sasl.config instead of sasl.mechanism
- Confusing property names
props.put("security.protocol", "SASL_SSL");
props.put("sasl.mechanism", "SCRAM-SHA-256");
props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"user1\" password=\"pass1\";");What will happen when the client tries to connect?
Solution
Step 1: Analyze security.protocol setting
Settingsecurity.protocoltoSASL_SSLmeans the client uses SASL authentication over SSL encrypted connection.Step 2: Check SASL mechanism and JAAS config
The mechanism isSCRAM-SHA-256with proper JAAS config providing username and password, so authentication will succeed if credentials are correct.Final Answer:
Client connects securely using SCRAM-SHA-256 with encrypted channel -> Option DQuick Check:
SASL_SSL + SCRAM = secure auth [OK]
- Assuming SASL_SSL means no encryption
- Ignoring JAAS config for credentials
- Confusing SCRAM with PLAINTEXT
props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "PLAIN");
props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin123\";");The client fails to authenticate. What is the most likely cause?
Solution
Step 1: Check security.protocol compatibility
SASL_PLAINTEXTmeans SASL without encryption. Many brokers disable this for security reasons.Step 2: Consider broker support
If the broker is configured to reject SASL_PLAINTEXT connections, authentication will fail even if credentials and mechanism are correct.Final Answer:
Broker does not support SASL_PLAINTEXT -> Option AQuick Check:
Broker must allow SASL_PLAINTEXT [OK]
- Assuming SSL is required for SASL_PLAINTEXT
- Misformatting sasl.jaas.config string
- Using wrong sasl.mechanism value
Solution
Step 1: Match security.protocol with SASL_SSL
To use encrypted SASL,security.protocolmust beSASL_SSL.Step 2: Confirm sasl.mechanism and JAAS config
For SCRAM-SHA-512,sasl.mechanismmust beSCRAM-SHA-512and JAAS config must useScramLoginModulewith username and password.Step 3: Eliminate incorrect options
props.put("security.protocol", "SSL"); props.put("sasl.mechanism", "SCRAM-SHA-512"); props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"user\" password=\"pass\";"); usesSSLwithout SASL, C uses wrong mechanism and protocol, D uses PLAIN mechanism instead of SCRAM-SHA-512.Final Answer:
Correct SASL_SSL with SCRAM-SHA-512 and proper JAAS config -> Option AQuick Check:
SASL_SSL + SCRAM-SHA-512 + JAAS = props.put("security.protocol", "SASL_SSL"); props.put("sasl.mechanism", "SCRAM-SHA-512"); props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"user\" password=\"pass\";"); [OK]
- Using SSL instead of SASL_SSL
- Mismatching mechanism and JAAS module
- Using wrong SASL mechanism version
Ready to prove it?
Pass the test and this pattern moves unknown
