Ankit Khare

Notes · Data engineering

Ingesting our PagerDuty history in 18 minutes instead of two hours

Ankit Khare · November 2026 · 3 min read

We wanted our PagerDuty history in the lakehouse: incidents, their log entries and their alerts. The first version took an hour and a half to two hours a run. The faster version then ran into a Spark limit I hadn't needed to know about before.

The slow version

Every run refetched about 10,000 incidents and then, for each one, paged through its log entries and alerts. With five worker threads that took 1.5 to 2 hours.

Bulk log entries, checked every run

PagerDuty also has an account-wide log entries endpoint, so the job now pulls log entries for the whole window in six-hour slices and groups them by incident afterwards. If a slice hits the API's 10,000-result offset cap, it's split in half and fetched again. Entries are de-duplicated and kept newest first, the order the per-incident endpoint uses, because tables built later depend on it.

I didn't want to switch sources and just hope they agreed, so each run checks. It takes a sample of incidents (the busiest half plus an even spread across the rest, 20 by default) and fetches those the old way as well. If the entry IDs or their order differ for any of them, the run logs a warning and falls back to the per-incident path for everything. A config setting can also force the old path.

Rate limits

Alerts have no account-wide endpoint, so they're still fetched per incident, alongside the log entries. Ten driver threads share one rate limiter. PagerDuty allows roughly 960 requests a minute per key, and I started at 840. That was too high, because another scheduled job uses the same key: at 840 we got 429s with about 25-second Retry-After waits every minute. At 600 a minute the run took just as long and the 429s stopped.

The 128 MB limit

The first full run on staging fetched everything (10,258 incidents, 113,000 log entries and 10,000 alerts in about 18 minutes) and then failed while writing. Our shared write helper sent each dataset's JSON through a single Spark task, and the incidents came to 237 MB, while spark.rpc.message.maxSize is 128 MB. The Spark operator restarted the job, which meant fetching everything again.

The fix was to take Spark out of that step. Ingestion now writes each dataset straight to S3 with PyArrow from the driver, in the same layout and formats as before, and drops each dataset from memory once it's written. A failed write raises an error now; the old helper only logged it. The flatten job reads only the .parquet files in the run folder, since the JSON copies sit next to them.

With the HTTP work on driver threads and Spark only writing 13 files, the job barely needed a cluster. The DAG went from a 2-core driver plus two to five 2-core executors with dynamic allocation, to one driver core and a single 1-core executor.

Tests

The tests run against a fake PagerDuty API. They check that bulk and per-incident modes produce identical datasets, derived tables and same-second ties included, and that a mismatch triggers the fallback. Late entries, the offset-cap split and the limiter's spacing each have their own test.

Looking back, the first version's real mistake was treating a pile of HTTP requests as a Spark job. Spark earns its keep when the data is large. Here the data was small and the requests were many, and a few threads with a plain file writer turned out simpler and cheaper.

More notes at ankitkhare.dev. Questions or corrections: ankit@ankitkhare.dev.