CDC in action
[debezium
CDC
postgres
sql
redis
]
This article is a work in progress. Proceed with caution. Please, don’t slip or trip over!
This article wraps up our discovery of CDC.
Based on what we already talked about in the series so far, we already have a solid plan for implementing data change propagation between beehive-external
and beehive-control
:
- The backend in our case will be a PostgreSQL WAL - which, as we know, can be used as CDC
- Any interesting data change will be written to the
outbox
table as a part of the same transaction which did the actual change - We’re going to insert a new aggregate, corresponding to the change, unambiguously understood by everone
- We’re subscribed to the
outbox
table changes by creating a dedicated PostgreSQL publication. This publication will be further managed by Debezium, which will push an event to the Redis Stream.
Let’s get to work!
Beehive Aggregates
At this point, you might be wondering where are DDD aggregates if we haven’t defined them explicitly yet. Let’s address that now!
DDD encourages us to think in terms of business events rather than simple CRUD operations. This is because CRUD operations are usually beyond comprehension of the other components, therefore unnecessarily coupling components together. In the context of our beehive management system, key lifecycle events for a hive could be represented by - but not limited to - the following aggregates:
1. HiveEstablished
Signifies the creation or establishment of a new beehive. The essential information we’d want to capture at this point would likely include a unique identifier for the hive and its initial location.
Example:
{
"hiveId": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"location": {
"latitude": 48.7654,
"longitude": 16.8765
},
"establishmentDateTime": "2025-04-19T11:09:00Z"
}
2. HiveRelocated
Indicates that an existing beehive has been moved from one location to another. We’d need to identify the hive that was moved and its new location.
Example:
{
"hiveId": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"newLocation": {
"latitude": 48.8765,
"longitude": 16.9876
},
"previousLocation": {
"latitude": 48.7654,
"longitude": 16.8765
},
"relocationDateTime": "2025-04-19T11:09:00Z"
}
3. HiveDisposed
Signifies the end of a beehive lifecycle. We primarily need to identify the hive that was disposed of, and optionally, the reason of disposal.
Example:
{
"hiveId": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"disposalReason": "PEST_INFESTATION",
"disposalDateTime": "2025-04-19T11:09:00Z"
}