Best practices for accessing Redis
In Function Compute, states are not shared between different execution environment instances. You can use databases to persist structured data to achieve state sharing. By accessing cloud databases through Function Compute, you can perform operations such as data queries and data insertion. This topic uses Python 3 as an example to describe how to access Tair (Redis OSS-compatible) within the same VPC or across VPCs and regions.
Prerequisites
Procedure
Step 1: Configure the database whitelist
Scenario 1: Access a Redis database within the same VPC
Make sure that the database instance that you create is in the same region as the function that needs to access the database instance.
You create a database instance in a zone that is supported by Function Compute. For more information, see Zones supported by Function Compute.
If your database instance is not deployed in a zone that is supported by Function Compute, create a vSwitch in your VPC. The vSwitch must be in the same zone as Function Compute. In addition, you must specify the vSwitch ID in the VPC configuration of the specified service in Function Compute. Because private networks can communicate with each other between different vSwitches in the same VPC, Function Compute can access resources in VPCs in other zones through the vSwitch. For more information, see What do I do if the vSwitch is in unsupported zone error occurs?.
-
Log on to the Tair (Redis OSS-compatible) console. In the top navigation bar, select a region. Then, click the target instance ID and view the VPC details of the Redis instance on the Instance information page.
-
Log on to the Function Compute console and create a Python web function. On the function details page, select the Configuration tab. Find Advanced Settings and click Modify. In the Advanced Settings panel, in the Network section, enable VPC access for the function, and configure the target VPC resources.
NoteMake sure that the VPC configured for the function is the same as the VPC to which the database instance is attached.
The network configuration section has three required fields: VPC, vSwitch, and Security group. Select the corresponding resource for each field from the drop-down lists. We recommend that you configure vSwitches in two or more zones to take full advantage of the multi-zone disaster recovery capabilities of Function Compute.
-
On the function details page, select the Configuration tab. In the Network section, obtain the CIDR block of the vSwitch configured for the function.
-
Add the CIDR block of the vSwitch in the function configuration obtained in the previous step to the database access whitelist.
ImportantUse an IP address whitelist to authorize functions to access the database. Do not use the security group mode. Otherwise, functions may occasionally fail to connect to the database, which affects the businesses.
Log on to the console, select a region in the top navigation bar, and then click the target instance ID.
-
On the instance details page, in the navigation pane on the left, choose Whitelist settings. On the Whitelist settings tab, find the target whitelist group and click Modify in the Actions column.
-
In the Modify whitelist group panel that appears, enter the vSwitch CIDR block in the IP addresses in whitelist group field. Then, click OK.
For Add method, select Manual add. You can enter a single IP address (for example,
192.168.1.1) or a CIDR block (for example,192.168.1.0/24). Separate multiple entries with a comma.
After the configuration is complete, the function can access the Redis database through the internal endpoint of the database.
Scenario 2: Access a Redis database across VPCs or regions
Different VPCs and regions are completely logically isolated from each other. In general, you cannot access databases across VPCs or regions. If you need to access a database across VPCs or regions, you can configure a static public IP address for the function. In this case, the system creates a public NAT Gateway in the virtual private cloud (VPC) to which the function is attached. You can access the database through the public IP address by using the public gateway.
-
Log on to the Tair (Redis OSS-compatible) console. Click the target instance ID and go to the Instance information page to view the VPC details of the Redis instance.
-
Log on to the Function Compute console and create a Python web function. On the function details page, select the Configuration tab. Find Advanced Settings and click Modify. In the Advanced Settings panel, locate the Network section. Enable the Static Public IP Address option and disable the Allow Default NIC to Access Internet option to ensure the static public IP takes effect. Then, click Deploy.
NoteSetting Allow Default NIC to Access Internet to No enables the static public IP. Function Compute disables the default network interface, forcing traffic to access the internet through the VPC-bound network interface.
-
On the function details page, select the Configuration tab. In the Network section, obtain the elastic IP address configured for the function.
-
Add the static public IP address of the function obtained in the previous step to the database access whitelist.
ImportantUse an IP address whitelist to authorize functions to access the database. Do not use the security group mode. Otherwise, functions may occasionally fail to connect to the database, which affects the businesses.
Log on to the Management Console, select a region at the top, and then click the target instance ID.
-
In the navigation pane on the left, choose Whitelist settings. On the Whitelist settings tab, find the target whitelist group and click Modify in the Actions column.
-
In the Modify whitelist group panel, enter the static public IP address into the IP addresses in whitelist group field. Then, click OK.
The panel notes that
0.0.0.0/0allows access from any address. You can enter individual IP addresses or CIDR blocks, separated by commas. For Add method, select Manual add.
After the configuration is complete, the function can access the Redis database through the public endpoint of the database.
Step 2: Access Redis in a function
-
Log on to the Function Compute console. Find the target function in the function list. On the function details page, click the Code tab and enter the following sample code in the code editor.
from flask import Flask,jsonify import os import redis app = Flask(__name__) # A global variable to store the singleton Redis connection. _redis_connection = None # Create a database connection (singleton pattern). def getConnection(): global _redis_connection try: # If the connection already exists and is active, return it. if _redis_connection is not None: try: # Test if the connection is valid with a simple ping command. if _redis_connection.ping(): return _redis_connection except redis.ConnectionError: # If the connection is broken, reset it. _redis_connection = None # If the connection does not exist or is broken, create a new one. _redis_connection = redis.Redis( host=os.environ['REDIS_HOST'], password=os.environ['REDIS_PASSWORD'], port=os.environ['REDIS_PORT'], db=1, decode_responses=True ) # Test if the new connection is successful. if not _redis_connection.ping(): raise Exception("Redis connection failed after creation.") return _redis_connection except Exception as e: print(f"Failed to initialize Redis connection: {e}") raise @app.route('/', defaults={'path': ''}) @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE']) def hello_world(path): try: r = getConnection() counter = r.get('counter') if counter is None: counter = 0 else: counter = int(counter) print('counter: ' + str(counter)) r.set('counter', str(counter + 1)) return str(counter) except Exception as e: print(f"An error occurred: {e}") return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0',port=9000) -
On the function details page, select the Configuration tab. Find Advanced Settings and click Modify. In the Advanced Settings panel, locate the Environment Variables section and configure the following variables. Then, click Deploy.
Environment variable name
Environment variable value
Description
REDIS_HOST
r-bp*****.redis.rds.aliyuncs.com
The endpoint of the database instance. If you select the Redis database within the same VPC scenario, set this environment variable to the VPC endpoint of the database. If you select the Redis database across VPCs or regions scenario, set this environment variable to the public endpoint of the database.
Go to the Instance List page, select a region, and click the target instance ID. In the navigation pane on the left, click Instance information. In the Connection information section, find the endpoint for your database.
The connection type is Direct connection, and the port number is
6379.REDIS_PASSWORD
*****
The password of the database.
REDIS_PORT
6379
The port number of the database
-
Set an initial value in the Redis database. On the function details page, select the Code tab and click Test Function. After the execution succeeds, check the result.
In the SQL Console of the DMS console, run the command
set counter 30to set the initial value of the string keycounterto30.In the Log output tab, you can see the output
counter: 30, indicating that the function successfully accessed Redis and read the counter's value.
More information
For more examples of accessing Tair (Redis OSS-compatible), see Function Compute Python accessing Tair (Redis OSS-compatible).
For more exception cases about accessing Tair (Redis OSS-compatible), see Common errors.
If the exception problem you encounter has not been resolved, you need to troubleshoot based on the problem symptoms. For more information, see Common causes of database access failures.
-
If you want to use the Serverless Devs command-line tool to create functions and access the Tair (Redis OSS-compatible) database, see the following steps.