1. Change data capture apply-set filter
Use case. CDC pipelines must not replay rows that are older than the stored watermark. Filter strictly by high-water key before any merge to save compute and prevent corrupting later state.
# Numeric LSN watermark; strings should be normalized by host.
const WATERMARK = 91234
function should_apply(lsn) {
if lsn > WATERMARK {
return {"apply": true, "reason": "NEWER_THAN_WATERMARK"}
}
return {"apply": false, "reason": "ALREADY_APPLIED"}
}
print "CDC={should_apply(91250).apply}"
Save as data-engineering_01.pea; check pea fmt --check data-engineering_01.pea && pea check data-engineering_01.pea; run pea run data-engineering_01.pea.
2. Schema evolution compatibility check
Use case. A downstream consumer only reads selected fields from upstream events. Before deploy, prove every required old field still exists with a compatible type, because breaking the contract breaks all readers at once.
# Producer v2 event arrives as portable map.
const REQUIRED_FIELDS = ["id", "email", "created_at"]
function event_compatible(event) {
let i = 0
while i < len(REQUIRED_FIELDS) {
if not (REQUIRED_FIELDS[i] in event) {
return {"ok": false, "missing": REQUIRED_FIELDS[i]}
}
i += 1
}
return {"ok": true, "missing": null}
}
print "SCHEMA={event_compatible({"id":1,"email":"a@b.com"}).ok}"
Save as data-engineering_02.pea; check pea fmt --check data-engineering_02.pea && pea check data-engineering_02.pea; run pea run data-engineering_02.pea.
3. Partition selection for hot vs cold storage
Use case. Analysts query recent data constantly and old data rarely. A deterministic tiering rule keeps hot partitions in SSD and moves cold data to object storage without guessing from filenames.
# days_old is computed by host from ingest_time.
const HOT_DAYS = 14
const WARM_DAYS = 90
function tier_for(days_old) {
if days_old <= HOT_DAYS { return {"tier": "HOT"} }
if days_old <= WARM_DAYS { return {"tier": "WARM"} }
return {"tier": "COLD"}
}
print "TIER={tier_for(120).tier}"
Save as data-engineering_03.pea; check pea fmt --check data-engineering_03.pea && pea check data-engineering_03.pea; run pea run data-engineering_03.pea.
4. Exactly-once Kafka consumer dedup key
Use case. A billing consumer can see the same event after rebalance or retry. The dedup key must combine topic, partition and offset, and the decision must be persisted with the write outcome so retries do not double count.
# offsets and partition are ints; topic came over wire.
function dedup_key(topic, partition, offset) {
if topic == "" or partition < 0 or offset < 0 {
return {"key": null, "ok": false, "reason": "INVALID"}
}
return {"topic": topic, "partition": partition, "offset": offset, "ok": true}
}
print "DEDUP_OK={dedup_key("orders", 3, 9876).ok}"
Save as data-engineering_04.pea; check pea fmt --check data-engineering_04.pea && pea check data-engineering_04.pea; run pea run data-engineering_04.pea.
5. Null sentinel found in data Lakehouse column
Use case. Upstream systems encode missing numerics as -1 or "N/A". Normalize sentinel values before aggregations, or dashboards will show impossible minima and alerts will fire for known data quirks.
# Generic sentinel normaliser for age column.
const SENTINEL = -1
function normalize_age(raw) {
if raw == SENTINEL or raw == null {
return {"value": null, "reason": "MISSING"}
}
if raw < 0 or raw > 150 {
return {"value": null, "reason": "OUT_OF_RANGE"}
}
return {"value": raw, "reason": "OK"}
}
print "AGE={normalize_age(-1).reason}"
Save as data-engineering_05.pea; check pea fmt --check data-engineering_05.pea && pea check data-engineering_05.pea; run pea run data-engineering_05.pea.
6. Incremental checksum to detect partial file copy
Use case. A data ingestion job must reject half-copied files without downloading gigabytes again. Compare segment checksums and total expected bytes against manifest before moving to processed.
# manifest has {bytes, sha256, segments:int}.
function file_complete(manifest, actual_bytes) {
if manifest.bytes != actual_bytes { return {"ok": false, "reason": "SIZE_MISMATCH"} }
if manifest.segments <= 0 { return {"ok": false, "reason": "BAD_MANIFEST"} }
return {"ok": true, "reason": "COMPLETE"}
}
print "FILE={file_complete({"bytes":10485760,"segments":8}, 10485760).ok}"
Save as data-engineering_06.pea; check pea fmt --check data-engineering_06.pea && pea check data-engineering_06.pea; run pea run data-engineering_06.pea.
7. Retention eligible by legal hold and age
Use case. A compliance job must delete only expired data without touching records under legal hold. The rule is small but dangerous: it must check both age and hold flag before any purge action.
# Age is authoritative when host computes it.
const RETENTION_DAYS = 2555 # 7 years
function retention_ok(age_days, legal_hold) {
if legal_hold {
return {"action": "KEEP", "reason": "LEGAL_HOLD"}
}
if age_days > RETENTION_DAYS {
return {"action": "DELETE", "reason": "EXPIRED"}
}
return {"action": "KEEP", "reason": "WITHIN_RETENTION"}
}
print "RETENTION={retention_ok(3000, false).action}"
Save as data-engineering_07.pea; check pea fmt --check data-engineering_07.pea && pea check data-engineering_07.pea; run pea run data-engineering_07.pea.
8. Shard skew detection for hot tenant partitions
Use case. One tenant can mono-polarise a shard and break global query budgets. Detect skew before the move so balancer can reassign tenants without violating data-residency policy.
# top_shard_bytes / total_bytes measured hourly.
const SKEW_ALERT = 0.35
const MIN_SHARDS = 8
function skew_status(top_shard, total, shard_count) {
if shard_count < MIN_SHARDS {
return {"action": "MONITOR", "reason": "TOO_FEW_SHARDS"}
}
let share = top_shard / total
if share > SKEW_ALERT {
return {"action": "REBALANCE", "reason": "HOT_SHARD", "share": share}
}
return {"action": "OK", "reason": "BALANCED", "share": share}
}
print "SKEW={skew_status(43, 100, 16).action}"
Save as data-engineering_08.pea; check pea fmt --check data-engineering_08.pea && pea check data-engineering_08.pea; run pea run data-engineering_08.pea.
9. Batch job retry budget in orchestrator
Use case. Data jobs retry transient failures but can exhaust cluster capacity. The orchestrator needs one policy decision for status, backoff and terminal state instead of every job reimplementing it.
# transient and permanent are already classified.
const MAX_TRIES = 3
function job_decision(attempt, permanent) {
if permanent {
return {"state": "FAILED", "reason": "PERMANENT"}
}
if attempt >= MAX_TRIES {
return {"state": "FAILED", "reason": "EXHAUSTED"}
}
return {"state": "RETRY", "reason": "TRANSIENT", "delay_ms": 1000 * attempt}
}
print "JOB={job_decision(2, false).state}"
Save as data-engineering_09.pea; check pea fmt --check data-engineering_09.pea && pea check data-engineering_09.pea; run pea run data-engineering_09.pea.
10. Index build readiness gate
Use case. Building an index on a hot table increases IOPS and replication lag. Block the migration unless replica lag is inside budget and estimated row-count growth is acceptable.
# lag is from monitoring; growth_pct is forecast.
const MAX_LAG_SEC = 5
const MAX_GROWTH_PCT = 30
function index_gate(lag_sec, growth_pct, hot_hours) {
if hot_hours {
return {"ok": false, "reason": "HOT_TRAFFIC"}
}
if lag_sec > MAX_LAG_SEC {
return {"ok": false, "reason": "REPLICA_LAG"}
}
if growth_pct > MAX_GROWTH_PCT {
return {"ok": false, "reason": "GROWTH_RISK"}
}
return {"ok": true, "reason": "GO"}
}
print "INDEX={index_gate(2, 12, false).reason}"
Save as data-engineering_10.pea; check pea fmt --check data-engineering_10.pea && pea check data-engineering_10.pea; run pea run data-engineering_10.pea.
← Back to the Code Library