Skip to content

perf(asyncio): gate pipeline packing offload on batch size - #4561

Merged
petyaslavova merged 4 commits into
redis:masterfrom
BlueX888:takeover/4210-pipeline-packing-threshold
Nov 4, 2026
Merged

petyaslavova merged 4 commits into
redis:masterfrom
BlueX888:takeover/4210-pipeline-packing-threshold

Conversation

@BlueX888

@BlueX888 BlueX888 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Takes over #4210 per @petyaslavova's open-for-takeover note. Builds on @lh0156's commit (72c9a1d9 is a cherry-pick -x of b2996ac; authorship preserved) and applies the review checklist. Follow-up to #3416 (large async pipelines block the event loop while packing).

Change

  • Packs async pipelines off the event loop with asyncio.to_thread only when the batch reaches a threshold — _PIPELINE_PACK_OFFLOAD_MIN_COMMANDS = 1000 (private module constant; measured: the dispatch costs ~0.07–0.10 ms while packing takes ~0.95 ms at 1000 commands, so below the threshold the inline path is cheaper). Small pipelines keep master's exact synchronous behavior.
  • HIMPORT preflight commands stay inside the packed batch, and transaction/pipeline reply semantics are unchanged; the transaction path now passes a materialized list instead of a generator (no shared iterator).
  • The _execute_pipeline conflict with current master was resolved keeping master's HIMPORT preflight inside the packed batch.

Checklist mapping (per the takeover note)

Tests

  • tests/test_asyncio/test_pipeline.py (7 selected, both transaction modes): small pipelines never call asyncio.to_thread; large pipelines offload with MULTI/EXEC ordering intact; the event-loop-not-blocked regression now runs at threshold size; HIMPORT PREPARE stays first in the offloaded batch.
  • Red on master: the new tests fail at collection, and the original regression fails assert 0 > 0.
  • Offloaded wire output verified byte-identical to inline packing across 16 batch sizes (0–5000 commands). ruff check / ruff format --check clean. The 79 live-server tests were not run locally (no Docker daemon) — CI runs them.

Note

Medium Risk
Changes the async pipeline execute hot path and thread-pool usage for large batches; behavior is threshold-gated and tests assert wire ordering, but mis-gating could add latency or reintroduce event-loop blocking.

Overview
Fixes large async pipelines blocking the event loop during RESP packing (#3416) by routing packing through a new _pack_commands helper on Pipeline.

Behavior: batches with fewer than 1000 commands still call connection.pack_commands on the event loop (unchanged). At 1000+ commands, packing runs via asyncio.to_thread so CPU-heavy work does not stall other coroutines. _PIPELINE_PACK_OFFLOAD_MIN_COMMANDS documents the threshold; async cluster pipelines are explicitly out of scope.

Call sites: _execute_transaction and _execute_pipeline now await _pack_commands instead of packing inline; the transactional path passes a materialized command list (not a generator). HIMPORT preflight commands remain in the same packed batch before send.

Tests: New asyncio pipeline tests cover non-blocking packing at threshold size, no to_thread for small batches, offload + MULTI/EXEC ordering for large batches, and HIMPORT PREPARE still first when offloaded.

Reviewed by Cursor Bugbot for commit bb08761. Bugbot is set up for automated code reviews on this repo. Configure here.

lh0156 and others added 2 commits November 3, 2026 01:54
Move async pipeline command packing to a worker thread so large transactions and pipelines do not monopolize the event loop.\n\nRefs redis#3416

(cherry picked from commit b2996ac)
Only move pipeline command packing onto a worker thread when the batch
has at least _PIPELINE_PACK_OFFLOAD_MIN_COMMANDS (1000) commands.
Small pipelines keep the synchronous path, so they avoid the
asyncio.to_thread dispatch and the loop's shared default executor;
large batches still pack off the event loop.

The 1000-command cut-off comes from measuring pack_commands against the
to_thread dispatch on this machine (median of 15 runs, tiny PING
commands):

  commands    pack ms   to_thread ms
        10      0.015          0.119
       100      0.099          0.209
       500      0.469          0.577
      1000      0.952          1.066
     10000      9.831          9.987

At ~1000 commands the packing alone is about a millisecond, which
outgrows the ~0.1 ms dispatch. The threshold is a module-level
constant, not a new public option, and can be tuned if maintainers
prefer a different cut-off.

HIMPORT PREPARE preflight commands stay inside the packed batch, and
the MULTI/EXEC framing and reply handling are unchanged on the
offloaded path.

Tests: small pipelines do not call asyncio.to_thread; large pipelines
do (both transaction modes); the event-loop-not-blocked regression uses
a large batch; HIMPORT preflight remains in the offloaded batch.

Follows the original offload in redis#4210.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T09:53:33.423445Z bb08761 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2df4f2dc95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/test_asyncio/test_pipeline.py Outdated
Comment on lines +61 to +62
timer = threading.Timer(0.05, release_packing.set)
timer.start()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Release the packer only after the worker has started

If worker dispatch takes over 50 ms, such as on a loaded CI runner, this timer sets release_packing before pack_commands starts. The worker then sets both packing_started and packing_finished without waiting, leaving event_loop_ticks_during_packing at zero even though offloading is correct. I reproduced this false failure in both transaction modes by delaying the default-executor worker for 150 ms. Have the observer release the worker after recording a tick during packing, retaining a timeout only as a deadlock safeguard.

Useful? React with 👍 / 👎.

@BlueX888 BlueX888 Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 79697c5. The observer now sets release_packing only after it has actually recorded a tick while packing_started is set and packing_finished is clear; the fixed 50 ms timer is gone, and the worker's wait(timeout=1) plus the outer wait_for remain as the deadlock guard. Reproduced the race by delaying asyncio.to_thread dispatch by 150 ms: the previous design recorded 0 ticks in both transaction modes, while the new design passes in both. ruff check and ruff format --check pass. (The branch head was force-updated once to normalize line endings; the diff content is unchanged.)

@BlueX888
BlueX888 force-pushed the takeover/4210-pipeline-packing-threshold branch from 3f73e52 to 79697c5 Compare November 3, 2026 03:17

@petyaslavova petyaslavova left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @BlueX888, thank you for your contribution!

This covers everything from the takeover note: the offload only kicks in at the batch-size threshold, small pipelines keep the existing inline path, and async cluster pipelines are left as a follow-up. The event-loop regression test with the observed-tick release is a nice touch. LGTM.

@petyaslavova petyaslavova added the maintenance Maintenance (CI, Releases, etc) label Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Maintenance (CI, Releases, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants