This demonstration shows how you can use Debezium to create, update, and delete data cached in Redis.
+------------+ +-----------+ +---------+
| | | | | |
| PostgreSQL | ---> | Debdezium | ---> | Redis |
| | | | | |
+------------+ +-----------+ +---------+
Prerequisites
- Docker
- To run Postgresl & Redis
- Java Developer Kit (8 or later)
- Apache Maven
-
Get the source code from the repository
> git clone https://github.com/redis-developer/sql-cache-invalidation-debezium.git > cd sql-cache-invalidation-debezium -
Start Redis
The Java Spring application use Jedis to connect to Redis. The connection string is located in the
application.propertiesfile.Open a new terminal and run the command:
> docker run -it --rm --name redis -p 6379:6379 redis -
Build and Run PostgreSQL database
In the sql-cache-invalidation-debezium run the following commands:
> cd posgresql > docker build -t postgresql-cdc . > docker run -it --rm --name postgresql-cdc -p 5432:5432 -e POSTGRES_PASSWORD=password postgresql-cdcThe PostreSQL instance is started with the configuration options needed by the Debezium Postgres Connector.
-
Connect to PostgreSQL & create tables
Use the Docker container to connect to PostgreSQL.
> docker exec -it postgresql-cdc bashConnect to PostgreSQL
bash-5.0# psql -U postgresCreate table and insert rows
CREATE TABLE customers ( customer_id serial PRIMARY KEY, username VARCHAR ( 50 ) UNIQUE NOT NULL, first_name VARCHAR ( 50 ) NOT NULL, last_name VARCHAR ( 50 ) NOT NULL, email VARCHAR ( 255 ) UNIQUE NOT NULL ); CREATE TABLE support_level ( level VARCHAR (30) UNIQUE NOT NULL ); CREATE TABLE orders ( cust_id INT NOT NULL, order_type VARCHAR (10) NOT NULL, order_date DATE DEFAULT CURRENT_DATE, comments TEXT, PRIMARY KEY (cust_id, order_type) );
You can check that tables are created using the command:
\dt
-
Insert rows into tables
INSERT INTO customers (username, first_name, last_name, email) VALUES ('jdoe1', 'John', 'Doe', 'jdoe1@demo.com');
-
Build and run the Java application
The Java application is a SpringBoot application that exposes a REST endpoint to start and stop the Debezium service. This service captures tthe connector continuously captures row-level changes that insert, update, and delete database content and that were committed to a PostgreSQL database
Open a new terminal in the
sql-cache-invalidation-debeziumdirectory and run the following commands:> cd debezium-redis-demo > mvn clean spring-boot:run -
Start the Debezium server
Open your browser and go to:
-
Insert new rows into tables
INSERT INTO customers (username, first_name, last_name, email) VALUES ('jdoe2', 'John', 'Doe', 'jdoe2@demo.com');
INSERT INTO orders (cust_id, order_type, comments) VALUES(1, 'ONLINE', 'comment for first order' );
INSERT INTO support_level (level) VALUES ('GOLD');
This last row is not synchronized to Redis since the
support_leveltable is not in thetable.include.list. (application.properties)
Note: When you restart the Debezium service, you need to send a transaction to start the replication, so be sure you reset the service using http://localhost:8082/reset if you have anty issue.