Scaling Connections in PostgreSQL Postgres Bangalore(PGBLR) Meetup-2 - Mydbops

MyDBOPS 233 views 79 slides Jul 05, 2024
Slide 1
Slide 1 of 88
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88

About This Presentation

This presentation, delivered at the Postgres Bangalore (PGBLR) Meetup-2 on June 29th, 2024, dives deep into connection pooling for PostgreSQL databases. Aakash M, a PostgreSQL Tech Lead at Mydbops, explores the challenges of managing numerous connections and explains how connection pooling optimizes...


Slide Content

Scaling Connections in PostgreSQL
Postgres Bangalore(PGBLR) Meetup-2, 29th June 2024
-Aakash M

About Me
•PostgreSQL Tech Lead at Mydbops
•ExpertiseindatabaseslikeMySQL,SQLServer
•Performancetuning/CostOptimization
•TechSpeakeronOpenSourceEvents
•ActiveBlogger
•6+yearsofExperience

About Us

Our Services
Focus on MySQL, MongoDB, PostgreSQL, TiDB,
Cassandra
Consulting
Services
Consulting
Services
Managed
Support
Services
24*7
DBA Team
Targeted
Engagement

Agenda
•Connection Pooling
•Why pooling is required ?
•Available Connection Poolers
•pgbouncer
•Benchmark Comparison

Connection Pooling

Application
What is Connection Pooling
•A pool of database connections
•Reusing unused connections from the pool
•Once purpose completes, return back to the pool
Connections to the database

Application
Connection
Pooler
What is Connection Pooling
•A pool of database connections
•Reusing unused connections from the pool
•Once purpose completes, return back to the pool
Connections to the Pool
Connections to the database

Advantages of Connection Pooling
•Limiting connections to the database
•Overhead of opening/closing connection is reduced

Why pooling is required ?

Why pooling is required ?
•Process based connection

Statistics Collector
WAL Writer
Background Writer
Checkpointer
Autovacuum
Archiver
Shared Memory
Postgres(Server
Process)
Log Writer
Why pooling is required ?
Application
Postgres
Architecture
PostgreSQL Server

Statistics Collector
WAL Writer
Background Writer
Checkpointer
Autovacuum
Archiver
Shared Memory
Postgres(Server
Process)
Log Writer
Why pooling is required ?
Application
Postgres(Backend
Process)
Postgres(Backend
Process)
PostgreSQL Server

Disadvantages
•More Resources (CPU & Memory)
•Slower Context Switching

Impact of Idle Connections
Reference:https://aws.amazon.com/blogs/database/resources-consumed-by-idle-postgresql-connections/

MySQL is handling More Connections ??

Receiver
Thread
Connection Creation in MySQL
Application
MySQL
Server
mysqld process
Connection Requests
Thread Cache
User Threads THDS

Connection Creation in MySQL
•Resource Efficiency (CPU & Memory)
•Faster Context Switching
•Better Scalability

Available Connection Poolers

Connection Pooler Available Since Setup Complexity Github Repository
pgbouncer 2007 Simple
https://github.com/pgbo
uncer/pgbouncer
Pgpool-II 2003 Medium to High
https://github.com/pgpo
ol/pgpool2
PgCat 2020 Medium
https://github.com/postg
resml/pgcat
Odyssey 2017 Medium to High
https://github.com/yand
ex/odyssey
pgagroal 2020 Low to Medium
https://github.com/agroa
l/pgagroal
Available Connection Poolers

pgbouncer

Pgbouncer
•Lightweight Process
•Easy to setup
•Single Threaded
•Same authentication as database

Pgbouncer

Configuration

Pooling Mode
•Session
•Transaction
•Statement

Session Pooling Mode
•Once the client disconnections, Connection will be
released back to the pool
•Client and Server Connection is paired

Application
Connection
Pooler
Session Pooling Mode
•Once the client disconnections, Connection will be
released back to the pool
•Client and Server Connection is paired
Connection 1 Connection 1
Connection 2
Connection 2

Tranaction Pooling Mode
•Server Connection is assigned to Client whenever a new
transaction is started
•Released back to the pool when transaction closes
•Server and client connection is not paired
•No support for session variables and prepared statements

Tranaction Pooling Mode
Application
Connection
Pooler
User1
Trx1:
Begin;
statement
;
commit or
rollback
User2
Trx2:
Begin;
statement
;

Statement Pooling Mode
•Server connection returns to pool immediately after
statement completes
•Multi statement is not supported
•Autocommit is enabled

Statement Pooling Mode
Application
Connection
Pooler
User1
drop table x;
drop table y;
User2
select 1;
select 2;
select 3;
select 1;
select 2;

Authentication Modes
•Authentication with Auth_file
•Authentication with Auth_user

Auth File
•Text file contains username and password
•Location of Auth_file requires

Auth File
•auth_type = md5 or password
•auth_file = /etc/pgbouncer/userlist.txt
cat /etc/pgbouncer/userlist.txt
"appuser" "md5e10adc3949ba59abbe56e057f20f883e" # Password:
123456
"reporting_user" "md5e10adc543ba59abbe56e057f20f8899"
"admin" "md5e10000543ba59abbe56e057f20f8877"

Auth User
•Load user/password directly from the server
•No maintenance overhead

Auth User
•auth_user=”low_priv_user”
•auth_query=”select usename, passwd FROM pg_shadow
WHERE usename=$1”
•auth_file = /etc/pgbouncer/userlist.txt
cat /etc/pgbouncer/userlist.txt
"low_priv_user" "md5e10adc3949ba59abbe56e057f20f883e"

Parameter Description Default
Max_client_conn
Maximum number of client
connections allowed
100
Default_Pool_Size
How many server
connections to allow
20
min_pool_size
Minimum number of server
connections to maintain
0(Disabled)
Configuration

Parameter Description Default
Reserve_pool_size
Additional connections
allowed to the pool
0(Disabled)
Reserve_pool_timeout
If timeout, use additional
connections in the pool
0(Disabled)
Listen_port Port to listen 6432
Configuration

Working of Pgbouncer

Working of pgbouncer
•Max_client_conn=10
•Default_pool_size=3
•Reserve_pool_size=1
•Reserve_pool_timeout=10
•Listen_port=6542
•Pooling_mode=Session

Working of pgbouncer
Application
pgbouncer

Working of pgbouncer
Application
pgbouncer
Pool
Area

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections 2 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections 2 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections 2 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 3
connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 3
connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 2 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 2 connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 3
connections

Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 4 connections

Monitoring Pgbouncer

CLI
•Admin Console
•psql -h127.0.0.1 -Upostgres pgbouncer
root@ip-172-31-3-53:~/# psql -h172.31.3.53 -Umydbops pgbouncer -p6432
psql (16.3 (Ubuntu 16.3 -1.pgdg20.04+1), server 1.12.0/bouncer)
pgbouncer=#

CLI
pgbouncer=# show help;
•NOTICE: Console usage
•DETAIL:
•SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION
•SHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM
•SHOW DNS_HOSTS|DNS_ZONES
•SHOW STATS|STATS_TOTALS|STATS_AVERAGES|TOTALS
•SET key = arg
•RELOAD
•PAUSE [<db>]
•RESUME [<db>]
•DISABLE <db>
•ENABLE <db>
•RECONNECT [<db>]
•KILL <db>
•SUSPEND
•SHUTDOWN

CLI
pgbouncer=# show pools;
-[ RECORD 1 ]-----------
•database | test
•user | mydbops
•cl_active | 50
•cl_waiting | 50
•sv_active | 50
•sv_idle | 0
•sv_used | 0
•sv_tested | 0
•sv_login | 0
•maxwait | 0
•maxwait_us | 19535
•pool_mode | transaction

Grafana
•Open Source Exporters and Dashboards
https://github.com/spreaker/prometheus -pgbouncer-exporter
https://blog.searce.com/grafana -dashboard-for-pgbouncer-and-monitor-with-percona-pmm-
3170d3eb4d14

Grafana

Benchmarking Results

Benchmark
•50 pool size
•Hosted on Application Server
•Small Instance Size
•Pgbench for benchmarking
•1, 50, 75, 100, 200, 400, 500 connections

Benchmark

Benchmark

Benchmark
•Direct connection performs good when number of connection is less
•Only for OLTP workload
•Maintain resource usage

Considerations

Few Considerations
•Single Point of Failure
•Single Threaded
•Multiple Instance for More Connections
•OS should be optimised

Production Use Case

Production Use Case
•High CPU usage for last 1 week
•AWS RDS db.r5.2xlarge (8 CPU, 64G RAM)
•No new deployment

Production Use Case

Production Use Case
•No CPU causing queries
•Increased connections
•Killing Idle Connections

Production Use Case

Production Use Case

Production Use Case
Application

Application
Production Use Case

Production Use Case

Pgpool -II

pgpool-II
•multi threaded Architecture
•Connection Pooling
•Read Write Routing (Load Balancing)
•Query Caching
•In-Built HA Architecture

General Considerations
•Complexity
•Higher Resource Usage
•Latency

Pgcat

pgcat
•multi threaded Architecture
•Simple to setup
•Sharding
•Read Write Routing (Load Balancing)
•Query Caching
•In-Built HA Architecture

pgcat vs pgbouncer
Ref: https://tembo.io/blog/postgres-connection-
poolers

pgcat vs pgbouncer
Ref: https://tembo.io/blog/postgres-connection-
poolers
Ref: https://tembo.io/blog/postgres-connection-
poolers

Summary
•pgbouncer works well for simpler setups
•pgpool/pgcat has features and complex settings
•Ensure to test load balancing
•OS tuning is mandatory
•Do not forget HA
•Good for OLTP workload

Reference:
•https://tembo.io/blog/postgres-connection-poolers
•https://www.mydbops.com/blog/getting-started-with-pgbouncer-in-postgresql/
•https://blog.searce.com/grafana-dashboard-for-pgbouncer-and-monitor-with-
percona-pmm-3170d3eb4d14
•https://postgresml.org/docs/product/pgcat/features
•https://www.pgpool.net/mediawiki/index.php/Main_Page

Thank You