# Syncing MySQL to Elasticsearch and BigQuery, and detecting drift

> MySQL stays the source of truth while search and analytics stores hold derived copies. Feed them through outbox or CDC, version the writes, reindex behind an alias and check regularly for drift.

Published 2026-11-25 · Language: en · Tags: elasticsearch, bigquery, data-engineering

Real systems use several data stores, each good at one thing. A relational database handles transactions, a search engine handles text and filters, and a warehouse handles large analytical scans. The hard part is keeping them consistent without turning every write into a distributed transaction.

 

## Who owns what

 

Treat MySQL as the **source of truth** for transactional data such as wallets, ledgers and orders. Elasticsearch holds a flattened, searchable copy for full-text queries, filters and aggregations. It is near real-time, with a refresh interval around one second, and it lacks full transactional guarantees. The rule that follows is simple: **the search index can be lost and rebuilt from MySQL, and never the other way round**. Do not use it as the only place money or orders live.

 

## How to keep the copy updated

 

- **Dual write in application code** (write MySQL, then write Elasticsearch) has the dual-write problem from the outbox article. One of the two will eventually fail and leave them different. Avoid it.
- **Outbox, then a consumer.** The application writes an event to an outbox table in the same transaction, a relay publishes it to Kafka, and a consumer indexes the document.
- **Change data capture.** A tool such as Debezium reads the MySQL binary log and publishes row changes to Kafka, and a sink connector writes them to Elasticsearch or BigQuery, with no change in the application.

 

Delivery is at least once and events can arrive out of order, so write with a **version**. Elasticsearch supports external versioning: a write is accepted only if its version is higher than the stored one, so a late, older event cannot overwrite newer data. Use the row's update timestamp or a monotonically increasing number as the version, and make the consumer idempotent.

 

## Reindexing with an alias swap

 

Changing a mapping often needs a new index. Have applications read and write through an **alias**, and switch the alias atomically when the new index is ready.

 

```json
{
  "actions": [
    { "remove": { "index": "orders_v1", "alias": "orders" } },
    { "add":    { "index": "orders_v2", "alias": "orders" } }
  ]
}
```

 

The sequence is: create `orders_v2` with the new mapping, load it from MySQL while the live stream keeps both in step, verify counts, then send this single alias update. Rolling back is the same call in reverse. There is no downtime and no half-switched state.

 

## Detecting drift

 

Any pipeline eventually drifts: a consumer bug, a dropped message, a manual change in one store. Plan to detect it instead of hoping.

 

- **Compare counts** per day or per partition between MySQL and the target. Cheap and catches missing or extra data.
- **Compare checksums by bucket.** Hash the id and update time of rows in each id range on both sides and compare the hashes, then drill into mismatching buckets.
- **Sample** random records and compare them field by field.
- **Watch the pipeline itself:** consumer lag, indexing error rate, dead-letter queue size and the age of the oldest unsent outbox row.

 

```mysql
SELECT id DIV 10000 AS bucket,
       COUNT(*) AS rows_in_bucket,
       SUM(CRC32(CONCAT_WS('|', id, updated_at))) AS checksum
FROM orders
GROUP BY bucket;
```

 

Run the same logic on the target, compare, alert past a threshold and repair by reindexing only the affected range.

 

## The warehouse side: OLTP versus OLAP

 

MySQL is **OLTP**: row-oriented storage tuned for many small reads and writes. A warehouse such as BigQuery is **OLAP**: columnar storage that reads only the columns a query needs, built for scanning billions of rows and aggregating them. Moving analytical queries off the transactional database protects it and makes the queries cheaper.

 

- Load changes with CDC through Kafka, or with scheduled batch exports.
- In BigQuery, **partition** tables by a date column and **cluster** by columns you filter on, so queries scan less data. On-demand pricing is based on bytes scanned.
- Select only the columns you need instead of `SELECT *`, and pre-aggregate common reports.

 

## Takeaway

 

Keep MySQL as the truth and treat search and warehouse stores as rebuildable copies. Feed them from an outbox or CDC, version writes so old events cannot win, reindex behind an alias, and monitor drift with counts, checksums and pipeline metrics.

Source: https://tdphat.io.vn/blog/syncing-mysql-to-search-and-warehouse
