Numerator
Near real-time search sync
Search was rebuilt once a night, so results could be up to a day old. I replaced that with a change data capture pipeline that streams MySQL changes into Elasticsearch in under a minute. It runs all the time, handles duplicate messages safely, and can replay history when needed.
The problem
Search was only as fresh as last night's rebuild.
The product ran search over a very large MySQL database, and the search index was rebuilt from scratch every night. A price change, a new product or a fixed attribute could take hours, sometimes most of a day, to show up in results. The rebuild was also expensive. It put heavy load on the database and took a long time, and fixing something during the day meant waiting or paying for another rebuild.
The platform needed continuous updates: when a row changes in MySQL, search should show it seconds later, without reprocessing everything else.
The approach
I replaced the nightly batch with a streaming change data capture pipeline. Instead of querying or dumping tables, it reads the database's binary log, turns each saved change into an event, and applies it to Elasticsearch.
- Debezium reads the MySQL binlog and sends out every insert, update and delete in order, without adding load to the app's own queries.
- Kafka holds those events for each table. Capturing changes and indexing them can run at different speeds, and the log is kept so it can be replayed.
- Consumers that handle repeats safely write to Elasticsearch by document id, so getting the same event twice does no harm. That is what makes retries safe.
- Retries and a dead-letter topic stop one bad message from blocking the stream, and a replay from the log can rebuild any part of the index.
Architecture
Key decisions
Read the log, don't query the tables
Reading the binlog with Debezium gives an ordered, lightweight stream of exactly what changed, in the order it was saved. Polling would have added load to the database and still missed deletes and quick back-to-back updates.
Make every write safe to repeat
Every consumer writes by document id, so when a message is delivered twice (and with this kind of messaging, it will be), the result is the same. That one rule makes retries, restarts and backfills safe instead of scary.
Use the log to recover
Kafka keeps the stream of changes, so rebuilding part of the index means a replay from a point in the log, not another full reindex.
Outcome
- <60 sfrom a MySQL change to search, down from a nightly rebuild
- Continuouslive updates replaced the scheduled full rebuilds
- Replayablewrites that are safe to repeat make retries and backfills safe
Stack
DebeziumKafkaElasticsearchMySQLPythonCelery