データベース接続プーリング
リクエストごとにデータベース接続を開くオーバーヘッドを接続プールでなくす方法と、性能に合わせたプールのサイズ設定・調整方法を学びます。
「データベース接続プーリング」はCoddyKit上の無料System Design Basics for Backend Developersレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSystem Design Basics for Backend Developers学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 System Design Basics for Backend Developersコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Cost of a Connection
Opening a database connection is expensive: a TCP handshake, authentication, and session setup can take tens of milliseconds. Doing this on every request adds latency and load you do not need.
What a Connection Pool Is
A connection pool keeps a set of open connections ready to reuse. A request borrows one, runs its query, and returns it — no setup cost per request.
- Lower latency
- Less load on the database
- A natural cap on concurrent connections
Borrow and Return
The lifecycle is simple: acquire a connection, use it, release it back to the pool. The critical rule is to always release, even on error, or the pool leaks.
conn = pool.acquire()
try:
conn.execute('SELECT 1')
finally:
pool.release(conn)Pool Size Basics
The key tuning knob is the maximum pool size. Too small and requests queue waiting for a connection; too large and you overwhelm the database with concurrent work and context switching.
Sizing the Pool
Bigger is not better. A common starting heuristic is based on CPU cores, not request count, because the database can only do so much in parallel.
cores = 4
# A popular rule of thumb (HikariCP):
pool_size = (cores * 2) + 1
print('suggested pool size:', pool_size)Queueing and Timeouts
When all connections are busy, new borrowers wait. Set an acquisition timeout so a request fails fast instead of hanging forever when the pool is exhausted. Surfacing the error early is better than a silent stall.
Connection Leaks
A leak happens when a borrowed connection is never returned. The pool slowly drains until every request blocks. Always release in a finally block (or use a context manager / try-with-resources) and enable leak detection in production.
with pool.acquire() as conn:
conn.execute('SELECT 1')
# released automatically on exitIdle and Max Lifetime
Tune connection longevity: an idle timeout closes connections the pool no longer needs, and a max lifetime recycles old connections to dodge stale sockets and server-side timeouts (e.g. a firewall dropping idle TCP).
Pools and Horizontal Scaling
Watch the math: 20 app servers each with a 50-connection pool means up to 1000 connections hitting one database — likely over its limit. Size per-instance pools with the total fleet in mind, or add a connection proxy.
instances = 20
per_instance = 50
print('total connections:', instances * per_instance)External Poolers
Tools like PgBouncer sit between apps and the database, multiplexing many client connections onto a few database connections. They are essential when you have many app instances or serverless functions that each open connections.
Tuning by Measurement
Do not guess. Monitor pool metrics — active connections, wait time, timeouts — under realistic load, then adjust size and timeouts. The right pool size is the smallest one that keeps wait time near zero.
Quick Check
Test your understanding of connection pooling.
Recap
You learned to optimize database access with pooling:
- Pools reuse connections to avoid per-request setup cost
- Size pools by capacity (e.g. cores), not request volume
- Always release; guard against leaks with timeouts and detection
- Account for total connections across all instances; use poolers like PgBouncer at scale
よくある質問
「データベース接続プーリング」レッスンは無料ですか?
はい。「データベース接続プーリング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、System Design Basics for Backend Developersコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 System Design Basics for Backend Developersコースには全4レッスンが含まれています。
「データベース接続プーリング」で何を学びますか?
リクエストごとにデータベース接続を開くオーバーヘッドを接続プールでなくす方法と、性能に合わせたプールのサイズ設定・調整方法を学びます。 ブラウザで直接実行するハンズオンコードでSystem Design Basics for Backend Developersを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
System Design Basics for Backend Developersを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSystem Design Basics for Backend Developersは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「データベース接続プーリング」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSystem Design Basics for Backend Developersレッスンでコードを書いて実行できますか?
はい。すべてのSystem Design Basics for Backend Developersレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- レイテンシとスループットの最適化
- 並行性と並列性
- パフォーマンステストとプロファイリング
- データベース接続プーリング