Skip to content

perf: research real-time event notification alternatives to reduce polling cost #863

@lancy

Description

@lancy

Background

The CLI currently uses short polling (500ms interval) to fetch agent run events from the server. While this approach is simple and serverless-friendly, it may incur unnecessary server costs due to frequent requests.

Current Implementation

  • Location: turbo/apps/cli/src/commands/run.ts:185-263
  • Polling interval: 500ms (2 requests/second)
  • Endpoint: GET /api/agent/runs/{runId}/events
  • Data sources: Events from Axiom, status from PostgreSQL
CLI (Polling Client)
    |
    | HTTP GET (every 500ms)
    v
Vercel Serverless Function
    |
    +---> PostgreSQL (run status)
    +---> Axiom (agent events)
    |
    v
JSON Response

Research Findings

SSE (Server-Sent Events) - Not Recommended

Vercel officially states:

"SSE is an inefficient transport. It keeps a persistent connection open between client and server, even when idle."

Problems with SSE on Vercel:

  • Persistent connections incur continuous billing (entire connection duration)
  • Function duration limits (300s-800s max) would require reconnection logic
  • SSE endpoint would still need to poll Axiom internally
  • MCP specification deprecated SSE in favor of Streamable HTTP (March 2025)

Long Polling - Mixed Results

  • Fewer requests but longer execution time per request
  • Still holds connection open, consuming serverless execution time
  • Net cost benefit unclear

Vercel's Recommended Approaches

  1. Third-party Pub/Sub services (Pusher, Ably, PubNub, etc.)

    • Client subscribes directly to the service
    • Serverless function only publishes events (quick execution)
    • Real-time push without holding connections
  2. SWR (Stale-While-Revalidate)

    • Vercel's own dashboard uses this pattern
    • On-demand fetching with intelligent caching
    • Supports adaptive polling intervals

Cost Comparison (Theoretical)

Approach Vercel Cost Third-party Cost
Short Polling (current) ~100ms execution × 2/sec None
SSE Entire connection duration None
Pusher/Ably Minimal (publish only) ~$49/month minimum

Proposal

Consider integrating Pusher (or similar service like Ably) for real-time event delivery:

How It Would Work

Sandbox                      Vercel                     Pusher                CLI
   |                           |                          |                    |
   |-- webhook (events) ------>|                          |                    |
   |                           |-- trigger(event) ------->|                    |
   |                           |                          |-- push ----------->|
   |                           |                          |                    |

Benefits

  • Reduced Vercel execution time (only publish, no polling)
  • Lower latency (real-time push vs 500ms polling)
  • Better scalability (Pusher handles connections)
  • Vercel officially recommends this pattern

Trade-offs

  • Additional service cost (~$49+/month)
  • New dependency to manage
  • Requires CLI to include Pusher client SDK

Research Tasks

  • Evaluate Pusher vs Ably vs alternatives for our use case
  • Calculate actual cost savings vs third-party service fees
  • Prototype Pusher integration
  • Measure latency improvement
  • Consider fallback strategy (polling as backup)

References


🤖 Generated from conversation with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions