Scaling Connections in PostgreSQL Postgres Bangalore(PGBLR) Meetup-2 - Mydbops
MyDBOPS
233 views
79 slides
Jul 05, 2024
Slide 1 of 88
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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...
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 performance and resource utilization.
Key Takeaways:
* Understand why connection pooling is essential for high-traffic applications
* Explore various connection poolers available for PostgreSQL, including pgbouncer
* Learn the configuration options and functionalities of pgbouncer
* Discover best practices for monitoring and troubleshooting connection pooling setups
* Gain insights into real-world use cases and considerations for production environments
This presentation is ideal for:
* Database administrators (DBAs)
* Developers working with PostgreSQL
* DevOps engineers
* Anyone interested in optimizing PostgreSQL performance
Contact [email protected] for PostgreSQL Managed, Consulting and Remote DBA Services
Size: 1.39 MB
Language: en
Added: Jul 05, 2024
Slides: 79 pages
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
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
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
Statement Pooling Mode
•Server connection returns to pool immediately after
statement completes
•Multi statement is not supported
•Autocommit is enabled
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
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