When we moved off Sqoop, a few MySQL tables switched to change data capture: the application emits an event every time it writes to the table, and a job applies those events to a copy in our data lake. It’s cheaper than copying a big, busy table every day, and the copy is fresher. What it doesn’t give you is any guarantee that the copy matches the source. Every change has to make it from the application, through our event pipeline, into the table, and nothing checks that it did.
So this month I checked. The answer was “almost”, and most of the gap came from what was sent into the pipeline, not from the pipeline losing things.
The method: diff two snapshots
I used a table of per-user attributes: a simple key-value schema, a few columns, lots of writes. By now our Spark JDBC import could copy the whole table in reasonable time, which made the test possible:
- Import a full snapshot of the source table.
- Import another one a day later.
- Diff them to get every row that was deleted, added or changed in between.
- For each of those rows, look for a matching change event in the same period.
The diffs are two set operations:
-- Rows that disappeared between the snapshots
CREATE TABLE deleted_rows AS
SELECT id, user_id, attr_key FROM snapshot_day1
EXCEPT ALL
SELECT id, user_id, attr_key FROM snapshot_day2;
-- Rows that are new or changed
CREATE TABLE changed_rows AS
SELECT * FROM snapshot_day2
EXCEPT
SELECT * FROM snapshot_day1;
Then a left join against the change events, keeping the rows with no match:
WITH delete_events AS (
SELECT user_id, attr_key
FROM change_events
WHERE source_table = 'user_attributes'
AND operation = 'd'
AND received_at >= :first_snapshot_time
)
SELECT d.attr_key, count(*) AS missing
FROM (SELECT DISTINCT user_id, attr_key FROM deleted_rows) d
LEFT JOIN delete_events e
ON e.user_id = d.user_id AND e.attr_key = d.attr_key
WHERE e.user_id IS NULL
GROUP BY d.attr_key
ORDER BY missing DESC;
Grouping the misses by attribute turned out to be the useful part. It turned one big number into a short list of specific causes.
Deletes: 67% missing, then 3
Between the two snapshots, 130,790 rows were deleted. 87,381 of them, about two thirds, had no delete event. That looked like a broken pipeline, but almost all of the misses were for just two attributes.
Change events are emitted by hooks in the application’s functions for writing user attributes. Those two attributes are maintained by a log-processing script that writes to the table with direct SQL. The hooks never run, so no events are ever sent for them. Excluding those two attributes left 41 missing deletes.
For 37 of those 41, the source table had several rows for the same user and key. The hook that emitted delete events checked that exactly one row had been deleted, $deleted == 1, so deleting several matching rows at once sent nothing. The fix was one character: $deleted >= 1.
Of the last four, one hadn’t actually changed in any copy of the data. That left three deletes out of 130,790 with no explanation.
Updates: 1.6% missing, then 0.002%
Inserts and updates followed the same pattern. Of 2,094,496 new or changed rows, 33,742 (about 1.6%) had no matching event. 28,239 of those were the same two direct-write attributes.
Almost all of the remaining 5,503 had something else in common: long values. Change events travel through our event pipeline as HTTP GET requests, with the data in the query string, and requests that large get rejected. Any attribute with a value longer than about 4,000 characters could never be captured. Excluding those left 41 rows out of almost 2.1 million, about 0.002%.
I went through the code for every attribute in that list, and all of them were written through the normal functions, so their hooks should have fired. The most likely explanation is that the events were sent and lost somewhere in transit. Our event pipeline is built for analytics, where losing a tiny fraction of events doesn’t matter. Using it to replicate a table makes that fraction visible.
What we did with it
- Fixed what was fixable. The multi-row delete bug was a real bug, and fixing it was trivial once we knew about it.
- Wrote down what isn’t. Attributes written with direct SQL aren’t captured, very long values aren’t captured, and a small fraction of events, under 0.005% in this test, are lost. For aggregate analysis that’s noise. For decisions about one specific user, such as checking a preference before sending someone an email, it isn’t, and people using these tables need to know that. We posted the known accuracy where the people who use the tables would see it.
- Reconsidered the choice. This table moved to change data capture because copying it with Sqoop caused locking problems on the database. Our Spark JDBC import handles it comfortably, so a full daily import is probably the better option again. The other table on this pipeline holds per-site settings, and those are in a separate table for every site. A full import of hundreds of millions of small tables isn’t practical, so that one stays on change data capture.
For that settings table, the errors partly correct themselves. A missing event leaves a value stale only until the next change to the same setting arrives. Looking at one frequently changed setting over a week, about 60% of the sites that changed it changed it more than once, and for those a single lost event leaves no lasting error.
Takeaways
- Change data capture built on application hooks only sees writes that go through those hooks. Scripts, migrations and manual fixes that write directly to the database are invisible to it. Capturing changes from the database’s own log, like the MySQL binlog, avoids that class of problem, if you have access to it.
- Check what your transport can carry. Packing changes into a channel designed for small analytics events puts a size limit on every value, and nothing tells you when a change is too big.
- Audit it against the source. Two snapshots and two
EXCEPTqueries are cheap, and they tell you how far the copy has drifted instead of leaving you to assume it hasn’t. - Break the misses down. “67% of deletes missing” is alarming and tells you nothing. The same number grouped by key pointed straight at one script and one bug.
Leave a Reply