CDC Lakehouse Reliability — E-commerce - #6
Open
Aliv22-code wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CDC Lakehouse Reliability — E-commerce
A small but deeply correct CDC pipeline keeping a lake (full history) and a warehouse
(latest snapshot + time travel) in sync with an e-commerce source.
Stack: PostgreSQL (source design) · Python (ingestion) · DuckDB (lake + warehouse) ·
pytest. CDC is simulated in-process (dependency-free); the production analogue is
documented throughout. Full design:
submission/ecommerce-cdc/SOLUTION_APPROACH.md.1. Source schema design
customers,products,orders(strong);order_items,payments(weak).order_itemsandpaymentshave no meaning without their parentorder(identity completed by the FK).orders→customers,order_items→orders/products,payments→orders(1—N throughout).updated_at(the change-capture path).price/total/amount/line_total), timestamps, enums(
status/method), nullable (settled_at).total = Σ line_total,line_total = qty*unit_price,non-negative money,
price>0,quantity>0,settled_at ≥ created_at, enum domains,legal status transitions.
2. CDC strategy
CDCRecord(op, table, pk, before, after, captured_at, sequence).sequenceis a monotonic offset (Kafka offset / Postgres LSN analogue) — the soleordering + checkpoint primitive.
records_since(checkpoint).ON CONFLICT DO NOTHING; warehouse skips events whosesequence ≤ the row's current
_cdc_seq(also handles out-of-order).deleteevents; snapshot soft-deletes, SCD2 history closesthe version.
3. Lake & warehouse modeling
lake_cdc_events): append-only, idempotent, stores full row image as JSON —every change is recoverable.
wh_*): upsert by PK +_deletedsoft-delete = latest snapshot.wh_*_history): SCD2 (_valid_from_seq/_valid_to_seq/_is_current).reconstruct_as_of(table, seq)reads the version valid atseq.restore_from_lake(target_seq)deterministically rebuilds the snapshot byreplaying the lake.
4. Schema change safety
SCHEMA_CONTRACT(columns, types, nullability, enum domains, keys).type change, nullable tightening, enum shrink) → raise
SchemaIncompatibilityError,stop ingestion, write nothing, exit non-zero. Additive changes warn and continue.
scripts/check_schema_contracts.py.5. Validation parity
price>0,quantity>0,settled_at ≥ created_at, legal status transitions (validatedfrom ordered lake history).
validation/checks.py,scripts/run_data_quality_checks.py, and tests.6. Catalog exposure
catalog/catalog.jsonregisters every lake + warehouse dataset (layer, owner,consumers, cadence, query path, per-column schema). Gate:
scripts/validate_catalog.py;a test also asserts catalog ↔ live tables stay in sync.
7. Responsible AI usage
AI assistance was used to scaffold boilerplate and draft documentation. All schema
design, CDC/SCD2 semantics, schema-safety rules, and validation logic were reviewed and
verified by running the test suite (
41 passed) and the CI gate scripts locally.Testing
pytest -q→ 41 passed, covering modeling/constraints, CDC correctness(insert/update/delete/duplicate/replay/restart), schema-change safety, warehouse
snapshot + time travel + restore, and catalog. Red→Blue→Green discipline followed.