我们在 Hasura 的当前设置中使用 Cloud Run 和 Cloud SQL
2.0.9
(GCP)
Cloud Run 设置为运行最少 5 个实例,最多 150 个实例,平均运行约 80-90 个实例。
Cloud SQL 设置为接受最多 500 个连接(4vCPU 和 15GB RAM)
Cloud Run 上每秒的平均请求约为 350 个
我在 Cloud SQL 上遇到错误:
db=postgres,user=postgres FATAL: remaining connection slots are reserved for non-replication superuser connections
和:
db=postgres,user=postgres FATAL: sorry, too many clients already
Cloud Run 上出现 500/503 错误:
severity: "ERROR". <--- 500
textPayload: "The request failed because the instance could not start successfully."
severity: "ERROR" <--- 503
textPayload: "The request failed because either the HTTP response was malformed or connection to the instance had an error."
这是
databases.yaml
- name: default
kind: postgres
configuration:
connection_info:
database_url:
from_env: HASURA_GRAPHQL_DATABASE_URL
isolation_level: read-committed
pool_settings:
connection_lifetime: 600
idle_timeout: 180
max_connections: 400
retries: 1
use_prepared_statements: true
tables: "!include default/tables/tables.yaml"
functions: "!include default/functions/functions.yaml"
上面的 yaml 可以使用吗?还是我应该将
max_connections
限制为 150 个实例/500(数据库连接限制)= ~2 max_connections
?现在,在数据库监控中,我发现连接超出了池的 400
max_connections
设置,并达到了 Cloud SQL 的 500
连接限制。
我找不到最佳位置,因此基础设施不会出现故障。我什至尝试使用
pgpool
(我已从pool_settings
中删除了databases.yaml
),但它不会变得更好。过去几天我一直在尝试组合,但没有成功。
非常感谢任何帮助。
如果您遇到 Cloud Run -> Cloud SQL 连接限制,根据 this 配额页面,该限制为 100 个连接。
一个好的升级是使用基于 VPC 网络的方法来交换基于连接器的数据库连接。
@guilaume 整理了一份关于 VPC 连接器与 Cloud Run 的详细指南。
接受的答案非常好,但如果有人发现这个问题并需要一个快速的替代方案:
Cloud SQL 设置为接受最多 500 个连接(4vCPU 和 15GB RAM)
500 是默认值。
尺寸 | 默认最大连接数 |
---|---|
很小(~0.5) | 25 |
小(~1.7) | 50 |
从 3.75 到 < 6 | 100 |
从 6 到 < 7.5 | 200 |
从 7.5 到 < 15 | 400 |
从 15 到 < 30 | 500 |
从 30 到 < 60 | 600 |
从 60 到 < 120 | 800 |
>=120 | 1,000 |
Google Cloud sql 文档有上表,以及添加标志的说明。