ADR 018: Use Celery threads for I/O concurrency with cooperative time limits

Status Proposed
Date 2026-12-02
Issue https://github.com/baserow/baserow/issues/6931
Author Davide Silvestri (@silvestrid)

Summary

Run the development queues and beat in one threaded Celery worker. Offer threads as a production opt-in in the next rollout step. Prefork remains the production default and the appropriate choice when a task requires forced termination.

Context

With prefork, each concurrent task needs a child process with Django loaded. Keeping just one slot saves memory, but a long export or model call can occupy that worker while other tasks wait. On the laptop measured for this change, the prefork dev setup used 1.3–3.6 GB. The single threaded worker used 0.3–0.6 GB. Threads can share that memory and make progress while another task waits for I/O. Development measurements

Decision

Prefork can terminate and replace one child at the hard limit. Python has no safe equivalent for a running thread. Injecting an exception as a thread acquires a lock can leave the lock held. Restarting the process would interrupt every task in the worker.

On threads, Baserow delivers a pending time limit at the task thread’s next ordinary Django query. At the hard limit it also asks PostgreSQL to cancel a query already running on that thread’s connections. Code waiting elsewhere can continue past either limit. The worker reports a task still running 60 seconds after its hard limit and keeps serving work on its remaining slots.

Contract for task authors

  • Bound blocking calls where they happen. Give network and other blocking I/O finite timeouts. For multiple calls or retries, carry a deadline through the operation so each attempt uses the time remaining. A single Redis send can do useful work without ever reaching a SQL query.
  • Give long loops a place to stop. Check the deadline between bounded batches, before starting more work. Let time-limit exceptions unwind the current operation and release locks and connections. A broad exception handler that retries the same work can keep the worker slot occupied.
  • Account for work handed off. Cancelling a wait can leave a tool thread running or a remote request already accepted. A timeout alone does not make retrying a write safe. For example, a chat accepts another message after its grace period, and warns that an earlier tool may still finish a write. Background chat runs

Consequences

A cooperative task can stop at a safe boundary. If a library blocks indefinitely or ignores cancellation, the slot stays occupied until the task exits. Use the overdue metric and Sentry report to identify that task and fix the blocking operation. Workloads that need their local execution stopped independently belong on prefork. Neither pool can recall an external request or undo a committed write.

Threads also share CPU and the worker’s process memory limit. A process crash loses all active tasks in that worker, and increasing concurrency can open more database connections. Before changing the production default, use the opt-in rollout to evaluate overdue tasks, worker memory, connection use and queue wait times. Rollout plan