Updated on 2024-09-24 GMT+08:00

What Is Database Proxy

Database Proxy is a network proxy service between GaussDB(for MySQL) and the application service. It is used to proxy all the requests for the application service to access GaussDB(for MySQL).

Read/write splitting means that read and write requests are automatically forwarded through database proxy addresses. After creating a GaussDB(for MySQL) instance, you can enable database proxy. Write requests are automatically forwarded to the primary node and read requests are forwarded to each node based on the routing policy of the proxy instance.

Basic Concepts

  • Proxy Address

    After purchasing a proxy instance, you can view the proxy address on the Database Proxy page. The proxy instance sends write requests to the primary node and read requests to read replicas through this address.

  • Proxy Mode

    There are read/write and read-only proxy modes.

    Read/Write: All write requests are routed only to the primary node, and all read requests are routed to the selected nodes based on the read weights or active connections.

    Read-only: All read requests are routed to the selected read replicas based on the read weights or active connections. The read requests will not be routed to the primary node.

  • Transaction Splitting

    With transaction splitting is enabled for a proxy instance, the proxy instance can route the read requests prior to write operations in a transaction to read replicas, reducing the pressure on the primary node.

    For more information about transaction splitting, see Enabling Transaction Splitting for a Proxy Instance.

  • Connection Pool

    Proxy instances provide session-level connection pools, which help reduce the database load caused by frequent establishment of short connections.

    For more information about connection pools, see Enabling the Connection Pool for a Proxy Instance.

  • Routing Policy

    Proxy instances support weighted and load balancing routing policies.

    • Weighted: Read requests are assigned to nodes based on the weights you specify.
    • Load balancing: Read requests are assigned to nodes with fewer active connections. In load balancing policy, you do not need to configure the weights of nodes.

    For more information about routing policies, see Modifying the Routing Policy for a Proxy Instance.

How Read/Write Splitting Works

You can create one or more proxy instances for your GaussDB(for MySQL) instance to enable read/write splitting.

  • Single proxy instance

    If your GaussDB(for MySQL) instance has only one proxy instance, applications connect to the proxy instance through the proxy address. Write requests are forwarded to the primary node and read requests to the primary node or read replicas based on the routing policy you specify.

    Figure 1 Read/write splitting with only one proxy instance
  • Multiple proxy instances

    To isolate workloads from different applications, you can create up to four proxy instances for your GaussDB(for MySQL) instance. Different applications can connect to different proxy instances as required. The associated nodes of the proxy instances process read requests from different applications for workload isolation.

    Figure 2 Read/write splitting with multiple proxy instances

Application Scenarios

  • The primary node is overloaded due to a large number of requests in a transaction.
  • The primary node is overloaded due to excessive connections.
  • Read/write splitting is required.

Read/Write Splitting Advantages

  • Compared with manual read/write splitting in applications, the read/write splitting using proxy addresses features flexible scale-out and low maintenance costs.
  • Read requests are distributed to your read replicas based on weights to balance your database traffic and improve resource utilization.
  • A proxy instance routes read requests of an application only to its associated read replicas to isolate workloads.
  • By default, proxy instances provide overload protection to prevent server OOM (out of memory) due to heavy pressure when you perform operations on large result sets. This function is enabled by default and does not need to be configured separately. The pressure caused by the slow kernel depends on flow control.

Request Routing Rules

  • Write requests sent only to the primary node
    • INSERT, UPDATE, and DELETE
    • All DDL operations (such as table/database creation, table/database deletion, table structure change, and permission change)
    • All requests in transactions (But if transaction splitting is enabled, some read requests in transactions may be sent to read replicas. For details, see Enabling Transaction Splitting for a Proxy Instance.)
    • User-defined functions
    • Stored procedures
    • EXECUTE statements
    • Multi-statement requests
    • Requests that use temporary tables
    • All changes to user variables
    • KILL in SQL statements (not command KILL)
  • Read requests sent only to the primary node
    • If query statements are in transactions, the transaction requests are routed to the primary node. If SET AUTOCOMMIT=0 is added before a query statement, the transaction requests are routed to the primary node.
    • If all read replicas are abnormal or the read weights allocated to the read replicas are 0, requests will be routed to the primary node. You can set read weights for the primary node and read replicas after read/write splitting is enabled.
    • When running SQL statements:
      • If multi-statements (for example, insert xxx;select xxx) are executed, all subsequent requests will be routed to the primary node. To restore read/write splitting, disconnect your application from your instance and then connect it back again.
      • Read operations with locks (for example, SELECT for UPDATE) will be routed to the primary node.
      • When /*FORCE_MASTER*/ is used, requests will be routed to the primary node.
      • If the HANDLER statement is executed, all subsequent requests will be routed to the primary node by default. To restore read/write splitting, disconnect the connection and reestablish a connection.
    • SELECT last_insert_id()
    • All queries of user variables
  • The following requests will be routed either to the primary node or a read replica.
    • SELECT not in a transaction
    • The COM_STMT_EXECUTE command
  • The following requests will always be routed to all nodes.
    • Changes to all system variables
    • The USE command

Read/Write Attribute Processing Logic

There are read-only and read/write modes for proxy instances. The read/write attribute processing logic varies depending on the proxy mode.

Proxy Mode

Routing Policy

Weight of Primary Node

Normal Case

All Read Replicas Are Faulty

Read only

Weighted

Load balancing

Not configurable

Primary node does not process read-only requests.

Proxy address: readable but not writable

Primary node does not process read-only requests.

Proxy address: connection error

Read/write

Load balancing

Assigned by system

Primary node: readable and writable

Proxy address: readable and writable

Primary node: readable and writable

Proxy address: readable and writable

Weighted

> 0

Primary node: readable and writable

Proxy address: readable and writable

Primary node: readable and writable

Proxy address: readable and writable

= 0

Primary node: not readable but writable

Proxy address: readable and writable

Primary node: readable and writable

Proxy address: readable and writable

Billing

Proxy instances are free of charge.

Precautions

Table 1 Precautions for proxy instances

Category

Precaution

Version constraints

  • If the kernel version of your GaussDB(for MySQL) instance is one of the following, proxy instances cannot be created:
    • From 2.0.26.2 to 2.0.28.3
    • 2.0.29.1
  • If the kernel version of your GaussDB(for MySQL) instance is earlier than 2.0.42.230601, only one proxy instance can be created.
  • If the kernel version of your GaussDB(for MySQL) instance is 2.0.42.230601 or later, up to four proxy instances can be created.

Unsupported functions

  • Proxy instances do not support compression protocols.
  • Proxy instances do not support the READ-UNCOMMITTED transaction isolation level.
  • Proxy instances do not support reads from and writes to any column containing more than 16 MB of data in a table.
  • Database proxies do not support the SQL mode parameter PAD_CHAR_TO_FULL_LENGTH.

Usage constraints

  • To create a proxy instance, a GaussDB(for MySQL) instance must have at least 8 vCPUs.
  • Read/write splitting can be enabled only when at least one read replica is created.
  • After read/write splitting is enabled, the database port and private IP address of your GaussDB(for MySQL) instance cannot be changed.
  • If multi-statements are executed, all subsequent requests will be routed to the primary node. To restore the read/write splitting function, disconnect the connection from your applications and establish a connection again.
  • When the IP address of a proxy instance is used, all transaction requests are routed to the primary node (you can use the transaction splitting feature to route read requests prior to write operations in a transaction to read replicas). The non-transaction read consistency is not ensured. To ensure read consistency, encapsulate the read requests into a transaction.
  • When the IP address of a proxy instance is used, you can run show processlist command on the proxy instance or GaussDB(for MySQL) instance. If show processlist is executed on a proxy instance, only the services delivered through proxy nodes are displayed.
  • If a proxy node is abnormal, running show processlist or Kill on the proxy instance may take a long time, but services are not affected.
  • After a proxy node is deleted, services on the deleted proxy node may be displayed when show processlist is executed on the proxy instance.
  • When Kill is executed, error information such as timeout may be displayed occasionally. You can run show processlist again to check whether the services are killed successfully.
  • If a proxy node is abnormal, there may be frame freezing for 2 seconds when you run show processlist on the proxy instance. The results will still be returned.
  • When a proxy instance is used, the size of a concatenate SQL statement cannot exceed 100 MB to prevent statement parsing from consuming too many resources.

HTAP analysis

  • Consistency levels and connection pools are not supported.
  • Only the weighted routing policy is supported.
  • Only the read/write proxy mode is supported.