How to connect to MSK with SASL/SCRAM using Java?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon MSK supports SASL/SCRAM for username-and-password authentication, which is a common choice when Kafka clients need authenticated and encrypted access without using IAM auth. The Java side is straightforward once the cluster, secret, and network path are ready, but small configuration mistakes usually cause the connection to fail immediately.
What Must Exist Before Java Can Connect
Before writing client code, verify four things in AWS:
- SASL/SCRAM is enabled on the MSK cluster.
- A Secrets Manager secret is associated with the cluster.
- The client can reach the brokers over the correct network path.
- You retrieved the
BootstrapBrokerStringSaslScramvalue for that cluster.
AWS exposes the correct broker list through get-bootstrap-brokers. Use the SASL/SCRAM-specific broker string, not the plaintext or IAM value.
Required Kafka Client Properties
For MSK with SASL/SCRAM, the core client settings are security.protocol=SASL_SSL, the appropriate SCRAM mechanism, and a JAAS configuration containing the username and password.
If your cluster is configured for SCRAM-SHA-256, change the mechanism accordingly. The rest of the setup is the same.
Producer Example
Once the properties are correct, producing is no different from any other Kafka Java client.
This code is runnable if the broker list, topic, and credentials are valid.
Consumer Example
Consumers use the same security properties plus deserializers and a group id.
Where Failures Usually Come From
When this setup fails, the problem is often outside the Java code. If the client is not inside the right VPC or cannot reach the brokers through peering, VPN, Transit Gateway, or public access, authentication never gets a chance to succeed.
Another frequent issue is choosing the wrong bootstrap broker string. MSK publishes different values for TLS, IAM, and SASL/SCRAM clients. The names are similar enough that copy-paste mistakes are common.
Certificate trust can also break connections. Because SASL/SCRAM on MSK uses TLS, your Java runtime must trust the broker certificate chain. In most standard environments the default trust store is enough, but locked-down corporate runtimes sometimes require extra work.
Handling Secrets More Safely
Hardcoding the username and password is acceptable for a minimal example but not for production. A better pattern is to fetch credentials from environment variables or a secret manager and build the JAAS config string at startup.
That keeps credentials out of source control while still using the standard Kafka client.
Common Pitfalls
The biggest pitfall is mixing authentication modes. IAM examples use callback handlers and different mechanisms, but those settings do not belong in a SASL/SCRAM client.
Another common error is using PLAINTEXT or SASL_PLAINTEXT instead of SASL_SSL. MSK's SASL/SCRAM setup expects TLS encryption with the authentication flow.
Developers also forget that broker connectivity is an infrastructure concern. Security groups, route tables, DNS resolution, and cluster access settings are just as important as the Java properties file.
Finally, avoid pasting credentials into exception logs or debugging output. SASL configuration strings often contain the password in plain text.
Summary
- Use the MSK broker list returned for
BootstrapBrokerStringSaslScram. - Configure Java clients with
security.protocol=SASL_SSLand the matching SCRAM mechanism. - Producer and consumer code look normal once the security properties are correct.
- Most failures come from network access, wrong broker strings, or bad credentials.
- Keep usernames and passwords out of source code in production systems.
Related reading
- How to connect to multiple clusters in a single Kafka Streams application?
- how to connect to rabbitmq using javascript without nodejs
- How to consume from Kafka Spring Cloud Stream by default and also consume a Kafka message generated by the confluent API?
- How to Consume from specific TopicPartitionOffset with Confluent.Kafka in .Net
- how to control access for pods/exec only in kubernetes rbac without pods create binded?
- How to control user access for Kafka Topics?
- How to construct a relative path in Java from two absolute paths (or URLs)?
- How to convert / cast long to String?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.