Change data capture: turning a database into a stream
Keeping other systems in step
Section titled “Keeping other systems in step”A search index, a cache and an analytics store all have to follow the database that holds the truth. Each time a row changes, something has to tell them. There are three common ways to do it, and they lose different things.
Dual write. The application writes the row, then writes to the other system, itself. No transaction spans the database and the other system, so a crash between the two lines leaves the row saved and the other system never told, and nothing records that it should have been. If the second write fails, the two stores disagree and the application must notice and repair it.
Polling. A job runs on a schedule and queries for the rows that changed, for example those whose updated_at is later than the last run. It needs nothing from the application, and it has two blind spots. It sees a row as it is now, so a row updated 5 times in a minute produces one change for the poller, and 4 intermediate states never appear. And a row that was deleted is no longer there to match the query. The delay is also up to the polling interval: a poll every 60 seconds makes the mean wait 30 seconds, by the same arithmetic as the hourly batch of the first lesson.
Log-based capture. The database already writes down every change in order, because it needs that log to recover from a crash and to feed replicas. PostgreSQL’s documentation describes logical decoding as extracting the persistent changes to the tables from the write-ahead log into a form that can be read without knowing the database’s internals, and a replication slot as a stream of changes that can be replayed to a client in the order they were made. Debezium’s connector for PostgreSQL reads that stream: it generates a data change event for each row-level insert, update and delete, with an op field that tells which one it was, and d is a delete.
Mental model. The database keeps a diary of everything it changes. Dual write asks the application to also tell someone, and hopes it remembers. Polling asks the database now and then what looks different. Log-based capture reads the diary.
Sort some statements by the way they describe.
Challenge
How are the changes captured?
Three ways to pass database changes to other systems: the application writes twice, a job polls for changed rows, or a connector reads the transaction log. Put each line under the way it describes.
Sorting needs JavaScript.
Categories
- Dual write
- The application writes to the database, then to the other system, itself.
- Polling
- A job queries the tables on a schedule for what changed.
- Log-based CDC
- A connector reads the changes from the database’s own log.
Check your answers at least once before looking at the answers.
What the events give you, and what they cost
Section titled “What the events give you, and what they cost”Because the events come from the log, they cover every writer to the database, whatever program made the change, and they arrive in the order of the log. The application does not change. Deletes are visible, and so is every intermediate state of a row, which makes the events a good input for the streams of the earlier lessons: they have a key, an order within the key, and a position that a consumer can commit.
They are not free of the problems of those lessons. Debezium documents at-least-once delivery. The connector records its position in the log from time to time, not after every event, and after a restart it resumes from the last position it recorded, so some events can arrive twice. PostgreSQL says the same of its slot: after a crash the slot might return to an earlier position, and recent changes are sent again. The consumers of captured changes have to be idempotent, as in the previous lesson.
For the other side of the problem, when the application has to publish an event and save a row together, the transactional outbox is a common answer: the application writes the event into a table in the same transaction as the row, and a relay, which can be a poller or a log reader, forwards it. The messaging module covers it. The point here is that the log reader, not the application, is what makes the forwarding reliable.
What happens if the connector is down for a day?
Section titled “What happens if the connector is down for a day?”A slot remembers where its reader got to, and PostgreSQL’s documentation says slots prevent the removal of the write-ahead log that a reader still needs. So a connector that is stopped does not make changes disappear: they wait. It follows from that, as a direct consequence and not as a measured figure, that the database has to keep that log for as long as the connector is away, and that a long stop makes the log it keeps grow. When the connector comes back, it has a backlog to read, and the lag is the same quantity as in the first lesson. A captured stream has to be watched like any other.
- A search index, a cache or an analytics store has to follow the database. Dual write can lose the second write, polling sees only the current state, and reading the log sees every change.
- Polling turns 5 updates in a minute into 1 change, never sees a deletion, and a poll every 60 seconds adds 30 seconds of delay on average.
- Log-based capture reads the write-ahead log through logical decoding. Each insert, update and delete becomes an event in the order of the log, and an
opfield says which. - The connector’s position is recorded from time to time, so after a restart some events arrive twice. The consumers must be idempotent.
- A stopped connector makes the database keep its log until the connector reads it. Watch the lag of a captured stream like any other.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.