Skip to content

[Feature Request] Gateway: bound async runtime worker threads with a sensible default and make it env-configurable #638

Open

Description

@xgerman

Purpose of the feature.

The standalone DocumentDB gateway builds its Tokio multi-threaded runtime with a
worker-thread count that defaults to the number of visible logical CPUs
(std::thread::available_parallelism()), with no upper bound and no
environment-variable override
. On large hosts (many cores, or bare metal), the
gateway spawns roughly one worker thread per core regardless of actual load. This
inflates the gateway's baseline memory footprint (thread stacks + per-worker
runtime structures) and makes the footprint unpredictable — which matters when the
gateway is co-located with PostgreSQL or packed densely on a node.

Current behavior (on main):

  • pg_documentdb_gw/documentdb_gateway/src/main.rs:57-59 builds the runtime with
    .worker_threads(setup_configuration.async_runtime_worker_threads()).
  • pg_documentdb_gw/documentdb_gateway_core/src/configuration/setup.rs:932-938 —
    when unset, async_runtime_worker_threads falls back to
    available_parallelism() (i.e. all visible CPUs, floored at 1).
  • The field async_runtime_worker_threads: Option<usize>
    (setup.rs:99) is only settable via the JSON setup configuration. The
    recognized environment variables (env_keys, setup.rs:23-47) are limited to
    DOCUMENTDB_PG_URL_FILE, DOCUMENTDB_LISTEN_ADDR, DOCUMENTDB_TLS_*, and
    DOCUMENTDB_LOG_LEVEL — there is no env var for worker threads.

Net effect: there is no ergonomic way to cap the gateway's runtime thread count,
and the out-of-the-box default is "use every CPU on the machine."

Describe the solution you'd like

  1. A reasonable bounded default when async_runtime_worker_threads is unset,
    instead of "all CPUs" — e.g. min(available_parallelism(), N) for a small,
    documented N, or a default derived from a documented heuristic. (The exact
    number is open for discussion.)
  2. An environment-variable override (e.g.
    DOCUMENTDB_ASYNC_RUNTIME_WORKER_THREADS) wired through apply_env_overlays,
    so deployments can tune/cap the thread count without editing JSON config or
    rebuilding — including bare-metal deployments where there is no cgroup CPU
    limit to constrain available_parallelism().
  3. Log and document the resolved value (the count is already logged at
    main.rs:64); document the new knob and its precedence.

Describe alternatives you've considered

  • Cgroup CPU limit: in containers, available_parallelism() is cgroup-aware
    on modern Linux/Rust, so a CPU limit indirectly caps the thread count. This does
    not help bare-metal / no-cgroup deployments, and it couples thread count to CPU
    quota, which is not always desirable.
  • OS CPU affinity (taskset/cpuset/numactl): works on bare metal but is
    operationally awkward and not discoverable.
  • Status quo (JSON-only field): requires shipping/managing a config file just
    to cap threads; no env-var ergonomics.

Additional context

Scenarios to validate:

  • Start the gateway on a many-core host with no override → worker-thread count is
    the new bounded default (not the core count); confirm via the startup log line.
  • Set DOCUMENTDB_ASYNC_RUNTIME_WORKER_THREADS=2 → exactly 2 worker threads
    regardless of host core count; verify reduced RSS at idle.
  • Explicit JSON AsyncRuntimeWorkerThreads still works; define and document
    precedence between the env var and the JSON field.
  • Invalid / zero value → rejected or clamped to >= 1 with a clear message.

Activity

  1. added
    enhancement
    gateway
    on Jun 23, 2026
  2. documentdb-triage-tool commented on Jul 8, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  3. documentdb-triage-tool commented on Jul 23, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  4. documentdb-triage-tool commented on Aug 7, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  5. AndrewKhoma commented on Aug 7, 2026

    @AndrewKhoma
    Member

    @xgerman did you tried setting the TOKIO_WORKER_THREADS? Looks like environment variable you've been looking for. And I don't think it should be part of the gateway since we need to utilize all of the available resources

  6. documentdb-triage-tool commented on Aug 22, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  7. documentdb-triage-tool commented on Sep 5, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  8. documentdb-triage-tool commented on Sep 19, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  9. documentdb-triage-tool commented on Oct 3, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  10. documentdb-triage-tool commented on Oct 28, 2026

    @documentdb-triage-tool

    👋 @AndrewKhoma — this issue has been assigned for 25 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  11. self-assigned this
    on Nov 3, 2026
  12. documentdb-triage-tool commented on Nov 17, 2026

    @documentdb-triage-tool

    👋 @tsharp, @AndrewKhoma — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementgatewayperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions