Configuration
Configure Kafka connection and event routing through environment variables.Basic Connection
.env
For production environments, specify multiple brokers for high availability. The client will automatically connect to available brokers.
Global vs Instance Topics
Configure Kafka to work in two modes:- Per-Instance Topics
- Global Topics
Each instance publishes to its own topics:Creates topics like:
.env
evolution.my_instance.messages.upsertevolution.my_instance.qrcode.updatedevolution.my_instance.connection.update
Topic Configuration
.env
Consumer Configuration
.env
SASL Authentication
Secure your Kafka connection with SASL authentication:.env
- PLAIN
- SCRAM-SHA-256
- SCRAM-SHA-512
Simple username/password authentication:
SSL Configuration
Enable SSL/TLS encryption for Kafka connections:.env
For development with self-signed certificates, you can set
KAFKA_SSL_REJECT_UNAUTHORIZED=false, but this is not recommended for production.Available Events
Configure which events are sent to Kafka:- Instance Events
- Message Events
- Contact Events
- Chat & Group Events
- Other Events
.env
Per-Instance Configuration
Configure Kafka events for a specific instance via API:Message Format
Messages published to Kafka include both headers and body:Message Headers
Message Body
The message key is set to the instance name for per-instance topics, or
instanceName-event for global topics. This ensures proper partitioning.Consuming Events
Examples of consuming events from Kafka:Connection Resilience
Evolution API implements automatic reconnection:- Initial delay: 5 seconds
- Maximum attempts: 10
- Exponential backoff: Delay doubles each attempt
- Retry on send: Up to 3 attempts per message
The producer is configured with
idempotent: true and maxInFlightRequests: 1 to ensure exactly-once semantics and message ordering.Best Practices
1
Choose Appropriate Partitions
Set
KAFKA_NUM_PARTITIONS based on your throughput needs:- More partitions = higher parallelism
- Start with 3-6 partitions per topic
- Can increase later, but can’t decrease
2
Set Replication Factor
For production:Protects against broker failures.
3
Use Consumer Groups
Multiple consumers in the same group share the load:
4
Handle Deserialization Errors
Always wrap JSON parsing in try-catch:
5
Monitor Lag
Set up monitoring for consumer lag to detect processing issues early.
Troubleshooting
Connection timeout
Connection timeout
- Verify Kafka brokers are running and accessible
- Check firewall rules allow connections to broker ports
- Increase
KAFKA_CONNECTION_TIMEOUTif network is slow - Verify
KAFKA_BROKERSaddresses are correct
SASL authentication failed
SASL authentication failed
- Verify credentials in
.envfile - Check SASL mechanism matches Kafka server configuration
- Ensure user has proper ACLs in Kafka
- Review Kafka server logs for authentication errors
Messages not appearing in topics
Messages not appearing in topics
- Check that specific events are enabled in configuration
- Verify
KAFKA_ENABLED=true - If
AUTO_CREATE_TOPICS=false, manually create topics - Check Evolution API logs for Kafka errors
- Verify instance is connected to WhatsApp
Consumer lag increasing
Consumer lag increasing
- Increase number of consumers in consumer group
- Optimize message processing code
- Increase number of partitions (requires topic recreation)
- Check for slow downstream dependencies
SSL certificate errors
SSL certificate errors
- Verify certificate paths are correct
- Check certificate validity dates
- Ensure CA certificate is properly formatted
- For testing, temporarily set
KAFKA_SSL_REJECT_UNAUTHORIZED=false