<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Quayen</title>
    <description>The latest articles on DEV Community by Quayen (@quayen_support).</description>
    <link>https://dev.to/quayen_support</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4234696%2F0e6bf9a2-58d3-4e06-ac38-8b07e4c896b5.png</url>
      <title>DEV Community: Quayen</title>
      <link>https://dev.to/quayen_support</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/quayen_support"/>
    <language>en</language>
    <item>
      <title>Streaming MySQL to BigQuery with the binlog: how CDC works, and the parts that bite</title>
      <dc:creator>Quayen</dc:creator>
      <pubDate>Mon, 09 Nov 2026 08:12:40 +0000</pubDate>
      <link>https://dev.to/quayen_support/streaming-mysql-to-bigquery-with-the-binlog-how-cdc-works-and-the-parts-that-bite-40jl</link>
      <guid>https://dev.to/quayen_support/streaming-mysql-to-bigquery-with-the-binlog-how-cdc-works-and-the-parts-that-bite-40jl</guid>
      <description>&lt;p&gt;Most teams start syncing MySQL to BigQuery with a nightly dump or a query like &lt;code&gt;SELECT * FROM orders WHERE updated_at &amp;gt; :last_run&lt;/code&gt;. It works until someone asks why deleted orders still show up in the dashboard, or why yesterday's numbers changed. This post walks through how log-based change data capture (CDC) from MySQL to BigQuery works end to end, and the details that decide whether it's correct and affordable.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://quayen.com" rel="noopener noreferrer"&gt;Quayen&lt;/a&gt;, a managed MySQL to BigQuery CDC service. Everything here applies whether you build this yourself with Debezium or use any managed tool.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why polling isn't enough
&lt;/h2&gt;

&lt;p&gt;Query-based syncs on an &lt;code&gt;updated_at&lt;/code&gt; column have three structural problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deletes are invisible.&lt;/strong&gt; A deleted row has no &lt;code&gt;updated_at&lt;/code&gt;; it's simply gone, so the query never returns it and BigQuery keeps it forever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not every write touches &lt;code&gt;updated_at&lt;/code&gt;.&lt;/strong&gt; Bulk updates, migrations and some ORMs skip it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They load production.&lt;/strong&gt; Every run scans an index on your busiest tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MySQL already records every committed change, including deletes, in its &lt;strong&gt;binary log&lt;/strong&gt; so replicas can follow along. Reading that log is what log-based CDC does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: configure the binlog
&lt;/h2&gt;

&lt;p&gt;CDC needs three settings, plus enough retention:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SHOW&lt;/span&gt; &lt;span class="k"&gt;GLOBAL&lt;/span&gt; &lt;span class="n"&gt;VARIABLES&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Variable_name&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'log_bin'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'binlog_format'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'binlog_row_image'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'binlog_expire_logs_seconds'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;log_bin = ON&lt;/code&gt;. On RDS this means automated backups must be on; on Cloud SQL it's point-in-time recovery.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;binlog_format = ROW&lt;/code&gt;. &lt;code&gt;STATEMENT&lt;/code&gt; logs the SQL text, not the resulting row values, so a CDC reader can't know what changed. &lt;code&gt;MIXED&lt;/code&gt; falls back to statements most of the time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;binlog_row_image = FULL&lt;/code&gt;. &lt;code&gt;MINIMAL&lt;/code&gt; logs only changed columns, so your BigQuery rows end up with holes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retention of at least 3 days.&lt;/strong&gt; If the pipeline stops and MySQL purges logs it hasn't read, you get &lt;code&gt;error 1236: Could not find first log file name in binary log index file&lt;/code&gt; and have to re-copy everything. On RDS and Aurora, retention is &lt;code&gt;CALL mysql.rds_set_configuration('binlog retention hours', 72);&lt;/code&gt;, not &lt;code&gt;binlog_expire_logs_seconds&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The replication user needs &lt;code&gt;SELECT&lt;/code&gt; on your schema plus &lt;code&gt;REPLICATION SLAVE&lt;/code&gt; and &lt;code&gt;REPLICATION CLIENT&lt;/code&gt; on &lt;code&gt;*.*&lt;/code&gt;. Nothing else. (If you want to check a server quickly, I made a &lt;a href="https://quayen.com/tools/mysql-binlog-cdc-checker" rel="noopener noreferrer"&gt;free binlog checker&lt;/a&gt; that reads the output of the query above.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: snapshot, then stream from the exact position
&lt;/h2&gt;

&lt;p&gt;Existing rows aren't in the binlog (it's been purged long ago), so a pipeline starts with a snapshot:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a consistent snapshot (&lt;code&gt;START TRANSACTION WITH CONSISTENT SNAPSHOT&lt;/code&gt;) and record the binlog position (file + offset, or GTID set) at that same moment.&lt;/li&gt;
&lt;li&gt;Copy every row of the selected tables into BigQuery.&lt;/li&gt;
&lt;li&gt;Start reading the binlog &lt;strong&gt;from the recorded position&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Changes that happen during a long copy aren't lost: they're in the binlog after the recorded position and are applied afterwards. The ordering rule below makes it safe if a change touches a row that the snapshot already copied.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: land changes in an append-only change log
&lt;/h2&gt;

&lt;p&gt;Don't try to &lt;code&gt;UPDATE&lt;/code&gt; BigQuery row by row as events arrive. BigQuery is built for appends and big set-based statements, and DML quotas will stop you. Instead, append every event to a change-log table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;column&lt;/th&gt;
&lt;th&gt;meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;_op&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;c&lt;/code&gt; insert, &lt;code&gt;u&lt;/code&gt; update, &lt;code&gt;d&lt;/code&gt; delete&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;_seq&lt;/code&gt;, &lt;code&gt;_row&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;binlog position of the event, and the row's index within it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;_ingested_at&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;when it reached BigQuery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;the table's columns&lt;/td&gt;
&lt;td&gt;the row values after the change (before, for deletes)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Appends via the Storage Write API land within seconds, and the change log is useful on its own: it's a full history of every row ("what was this order's status last Tuesday?").&lt;/p&gt;

&lt;p&gt;Two details matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Only save your read position after BigQuery has accepted the batch.&lt;/strong&gt; Then a crash replays a few events instead of losing them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replays must be harmless.&lt;/strong&gt; That's what &lt;code&gt;_seq&lt;/code&gt; is for.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: merge into an up-to-date table
&lt;/h2&gt;

&lt;p&gt;Analysts want a table that looks like MySQL, not an event log. On a schedule, apply the new changes with a &lt;code&gt;MERGE&lt;/code&gt;, keeping only the latest change per primary key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;MERGE&lt;/span&gt; &lt;span class="nv"&gt;`analytics.orders`&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;EXCEPT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ROW_NUMBER&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_row&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;rn&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`analytics_changelog.orders`&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;last_applied_seq&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;MATCHED&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_op&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;MATCHED&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
  &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_seq&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;MATCHED&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_op&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_seq&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ordering by binlog position (not by arrival time) means applying the same batch twice, or overlapping batches, can't produce a wrong row. The &lt;code&gt;s._seq &amp;gt; t._seq&lt;/code&gt; guard protects rows the snapshot copied after a change was already applied.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: handle schema changes and the ugly types
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;New columns:&lt;/strong&gt; when MySQL gains a column, add it to both BigQuery tables before applying events that contain it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type mapping:&lt;/strong&gt; &lt;code&gt;BIGINT UNSIGNED&lt;/code&gt; doesn't fit &lt;code&gt;INT64&lt;/code&gt; (use &lt;code&gt;NUMERIC&lt;/code&gt;), wide &lt;code&gt;DECIMAL&lt;/code&gt;s need &lt;code&gt;BIGNUMERIC&lt;/code&gt;, &lt;code&gt;TIME&lt;/code&gt; can exceed 24 hours so store it as &lt;code&gt;STRING&lt;/code&gt;, and &lt;code&gt;TINYINT(1)&lt;/code&gt; is MySQL's boolean. (&lt;a href="https://quayen.com/tools/mysql-to-bigquery-schema-converter" rel="noopener noreferrer"&gt;Free converter&lt;/a&gt; that applies these rules to a &lt;code&gt;CREATE TABLE&lt;/code&gt;.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Primary keys are mandatory.&lt;/strong&gt; Without one there's no way to match an update or delete to a row.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The part that surprises people: BigQuery cost
&lt;/h2&gt;

&lt;p&gt;With on-demand pricing, a &lt;code&gt;MERGE&lt;/code&gt; that updates or deletes rows is billed for the whole target table it scans (or the partitions it touches). Refresh a 200 GiB table every 5 minutes and that's hundreds of TiB a month. The fixes: refresh less often, partition by a column that's set once (like &lt;code&gt;created_at&lt;/code&gt;), use insert-only merges when a batch has no updates or deletes, and skip tables with no new changes. I wrote up the numbers in a separate post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build or buy
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Debezium + Kafka + a BigQuery sink:&lt;/strong&gt; flexible and open source, but you operate Kafka Connect, schema history, and write the merge logic above yourself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Datastream:&lt;/strong&gt; serverless and inside your GCP project, priced per GiB processed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fivetran, Airbyte, Hevo, Estuary:&lt;/strong&gt; broad connector platforms; check how often they actually merge into BigQuery and how they meter volume.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://quayen.com" rel="noopener noreferrer"&gt;Quayen&lt;/a&gt;:&lt;/strong&gt; does only MySQL to BigQuery, with the snapshot, change log, ordered merges and cost controls above built in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whichever you choose, check these four things: deletes are removed (not just flagged), changes are ordered by log position, the read position is saved only after BigQuery accepts a batch, and you know what each refresh costs.&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>bigquery</category>
      <category>dataengineering</category>
      <category>database</category>
    </item>
  </channel>
</rss>
