Skip to content

Migrate long-running sync pipelines to async task queue (arq/SAQ) #7371

Closed as not planned

Description

@beastoin

Context

PR #7362 fixed sync pool starvation (#7361) by replacing submit_with_context(postprocess_executor, pipeline) with start_background_task(async_pipeline). This eliminates thread pool slot holding during 60-300s pipelines.

However, in-process asyncio.create_task has known production risks on Cloud Run:

Problem

  1. Scale-down kills in-flight tasks — Cloud Run sends SIGTERM with 10s grace. A 300s pipeline is lost.
  2. No persistence — deploy/restart = all in-flight pipelines silently dropped.
  3. No retry — transient DG/LLM failures = permanent failure.
  4. CPU throttling — Cloud Run may throttle CPU after HTTP response unless --no-cpu-throttling is set.
  5. No cross-instance backpressure — 25 instances × 12 concurrency = 300 slots with no global coordination.

Proposal

Move sync pipelines (and other long-running background work) to an async-native Redis-backed task queue:

Option Pros Cons
arq Async-native, lightweight, established Maintenance-mode, polling-based (~500ms latency)
SAQ Async-native, event-based (<5ms), built-in web UI, actively maintained Smaller ecosystem
Dramatiq Reliable delivery, simpler than Celery Less async-native

Recommended: arq or SAQ — both are async-native and fit the existing FastAPI/asyncio stack. SAQ has lower dispatch latency and a monitoring UI.

Architecture

FastAPI handler → validate → write job record → enqueue to Redis → return 202
                                                     ↓
                                              Worker process(es)
                                                     ↓
                                    async pipeline (decode → VAD → STT → LLM)
                                    each step: await run_blocking(pool, fn)
  • Pipelines must be idempotent (Redis redelivery on worker crash)
  • Job status already tracked in Firestore (sync_jobs collection) — no new state store needed
  • Workers can scale independently from API instances

References

  • Python asyncio docs: strong-reference set + done_callback pattern
  • CPython issue #104091: create_task fire-and-forget considered bad practice
  • Cloud Run CPU throttling: blog post
  • FastAPI docs: recommends Celery/arq for heavy background work
  • aiohttp: recommends aiojobs over bare create_task

Acceptance Criteria

  • Choose arq vs SAQ (spike: deploy both with a test pipeline, compare latency + ops)
  • Redis instance provisioned (or reuse existing cache Redis)
  • Sync pipeline runs as queue worker task with retry/backoff
  • Job status updates continue to Firestore (existing polling API unchanged)
  • Graceful shutdown: worker drains in-flight tasks before exit
  • Monitoring: queue depth, worker count, task duration metrics
  • No regression in sync v2 latency (P50/P90/P99)
  • Cloud Run scale-down no longer loses in-flight pipelines

Activity

  1. added
    enhancement
    backend
    p2
    intelligence
    p3
    and removed
    p2
    on May 23, 2026
  2. beastoin commented on Jun 10, 2026

    @beastoin
    CollaboratorAuthor

    Closing as not planned per CTO directive (2026-06-10).


    by AI for @beastoin

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

    backendenhancementintelligencep3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions