Sub topic 14
Pooling connections with RDS Proxy
This chapter assumes you have provisioned an RDS instance with a Secrets Manager secret (see "Creating a DB instance" and "Connecting and managing credentials"). You should also be comfortable with security groups and IAM roles.
Why connections become a problem
Every client connection to a database uses memory and a server process or thread on the instance. A small instance such as db.t4g.micro has little memory, so a few hundred connections can exhaust it. This is common with AWS Lambda, where each concurrent invocation opens its own connection. It also happens with autoscaled containers that each keep a pool of their own.
Opening a connection is also slow, because it involves a TCP handshake, TLS negotiation, and authentication.
What RDS Proxy does
RDS Proxy is a managed service that sits between your application and an RDS instance. The application connects to the proxy endpoint. The proxy keeps a pool of long-lived connections to the database and shares them among many client connections.
The proxy provides three main benefits:
- Connection pooling. Many client connections are multiplexed onto fewer database connections.
- Faster failover. During a Multi-AZ failover, the proxy keeps client connections open and reconnects to the new primary. This typically shortens the disruption your application sees.
- Credential handling. The proxy reads the database credentials from Secrets Manager. It can also require IAM authentication from clients.
Creating a proxy
A proxy needs an IAM role that can read the secret, at least two subnets in your VPC, and a security group. The example below assumes the role and secret already exist. The ARNs are placeholders.
aws rds create-db-proxy \
--db-proxy-name demo-proxy \
--engine-family POSTGRESQL \
--auth '[{"AuthScheme":"SECRETS","SecretArn":"arn:aws:secretsmanager:us-east-1:111122223333:secret:rds!db-example","IAMAuth":"REQUIRED"}]' \
--role-arn arn:aws:iam::111122223333:role/demo-proxy-role \
--vpc-subnet-ids subnet-0123456789abcdef0 subnet-0fedcba9876543210 \
--vpc-security-group-ids sg-0123456789abcdef0 \
--require-tls
aws rds register-db-proxy-targets \
--db-proxy-name demo-proxy \
--db-instance-identifiers demo-pgThe first command creates the proxy. The second registers demo-pg as its target. Once the proxy is available, point your application at the proxy endpoint instead of the instance endpoint. Nothing else in the connection string changes.
Two network rules apply:
- The proxy's security group must be allowed by the database's security group on the database port.
- Clients must be allowed by the proxy's security group.
A proxy is reachable only from inside the VPC by default.
Pinning: the common surprise
The proxy can only share a database connection between clients when the session state is identical. If a client changes session state, the proxy pins that client to one database connection until the client disconnects. Examples include SET statements that change session variables, temporary tables, and some prepared statement usage, depending on the engine. A pinned connection loses the benefit of pooling.
Check the DatabaseConnectionsCurrentlySessionPinned metric in CloudWatch. If it is high, move session settings into the parameter group or the connection options, and avoid session-level state in application code.
Limits and costs
- The proxy has its own hourly charge, based on the vCPUs of the instances it fronts. It is not free, so it makes sense mainly when connection counts or failover time are real problems.
- The pool size is controlled by
MaxConnectionsPercent, which is a percentage of the database'smax_connectionssetting. Leave room for administrative and non-proxy connections. - The proxy adds a small amount of latency to each request.
- Not every engine and version is supported, so check the current RDS Proxy documentation for your engine.
Exercise
A Lambda function that connects directly to an RDS instance causes "too many connections" errors during traffic spikes. After adding RDS Proxy, the errors continue and DatabaseConnectionsCurrentlySessionPinned is high. What is the likely cause, and what should you change?
Show answer
The function runs session-level statements, such as SET commands or temporary tables, that pin each client to its own database connection. This defeats pooling. Move those settings into the parameter group or connection options, and remove session state from the code, so the proxy can share connections.