Traditionally, running artificial intelligence (AI), machine learning (ML), or large-scale data analytics tasks requires moving data from a database to an external compute cluster. This process complicates the system architecture and adds data transfer costs and latency. PolarDB for MySQL integrates Ray applications (also known as PolarDB for Ray), enabling you to run these complex tasks directly within the database using the Python ecosystem and the Ray distributed computing framework. This significantly improves the efficiency of data processing and AI inference while reducing costs.
Ray applications are currently in invitational preview. To request access, please submit a ticket to have this feature enabled.
Features
Ray is an open source distributed computing framework designed for large-scale AI and Python applications. PolarDB deeply integrates this powerful computing capability with the database through built-in Ray applications. This means you can write and run Python code directly in the database. You can use a rich set of AI and ML libraries, such as Pandas, NumPy, Scikit-learn, and PyTorch, to process data stored in the database. You can perform model training, data pre-processing, and complex vector calculations all within the database without moving data between the database and an external computing environment.
Benefits
-
Serverless architecture: Provides stable nodes that never scale in to support regular business operations. Elastic nodes automatically scale based on real-time traffic, balancing stability and cost-effectiveness.
-
High availability: The Ray Head node supports high availability (HA) and disaster recovery to ensure cluster stability.
-
Secure and reliable:
-
Provides runtime isolation through secure containers to ensure a safe code execution environment.
-
Supports multi-tenant data isolation to prevent information leakage between tenants.
-
Ray Dashboard and Ray Job submissions support username/password and JSON Web Token (JWT) authentication to prevent unauthorized access.
-
-
Integrated metadata management: Resides in the same virtual private cloud (VPC) as the PolarDB cluster. Metadata processed by PolarDB for Ray can be stored directly in the PolarDB cluster, enabling data interaction without extra network configuration.
-
Unified resource pool: Provides a unified CPU resource pool and supports GPU Workers, offering flexible resource support for different types of computing tasks.
-
Storage service: Supports mounting PolarDB File System 2.0 High-Performance Edition storage. You can read and write data directly through the file interface.
-
Open source customization: PolarDB for Ray is a downstream version of open source Ray and is fully compatible with the open source Ray ecosystem. It also provides developers with customized optimizations and enhancements.
Scope
All product editions and series support creating Ray applications, except for Multi-master Cluster (Limitless) Edition.
Billing
-
Component fees: Ray applications are billed for their Head node and each Worker node. The fee is calculated based on the component specifications (CPU and memory) and subscription duration you select.
-
Storage fee: Data and files generated by Ray applications do not incur extra charges. However, data written by a Ray Job to a PolarDB for MySQL cluster or PolarDB file system storage is subject to corresponding storage space fees.
-
Traffic and bandwidth: No fees are charged.
Quick start
1. (Optional) Create a PolarDB for MySQL cluster
If you do not have a cluster that meets the availability requirements, or if your existing cluster cannot be modified for business reasons, create another cluster in the PolarDB console to try out Ray applications.
2. Create a Ray application
-
Log on to the PolarDB console. On the cluster details page, click in the navigation pane on the left. Then, click Create AI Application.
-
On the application purchase page, select the appropriate configurations as needed:
Configuration item
Description
Billing Method
-
Subscription: An upfront payment mode. When you create an application, you select resources with defined specifications and pay for the application in advance. The longer the subscription duration, the larger the discount. This mode is suitable for scenarios with stable, long-term business requirements.
-
Pay-as-you-go: A pay-as-you-go billing method. When you create an application, you select resources with defined specifications but do not need to pay in advance. This mode bills you based on your actual usage duration and is suitable for scenarios with flexible business requirements.
Engine
Fixed at PolarDB.
Region
Select the geographic location where the application is located.
Note-
You cannot change the region after you purchase the application.
-
The application must be in the same region as the PolarDB for MySQL cluster. Therefore, select the same region as the cluster.
-
We recommend that you create the application and the ECS instances that need to connect to it in the same region. Otherwise, they cannot communicate over the internal network (private network) and must use the public network (Internet), which prevents optimal performance.
Architecture
Fixed at AI Application.
Ecosystem
For the database ecosystem, select MySQL.
Source PolarDB Cluster
Select the PolarDB cluster for which you want to create the application.
AI Application
The AI application type. Select Ray.
Component Set
A Ray application can contain one Head component and multiple Worker components, which form the application's component set.
-
Head Component: A managed Ray Head node.
-
CPU Worker Component: A managed Ray Worker node.
Note-
The minimum and maximum number of Head Components can only be 1.
-
The minimum number of CPU Worker Components can be set to 0. The minimum number is the number of stable Ray Worker nodes. The difference between the maximum and minimum numbers determines the maximum number of Worker nodes for the Ray application in its serverless form.
-
The GPU Worker component is currently in invitational preview. To request access, please submit a ticket to have this feature enabled.
AI Application Name
You can enter a custom name for the application.
NoteThe name cannot start with http:// or https:// and must be 2 to 256 characters in length.
Storage Type
Fixed at AI Cache.
AI Cache Instance ID/Name
(Optional) You can attach a separate AI Cache instance to the Ray application.
Note-
AI Cache is currently in invitational preview. To request access, please submit a ticket to have this feature enabled.
-
Only High-Performance Edition AI Cache instances are supported.
Network Type
Fixed at VPC.
VPC Network
Automatically populated with the VPC of the source PolarDB cluster. No manual entry is required.
Zone and vSwitch
Configure the vSwitch for the VPC. We recommend selecting the same vSwitch as the primary zone of the PolarDB for MySQL cluster for optimal network performance.
NoteIf the existing vSwitches do not meet your requirements, you can create a vSwitch.
Security Group
Configure the security group for the application.
Quantity
Select the number of applications to purchase.
NoteThis is configurable only when Billing Method is set to Subscription.
Duration
Select the subscription duration for the application.
NoteThis is configurable only when Billing Method is set to Subscription.
Auto-renewal
Configure whether to enable auto-renewal. We recommend enabling auto-renewal to avoid business interruptions caused by forgetting to renew.
NoteThis is configurable only when Billing Method is set to Subscription.
-
-
After the purchase is successful, return to the AI Applications page of the cluster to view the newly created application.
NoteThe system takes 3 to 5 minutes to create the application. Please wait.
3. Connect to the Ray application
-
Configure the application whitelist: On the AI Applications list page, click your application ID to go to the application details page. On the Whitelist tab, Add Whitelist, Select Security Groups, or Configure an existing whitelist group.
Note-
The application whitelist is independent of the cluster whitelist and must be configured separately.
-
If your ECS instance needs to access the application, view the IP address of the ECS instance on the ECS Instance Details page and add it to the IP whitelist.
-
If your ECS instance and the application are in the same VPC, you can add the private IP address of the ECS instance or its VPC CIDR block.
-
If your ECS instance and the application are not in the same VPC, you can add the public IP address of the ECS instance or the security group to which the ECS instance belongs.
-
-
If your local server, computer, or another cloud server needs to access the application, add its public IP address to the IP whitelist.
-
-
Obtain the endpoint: On the AI Applications list page, click your application ID to go to the application details page. On the Topology tab, view the public endpoint or private endpoint.
Note-
You must apply for a public endpoint separately. Click the Apply button to submit a request.
-
The public endpoint provides only an IP address and port, not a domain name. If you need a domain name, you can attach one yourself.
-
-
Obtain the application configuration information: On the AI Applications list page, click your application ID to go to the application details page. On the Configuration tab, view the relevant configuration information.
4. Develop the application
You can access the application directly over the Internet. On the AI Applications list page, click Develop Application to go to the public endpoint of the Ray Dashboard. Alternatively, enter the public IP address and port of the application in your browser's address bar to access the Ray Dashboard.
-
First, add your public IP address to the application whitelist.
-
The username and password for the Ray Dashboard are the
secret.dashboard.usernameandsecret.dashboard.passwordvalues in the application configuration information.
5. Submit a Ray Job using an SDK
You can use the Ray software development kit (SDK) to programmatically submit a Ray Job.
-
Prepare the environment
NoteFirst, install the Python 3 environment on your local machine.
Install the Ray SDK:
pip install ray -
Prepare the code files
In your working directory, create two Ray application scripts. One is for business processing and the other is for submitting the job. The following is an example of the working directory structure:- working-dir/ - script.py # Your Ray business logic script - ray_job_submit.py # The script used to submit the jobThe following are example scripts:
script.pyimport ray import time @ray.remote def retrieve_task(item, db): time.sleep(item / 10.) return item, db[item] if __name__ == "__main__": database = [ "Learning", "Ray", "Flexible", "Distributed", "Python", "for", "Machine", "Learning" ] ray.init() db_object_ref = ray.put(database) retrieve_refs = [ retrieve_task.remote(item, db_object_ref) for item in [0, 2, 4, 6] ] result = [print(data) for data in ray.get(retrieve_refs)]ray_job_submit.pyimport os import json import base64 import hmac import hashlib import requests from datetime import timezone, timedelta, datetime from requests.auth import HTTPBasicAuth from ray.job_submission import JobSubmissionClient # Your Ray Dashboard address address = os.getenv("RAY_DASHBOARD_ADDRESS") # You can directly use 'secret.jwt.anonKey' for authentication token = os.getenv("RAY_ANON_TOKEN") # You can also use 'secret.jwt.secret' to generate a JWT secret = os.getenv("RAY_JWT_SECRET") header = {"alg": "HS256", "typ": "JWT"} now = datetime.now(tz=timezone.utc) payload = { "sub": "anon", "iat": int(now.timestamp()), "exp": int((now + timedelta(hours=12)).timestamp()), "iss": "ray-dashboard", "aud": "ray-dashboard-client" } header_encoded = base64.urlsafe_b64encode( json.dumps(header, separators=(',', ':')).encode()).decode('utf-8').rstrip('=') payload_encoded = base64.urlsafe_b64encode( json.dumps(payload, separators=(',', ':')).encode()).decode('utf-8').rstrip('=') message = f"{header_encoded}.{payload_encoded}" signature = hmac.new(secret.encode(), message.encode(), hashlib.sha256).digest() signature_encoded = base64.urlsafe_b64encode(signature).decode('utf-8').rstrip( '=') token = f"{message}.{signature_encoded}" # You can also obtain a JWT for authentication by sending a POST request to /api/auth/token. You must provide a username and password. username = os.getenv('RAY_USERNAME') password = os.getenv('RAY_PASSWORD') response = requests.post(f'{address}/api/auth/token', auth=HTTPBasicAuth(username, password), timeout=5) if response.status_code == 200: token_data = response.json() token = token_data['access_token'] else: print(f"❌ Failed to get JWT: {response.status_code}") client = JobSubmissionClient(address, headers={"Authorization": f"Bearer {token}"}) job_id = client.submit_job( # Entrypoint shell command to execute entrypoint="python script.py", # Path to the local directory that contains the script.py file runtime_env={"working_dir": "./"}) print(job_id) -
Configure and run the script
-
On the Ray Application Configuration page, you can view the following information and then set it as environment variables or replace it in the
submit_job.pyscript.Variable
Corresponding configuration item
Example value
RAY_DASHBOARD_ADDRESSThe endpoint of the Ray application. Select the public or private endpoint based on your environment.
http://123.xxx.xxx.xxx:8265RAY_ANON_TOKENsecret.jwt.anonKeyeyJhbGciOi...RAY_JWT_SECRETsecret.jwt.secrettNhVxysSRD...RAY_USERNAMEsecret.dashboard.usernameadminRAY_PASSWORDsecret.dashboard.passwordUTLof$rMVM... -
In your working directory, run the submission script:
python ray_job_submit.py -
After the script is successfully run, you can log on to the Ray Dashboard and view the running status of the submitted job on the Jobs page.

-