Spring Cloud Stream
Kafka Producers
Kafka Consumers
KStreams Configuration
Value.Subject.Name.Strategy

How can we configure value.subject.name.strategy for schemas in Spring Cloud Stream Kafka producers, consumers and KStreams?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

To effectively manage schema definitions and ensure compatibility across various components utilizing the Kafka platform in Spring Cloud Stream, it is critical to understand the configuration of value.subject.name.strategy for Kafka producers, consumers, and Kafka Streams (KStreams). This configuration plays a key role in schema serialization and deserialization processes, impacting how schemas are associated with Kafka messages.

Understanding value.subject.name.strategy

In Kafka, the value.subject.name.strategy property determines the strategy for deriving the subject name for the value schema in the Schema Registry. A subject refers to the name under which the schema is registered and stored in the Registry. The strategy affects how producers write data and how consumers read data, ensuring compatibility and version management of schemas.

Default Strategy

By default, Kafka uses the TopicNameStrategy, which means that the subject name is the same as the Kafka topic name appended with -value for the value part of the message. This strategy works well in simple scenarios where each topic has a single schema type for its messages.

Alternatives

  1. RecordNameStrategy: This strategy uses the fully-qualified name of the Avro record type as the subject name. This allows multiple types of Avro records to be produced to the same topic, each with its own independent versioning.
  2. TopicRecordNameStrategy: Combines both the topic name and the record name. This allows for the topic to have different types of records (schemas) while maintaining a link to the topic name which enhances traceability and granularity in schema management.

Configuring value.subject.name.strategy

In Spring Cloud Stream, the configuration can be set up in application.properties or application.yml for Kafka producers, consumers, and KStreams applications.

Producers

properties
spring.cloud.stream.kafka.binder.producer-properties.value.subject.name.strategy=io.confluent.kafka.serializers.subject.TopicRecordNameStrategy

Consumers

properties
spring.cloud.stream.kafka.binder.consumer-properties.value.subject.name.strategy=io.confluent.kafka.serializers.subject.RecordNameStrategy

Kafka Streams

For applications using Kafka Streams, the configuration would typically look like this:

properties
spring.cloud.stream.kafka.streams.binder.configuration.value.subject.name.strategy=io.confluent.kafka.serializers.subject.TopicNameStrategy

Examples

Let's consider you have an Avro schema for User and another for Product. You can use the RecordNameStrategy to allow both these record types to be sent over the same Kafka topic without collision in the schema registry.

Detailed Configuration

Kafka configuration can sometimes become quite complex, especially when dealing with multiple environments or nuanced schema management requirements. Below is a table summarizing the potential configurations and their uses:

StrategyUse CaseProsCons
TopicNameStrategySingle schema type per topic.Simple, easy to manage.Limited flexibility.
RecordNameStrategyMultiple schema types per topic, identified by schema name.Flexible schema management.More complex to trace in large topics.
TopicRecordNameStrategyMultiple schema types per topic, linked to specific topics.Balance between flexibility and traceability.Potentially verbose, especially with many topics.

Best Practices and Considerations

  1. Compatibility Settings: Always accompany this with compatible data compatibility settings in the Schema Registry to avoid runtime errors and data incompatibility.
  2. Version Management: When using RecordNameStrategy or TopicRecordNameStrategy, you must ensure proper version management as independent evolution of schemas can lead to complexities.

Conclusion

Understanding and correctly configuring value.subject.name.strategy is crucial when working with Kafka in a microservices architecture. As you integrate more services and handle more diverse data, selecting the right strategy allows you not only to manage your schemas effectively but also to ensure robust, scalable data interaction across your services.


Course illustration
Course illustration

All Rights Reserved.