Best practices for accessing Redis

Updated at:

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

Create a Redis instance

Procedure

Step 1: Configure the database whitelist

Scenario 1: Access a Redis database within the same VPC

Important
  • 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?.

  1. 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.

  2. 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.

    Note

    Make 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.

  3. On the function details page, select the Configuration tab. In the Network section, obtain the CIDR block of the vSwitch configured for the function.

  4. Add the CIDR block of the vSwitch in the function configuration obtained in the previous step to the database access whitelist.

    Important

    Use 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.

    1. Log on to the console, select a region in the top navigation bar, and then click the target instance ID.

    2. 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.

    3. 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.

  1. 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.

  2. 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.

    Note

    Setting 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.

  3. On the function details page, select the Configuration tab. In the Network section, obtain the elastic IP address configured for the function.

  4. Add the static public IP address of the function obtained in the previous step to the database access whitelist.

    Important

    Use 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.

    1. Log on to the Management Console, select a region at the top, and then click the target instance ID.

    2. 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.

    3. 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/0 allows 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

  1. 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)
    
  2. 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

  3. 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 30 to set the initial value of the string key counter to 30.

    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.

    Click here to view the Serverless Devs operation steps

    1. Install Serverless Devs and Docker, and add key information. For more information, see Quick Start.

    2. Create a code directory named mycode, and create an s.yaml file and a code file named app.py. For the sample code for app.py, see the code provided in Step 2: Access Redis in a function. An example s.yaml file is shown below.

      The following s.yaml example applies to accessing a Redis database in the same VPC. If you need to access a database across VPCs or regions, see Scenario 2: Access a Redis database across VPCs or regions.

      # ------------------------------------
      #   Official documentation: https://manual.serverless-devs.com/user-guide/aliyun/#fc3
      #   Common tips: https://manual.serverless-devs.com/user-guide/tips/
      #   If you have questions, join the DingTalk group: 33947367
      # ------------------------------------
      edition: 3.0.0
      name: hello-world-app
      access: "default"
      vars: # Global variables
        region: "cn-hangzhou"  # If you are accessing the database within the same VPC, make sure the function is deployed in the same region as the database.
      resources:
        hello_world:
          component: fc3 
          actions:       
            pre-${regex('deploy|local')}: 
              - component: fc3 build 
          props:
            region: ${vars.region}              
            functionName: "start-python-redis"
            runtime: custom.debian10
            description: 'hello world by serverless devs'
            timeout: 10
            memorySize: 512
            cpu: 0.5
            diskSize: 512
            code: ./code
            customRuntimeConfig:
              port: 9000
              command:
                - python3
                - app.py  
            internetAccess: true
            vpcConfig:
             vpcId: vpc-bp1dxqii29fpkc8pw**** # VPC ID of the database instance.
             securityGroupId: sg-bp12ly2ie92ixrfc**** # Security group ID.
             vSwitchIds: 
              - vsw-bp1ty76ijntee9z83**** # Ensure the CIDR block for this vSwitch is in the database instance's access whitelist.
            environmentVariables:
              PYTHONPATH: /code/python
              PATH: /code/python/bin:/var/fc/lang/python3.10/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
              REDIS_HOST: r-bp*****.redis.rds.aliyuncs.com  # Database endpoint.
              REDIS_PASSWORD: *****     # Database password.
              REDIS_PORT: "6379"         # Database port.
             

      The directory structure of the code is as follows.

      ├── code
      │ ├── app.py
      │ └── requirements.txt
      └── s.yaml

      The dependencies specified in the requirements.txt file are as follows.

      flask==2.2.5
    3. Run the following command to build the project.

      sudo s build --use-docker
    4. Run the following command to deploy the project.

      sudo s deploy -y
    5. Run the following command to invoke the function.

      Note

      Make sure that the CIDR block of the vSwitch configured for the function has been added to the database instance access whitelist. For more information, see Step 4.

      sudo s invoke -e "{}"