DEV Community

Cover image for Where Was Pole Actually Won? Aligning Asynchronous F1 Telemetry Streams in Go
Okikiola Ashiru
Okikiola Ashiru

Posted on

Where Was Pole Actually Won? Aligning Asynchronous F1 Telemetry Streams in Go

Track Replay View

Full-resolution MP4 recording: VID_20261118_150957_030.mp4

Scope Note: ghost-lap is a personal educational project that replays historical Formula 1 qualifying data from the public OpenF1 API (session_key=9554) as a live stream. It is not connected to a live car feed and is not a race-strategy tool.

The Human Question: Where Was Pole Actually Won?

At the 2024 British Grand Prix Qualifying session at Silverstone, George Russell (RUS) took pole position in 85.819 s, edging out Lewis Hamilton (HAM) at 85.990 s by +0.1710 s.

A single finish-line gap tells you who won, not where on the 5.89 km circuit the lap was won or lost. Answering that question requires a continuous delta curve across the entire lap, comparing both cars at every meter of track distance rather than by wall-clock time.

The Engineering Problem: Time Alignment vs. Spatial Alignment

Car telemetry does not arrive indexed by track distance. In the OpenF1 dataset (session_key=9554), car_data and location arrive as asynchronous timestamped streams at roughly 3.8 Hz (about every 260 ms, or roughly 18 m of travel per sample at 70 m/s):

  • RUS (Lap 25): 324 samples at 3.74 Hz (90.0–325.0 km/h)
  • HAM (Lap 24): 335 samples inside [0.0 s, 85.990 s] at 3.88 Hz (86.0–324.0 km/h)

If you compare two drivers at identical elapsed timestamps (e.g., t = 42.100 s), you are comparing two cars at different physical coordinates on the track, especially once one driver opens a gap. To compare speed and cumulative time loss accurately, you have to transform both streams from $f(t)$ into $f(S)$, mapping both drivers onto a common 1.0 m spatial distance grid in a streaming pipeline.

Architecture

flowchart LR
    OpenF1["OpenF1 API\n(Session 9554, Q3)"] -->|export_telemetry.py| Data["data/*.json\n(324 RUS / 338 HAM)"]
    Data -->|cmd/producer| Redpanda[("Redpanda\ntopic: telemetry-raw")]
    Redpanda -->|cmd/consumer| Stream["DriverStreamState\nTrim [0, T_lap] & 1.0m Grid"]
    Data -->|cmd/observer| Metrics["Prometheus /metrics\n& /api/trace (:9090)"]
    Data -->|cmd/replay-web| Canvas["HTML5 Canvas Replay\n(:8080)"]
  1. Replay Producer (cmd/producer): Reads exported Q3 laps, trims samples strictly to [0.0 s, official_lap_time] via linear boundary interpolation, and publishes chronologically interleaved records to Redpanda (telemetry-raw), partitioned by driver code (RUS, HAM).

  2. Stateful Streaming Consumer (cmd/consumer, pkg/telemetry): Consumes telemetry-raw via franz-go, maintains per-driver incremental state (DriverStreamState), projects 2D coordinates onto a 1.0 m arc-length grid, and emits incremental delta points without re-scanning prior frames.

  3. Observability and Replay (cmd/observer, cmd/replay-web): Serves a dependency-free HTML5 <canvas> replay view (:8080) and exposes bounded-cardinality Prometheus metrics (/metrics) plus aligned trace arrays (/api/trace) at :9090.

The Two Things That Should Have Stopped Me

In my first pass, two validation results looked too clean:

  1. Zero Finish-Line Error (0.0000 s): I had calibrated the final grid point to match the official +0.1710 s gap. That guaranteed zero error by construction, grading my own homework with the answer key.

  2. Impossible Segment Speeds (~490 km/h): My per-segment table had one car covering 960 m in 7.053 s. I initially wrote a note blaming pit-exit and cool-down transitions in the data, even though a flying Q3 lap has no pit exit.

When I cut each driver's export strictly to the official lap start and duration, stripped out finish-line calibration, and added a CI check requiring every band over 600 m to average between 35 and 90 m/s, the real behavior surfaced.

Distance Alignment and Resampling Choice (Stages 2 & 3)

Before building the delta curve, I compared two independent ways of computing cumulative track distance $S$ across the trimmed Q3 laps:

Alignment Method RUS Computed Distance HAM Computed Distance Difference
Speed Integration (int v, dt, km/h to m/s) 5828.112 m 5825.070 m 3.042 m
2D Arc-Length Projection (X, Y decimeters to m) 5828.725 m 5823.350 m 5.376 m
  1. Speed Integration: Converting km/h to m/s and applying trapezoidal integration across ~260 ms sample intervals yields 5,828.1 m (RUS) and 5,825.1 m (HAM), with 3.042 m inter-driver drift.

  2. 2D Arc-Length Projection ($\sqrt{\Delta x^2 + \Delta y^2} / 10$): OpenF1 reports track coordinates in decimeters. Summing Euclidean steps in meters yields 5,828.725 m (RUS) and 5,823.350 m (HAM), landing about 1% below Silverstone's official 5,891 m centerline, which is plausible for a racing line sampled at 3.8 Hz.

To map non-uniform samples onto a uniform 1.0 m grid (5,824 overlapping points from 0 to 5,823 m), I used monotonic linear interpolation rather than cubic splines. Elapsed time RelTimeSec stays strictly monotonic, and binary step channels (throttle snaps and hard braking) never ring or overshoot between samples.

The Honest Number: Explaining the 85 ms Gap (Stage 4)

Without finish-line calibration, computing DeltaSec = time_ham(S) - time_rus(S) directly from each sample's timestamp and arc distance produces:

  • Official Ground-Truth Gap: +0.1710 s (RUS 85.819 s vs. HAM 85.990 s)
  • Computed Finish-Line Delta (Uncalibrated, at 5,823 m): +0.2560 s
  • Absolute Error vs. Official: 0.0850 s (84.99 ms)

The table below shows where that 85 ms comes from. Both drivers are compared up to 5,823 m, the end of the shorter of the two position traces (HAM's 5,823.35 m trace, where his elapsed time is 85.984 s vs. 85.990 s official, 6 ms short). Because RUS's trace is 5,828.73 m long, comparing him at 5,823 m stops 5.7 m before his finish line. His elapsed time at 5,823 m is 85.728 s against his official 85.819 s (91 ms short). Net difference: 85 ms.

The same distance travelled is not the same place on the circuit. The two position traces differ in length by 5.376 m (a mix of racing-line choice and 3.8 Hz position sampling), and that path-length difference accounts for roughly half of the 0.171 s gap being measured.

Streaming Equivalence and Packet-Loss Stress Test (Stages 5 & 6)

To verify that incremental streaming (DriverStreamState) introduces no drift relative to batch processing, cmd/stream-check compares the Redpanda consumer's incremental output against batch resampling:

  • Batch Finish-Line Delta: +0.2560 s
  • Incremental Stream Delta: +0.2560 s
  • Divergence: 0.000000 s (0.0000 ms)

Next, I dropped intermediate samples at 5%, 10%, and 25% using a single fixed PRNG seed (seed = 42) with start and finish boundary samples anchored:

Loss Rate Finish Delta (s) Error vs. Official (ms) Max Curve Error vs. Base (ms) MAE vs. Base (ms)
0% (Baseline) +0.2560 s 84.99 ms 0.00 ms 0.00 ms
5% Loss +0.2594 s 88.37 ms 131.89 ms 3.87 ms
10% Loss +0.2588 s 87.82 ms 139.23 ms 13.29 ms
25% Loss +0.3066 s 135.63 ms 217.24 ms 52.41 ms

Because this run uses a single random draw, it serves as a rough sensitivity check rather than a statistical curve: finish-line error shifts modestly at 5% and 10% loss (88 ms), before jumping to 136 ms at 25% loss as missed samples widen local interpolation gaps (217.24 ms peak curve error).

Distance-Band Breakdown and What Remains Unverified (Stage 7)

Partitioning the 5,823 m overlapping grid into seven contiguous distance bands, and asserting in CI that every band over 600 m averages between 35 and 90 m/s, gives the per-band timing breakdown:

Distance Band Length (m) RUS (s) HAM (s) RUS Avg (m/s) HAM Avg (m/s) Delta (s) Ahead
Band 1 (0-780 m) 780 9.518 9.581 81.95 81.41 +0.0625 RUS
Band 2 (780-1560 m) 780 15.474 15.482 50.41 50.38 +0.0072 RUS
Band 3 (1560-2520 m) 960 15.884 15.775 60.44 60.86 -0.1095 HAM
Band 4 (2520-3180 m) 660 7.972 7.979 82.79 82.72 +0.0069 RUS
Band 5 (3180-3920 m) 740 9.050 9.157 81.77 80.82 +0.1064 RUS
Band 6 (3920-4780 m) 860 10.888 11.018 78.98 78.05 +0.1297 RUS
Band 7 (4780-5823 m) 1043 16.942 16.994 61.56 61.37 +0.0527 RUS
Total / Sum 5823 85.728 85.984 67.92 67.72 +0.2560 RUS

Two caveats are worth stating plainly:

  1. Conservation is arithmetic, not a physical test: The band deltas sum to +0.2560 s (0.000000 ms error) because telescoping differences always sum to the endpoint difference.

  2. Corner boundaries are unverified: Without aligning braking-point signatures from the brake and speed channels to an official FIA corner map, fixed distance bands cannot be reliably labeled with corner names, so they stay numbered as distance bands.

Bounded-Cardinality Observability

Exposing all 5,824 spatial steps as Prometheus labels would create high-cardinality time series. Instead, cmd/observer restricts Prometheus labels strictly to bounded enums on GET /metrics, recording a 2.64 ms resample duration (ghostlap_resample_duration_seconds_sum 0.002636245), while serving the full 1.0 m trace array over GET /api/trace for Grafana visualization.

What This Project Isn't

It isn't live telemetry, it isn't a strategy tool, and it covers one pair of laps from one qualifying session. What it demonstrates is that a validation check that cannot fail is not a validation, and that when an uncalibrated check shows an 85 ms discrepancy, tracing it to the 5.4 m path-length difference explains the system far better than calibrating the error away.

Repo: github.com/aashiruu/ghost-lap
Author: Okikiola Ashiru, Cloud & DevOps Engineer

Top comments (0)