This repository is a companion to a blogpost on the Neon Blog. It provides a template for getting started with Neon and Debezium to perform Change Data Capture.
Using Change Data Capture enables you to stream database changes to messaging infrastructure such as Kafka, Redis, and Pub/Sub systems without suffering from data inconsistency caused by dual writes. Downstream systems can consume these messages to enable Event-Driven Architectures.
- Obtain a Neon Postgres database.
- Obtain a Redis instance from Upstash.
- Define environment variables.
- Start Debezium Server.
- Confirm that changes are streamed to Redis.
- Sign up to console.neon.tech and create a project to obtain a serverless Postgres database.
- Enable Logical Replication for your Neon project.
- Create a table and insert data using Neon's SQL Editor:
CREATE TABLE playing_with_neon(id SERIAL PRIMARY KEY, name TEXT NOT NULL, value REAL); INSERT INTO playing_with_neon (name, value) VALUES ('Mario', random()), ('Peach', random()), ('Bowser', random()), ('Luigi', random()), ('Yoshi', random());
- Sign up and create a Redis database on console.upstash.com with the following settings:
- Name: neon-debezium
- Type: Regional
- Region: Select the region closest to your Neon Postgres database.
- TLS (SSL) Enabled: Yes
- Eviction: Yes
- Copy the
.env.example
to a file named.env
- Replace the Postgres connection parameters in
.env
with your own values from Neon - Replace the Redis connection parameters with your own values from Upstash.
Start a Debezium Server container, passing the .env
file and
application.properties
to it:
docker run --net neon-debezium-redis \
--rm \
--name debezium-server \
--env-file=.env \
-v $PWD/debezium:/debezium/conf \
debezium/server:2.5.1.Final
A few moments after Debezium starts, you should see that it prints logs that
confirms it has performed an initial snapshot of the playing_with_neon
table,
and is now listening for WAL changes.
Visit the Data Browser for your Redis instance in Upstash and you should see
that a debezium.public.playing_with_neon
Redis stream
has been created, and contains records corresponding to INSERT
events from
your playing_with_neon
table.
Perofrming more INSERT
or UPDATE
operations will result in new messages appearing in
the Redis stream.
A sample application is included in the stream-consumer/ folder. It uses the
Redis Node.js client to consume the stream of database changes in the
debezium.public.playing_with_neon
Redis key, and create a sum of "scores" for
each player in Redis.
For example, performing the following two INSERT
operations would result in a
sum:mario
key being created in Redis with a value of 2.1
:
INSERT INTO playing_with_neon (name, value) VALUES ('Mario', '1.5')
INSERT INTO playing_with_neon (name, value) VALUES ('Mario', '0.6')
To run the Node.js application:
- Create a copy of the
.env.example
named.env
in thw stream-consumer/ folder. - Replace the sample values with your Upstash Redis connection parameters. Note that
rediss://
is the correct protocol to use if SSL is enabled for your Upstash Redis instance. - Run
npm install
to install dependencies. - Run
npm run
to start the application.
Once the application starts it will continuously update Redis keys as INSERT
events are written to the stream key by Debezium.