CDC with Cassandra and Pulsar
Change Data Capture (CDC) is a design pattern used in software development to capture and propagate changes made to data in a system. The CDC pattern is commonly used in real-time data streaming applications to enable near-real-time processing of data changes. In a typical CDC implementation, a change to a row of data (insert, update, delete) is detected and recorded. The change (or mutation) is made available to downstream systems as an event for further processing. This allows applications to react quickly to changes in the data while not adding unneeded load on the data store, enabling real-time data processing and analytics.
Before we get into the specifics of CDC, let’s first understand the resources needed to complete the flow.
Throughout this document, "Source Connector" will refer to the CDC Source Connector component, while "source connector" refers to any other source connector in a Pulsar deployment.
Source connectors in Apache Pulsar are responsible for ingesting data from external sources into the Pulsar system. They can be used to collect data from a variety of sources including databases, message queues, and file systems. When the source connector “sees” data, it streams the data to a Pulsar topic. This enables users to easily integrate data from disparate sources into their Pulsar-based applications. Source connectors make it easy to ingest, process, and analyze large volumes of data from a variety of sources into Pulsar.
Pulsar offers extensible APIs where developers can use a defined interface to develop their own connector. The interface takes much of the boilerplate burdens away from a developer and gets them right to the purpose of the connector. Creating a connector means adding in the know-how to work with data from the source and adapt it to produce a compliant message with the Pulsar client.
As you’ll learn in the next section, among the processes needed to capture change data, the Cassandra Source Connector is a very important part. To run a source connector, you provide configuration about what data will be selected, how to connect with the upstream system, and the destination topic for the new message. The source connector takes care of producing the message. Pulsar source connectors run as Pulsar functions within the cluster, so many of the features of functions apply (like the number of instances to run and how to configure the function instance running environment). Metrics and logs for a source connector are automatically made a part of the cluster.
Monitoring source connectors
Monitoring a source connector includes two areas: health and performance. Every connector in Pulsar emits basic metrics about its health, including stats like the number of records received from the source, and the number of messages written to the destination topic. Connectors also emit debugging metrics like the number of exceptions thrown by the source. Refer to the connectors area of Pulsar metrics↗ for a complete list and explanation of metrics.
Performance metrics include health metrics as well as specific knowledge about the source. The Cassandra Source Connector includes quite a few performance metrics. Refer to the Monitoring CDC for Cassandra reference.
Source connector logs
Most Pulsar source connectors emit logs that show lifecycle events as well as custom events specific to the connector type. All logs are handled the same way core cluster logs are handled. By default, they are written to the console and collected by log4j destinations. If you are using function workers, you can access log files on their disk. Refer to Pulsar’s connector debugging guide↗ for more information.
The Apache Pulsar schema registry is a feature of a Pulsar cluster that manages the schemas of messages sent and received on Pulsar topics. In Pulsar, messages are stored as bytes. Schemas provide a way to serialize and deserialize messages with a particular structure or type, allowing for interoperability between different systems.
The schema registry in Pulsar stores and manages schema definitions for all message types sent and received in Pulsar. The schema registry enforces schema compatibility rules, such as requiring a producer to send messages that conform to a certain schema, or rejecting messages that don’t match the schema.
Schemas follow a primitive or complex type. Primitive schemas are simple data types like bool, int, string, and float. Because Pulsar is written in Java that is where the primitives are based. When a different client runtime is used, a conversion may need to be done. Refer to the Pulsar primitive types table↗ for a full reference.
Complex schemas introduce a more structured way of messaging. The two types of complex messages are KeyValue and Struct. KeyValue is JSON formatted text that offers a separation of custom labels and their values. Struct is a custom class definition set as Avro, Json, or Protobuf.
KeyValue offers an interesting way to encode a message called “Separated”. This option separates a message key and the message payload. This in turn has the option to store message key information as a different data type than the message payload. It also offers special compression capabilities. CDC takes advantage of separating KeyValue messages when it produces both the event and data topic. Learn more in the "CDC for Cassandra Events" reference.
Namespace schema configurations
In the context of CDC there are a few schema configurations of note. All of these are specific to the namespace where the event and data topics are logically located.
schema-compatibility-strategy: this setting instructs the Pulsar broker how to handle new schemas introduced to existing topics by producers. This is relevant to CDC when a table’s design is changed. For example, if a new column is added, the registered schema is changed to include that new value. The chosen schema-compatibility-strategy decides if the namespace will allow this. If schema validations are enabled, this option decides what strategy is used. Pulsar’s default strategy is "FULL" which means existing optional table columns can be modified. Learn more about the different types of strategies in the Pulsar docs↗.
allow-auto-update-schema: given the compatibility strategy, this setting is a flag that determines if an update to the schema is generally allowed. CDC sets this to ‘true’ so changes in a table’s design can automatically propagate to the topic.
schema-autoupdate-strategy: when auto update is enabled (set to true) this setting directs the Broker how to ensure consumers of a topic are able to process messages. If a consumer attempts to connect with a schema that does not match the current schema, this strategy will decide if it is allowed to receive messages. CDC sets this to 'BACKWARDTRANSITIVE', which means if optional table columns have been added or a column has been removed, the old schema is allowed.
schema-validation-enforce: this flag limits how producers and consumers are allowed to be configured. When enabled (true) producer and consumer clients must have a schema set before sending the message. When disabled (set to false) Pulsar will allow producers and consumers without a set schema to send or receive messages. CDC disables this option (set to false), so producers and consumers do not have to know the message schema ahead of time.
The Cassandra CDC agent is a process running on each node in a Cassandra cluster that watches for data changes on tables that have enabled the CDC feature. Using Cassandra’s commitlog_sync option↗, the agent periodically syncs a separate log in a special “cdc_raw” directory. Each log entry is a CDC event. The CDC agent creates a new event message containing the row coordinates of the changed data and produces the message to a downstream Pulsar cluster. For more information about the agent, how to include its configuration in cassandra.yaml, and event data specifics read the "DataStax CDC for Apache Cassandra® Documentation".
Each table that has CDC enabled also has a corresponding Source Connector in Pulsar. This is unlike the CDC agent where the process runs on each Cassandra node, keeping a log of all table changes. Each table-specific Source Connector subscribes to the events topic the agent is producing messages to. When the connector “sees” a message for its table, it uses the row coordinates within the message to retrieve the mutated data from Cassandra and create a new message with the specifics. That new message is written to a data topic where others can subscribe and receive CDC messages. For more information about the Cassandra Source Connector, its configuration, and how to create it read the "DataStax CDC for Apache Cassandra® Documentation".
A particular advantage in the Source Connector is its deduplication feature. You might have read about Pulsar’s built in deduplication capabilities↗ - this is not utilized in the message flow because CDC needs a finer grain control to detect duplicates. As the CDC agent discovers a new commit log, an authentic identifier is created using the MD5 hash algorithm. That key identifier is added to the event message.
When message consumers like the Source Connector connect to the event topic, they establish a subscription type. Pulsar has 4 types of subcriptions: exclusive, shared, failover, and key_shared. In a typical CDC flow, the Source Connector will have multiple instances running in parallel. When multiple consumers are a part of a key_shared subscription, Pulsar will deliver a duplicate hash key to the same consumer no matter how many times it’s sent.
When a Cassandra cluster has multiple hosts (with multiple commit logs), and they all use the same mutation to calculate the same hash key, the same consumer will always receive it. Each Source Connector keeps a cache of hashes it has seen and ensures duplicates are dropped before producing the data message.
Learn more about Pulsar’s key_shared subscription type in the Pulsar documentation↗.
Now that you understand the different resources used in this CDC pattern, let’s follow the flow to see how a CDC message is produced.
Create a Pulsar tenant to hold CDC messages.
Create a namespace (or use the “default”).
Create a topic for event messages.
Create a topic for data messages.
Start the CDC source connector in Pulsar by setting the destination topic (aka the data topic), the event topic, and Cassandra connection info (along with other settings).
Configure the Cassandra change agent with a working directory and the pulsar service URL (along with other settings) in the Cassandra node (restart is required).
Create a Cassandra table and enable CDC.
Insert a row of data into the table.
The change agent will detect a mutation to the table and write a log.
The log will be converted to an event message and written to the events topic.
The source connector will complete the flow by producing a final change message to the data topic.
With a solid understanding of the resources and flow used within the CDC pattern, let’s move on to the next section to learn about Cassandra table schema evolution with CDC.