Skip to content

Backend storage_executor queue spikes from unbounded fan-out submissions #7387

Closed

Description

@beastoin

Problem

Backend (non-sync) storage_executor queue spikes to 738-844 at 96 workers while backend-sync stays stable at 173 max queue with identical pool sizing. The difference is caused by unbounded fan-out patterns in storage task submissions.

Evidence

Post-deploy monitoring (PR #7378, #7380) — storage pool at 96 workers:

Service Avg Queue Max Queue Status
backend-sync 37.6 173 Stable
backend 350.8 844 Worsening with traffic

backend-sync queue is bounded; backend queue scales with traffic volume.

Root Cause: 3 Unbounded Fan-Out Patterns

1. Audio chunk downloads (utils/other/storage.py:764-767)

  • download_audio_chunks_and_merge() submits one storage_executor.submit() per audio timestamp
  • 30-second audio = 30 parallel tasks per merge call
  • No per-request concurrency limit

2. Audio precaching (routers/sync.py:209,308 + utils/other/storage.py:997)

  • _precache_all_parallel() and _cache_uncached_parallel() submit one task per audio file
  • Each file's merge triggers chunk downloads (pattern Client #1)
  • Single precache request with 10 files × 30 chunks = 300+ tasks

3. Knowledge graph rebuild (utils/llm/knowledge_graph.py:263)

  • rebuild_knowledge_graph() submits one task per memory in user history
  • User with 1,000 memories = 1,000 concurrent storage tasks
  • No batching or limit

Why Backend >> Backend-Sync

  • backend-sync: mobile sync uploads use 60s batch accumulation (pusher), 3 tasks per batch flush. Predictable, bounded.
  • backend: HTTP API serves on-demand audio downloads and precaching with per-chunk fan-out. One request can monopolize the pool.

Call Site Inventory

42 total storage_executor submission sites across 15 files:

  • routers/sync.py: 10 calls (precaching + cleanup)
  • routers/apps.py: 7 calls (logo/thumbnail uploads)
  • utils/other/storage.py: 4 calls (chunk download fan-out, merge upload, precache)
  • routers/pusher.py: 3 calls (batch flush)
  • routers/imports.py: 3 calls (ZIP processing, long-running import)
  • utils/chat.py: 3 calls (voice message cleanup)
  • routers/transcribe.py: 2 calls (speaker profile audio)
  • utils/speaker_sample_migration.py: 4 calls
  • Others: 6 calls (knowledge_graph, notifications, postprocess, vad, speech_profile, speaker_embedding)

Affected Services

  • backend (Cloud Run) — primary impact
  • backend-sync (Cloud Run) — stable but same code paths exist
  • backend-listen (GKE) — shares codebase

References

Activity

  1. added
    p1
    on May 19, 2026
  2. beastoin commented on May 19, 2026

    @beastoin
    CollaboratorAuthor

    Update: Mon's Production Traffic Analysis

    The burst analysis reveals this is primarily a traffic volume + instance count problem, not just fan-out patterns.

    Key Finding: Backend Instance Imbalance

    Service Instances Req/Min (total) Req/Min/Instance
    backend 3 ~3,350 ~1,117
    backend-sync 5 ~35 ~7

    Backend handles 160x more load per instance than backend-sync with identical pool sizing.

    Top Storage-Heavy Endpoints (45-min window)

    Endpoint Slow Requests Avg Latency p99
    /v3/memories (GET) 1,136 1.69s 8.39s
    /v1/dev/user/conversations 510 1.42s 6.55s
    /v1/conversations 256 2.04s 19.58s
    /v1/knowledge-graph 144 1.73s 6.52s

    /v3/memories alone consumes ~44% of storage pool capacity (25 slow ops/sec at 1.7s avg, 96 workers can process ~56 ops/sec).

    Queue Pattern: Bursty, Not Monotonic

    At 96 workers the queue recovers between bursts (hit 0 at 10:07, spiked to 844 at 10:57). This confirms 96 workers handle steady-state but not burst peaks. Background cron/batch contributes ~20 ops/min — negligible vs 3,300 req/min API traffic.

    Visibility Gap

    Only 1 of 3 backend instances reports executor_pool_health logs. The 844 max queue is from a single instance.

    Updated Recommendations

    The unbounded fan-out patterns (audio chunk downloads, precaching, KG rebuild) from the original issue remain valid as medium-term optimizations, but the primary driver is sustained API traffic volume on too few instances.

  3. beastoin commented on May 19, 2026

    @beastoin
    CollaboratorAuthor

    Correction: /v3/memories is NOT the storage queue driver

    Previous analysis incorrectly attributed storage_executor saturation to /v3/memories. GET /v3/memories does not use storage_executor at all — it's a pure Firestore read (memories_db.get_memories) running on db_executor. The 1.7s avg latency on memories is a symptom of overall instance overload, not a cause of storage queue buildup.

    Actual storage_executor load sources (code-traced)

    Primary driver: process_conversation → precache_conversation_audio (utils/conversations/process_conversation.py:820)

    Every completed conversation triggers precache_conversation_audio(), which fans out into storage_executor:

    • Per audio file: storage_executor.submit(_cache_single, af) (storage.py:997)
    • Per file merge: download_audio_chunks_and_merge() → per-chunk fan-out (storage.py:764-767)
    • A conversation with 10 audio files × 30 chunks each = 300+ storage_executor tasks

    process_conversation is called from 6 different entry points on backend:

    • routers/pusher.py:134 — pusher conversation processing
    • routers/transcribe.py:778,1749,2793 — WebSocket session conversation timeouts
    • routers/conversations.py:95,120 — conversation create/reprocess API
    • routers/developer.py:882,1047 — developer API conversation creation
    • routers/integration.py:144 — integration webhook processing
    • routers/sync.py:717,1022 — sync pipeline conversation processing

    Secondary: direct audio download fan-out

    • routers/sync.py:374,386 — on-demand audio merge for sync endpoint
    • utils/speaker_identification.py:391 — speaker sample audio download

    Tertiary: apps uploads, imports, speaker profile I/O

    • routers/apps.py — 7 call sites for logo/thumbnail GCS uploads (per-request, low volume)
    • routers/imports.py:78 — long-running import job holds storage thread >30s
    • routers/transcribe.py:1779,1786 — speaker profile audio reads (per-session)

    Why backend has 4x the storage load vs backend-sync

    Both services run process_conversation, but backend receives it from more entry points at higher volume:

    • Backend handles pusher callbacks, WebSocket conversation timeouts, conversation API, developer API, and integration webhooks — all triggering audio precache fan-out
    • Backend-sync only calls process_conversation from the sync router
    • Backend's 3,300 req/min includes WebSocket-triggered conversation processing, not just HTTP GETs

    What to fix

    The root cause is unbounded fan-out in audio precaching, not endpoint latency or instance count. Each precache_conversation_audio call submits N×M tasks (files × chunks) to storage_executor with no per-request concurrency limit. Adding a per-call semaphore (e.g., max 16 concurrent chunk downloads per merge operation) would cap burst submissions without requiring more threads or instances.

    Instance scaling (3→5) would still help by distributing process_conversation calls across more instances, but won't fix the fundamental per-request fan-out pattern.

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

    p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions