Backend Performance··2 min read

How We Cut Real-Time GPS Transit API Latency by 45% on Colorado's Statewide Bus Network (ridebustang.com)

Shafikul Islam

Shafikul Islam

Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

How We Cut Real-Time GPS Transit API Latency by 45% on Colorado's Statewide Bus Network (ridebustang.com)

When Commuter Polling Collides with Live GPS Telemetry

On a statewide public transit network like ridebustang.com (serving Colorado's intercity bus corridors along I-25 and I-70), traffic patterns are spiky. During morning rush hour and winter snowstorms, thousands of riders open the mobile app simultaneously to check live bus ETAs and seat availability.

Initially, every route status request joined static schedule tables with recently ingested GTFS-Realtime vehicle coordinates directly inside PostgreSQL. Under peak commuter load, database read IOPS spiked and P95 API latency climbed past 1.8 seconds.

Here is the three-part architecture change that reduced P95 API response latency by 45%.


1. Separating Ephemeral GPS Telemetry from Relational State

A vehicle's GPS coordinate updates every 3 to 5 seconds. Writing and querying ephemeral coordinates inside heavy relational SQL joins creates unnecessary WAL churn and buffer eviction.

We split the data path into two distinct storage tiers:

  • In-Memory Redis Geo & Hash State: Incoming GTFS-Realtime vehicle positions are written directly to Redis keys (transit:vehicle:<route_id>) with a 15-second TTL and broadcast to connected clients over WebSockets.
  • PostgreSQL for Durable Ticketing & Schedules: PostgreSQL handles only transactional ticket purchases, rider accounts, and timetable versions.
export async function getRouteLiveStatus(routeId: string) {
  const cacheKey = `transit:route:status:${routeId}`;
  const cached = await redis.get(cacheKey);
  if (cached) {
    return JSON.parse(cached);
  }

  const [schedule, liveVehicles] = await Promise.all([
    db.query(
      `SELECT stop_id, arrival_time, sequence_no
       FROM route_stops
       WHERE route_id = $1 AND active = true
       ORDER BY sequence_no ASC`,
      [routeId]
    ),
    redis.hgetall(`transit:vehicles:route:${routeId}`),
  ]);

  const payload = mergeScheduleWithTelemetry(schedule.rows, liveVehicles);
  await redis.set(cacheKey, JSON.stringify(payload), "EX", 5);
  return payload;
}

2. Eliminating N+1 ORM Queries on Multi-Stop Route Timetables

Our second bottleneck came from an ORM relation loader that executed a separate SQL query for each bus stop along a 28-stop corridor (1 + 28 round-trips per request).

We replaced the nested ORM relation calls with a single composite-indexed SQL query using PostgreSQL jsonb_agg so the entire route timetable returned in a single network round-trip in under 4.2ms.


3. Stampede Protection with Single-Flight Cache Refresh

With a 5-second TTL on hot route keys, 500 concurrent requests arriving at second 6 could all miss the cache simultaneously and hammer PostgreSQL. We wrapped the cache-miss loader in an in-memory single-flight promise coalescer so only 1 query hits PostgreSQL per route every 5 seconds while the other 499 requests share the resolved payload.

Infrastructure & PostgreSQL Advisory

Scaling Infrastructure or Diagnosing P95 Latency?

I help engineering teams diagnose PostgreSQL indexing bottlenecks, concurrency limits, and K3s/Docker deployments. Book a 20-minute call with me to review your setup.

Related Technical Articles