karsh.vashi / data engineer

Project

wiki-stream-lake

A streaming and batch data lake on AWS, fed by the live stream of Wikipedia edits, with four experiments that measure its trade-offs. All infrastructure is Terraform.

status
complete
stack
Python, Kinesis, Firehose, Lambda, Glue (Spark), Athena, S3, ECS Fargate, Terraform
published

Wikimedia publishes every edit to every Wikipedia, Wikidata and Commons page as a public stream. This project captures it with a Python producer on ECS Fargate, moves it through Kinesis Data Streams and Firehose into S3, curates it into partitioned Parquet with a Glue Spark job, and queries it with Athena. Four experiments then measure the trade-offs, and every number on this page comes from the repo’s findings.

Architecture

The pipeline has two halves with different rhythms. Ingest runs only during a capture session; the stream is removed between sessions to stop its hourly charge.

streaming ingest · runs only during a capture sessionfrom docs/architecture.md

The producer, a one-off ECS Fargate task, reads Wikimedia EventStreams and sends records to a 2-shard Kinesis Data Stream with PutRecords, writing a run summary to S3 runs/ when it stops. Firehose reads the stream, passes each batch through a Lambda that filters, flattens and masks it, and writes gzip JSON to S3 raw/edits/, partitioned by arrival hour. Records the Lambda cannot parse go to S3 errors/.

  • Wikimedia feeds Producer
  • Producer feeds Kinesis
  • Producer feeds runs/
  • Kinesis feeds Firehose
  • Firehose feeds raw/edits/
  • Firehose feeds errors/

Everything after the raw zone runs in short batches when started.

catalog, ETL and query · short batch runsmake run-etl

A Glue crawler scans raw/edits/ and registers the raw_edits table in the Glue Data Catalog. When the crawler succeeds, a Glue Spark job reads raw, deduplicates and types the records, and writes Snappy Parquet to S3 curated/edits/, partitioned by event date. Athena, in a workgroup with a 1 GB scan limit per query, reads table definitions from the catalog and queries the curated data.

  • raw/edits/ feeds Glue crawler
  • Glue crawler feeds Data Catalog
  • Glue crawler feeds Glue Spark job
  • raw/edits/ feeds Glue Spark job
  • Glue Spark job feeds curated/edits/
  • Data Catalog feeds Athena
  • curated/edits/ feeds Athena

Why each piece is there

  • A one-off Fargate task for the producer. A capture runs for hours, which rules out Lambda’s 15-minute limit, and an EC2 instance would bill while idle. The task stops itself, bills per second, and runs in public subnets with outbound HTTPS only, so it needs no NAT gateway.
  • Kinesis Data Streams in front of Firehose. Firehose could take records directly, but a stream adds 24 hours of retention, partition keys and shards (Experiment 1), and room for a second consumer.
  • Cleaning before anything is stored. The Firehose Lambda drops canary and categorisation events, flattens each event to 16 fields, and replaces IP addresses and temporary-account names with anonymous, including inside titles and comments. Raw storage never holds personal data; records it can’t parse land in errors/ instead of failing the batch.
  • Two zones on two clocks. raw/ is partitioned by arrival hour, the only time Firehose knows; curated/ by event date, the time queries ask about. They disagree at midnight in both directions, so rebuilding one day reads three days of raw.
  • A crawled raw table and a coded curated table. The crawler discovers the raw schema and its hourly partitions. The curated schema is owned by the job, defined in Terraform with partition projection, so new days need no crawler or repair.
  • An idempotent curate job. Every run rebuilds whole event_date partitions with dynamic partition overwrite. Running it twice gives the same table, which makes reprocessing after a bug safe.

The dataset

Main capture 12 hours, 24–25 October 2026
Events sent to Kinesis 1,674,104 (38.8 per second; 0 failed, 0 throttled)
Rows in curated_edits 847,303 across 417 wikis
Bot edits 53.0%
Users masked as anonymous 2.7%

Experiment 1: the hot shard

Kinesis routes each record to a shard by hashing its partition key. A key with a few very common values, the wiki’s domain, piles records onto one shard; a unique key, the event ID, spreads them. The same 847,307 captured records were replayed into a 4-shard stream at a target of 2,500 records per second, once per key.

share of records per shard, replay at 2,500 rec/s targetone shard's write limit (1,000 of 2,500 rec/s)

key = domain · 2,089 rec/s achieved · 158,929 throttled

ShardShare of records
shard 350.1%
shard 233.3%
shard 012.3%
shard 14.3%

key = event_id · 2,498 rec/s achieved · 0 throttled

ShardShare of records
shard 025.0%
shard 125.0%
shard 225.0%
shard 324.9%

docs/runs/, replay-domain and replay-event_id

Keyed by domain, Commons and English Wikipedia hash to the same shard, so one shard got half of all records and needed about 1,250 writes per second against a limit of 1,000. Kinesis rejected writes 158,929 times and throughput fell 16%; nothing was lost, because the producer resends only rejected records. Keyed by event ID, the shards were within 0.1% of each other and the target rate held. Adding shards wouldn’t have helped: a hot key always maps to one shard.

Experiment 2: JSON versus Parquet

The same aggregation (edits, bot edits and bytes added, per wiki) on the same day, once on gzip JSON and once on Snappy Parquet.

data scanned by Athena for the same query
TableData scanned
gzip JSON, one day43.1 MB
Parquet, one day1.14 MB
Parquet, no date filter2.19 MB

docs/findings.md, Experiment 2

Parquet cut the data scanned by 38×. Athena read only the 3 columns the query uses, while for JSON it had to read and decompress every byte of the day’s files. Dropping the event_date filter roughly doubled the Parquet scan: that difference is partition pruning on its own. Run time improved less (1,253 ms to 697 ms), because at this size every query is dominated by fixed startup cost. At scale, Athena bills by bytes scanned, so 38× is the ratio that matters.

Experiment 3: replay with no duplicates

Rebuilding a day from raw must give the same result every time, so the pipeline can recover from a bug by running again.

curated_edits rows for 24 October
StepRows
before430,963 · 0 duplicates
after backfill #1430,963 · 0 duplicates
after backfill #2430,963 · 0 duplicates

docs/findings.md, Experiment 3

The job overwrites whole event_date partitions, so running it again replaced the day with identical content instead of appending a second copy.

Reconciling row counts afterwards found a problem the experiment itself didn’t catch. 1,209 edits made just after midnight on 25 October were stored in raw/ under the 24th, because Firehose names a file’s partition by the time its buffer started, and two buffers opened at 23:59 kept filling past midnight. The job assumed an edit on day D could only be filed under D or D+1, so a backfill of the 25th would have silently dropped them. It now reads D-1, D and D+1, and a backfill of the 25th returned 413,404 rows, the same as the incremental run.

Experiment 4: the freshness trade-off

Firehose doesn’t write each record to S3 as it arrives. It collects records in a buffer and writes them out as one file when the buffer reaches 5 MB or a time limit passes, whichever comes first. The time limit is a setting, and it pulls two ways. A shorter limit means fresher data: edits become queryable sooner. It also means more, smaller files, and small files are a cost of their own: Athena and Glue open each one separately, and S3 charges per request. The experiment ran the same capture with limits of 60, 120 and 300 seconds to measure both sides.

delay to the first file in S3
Buffer limitDelay to first file
60 s buffer67 s
120 s buffer120–121 s
300 s buffer301 s

docs/findings.md, Experiment 4

files written per hour, and their average size
Buffer limitFiles per hour
60 s buffer144 / h · 41.9 KB avg
120 s buffer63 / h · 106.4 KB avg
300 s buffer32 / h · 168.7 KB avg

docs/findings.md, Experiment 4

  • The delay equals the time limit. The cleaned stream is only about 10 KB per second, so the buffer never gets near 5 MB and the timer triggers every write. In practice the setting is a direct dial for how stale the data is.
  • Fresher costs proportionally more files. Going from 300 s to 60 s made the data 4.5× fresher and produced 4.5× more files, each about a quarter of the size.
  • Two shards, two buffers. Firehose keeps one buffer per Kinesis shard, so it writes about two files per interval: 63 an hour at 120 s, not 30.
  • Why 120 s is the default. The curate job compacts each day back into one Parquet file, so the small files only matter until it runs. Paying for 60-second freshness buys little, and 300 s leaves data waiting five minutes for a modest saving.

Cost

A 12-hour capture session, including the ETL, costs about $1. Only the Kinesis streams bill while idle, so they exist only during capture sessions.

Control How
Streams exist only while needed created per session, removed with make stream-down
Captures stop themselves duration capped at 24 hours
Glue job 2 G.1X workers, 15-minute timeout, one run at a time
Athena 1 GB scan limit per query, enforced by the workgroup
Lifecycle rules raw to Infrequent Access at 30 days, deleted at 90
Budget $10 a month, alerts at 50, 80 and 100%

What would change for production

  • Terraform state in an S3 backend with locking, not on one laptop.
  • On-demand Kinesis or automatic resharding, with alarms on iterator age and write throttling.
  • A long-running producer (an ECS service) with health checks, instead of fixed-length captures.
  • Data quality checks in the curate job, such as row-count reconciliation between raw and curated, which is how the midnight bug was found.
  • A table format such as Apache Iceberg for atomic overwrites, schema evolution and time travel.
  • Deployment from CI, with a plan on each pull request and an approval step before apply.

The architecture notes and findings cover each decision and measurement in more depth.