Skip to content

Conversation

JoshKarpel
Copy link
Contributor

@JoshKarpel JoshKarpel commented Jul 29, 2025

Why are these changes needed?

We're seeing a lot of pressure on the Serve Controller from metrics push tasks when running thousands of Serve apps. A lot of this pressure is purely from the overhead of lots of RPC connections incoming to the controller. We can amortize this overhead (and presumably similar overhead in the handles too) by having the metrics push happen at the per-process level instead of the per-handle level.

We've made this change on our setup and it has reduced CPU time spent on this in the Controller, and also our ingress application replicas that have all the handles.

See also #55102

Related issue number

Closes #45777

Second attempt on #45957 , see previous discussion there for more context.

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @JoshKarpel, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on optimizing the autoscaling metrics reporting within Ray Serve to alleviate RPC overhead on the Serve Controller. The core change involves grouping DeploymentHandle autoscaling metrics at the process level before transmitting them, rather than sending individual reports. This refactoring is expected to improve the scalability and performance of the Serve system, particularly when managing a large number of applications. Additionally, it introduces more explicit and configurable intervals for metrics collection and pushing.

Highlights

  • Metrics Grouping for Performance: I've introduced a SharedHandleMetricsPusher class that aggregates autoscaling metrics from multiple DeploymentHandles within the same process. This allows for sending these metrics to the controller in a single bulk RPC call, significantly reducing network overhead and pressure on the Serve Controller, especially in large-scale deployments.
  • Standardized Metrics Reporting: I've refactored the internal metrics reporting by creating a dedicated HandleMetricReport dataclass. This new structure encapsulates all necessary handle metrics, simplifying the API for AutoscalingStateManager and the Serve Controller, which now directly consume these unified report objects.
  • Granular Control over Intervals: I've deprecated the metrics_interval_s field in AutoscalingConfig in favor of new environment variables (e.g., RAY_SERVE_REPLICA_AUTOSCALING_METRIC_PUSH_INTERVAL_S, RAY_SERVE_HANDLE_AUTOSCALING_METRIC_PUSH_INTERVAL_S). This provides more precise and independent control over the recording and pushing intervals for both replica and handle autoscaling metrics.
  • Controller API Enhancement: I've added a new bulk_record_handle_metrics method to the Serve Controller. This method is designed to efficiently receive and process sequences of HandleMetricReport objects, supporting the new grouped metrics push mechanism.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

Overall, this is a well-executed pull request that addresses a significant performance bottleneck in Ray Serve. The introduction of SharedHandleMetricsPusher to batch metric reports is a smart solution. The related code refactoring, such as using the HandleMetricReport dataclass, improves code clarity and maintainability.

My review includes one high-priority comment regarding a potential bug in an environment variable name and one medium-priority comment about a future performance consideration for the new shared pusher. Once these are addressed, the PR should be in great shape.

zcin pushed a commit that referenced this pull request Aug 7, 2025
…deprecated (#55102)

## Why are these changes needed?

Deprecation announcements for this future PR
#55016

## Related issue number

Related to #45777

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Josh Karpel <[email protected]>
# Conflicts:
#	python/ray/serve/_private/constants.py
#	python/ray/serve/_private/replica.py
#	python/ray/serve/_private/router.py
#	python/ray/serve/config.py
#	src/ray/protobuf/serve.proto
sampan-s-nayak pushed a commit that referenced this pull request Aug 12, 2025
…deprecated (#55102)

## Why are these changes needed?

Deprecation announcements for this future PR
#55016

## Related issue number

Related to #45777

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Josh Karpel <[email protected]>
Signed-off-by: sampan <[email protected]>
@JoshKarpel JoshKarpel force-pushed the issue-45777-amortize-metrics-pushing-v2 branch from 0c63f4c to ea4bf41 Compare September 16, 2025 17:22
Signed-off-by: Josh Karpel <[email protected]>
Signed-off-by: Josh Karpel <[email protected]>
pytest==7.4.4
pytest-asyncio==0.17.2
pytest-aiohttp==1.1.0
pytest-env
Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://pypi.org/project/pytest-env/

I was struggling with how to conveniently override the environment variables that control the metrics push interval early enough, and in every case where Pytest is being used, so I introduced this new dependency to handle it.

Copy link
Contributor

Choose a reason for hiding this comment

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

i mentioned this in the other comment. Use python/ray/serve/tests/BUILD.bazel to set env variables.

you should revert this change

@JoshKarpel
Copy link
Contributor Author

Could you update the description with profile/benchmark information that showcases the benefit from this implementation.

I went back through our internal discussions on this and unfortunately I can't find any profiling results from this change specifically in our setup :(

I'll see if I can get a chance to reproduce them but my time is pretty limited right now.

Signed-off-by: Josh Karpel <[email protected]>
Signed-off-by: Josh Karpel <[email protected]>
pytest.ini Outdated
Comment on lines 18 to 19
RAY_SERVE_REPLICA_AUTOSCALING_METRIC_PUSH_INTERVAL_S = 0.1
RAY_SERVE_HANDLE_AUTOSCALING_METRIC_PUSH_INTERVAL_S = 0.1
Copy link
Contributor

Choose a reason for hiding this comment

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

The place you want to add this is python/ray/serve/tests/BUILD.bazel. In ray we manage env variables through bazel

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The trouble with that is it won't apply to local runs if you're not using bazel - is the expectation that bazel is used for local test runs too? If so I'm happy to set these there, but I guess I assumed that it should be possible to use plain pytest commands locally.

Copy link
Contributor

Choose a reason for hiding this comment

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

yea, that is a pain point we have accepted for now, to use bazel to run local tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see! I will set them there then.

2 * self._config.metrics_interval_s,
RAY_SERVE_MIN_HANDLE_METRICS_TIMEOUT_S,
)
timeout_s = 2.5 * RAY_SERVE_HANDLE_AUTOSCALING_METRIC_PUSH_INTERVAL_S
Copy link
Contributor

Choose a reason for hiding this comment

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

right but why drop RAY_SERVE_MIN_HANDLE_METRICS_TIMEOUT_S, wouldn't that be regression in feature?

@@ -1,3 +1,5 @@
from __future__ import annotations
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

)
shared.register(self)
else:
self.metrics_pusher.register_or_update_task(
Copy link
Contributor

Choose a reason for hiding this comment

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

i think we should also use the shared pusher in the else case.

self._metrics_pusher.start()

self._metrics_pusher.register_or_update_task(
"push_metrics_to_controller",
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extract ReplicaMetricsManager.PUSH_METRICS_TO_CONTROLLER_TASK_NAME into a constant in commons.py. And use that here

pytest==7.4.4
pytest-asyncio==0.17.2
pytest-aiohttp==1.1.0
pytest-env
Copy link
Contributor

Choose a reason for hiding this comment

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

i mentioned this in the other comment. Use python/ray/serve/tests/BUILD.bazel to set env variables.

you should revert this change

@JoshKarpel JoshKarpel force-pushed the issue-45777-amortize-metrics-pushing-v2 branch from 2bf1f57 to e6a6c25 Compare September 17, 2025 20:36
dstrodtman pushed a commit to dstrodtman/ray that referenced this pull request Oct 6, 2025
…deprecated (ray-project#55102)

## Why are these changes needed?

Deprecation announcements for this future PR
ray-project#55016

## Related issue number

Related to ray-project#45777

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Josh Karpel <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
# Conflicts:
#	python/ray/serve/_private/autoscaling_state.py
#	python/ray/serve/_private/controller.py
#	python/ray/serve/_private/replica.py
#	python/ray/serve/_private/router.py
#	python/ray/serve/tests/BUILD.bazel
#	python/ray/serve/tests/test_deploy_2.py
Signed-off-by: Josh Karpel <[email protected]>
Signed-off-by: Josh Karpel <[email protected]>
cursor[bot]

This comment was marked as outdated.

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

Labels

community-contribution Contributed by the community serve Ray Serve Related Issue unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Serve] Amortize handle metrics pushing by grouping metrics by process

2 participants