A VPC direct connection is a high-performance method for calling services. It allows clients inside a Virtual Private Cloud (VPC) to bypass public gateways and access PAI-EAS service instances directly. This topic explains how it works, how to configure it, and provides detailed instructions for calling a service using the official SDKs or building a custom client.
Overview
Use cases
High-performance requirements: For high-traffic services with a large number of queries per second (QPS), such as image or speech recognition, a direct connection can significantly improve access performance.
Latency-sensitive applications: For services that require fast response times, such as online recommendation and financial risk control, a direct connection can effectively reduce latency.
How it works
A VPC direct connection relies on two mechanisms:
Network connectivity: After you configure a VPC for a service, PAI-EAS creates and attaches a free auxiliary elastic network interface for each service instance within the specified VPC and vSwitch. This establishes a direct network path between the VPC and the PAI-EAS service instances. As a result, clients within the VPC can directly access PAI-EAS service instances without passing through a gateway, avoiding Layer 4 SLB and Layer 7 network forwarding.
Service discovery: PAI-EAS provides a service discovery mechanism that allows clients to retrieve a real-time list of
IP:PORTpairs for all service instances. Clients can use this list to implement load balancing and failover.
Cross-region access and billing
Q: What is the difference between cross-region VPC internal access and public network access?
The key difference depends on your gateway type:
Dedicated gateway: In addition to establishing VPC connectivity, you must perform additional CEN-related configuration steps in PAI-EAS. For more information, see Cross-VPC access to a dedicated gateway using CEN.
Q: Does cross-region VPC internal access incur traffic costs? Why is it still recommended?
Yes, cross-region internal network traffic incurs fees. Despite this, VPC internal access is recommended for production environments for the following reasons:
Security: Traffic does not traverse the public internet, eliminating public exposure risk.
Performance: Lower latency and more stable connections compared to public network access.
Cost efficiency: Cross-region internal network traffic costs are typically lower than public network egress traffic costs.
Prerequisites
Before you begin, make sure you have configured a VPC for your service. For more information, see Network configuration.
Each elastic network interface consumes one IP address from the vSwitch. Ensure the vSwitch has enough available IP addresses for all service instances.
Configure security group rules to manage network isolation and access control.
ImportantSecurity group rules control network access between clients, such as ECS instances, and PAI-EAS services.
By default, instances within the same basic security group can communicate with each other over the internal network. You can place the client and the PAI-EAS service in the same security group.
If the client and the PAI-EAS service must use different security groups, you must configure rules to allow communication between them. For more information, see Allow access between instances in different security groups in a classic network.
Stable endpoint configuration for multi-replica services
When your service scales to multiple replicas, backend instance IPs may change. Use the following approaches to maintain a stable connection address.
Underlying principle: PAI-EAS uses SLB (Server Load Balancer) to expose a unified Service Endpoint for each service. Regardless of how many backend replicas are running, clients only need to connect to this single address — you do not need to track individual instance IP changes.
Option 1 (Recommended): Use the public domain with a backend IP allowlist. Configure the backend to allow traffic only from your peer VPC's IP range. This provides a stable domain address without additional DNS configuration.
Option 2: Configure Alibaba Cloud PrivateZone in your peer VPC to resolve a custom domain to the PAI-EAS private IP address. Note that if the IP address changes, you must update the DNS record or integrate a service discovery mechanism to keep resolution current.
Calling other EAS services from an EAS container
If your EAS service needs to call another EAS service from within its container, note the following:
401 errors or connection failures: These typically occur when a container application calls another EAS service using a public endpoint. The usual causes are network restrictions in the container environment or incorrect token configuration.
Recommended approach: Use the VPC internal address for inter-service calls. This ensures connectivity and improves security.
Alternative: If you must use a public endpoint, configure a dedicated gateway. Note that this involves additional ALB costs.
Authentication requirement: Regardless of which address type you use, every request must include a valid Authorization token in the request header.
Call services using an official SDK (Recommended)
Core logic
When you use a VPC direct connection, the client bypasses the gateway and connects directly to service instances. This means the client is responsible for handling service discovery, load balancing, and failover retry. The official PAI-EAS SDKs encapsulate this logic for you:
Service discovery: The SDK starts a background thread during initialization to periodically call the PAI-EAS service discovery API. It retrieves and caches a list of backend instance
IP:PORTpairs and weights. If the API call fails, the SDK continues to use the last cached list to ensure availability.Load balancing: For each request, the SDK selects a target instance from the local cache using a weighted round-robin algorithm. The weights are determined by the instance weights returned from the service discovery API.
Failover retry: If a request fails due to a network error, connection timeout, or 5xx response, the SDK retries the request on a different instance from the local cache. In a multi-instance deployment, this prevents a single faulty instance from causing request failures.
If you plan to build a custom client without using an SDK, you must implement equivalent capabilities. For more information, see Build a custom client.
Examples
Python SDK
Install or upgrade the SDK.
pip install -U eas-prediction --userFor more information, see Python SDK guide.
Write the invocation code.
The following code provides an example of a program that uses strings as input and output. For examples that use other input and output formats, such as TensorFlow or PyTorch, see Python SDK guide.
#!/usr/bin/env python from eas_prediction import PredictClient from eas_prediction import StringRequest from eas_prediction import TFRequest from eas_prediction import ENDPOINT_TYPE_DIRECT # Replace with the actual endpoint in the format {Uid}.vpc.{RegionId}.pai-eas.aliyuncs.com. # Uid is your Alibaba Cloud account ID, and RegionId is the region ID of your PAI-EAS service. ENDPOINT = "123**********.vpc.cn-shanghai.pai-eas.aliyuncs.com" # Replace with your PAI-EAS service name. SERVICE_NAME = "mnist_saved_model_example" # Replace with your service token. You can find the token on the service details page. We recommend managing tokens using environment variables or Key Management Service (KMS). TOKEN = "M2FhNjJlZDBmMzBmMzE4NjFiNzZhMmUxY2IxZjkyMDczNzAzYjFi****" if __name__ == '__main__': client = PredictClient(ENDPOINT, SERVICE_NAME) client.set_token(TOKEN) client.set_endpoint_type(ENDPOINT_TYPE_DIRECT) # Access the service over the direct connection. client.init() # request = StringRequest('[{}]') req = TFRequest('predict_images') req.add_feed('images', [1, 784], TFRequest.DT_FLOAT, [1] * 784) resp = client.predict(req) print(resp)Where:
The
client = PredictClient()constructor accepts the VPC direct connection endpoint and the service name as arguments.VPC direct connection endpoint: This endpoint is region-specific and uses the format
{Uid}.vpc.{RegionId}.pai-eas.aliyuncs.com.{Uid}is your Alibaba Cloud account ID. For example, the VPC direct connection endpoint for China (Shanghai) is123**********.vpc.cn-shanghai.pai-eas.aliyuncs.com.TOKEN: You can find the token under Invocation Information on the service details page.
Java SDK
Add the Maven dependency.
To use the EAS Java SDK in your Maven project, add the eas-sdk dependency to the <dependencies> section of your pom.xml file. For the latest version, check the Maven repository.
<dependency> <groupId>com.aliyun.openservices.eas</groupId> <artifactId>eas-sdk</artifactId> <version>2.0.20</version> </dependency>For more information, see Java SDK guide.
Write the invocation code.
import com.aliyun.openservices.eas.predict.http.PredictClient; import com.aliyun.openservices.eas.predict.http.HttpConfig; public class TestString { public static void main(String[] args) throws Exception { // To ensure the client object is shared correctly, create and initialize it at startup. Do not create a new client for each request. PredictClient client = new PredictClient(new HttpConfig()); // Replace with your service token. You can find the token under Invocation Information on the service details page. client.setToken("YWFlMDYyZDNmNTc3M2I3MzMwYmY0MmYwM2Y2MTYxMTY4NzBkNzdj****"); // For direct connections, use the setDirectEndpoint method with a domain name in the format {Uid}.vpc.{RegionId}.pai-eas.aliyuncs.com. Replace {Uid} with your account ID. For example, the region ID for China (Shanghai) is cn-shanghai. client.setDirectEndpoint("123**********.vpc.cn-shanghai.pai-eas.aliyuncs.com"); // Replace with your service name. client.setModelName("scorecard_pmml_example"); // Define the input string. String request = "[{\"money_credit\": 3000000}, {\"money_credit\": 10000}]"; System.out.println(request); // Get the string response from PAI-EAS. try { String response = client.predict(request); System.out.println(response); } catch (Exception e) { e.printStackTrace(); } // Shut down the client. client.shutdown(); return; } }
Go SDK
When you use the Go SDK, the Go package manager automatically downloads the SDK source code from GitHub during compilation. You do not need to install it beforehand. For more information about using the Go SDK, see Go SDK guide.
The following code shows how to call a service using the Go SDK:
package main
import (
"fmt"
"github.com/pai-eas/eas-golang-sdk/eas"
)
func main() {
// Use the endpoint in the format {Uid}.vpc.{RegionId}.pai-eas.aliyuncs.com. Replace {Uid} with your account ID and specify the service region and name. For example, the region ID for China (Shanghai) is cn-shanghai.
client := eas.NewPredictClient("123**********.vpc.cn-shanghai.pai-eas.aliyuncs.com", "scorecard_pmml_example")
// Replace with your service token. You can find the token under Invocation Information on the service details page.
client.SetToken("YWFlMDYyZDNmNTc3M2I3MzMwYmY0MmYwM2Y2MTYxMTY4NzBkNzdj****")
client.SetEndpointType(eas.EndpointTypeDirect)
client.Init()
req := "[{\"fea1\": 1, \"fea2\": 2}]"
for i := 0; i < 100; i++ {
resp, err := client.StringPredict(req)
if err != nil {
fmt.Printf("failed to predict: %v\n", err.Error())
} else {
fmt.Printf("%v\n", resp)
}
}
}Build a custom client
If you need to use another programming language or have custom requirements, you can follow these guidelines to implement the HTTP invocation logic.
When you build a custom client, you are responsible for handling complex fault-tolerance logic, including service discovery, load balancing, and failover retry. The implementation of your client directly affects service availability. We strongly recommend using an official SDK. The platform Service Level Agreement (SLA) does not cover service interruptions caused by improperly implemented clients.
Service discovery API
PAI-EAS provides an HTTP API for service discovery within the configured VPC. The API returns a list of IP addresses, ports, and weights for all backend instances of a service.
URL:
http://<Your Alibaba Cloud Account ID>.vpc.<RegionID>.pai-eas.aliyuncs.com/exported/apis/eas.alibaba-inc.k8s.io/v1/upstreams/<Service Name>Authentication: No token is required. The API is accessible only from within the VPC configured for the service.
Polling interval: This is a side-channel API and should be called periodically by a background task, for example, every 5 to 10 seconds. Do not call this API for every inference request. Doing so severely degrades performance.
Example:
The following example shows how to query a service named mnist_saved_model_example that is deployed in China (Hangzhou) and has two instances.
$curl http://123**********.vpc.cn-hangzhou.pai-eas.aliyuncs.com/exported/apis/eas.alibaba-inc.k8s.io/v1/upstreams/mnist_saved_model_exampleReplace 123********** with your account ID.
The following response shows a list of backend service addresses:
{
"correlative": [
"mnist_saved_model_example"
],
"endpoints": {
"items": [
{
"app": "mnist-saved-model-example",
"ip": "172.16.XX.XX",
"port": 50000,
"weight": 100
},
{
"app": "mnist-saved-model-example",
"ip": "172.16.XX.XX",
"port": 50000,
"weight": 100
}
]
}
}Troubleshooting: VPC domain access failures
Symptom: The VPC domain is unreachable, but the service is accessible via the direct IP address returned by the service discovery API.
Common cause: Incorrect curl command syntax — for example, the URL is not enclosed in quotes, which causes the shell to misinterpret special characters such as & or ? in the URL.
Solution: Enclose the URL in double quotes and run a minimal test request via the IP address or VPC domain to verify connectivity:
# Test using the VPC domain (URL must be enclosed in double quotes)
curl "http://123**********.vpc.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/your-service-name" \
-H "Authorization: your-token"
# Test using a direct IP address (replace with an IP from the service discovery API response)
curl "http://172.16.XX.XX:50000/api/predict/your-service-name" \
-H "Authorization: your-token"Implementation requirements
When you build a custom client, you must handle service discovery, load balancing, and failover retry. For a complete reference implementation, see the Python SDK source code. The following are key implementation requirements:
Cache the instance list locally and refresh it periodically: Start a background thread to periodically call the service discovery API, for example, every 5 to 10 seconds. Overwrite the locally cached instance list with the returned list.
ImportantThe service discovery API is a side-channel service. Do not call it for every inference request. If the API call fails or returns an empty list, you must continue to use the locally cached list. Do not clear the cache. Failing to do so will make your service unavailable.
Load balance requests: For each inference request, select an instance from the local cache. We recommend using a weighted round-robin algorithm, but you can also use a custom algorithm based on your business logic.
Retry failed requests: If a request fails due to a connection error, timeout, or 5xx response, retry the request on a different instance from the local cache. This requires at least two available instances.
Related documentation
For more information about service invocation methods, see Invocation methods overview.