Skip to content

[Serve] [Docs] Async io + Ray Serve best practices#58909

Merged
abrarsheikh merged 6 commits intomasterfrom
SERVE-1472-abrar-docs
Dec 3, 2025
Merged

[Serve] [Docs] Async io + Ray Serve best practices#58909
abrarsheikh merged 6 commits intomasterfrom
SERVE-1472-abrar-docs

Conversation

@abrarsheikh
Copy link
Contributor

@abrarsheikh abrarsheikh commented Nov 22, 2025

@abrarsheikh abrarsheikh marked this pull request as ready for review November 22, 2025 01:00
@abrarsheikh abrarsheikh requested review from a team as code owners November 22, 2025 01:00
@ray-gardener ray-gardener bot added serve Ray Serve Related Issue docs An issue or change related to documentation labels Nov 22, 2025
Copy link
Contributor

@marwan116 marwan116 left a comment

Choose a reason for hiding this comment

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

Thank you so much for putting this guide together, really clarifies how to think of asynchronous execution with Ray Serve.

I left some comments which I am curious about.

return str(result)
```

### Using Ray tasks or remote actors for true parallelism
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we explicitly encourage Ray core usage ? For example remote actors require proper autoscaling and observability tooling to help scale and debug when things go wrong. Can we instead advocate for composition in this case ?

Here is what cursor suggested as an alternative section:

### Using serve composition for true parallelism

With serve composition, you can split CPU-heavy work into separate deployments that run in independent processes:

```python
@serve.deployment
class HeavyCompute:
    def compute(self, x):
        # Heavy compute runs in its own deployment process.
        return x * x


@serve.deployment
class ParallelProcessor:
    def __init__(self, compute_handle):
        self._compute_handle = compute_handle

    async def __call__(self, request):
        values = [1, 2, 3, 4]
        # Call the composed deployment in parallel.
        refs = [self._compute_handle.compute.remote(v) for v in values]
        results = await asyncio.gather(*[ref for ref in refs])
        return {"results": results}


# Compose the deployments.
app = ParallelProcessor.bind(HeavyCompute.bind())

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 cluster should still be able to autoscale even if ray tasks are created from serve application. But good point about observability. Creating another deployment and offloading the work there has the same problem because then that deployment becomes blocking, so we just kick the can forward to a downstream deployment.

I have seen users use this pattern especially in OSS. So I am inclined to keep this but will mark this a advanced usage pattern, call out the observablity pitfall and not mention it repeatedly in the document.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me about calling it an advanced pattern and leaving it to a dedicated section.

Yes I agree remote tasks should scale just fine. In my mind with composition they can set target and max requests to a low number close to 1 and use serve's queue based replica scaling to parallelize the compute but I recognize this probably adds more performance overhead than directly submitting tasks.

As for my comment on auto scaling - I was referring to remote actors given the document originally stated "use remote tasks or actors" which would require an auto scaling actor pool abstraction...


This warning means:

- You have a `def` method that is currently running on the event loop.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- You have a `def` method that is currently running on the event loop.
- You have a `def` method that is currently running on the event loop.

@abrarsheikh abrarsheikh force-pushed the SERVE-1472-abrar-docs branch from 051f5cd to febb508 Compare November 24, 2025 17:44
@abrarsheikh abrarsheikh added the go add ONLY when ready to merge, run all tests label Nov 24, 2025
abrarsheikh and others added 5 commits November 24, 2025 19:39
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Co-authored-by: angelinalg <122562471+angelinalg@users.noreply.github.com>
Signed-off-by: Abrar Sheikh <abrar2002as@gmail.com>
Signed-off-by: abrar <abrar@anyscale.com>
Co-authored-by: angelinalg <122562471+angelinalg@users.noreply.github.com>
Signed-off-by: Abrar Sheikh <abrar2002as@gmail.com>
Signed-off-by: abrar <abrar@anyscale.com>
@abrarsheikh abrarsheikh force-pushed the SERVE-1472-abrar-docs branch from 2316222 to 6f35807 Compare November 24, 2025 19:39

How this method executes depends on configuration:

- With `RAY_SERVE_RUN_SYNC_IN_THREADPOOL=0` (current default), `__call__` runs directly on the user event loop and blocks it for 1 second.
Copy link
Contributor

Choose a reason for hiding this comment

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

if there isn't, can we add telemetry on the usage of this? Worth considering if this should be a deployment level config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no telemetry on RAY_SERVE_RUN_SYNC_IN_THREADPOOL. I cannot make a strong case for this to be a deployment config, env var is fine IMO. But we should turn this on by default in new release.

@edoakes edoakes self-requested a review November 24, 2025 22:59
@edoakes edoakes self-assigned this Dec 1, 2025
@edoakes edoakes requested review from edoakes and removed request for edoakes December 1, 2025 22:54
Copy link
Collaborator

@edoakes edoakes left a comment

Choose a reason for hiding this comment

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

Looks very useful overall. A few minor comments inline.

More macro comment: we are promoting the usage of a lot of different environment variable feature flags here.

  1. Are there default changes we can make to simplify the matrix? For example, making RAY_SERVE_RUN_SYNC_IN_THREADPOOL on by default would help reduce the support matrix a lot.
  2. Should any of these be promoted to proper public APIs instead of env var FFs?

@abrarsheikh
Copy link
Contributor Author

  • Are there default changes we can make to simplify the matrix? For example, making RAY_SERVE_RUN_SYNC_IN_THREADPOOL on by default would help reduce the support matrix a lot.

In the next ray release will announce that we will make this default.

  • Should any of these be promoted to proper public APIs instead of env var FFs?

RAY_SERVE_RUN_SYNC_IN_THREADPOOL and RAY_SERVE_RUN_ROUTER_IN_SEPARATE_LOOP can be made deployment-level configs. I think that makes sense.

Signed-off-by: abrar <abrar@anyscale.com>
@edoakes
Copy link
Collaborator

edoakes commented Dec 2, 2025

RAY_SERVE_RUN_SYNC_IN_THREADPOOL should just be turned on by default w/o adding a config option IMO (to match fastapi)

@abrarsheikh
Copy link
Contributor Author

RAY_SERVE_RUN_SYNC_IN_THREADPOOL should just be turned on by default w/o adding a config option IMO (to match fastapi)

you are right, sorry I copy pasted the wrong env var, meant to put RAY_SERVE_RUN_USER_CODE_IN_SEPARATE_THREAD

@edoakes
Copy link
Collaborator

edoakes commented Dec 3, 2025

lgtm

@abrarsheikh abrarsheikh merged commit 0ad7ff3 into master Dec 3, 2025
6 checks passed
@abrarsheikh abrarsheikh deleted the SERVE-1472-abrar-docs branch December 3, 2025 16:23
peterxcli pushed a commit to peterxcli/ray that referenced this pull request Feb 25, 2026
https://anyscale-ray--58909.com.readthedocs.build/en/58909/serve/advanced-guides/asyncio-best-practices.html

---------

Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: Abrar Sheikh <abrar2002as@gmail.com>
Co-authored-by: angelinalg <122562471+angelinalg@users.noreply.github.com>
Signed-off-by: peterxcli <peterxcli@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs An issue or change related to documentation go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants