This guide shows you how to set up Postgres change data capture (CDC) and stream changes to Kafka using Sequin.

With Postgres data streaming to Kafka, you can trigger workflows, keep services in sync, build audit logs, maintain caches, and more.

By the end of this how-to, you’ll have database changes flowing to a Kafka topic.

Prerequisites

If you’re self-hosting Sequin, you’ll need:

  1. Sequin installed
  2. A database connected
  3. A Kafka cluster ready to go

If you’re using Sequin Cloud, you’ll need:

  1. A Sequin Cloud account
  2. A database connected
  3. A Kafka cluster ready to go

Basic setup

Prepare your Kafka cluster

You’ll need a Kafka cluster and topic ready for Sequin to stream changes to. You can use either a local cluster for development or a cloud-hosted Kafka service in production.

Local development with Docker

For local development with Sequin, you can quickly spin up a Kafka cluster using Docker Compose:

Start the cluster:

docker-compose up -d

Create a topic for Sequin to publish to:

docker exec -it <kafka-container-id> kafka-topics.sh \
  --create \
  --topic my-topic \
  --bootstrap-server kafka:9093 \
  --partitions 1 \
  --replication-factor 1

Production Kafka

For production environments, Sequin works with major managed Kafka providers as well as self-hosted Kafka clusters. To prepare your Kafka cluster for Sequin, you’ll need to:

  1. Create a dedicated topic for Sequin to publish to
  2. Create credentials (username/password) for Sequin to use
  3. Configure appropriate ACLs to allow Sequin to publish to your topic

Sequin currently supports SASL PLAIN, SCRAM-SHA-256, and SCRAM-SHA-512 authentication mechanisms with TLS encryption. Custom client certificates for verifying TLS connections are coming soon, let us know if you need them.

Once your cluster is ready, make note of:

  • Bootstrap server addresses
  • Topic name
  • Authentication credentials
  • Whether TLS is required

You’ll need these details when configuring your Kafka sink in the next section.

Create Kafka sink

Navigate to the “Sinks” tab, click “Create Sink”, and select “Kafka Sink”.

Configure the source

1

Select source table

Under “Source”, select the table you want to stream data from.

2

Choose message type

Specify whether you want to receive changes or rows from the table.

If you’re not sure which to choose, you can start with the default, “Changes”.

3

Specify filters

If you selected changes, in “Records to process”, you can indicate whether you want to receive insert, update, and/or delete changes.

You can also specify SQL filters to narrow down the events you want to receive. For example, if you only want to receive events for subscriptions that currently have an mrr greater than $100, you can add a filter on mrr > 100.

4

Specify backfill

You can optionally indicate if you want to backfill of all or a portion of the table’s existing data into Kafka. Backfills are useful if you want to use Kafka to process historical data. For example, if you’re materializing a cache, you might want to warm it with existing rows.

You can backfill at any time. If you don’t want to backfill, toggle “Backfill” off.

5

Specify message grouping

DOCTODO: Do we let them specify a message key?

Under “Message grouping”, you’ll most likely want to leave the default option selected to ensure events for the same row are sent to Kafka in order.

Configure Kafka

1

Enter Kafka connection details

Fill in your Kafka connection details:

  • Hosts (required): A comma-separated list of host:port pairs (e.g., kafka1.example.com:9092,kafka2.example.com:9092)
    • This can be either the full list of Kafka brokers or just the bootstrap server
  • Topic (required): The Kafka topic to publish messages to (max 255 characters)
  • Username: Username for SASL PLAIN authentication (optional)
  • Password: Password for SASL PLAIN authentication (optional)
  • TLS: Enable TLS/SSL encryption for the connection
2

Create the sink

Give your sink a name, then click “Create Kafka Sink”.

Verify & debug

To verify that your Kafka sink is working:

  1. Make some changes in your source table
  2. Verify that the count of messages for your sink increases in the Sequin web console
  3. Using your Kafka consumer tools, check your topic:
    kafka-console-consumer --bootstrap-server localhost:9092 --topic your-topic --from-beginning
    
    You should see the messages from Sequin appear in the topic.

If messages don’t seem to be flowing:

  1. Click the “Messages” tab to view the state of messages for your sink
  2. Click any failed message
  3. Check the delivery logs for error details, including any Kafka connection errors

Next steps

  • Setup a processor

    Now that your Postgres data is flowing into Kafka, you can setup a consumer group to read from the topic and process the data.

    Refer to the Kafka sink reference for the shape of messages that Sequin will publish to Kafka.

  • Deploy your implementation

    When you’re ready to deploy your implementation, see “How to deploy to production”.

  • Advanced configuration

    For more about how Kafka sinks work, see the Kafka sink reference.

Was this page helpful?