Skip to content

perf: Frontend polling storm + backend N+1 causing production sluggishness #269

Closed

Description

@vybe

Problem

Production UI is sluggish — pages load but content takes seconds to appear, and navigation feels stuck/unresponsive. Root cause investigation identified compounding bottlenecks across frontend polling, backend API design, and database contention.

Key Metrics (Current State)

  • 64+ API requests/minute from frontend polling alone (Dashboard + AgentDetail + NavBar)
  • 12 sequential API calls on AgentDetail mount before anything renders
  • N+1 Docker + HTTP + DB queries on every /api/agents/context-stats poll (every 5s)
  • 100ms artificial delay on every route navigation
  • SQLite single-writer contention from 3 concurrent background services

High-Impact Fixes

Quick Wins (1-2 hours total)

  • Cache context-stats with 10-30s TTL on backend — eliminates repeated N+1 Docker lookups, HTTP calls to every agent, and DB queries that fire every 5 seconds
  • Parallelize mount API calls with Promise.all() — Dashboard (6 sequential calls) and AgentDetail (12 sequential calls) should batch independent requests. Expected: 60-70% faster initial page load
  • Remove 100ms setTimeout in router guard (src/frontend/src/router/index.js ~line 210) — artificial delay on every navigation
  • Add document.hidden checks to skip polling when tab is backgrounded — cuts background load by ~50%
  • Set axios request timeout (30s) — prevents UI freeze when backend is slow; currently no timeout configured

Medium Effort (half day)

  • Reduce polling frequencies — context polling 5s→15s, agent refresh 10s→30s. Reduces request volume by ~70%
  • Batch Docker lookups in context-stats — replace per-agent containers.get() (sync, blocks event loop) with single containers.list(filters=...) call
  • Wrap Docker SDK calls in run_in_executor in src/backend/services/agent_service/stats.py — Docker SDK is synchronous, currently blocking the async event loop
  • Stagger background services — cleanup service (5min) and operator queue (5s) both write to SQLite; stagger to reduce lock contention
  • Fix KeepAlive polling leak — AgentDetail uses <KeepAlive> in App.vue; switching agents can accumulate polling timers that don't get properly cleaned up in onDeactivated

Larger Effort (1-2 days)

  • Push agent stats via WebSocket instead of polling — single connection replaces dozens of polling intervals; would reduce request volume by ~90%
  • Switch to aiosqlite or PostgreSQL — eliminates single-writer contention from concurrent background services and HTTP handlers

Files Affected

File Issue
src/frontend/src/views/Dashboard.vue (lines 573-622) Waterfall mount + 3 polling intervals
src/frontend/src/views/AgentDetail.vue (lines 937-1008) 12 sequential mount calls + 3 polling intervals
src/frontend/src/stores/network.js (lines 1072-1164) Context/agent/activity polling
src/frontend/src/stores/agents.js (lines 677-704) Context polling (5s)
src/frontend/src/components/NavBar.vue (lines 258-275) Global alert/notification polling
src/frontend/src/router/index.js (~line 210) 100ms navigation delay
src/backend/services/agent_service/stats.py (lines 22-82) N+1 Docker lookups, sync blocking
src/backend/db/connection.py (lines 16-28) No connection pooling, 30s timeout
src/backend/main.py (lines 292-303) 3 competing background services

Acceptance Criteria

  • Dashboard initial load < 2 seconds (currently 5-10s+)
  • Background API request volume < 15 req/min (currently 64+)
  • No UI freeze during cleanup service sweeps
  • Navigation between pages feels instant (no perceptible delay)

Activity

  1. added
    enhancement
    priority-p1
    type-refactor
    status-in-progress
    on Apr 5, 2026
  2. removed
    status-in-progress
    on Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpriority-p1type-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions