Does CouchDB need a server‑side abort mechanism for cancelled _changes feeds and view queries?
0 reputation · 11 Jun 2021, 01:50 UTC
0 reputation · 11 Jun 2021, 01:50 UTC
The goal is to ensure that when a client cancels a request—by closing the TCP connection or exceeding a timeout—any server‑side work such as ongoing map‑reduce index updates or _changes feed processing is halted promptly to avoid wasting CPU and I/O resources.
CouchDB currently stops transmitting _changes events after detecting a closed socket, but ongoing view index updates continue until the next batch boundary. The built‑in _changes endpoint does not honor HTTP Timeout or Connection: close to abort server‑side work, and official client libraries do not propagate cancellation contexts. Behavior may differ between CouchDB 2.x and 3.x and can be masked by reverse proxies that delay socket closure detection.
35775 reputation · 11 Jun 2021, 02:54 UTC
CouchDB does not provide a server-side abort mechanism that immediately halts ongoing map-reduce computations or view index updates when a client disconnects. While the server detects a closed socket and stops transmitting data via _changes feeds, the background worker process often continues until it reaches the next batch boundary or internal checkpoint.
In CouchDB (specifically versions 3.x using the mem3 engine), view queries and index updates are processed in discrete batches. When a client cancels a request via a timeout or TCP closure:
Whether a server-side abort signal is required depends on your resource constraints. In high-concurrency environments where "zombie" queries consume significant CPU and I/O after a client has left, a mechanism would be beneficial. However, introducing a hard abort risks corrupting index state if the storage engine is not designed to roll back partial batch-level map-reduce cycles safely.
If CouchDB were to implement a granular abort signal, it would need to address the following:
<mem3 engine must ensure that an aborted query correctly discards its in-memory updates without triggering unnecessary flushes or locking the B-tree structure.Context propagation (in languages like Go or Python) to ensure that a timeout in the application code actually triggers the underlying socket closure that CouchDB monitors.To manage resource exhaustion without a native abort mechanism, consider these scoped approaches:
proxy_read_timeout and proxy_connect_timeout in your reverse proxy to ensure sockets are closed quickly when the client fails.limit values in view queries to reduce the size of each batch, allowing the server to check the connection status more frequently.To provide a more specific recommendation, are you observing high CPU spikes that persist long after client-side requests have been terminated?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.