Implementation:Ray project Ray Serve ReplicaContext
| Knowledge Sources | |
|---|---|
| Domains | Model_Serving, Distributed_Computing |
| Last Updated | 2026-02-13 16:00 GMT |
Overview
ReplicaContext is a data class that stores per-replica context information accessible via Serve.getReplicaContext(), allowing deployment code to introspect its own identity and configuration at runtime.
Description
ReplicaContext is a simple POJO that holds the deployment name, replica tag, servable object, configuration map, and application name for a running replica. It is created during replica initialization in Serve.setInternalReplicaContext() and stored as a static field, making it accessible from anywhere within the replica's execution context. The class provides standard getter/setter methods for all fields.
Usage
Use ReplicaContext within deployment code to access the current replica's identity (deployment name, replica tag), retrieve the servable object, read configuration properties, or determine the application name. It is obtained by calling Serve.getReplicaContext() and is also used internally by components like LongPollClientFactory to read configuration settings.
Code Reference
Source Location
- Repository: Ray
- File:
java/serve/src/main/java/io/ray/serve/replica/ReplicaContext.java
Signature
public class ReplicaContext {
public ReplicaContext(
String deploymentName,
String replicaTag,
Object servableObject,
Map<String, String> config,
String appName)
}
Import
import io.ray.serve.replica.ReplicaContext;
I/O Contract
Constructor Parameters
| Parameter | Type | Description |
|---|---|---|
deploymentName |
String |
Name of the deployment this replica belongs to |
replicaTag |
String |
Unique tag identifying this specific replica instance |
servableObject |
Object |
The user-defined servable object (deployment class instance) |
config |
Map<String, String> |
Configuration properties for the replica |
appName |
String |
Name of the Ray Serve application |
Key Methods
| Method | Return Type | Description |
|---|---|---|
getDeploymentName() / setDeploymentName(String) |
String / void |
Gets or sets the deployment name |
getReplicaTag() / setReplicaTag(String) |
String / void |
Gets or sets the unique replica tag |
getServableObject() / setServableObject(Object) |
Object / void |
Gets or sets the user-defined servable object |
getConfig() / setConfig(Map<String, String>) |
Map<String, String> / void |
Gets or sets the configuration map |
getAppName() / setAppName(String) |
String / void |
Gets or sets the application name |
Usage Examples
// Access replica context from within a deployment handler
ReplicaContext ctx = Serve.getReplicaContext();
String deploymentName = ctx.getDeploymentName();
String replicaTag = ctx.getReplicaTag();
String appName = ctx.getAppName();
logger.info("Running in deployment '{}', replica '{}', app '{}'",
deploymentName, replicaTag, appName);
// Read configuration properties
Map<String, String> config = ctx.getConfig();
String modelPath = config.get("model_path");
// Access the servable object
Object servable = ctx.getServableObject();