Lesson 8 of 9 · 26m
Incremental Pipelines, Dedupe, and Storage
Design raw-to-curated dataflow with immutable captures, deterministic identities, idempotent writes, dedupe, and replayable transformations.
A scraper that writes directly to the production database is a parser coupled to history. When the layout changes or a bad deploy corrupts records, you cannot replay what the web looked like. Mature scraping pipelines separate collection, extraction, normalization, and publication. That separation is what lets you repair mistakes without another crawl.
Keep raw evidence
Store raw responses or snapshots with metadata before interpretation:
capture_id, source_url, fetched_at, http_status
headers_hash, content_hash, body_uri
extractor_version, fetch_context(crawler, identity, policy tag)Object storage is usually better than stuffing large bodies into relational rows. Keep bodies long enough for reprocessing and legal/provenance needs, then expire them according to retention policy.
Choose entity identity deliberately
URL identity and entity identity are not the same thing. A product can move URLs; a URL can be reused for a different product; a profile slug can change. Prefer stable source IDs when available:
source-native ID best: SKU, post ID, listing ID
canonical URL acceptable when stable and unique
composite key source + type + native ID; fingerprints are weakRecord identity confidence. Merging records incorrectly is worse than keeping duplicates visible.
Make writes idempotent
Crawlers retry. Workers crash after writing but before acknowledging. Queues redeliver. Therefore writes must tolerate repetition:
INSERT INTO product_observations (
source, source_id, observed_at, price, currency, content_hash
) VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (source, source_id, observed_at)
DO NOTHING;For current-state tables, compare observation time and apply monotonic update rules. The newest fetch is not always the newest truth if requests are reordered.
Dedupe at the right layer
There are at least three kinds of duplication:
exact duplicate same bytes fetched twice
representation duplicate same entity through different URLs
semantic duplicate same real-world thing with different source IDsExact duplicates are cheap: hash content. Representation duplicates require canonicalization and identity mapping. Semantic duplicates require matching rules, human review, or probabilistic entity resolution. Do not pretend one hash solves all three.
Queue design is a politeness control
Shape the flow as frontier -> host queue -> worker -> fetch -> raw store -> extract -> validate -> publish. Per-host tokens enforce rate limits globally across workers. Dead-letter queues capture malformed or repeatedly failing URLs; backoff is flow control, not an afterthought.
Design replay before you need it
When extraction changes, replay should be boring:
python replay_extract.py --source books.toscrape.com --captured-after 2026-11-01 --write-stagingReplay into staging, compare counts and validation failures, then promote. If replay requires editing production rows by hand, the pipeline is not yet engineered.
Field notes
- Raw capture retention is not hoarding; apply purpose-based retention and deletion rules.
- Content hashes differ when ads, timestamps, or randomized tokens render; hash normalized extraction too.
- Queue redelivery means duplicate writes are normal, not exceptional.
- A current-state table without observation history hides regressions and makes incident review impossible.
- Backfills are crawls too. Apply the same rate and permission boundaries.
Was this lesson helpful?