ray icon indicating copy to clipboard operation
ray copied to clipboard

[Serve] [RFC] Topology spread rules for replica scheduling

Open YashwanthRanjanSingaravel opened this issue 3 weeks ago • 5 comments

Summary

Add topology_spread to Serve deployments: an ordered list of rules, each a floor on how many distinct nodes, zones or slices the replicas cover. Each rule can be marked required=False, the default, making it a best effort rule that never blocks placement, or required=True, making it a required rule that holds a replica pending until a node satisfies it, optionally only up to a per rule timeout. A rebalancer moves running replicas back toward their rules once their deployment settles.

Motivation

Serve can't express "spread this deployment over at least N nodes, zones or slices, then pack the rest."

  • Pack has no floor. Six replicas on two nodes can land 6 and 0, so one node failure stops the deployment.
  • Ray SPREAD isn't deployment aware. One round robin cursor serves every actor, so two replicas of one deployment can share a node.
  • max_replicas_per_node is a ceiling. It needs the node size and replica count up front.
  • Downscaling drops the spread. A 3 and 1 split across two nodes can become 3 and 0.
  • The node isn't the only failure unit. A zone, rack or TPU slice can fail as a whole.
  • Goals conflict. A deployment can want two zones and three nodes. When both can't hold, users can't say which matters more, or whether a replica should wait.
  • Placement happens once. Replicas that left a failed zone don't return when it recovers.

Goals

  • A per deployment floor on any node label.
  • An explicit order between rules, and a per rule choice: wait for a node that satisfies it, or place the replica anyway.
  • Floors that hold through downscaling and rolling updates.
  • Running replicas that move back toward their rules.

Non goals

  • Even replica counts. A floor guarantees coverage, not balance.
  • Replacing pack or spread scheduling, max_replicas_per_node, label selectors or placement groups.
  • Changing request routing.
  • Spreading gang scheduled deployments.

Background: labels as domains

A domain is the set of nodes that share one value of a node label. Ray sets ray.io/node-id on every node, ray.io/availability-zone when the environment names a zone, and ray.io/tpu-slice-name on TPU nodes. Every node has its own ray.io/node-id, so a floor on that key counts distinct nodes.

Proposed API

from ray.serve.config import TopologySpread

@serve.deployment(
   num_replicas=6,
   topology_spread=[
       TopologySpread(
           label_key="ray.io/availability-zone",
           min_domains=2,
           required=True,
           wait_timeout_s=600,
       ),
       TopologySpread(label_key="ray.io/node-id", min_domains=3),
   ],
)
class Ranker: ...

The same field works in a config file:

deployments:
 - name: Ranker
   num_replicas: 6
   topology_spread:
     - label_key: ray.io/availability-zone
       min_domains: 2
       required: true
       wait_timeout_s: 600
     - label_key: ray.io/node-id
       min_domains: 3

Ranker covers two zones before any zone gets a second replica. Its replicas wait up to 10 minutes for a second zone before doubling up. It also wants three nodes, and gives that up first.

Field Type Meaning
label_key str Node label whose values are the domains.
min_domains int, at least 1 Number of domains the replicas must cover.
required bool, default False False: best effort, never holds a replica back. True: the replica waits for a node that satisfies the rule.
wait_timeout_s float or None, default None Only with required=True. How long a replica waits before Serve places it without the rule. None never relaxes.

Cluster settings:

Variable Default Meaning
RAY_SERVE_MIN_REPLICA_NODES 1 Best effort node floor for every deployment without topology_spread, except gang deployments. Same as [TopologySpread(label_key="ray.io/node-id", min_domains=N)].
RAY_SERVE_REBALANCE_RULES 1 Turns the rebalancer on.

Behavior

Placement. While the replicas cover fewer domains than a floor, the next replica goes to a domain they don't cover yet. Once every floor holds, pack or spread scheduling places the rest.

Default node floor. A deployment without topology_spread gets one best effort rule automatically: TopologySpread(label_key="ray.io/node-id", min_domains=RAY_SERVE_MIN_REPLICA_NODES). With RAY_SERVE_MIN_REPLICA_NODES=2, every such deployment covers two nodes first, then pack or spread places the rest. The default of 1 adds no rule. Setting topology_spread on a deployment overrides this default, and an empty list turns it off. Gang deployments are exempt.

Best effort rules never block. A best effort floor is capped at the replica count and at the number of domains among active nodes that could host the replica, given its label selectors. With 6 replicas and 2 zones, a best effort floor of 3 zones acts as 2.

Precedence. The scheduler looks for a node with room that holds every rule. If none has room, it drops the last listed best effort rule and looks again, until only required rules remain.

A zone floor of 2, then a ray.io/market-type floor of 2. Replica 1 runs on a1 (zone a, spot). a2 (zone a, on demand) holds only the market type floor. b1 (zone b, spot) holds only the zone floor. Replica 2 goes to b1. With the order reversed, it goes to a2.

Required rules wait. A replica stays pending until a node with room satisfies the rule, whether the domain is full or doesn't exist yet. Only as many replicas wait as the floor has missing domains, and Serve places the rest using only the remaining rules, so a zone outage holds back at most one replica per missing zone. While the floor stays unmet, a probe asks the autoscaler for the missing domain. If the rule sets wait_timeout_s, Serve places the replica using only the remaining rules once that time passes, so the deployment reaches its target count.

Downscaling keeps floors. A stop that would leave fewer domains than a floor at the new replica count is refused. If no stop satisfies every rule, Serve drops best effort rules, last listed first, until enough stops are allowed. Required rules are never dropped here, so one can stop a downscale short of its target.

Rolling updates keep floors. While a deployment rolls out a new version, floors count only replicas of the target version, so new replicas spread on their own instead of relying on outdated replicas that are about to stop. Stopping an outdated replica is never refused. If a new replica's required rule needs a domain that outdated replicas fill, an outdated replica there stops first, so the update can't stall.

Status and metrics. When a best effort rule is unmet or a required rule has relaxed, the deployment's status stays HEALTHY, but the status message names any unmet or relaxed rule, and a per rule metric reports covered domains next to the floor.

Validation.

  • min_domains must be a positive integer.
  • wait_timeout_s must be a positive number or None, and is only allowed with required=True.
  • A required rule's min_domains can't exceed num_replicas, or min_replicas with autoscaling. This keeps the target at or above every required floor, and rules out scaling to zero.
  • A rule's expressions merge with the deployment's own label selectors on the same key, so a user's in(a,b,c) and a rule's !in(a) both apply. A config is rejected only when a required rule's selectors allow fewer values than its min_domains. A single value or in(...) allows the values it lists, and the count is the union across label_selector and every fallback_strategy entry, or bundle 0 of placement_group_bundle_label_selector. A selector that doesn't name the key, or names it only with !in(...), doesn't limit the count.
  • topology_spread can't be set with gang_scheduling_config, the same as max_replicas_per_node.

Updates. Changing topology_spread restarts the replicas, like any other placement option.

Rebalancing

The rebalancer moves a running replica to a node with room where it holds more of its rules, counted from the top: every required rule, then best effort rules in list order. It never gives up an earlier rule to gain a later one.

  • When. Only after the moving deployment has been healthy at its target count for RAY_SERVE_NODE_COMPACTION_DELAY_S (300 s), with none of its replicas moving, and with no compaction or drain in progress in the cluster. A deploy, scale event or crash in that deployment restarts its window. Changes in other deployments and nodes joining don't. After a move, the deployment's next move waits only until the replacement runs. Replicas waiting on a required rule, and probes, don't hold back compaction or other deployments' moves.
  • How. Moves use compaction's migration path, so capacity never dips: the replacement starts first, and the old replica stops once the replacement runs. Unlike compaction, a move binds the replacement to the planned node, and is cancelled if that node fills first. A replacement not running within RAY_SERVE_COMPACTION_TIMEOUT_S (1800 s) fails the move, and retries back off, doubling up to RAY_SERVE_COMPACTION_MAX_BACKOFF_TIME_S (3600 s).
  • Compaction keeps rules. Under pack scheduling, compaction moves a replica only to a node where it keeps every rule it holds. A node whose replicas can't all move that way isn't compacted, so compaction never takes a deployment below a floor.
  • Probes. While a required floor is unmet, Serve keeps one probe per deployment pending. A probe is an extra replica that carries only that rule and holds no resources until placed. Its pending demand lets the autoscaler launch a node in a missing domain. When the probe starts, it takes the place of a pending replica, or of one in a doubled up domain. Probes don't count toward the target, health or the settle window.
  • Deterministic. Ties break by id: nodes, then replicas, then deployments. A restarted controller makes the same choices.
  • Not optimal. It makes single moves into free room only. Layouts that need swaps or chains of moves aren't reached.

Three replicas, a best effort floor of 3 nodes, and only w1 and w2 exist. The replicas land on w1, w2 and w1, since the floor is capped at 2. w3 joins. One settle window after the deployment's last change, a replacement starts on w3, and one replica on w1 stops once it runs.

Compatibility

With no topology_spread and the default node floor of 1, placement is unchanged and the rebalancer has nothing to move.

One behavior changes. In the released scheduler, a PACK, SPREAD or STRICT_SPREAD placement group in any deployment turns off pack scheduling for the whole cluster. With this change, pack stays on for every other deployment.

On clusters with pack scheduling on, this also turns on compaction where such a placement group kept it off. Compaction never moves replicas of those placement group deployments, so it never compacts a node that holds one.

Ray Core changes

  • exists() and !exists() label selector operators. A probe for a missing zone asks for exists(),!in(<covered zones>), so a node type that declares no zone never matches.
  • The same operators in autoscaler v2, which treats any operator other than in and !in as no match. Probes rely on autoscaler v2 to launch a node in a missing domain.
  • Several expressions per label key in one selector, so a rule's !in(...) merges with a user's in(...) on the same key.
  • Fallback strategies for placement groups. An actor follows the rule order through label_selector and fallback_strategy, so a best effort rule falls through when no node holds it. A placement group has no fallback, so its rules would sit on bundle 0 as a hard selector, and a best effort rule would make the group wait. Fallbacks let a placement group fall through the same way.

  1. Compaction. The RFC only has the rebalancer wait for compaction; it doesn't say compaction respects rules, and today it doesn't (_find_best_node_to_compact). With pack on, compaction can empty the only zone-b node of a deployment with a required zone rule; the probe then asks for a new zone-b node, which compaction can empty again. Compaction should refuse moves that break a rule.

  2. Rolling updates. Only downscaling keeps rules. With max_surge_percent > 0, replacements are placed while old replicas still cover zone b, then the old ones stop, so an update (including one that changes topology_spread) can end with every replica in zone a. Maybe count only target-version replicas when placing during an update?

  3. required relaxes. A rule that gives up after 300s isn't required, and one cluster-wide wait doesn't fit every deployment, since how long a node in a missing zone takes depends on its node type. Maybe a per-rule wait_timeout_s (None = never relax)?

  4. Visibility. Suggest reporting an unmet or relaxed rule, after a required rule relaxes, a single-zone deployment shows HEALTHY. Add a status message and a metric?

  5. Rebalancer gate. Every deployment must stay at target for 300s and each move restarts that window, so N moves take at least N×300s, and any deployment that scales more often than every 300s blocks them. Gate on the moving deployment only as a move whose target fills is already cancelled.

  6. Validation. Only a single-value selector (a) pins a key; in(a,b,c) and !in(x) narrow the allowed values. ray.io/availability-zone: in(a,b,c) with a zone rule of 2 is valid but would be rejected. Reject only a required rule whose selector allows fewer values than min_domains, and cap best-effort rules at the allowed values?

  7. Gang deployments. The RFC doesn't cover gang scheduling, where replicas are placed and stopped as whole gangs, yet the RAY_SERVE_MIN_REPLICA_NODES default reaches them. Reject topology_spread with gang_scheduling_config (as max_replicas_per_node is) and exempt gangs from the default, or define per-gang spreading.

  8. Unstated effects. On clusters with pack on, keeping it on alongside PG deployments also turns on compaction there; say so under Compatibility. Say what PG fallback strategies are for. Probes also need autoscaler v2 to understand exists(), which it rejects today (scheduler.py).

  9. Reuse compaction's migration. The rebalancer's start-then-stop move, 1800s timeout and 1h backoff cap match compaction's (RAY_SERVE_COMPACTION_TIMEOUT_S, RAY_SERVE_COMPACTION_MAX_BACKOFF_TIME_S). Reuse that path and its settings instead of new ones?

  10. One nit is that the precedence example puts slice s1 in two zones, but a TPU slice sits in one zone. Use labels that really cross, e.g. zone × ray.io/market-type.

johntaylor-cell avatar Nov 10 '26 22:11 johntaylor-cell

Thanks @johntaylor-cell I've updated the RFC to address the comments:

  1. Compaction. The RFC only has the rebalancer wait for compaction; it doesn't say compaction respects rules, and today it doesn't (_find_best_node_to_compact). With pack on, compaction can empty the only zone-b node of a deployment with a required zone rule; the probe then asks for a new zone-b node, which compaction can empty again. Compaction should refuse moves that break a rule.
  • Compaction now moves a replica only to a node where it keeps every rule it holds, so it never takes a deployment below a floor and the probe loop can't start.
  1. Rolling updates. Only downscaling keeps rules. With max_surge_percent > 0, replacements are placed while old replicas still cover zone b, then the old ones stop, so an update (including one that changes topology_spread) can end with every replica in zone a. Maybe count only target-version replicas when placing during an update?
  • During a rollout, floors count only target version replicas. This applies with max_surge_percent at 0 too, since outdated replicas stop outside the floor checks. If a required rule needs a domain that outdated replicas fill, one of them stops first so the update can't stall.
  1. required relaxes. A rule that gives up after 300s isn't required, and one cluster-wide wait doesn't fit every deployment, since how long a node in a missing zone takes depends on its node type. Maybe a per-rule wait_timeout_s (None = never relax)?
  • Replaced RAY_SERVE_REQUIRED_FLOOR_WAIT_S with a per rule wait_timeout_s. None, the default, never relaxes.
  1. Visibility. Suggest reporting an unmet or relaxed rule, after a required rule relaxes, a single-zone deployment shows HEALTHY. Add a status message and a metric?
  • When a best effort rule is unmet or a required rule has relaxed, the deployment's status stays HEALTHY, but the status message names any unmet or relaxed rule, and a per rule metric reports covered domains next to the floor.
  1. Rebalancer gate. Every deployment must stay at target for 300s and each move restarts that window, so N moves take at least N×300s, and any deployment that scales more often than every 300s blocks them. Gate on the moving deployment only as a move whose target fills is already cancelled.
  • Moves now gate on the moving deployment only. Drains and compaction stay cluster wide gates, and a deployment's own move no longer restarts its window. Replicas waiting on a required rule, and probes, don't hold back compaction or other deployments' moves.
  1. Validation. Only a single-value selector (a) pins a key; in(a,b,c) and !in(x) narrow the allowed values. ray.io/availability-zone: in(a,b,c) with a zone rule of 2 is valid but would be rejected. Reject only a required rule whose selector allows fewer values than min_domains, and cap best-effort rules at the allowed values?
  • A rule's expressions now merge with the user's selector on the same key. Only a required rule whose selectors allow fewer values than min_domains is rejected, and best effort rules cap at the allowed values.
  1. Gang deployments. The RFC doesn't cover gang scheduling, where replicas are placed and stopped as whole gangs, yet the RAY_SERVE_MIN_REPLICA_NODES default reaches them. Reject topology_spread with gang_scheduling_config (as max_replicas_per_node is) and exempt gangs from the default, or define per-gang spreading.
  • topology_spread is rejected with gang_scheduling_config, like max_replicas_per_node, and gangs are exempt from the default floor. Spreading gangs is a non goal for now.
  1. Unstated effects. On clusters with pack on, keeping it on alongside PG deployments also turns on compaction there; say so under Compatibility. Say what PG fallback strategies are for. Probes also need autoscaler v2 to understand exists(), which it rejects today (scheduler.py).
  • Compatibility now covers compaction turning on for clusters with placement group deployments. Ray Core changes now say why placement groups need fallbacks, so a best effort rule never makes a group wait, and list exists() support in autoscaler v2.
  1. Reuse compaction's migration. The rebalancer's start-then-stop move, 1800s timeout and 1h backoff cap match compaction's (RAY_SERVE_COMPACTION_TIMEOUT_S, RAY_SERVE_COMPACTION_MAX_BACKOFF_TIME_S). Reuse that path and its settings instead of new ones?
  • Moves reuse compaction's migration path and its timeout and backoff settings, adding only a fixed target node. RAY_SERVE_REBALANCE_TIMEOUT_S is gone.
  1. One nit is that the precedence example puts slice s1 in two zones, but a TPU slice sits in one zone. Use labels that really cross, e.g. zone × ray.io/market-type.
  • The precedence example now crosses zone with ray.io/market-type.

Thanks @YashwanthRanjanSingaravel for addressing the 10 comments. I have one last question: Required rules in a zone outage. Since replicas wait rather than double up, losing zone b leaves its replicas' replacements pending until it returns (indefinitely with the default wait_timeout_s=None), so a 3/3 deployment runs on 3 of 6. Is this intended? Holding back only the replicas the floor needs (min_domains minus covered) might keep the floor without that loss, since balance is a non-goal.

johntaylor-cell avatar Nov 17 '26 16:11 johntaylor-cell

Thanks @YashwanthRanjanSingaravel for addressing the 10 comments. I have one last question: Required rules in a zone outage. Since replicas wait rather than double up, losing zone b leaves its replicas' replacements pending until it returns (indefinitely with the default wait_timeout_s=None), so a 3/3 deployment runs on 3 of 6. Is this intended? Holding back only the replicas the floor needs (min_domains minus covered) might keep the floor without that loss, since balance is a non-goal.

Thanks @johntaylor-cell, agreed. A required rule now holds back only min_domains minus covered replicas, and the rest place under the remaining rules. I've updated the description to match.

Thanks @YashwanthRanjanSingaravel . One leftover seems there. Two passages still describe the old behavior. "Ranker covers two zones before any zone gets a second replica. Its replicas wait up to 10 minutes…" and Placement ("the next replica goes to a domain they don't cover yet" while a floor is unmet) now contradict Required rules wait, where only one replica per missing domain waits and the rest place. Suggest "Ranker holds one replica up to 10 minutes for a second zone while the rest place", and have Placement count waiting replicas toward the domains they wait for.

johntaylor-cell avatar Nov 29 '26 23:11 johntaylor-cell