0Pricing
System Design Basics for Backend Developers · บทเรียน

การใช้กลุ่มการเชื่อมต่อฐานข้อมูล

เรียนรู้ว่ากลุ่มการเชื่อมต่อช่วยขจัดต้นทุนในการเปิดการเชื่อมต่อฐานข้อมูลใหม่ทุกคำขอได้อย่างไร และจะกำหนดขนาดกับปรับแต่งกลุ่มเพื่อประสิทธิภาพได้อย่างไร

การใช้กลุ่มการเชื่อมต่อฐานข้อมูล เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 exit

Idle 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

คำถามที่พบบ่อย

บทเรียน “การใช้กลุ่มการเชื่อมต่อฐานข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การใช้กลุ่มการเชื่อมต่อฐานข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การใช้กลุ่มการเชื่อมต่อฐานข้อมูล”

เรียนรู้ว่ากลุ่มการเชื่อมต่อช่วยขจัดต้นทุนในการเปิดการเชื่อมต่อฐานข้อมูลใหม่ทุกคำขอได้อย่างไร และจะกำหนดขนาดกับปรับแต่งกลุ่มเพื่อประสิทธิภาพได้อย่างไร คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การใช้กลุ่มการเชื่อมต่อฐานข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม

ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเพิ่มประสิทธิภาพเวลาแฝงและอัตราการประมวลผล
  2. การทำงานพร้อมกันและการประมวลผลแบบขนาน
  3. การทดสอบประสิทธิภาพและการวิเคราะห์การทำงาน
  4. การใช้กลุ่มการเชื่อมต่อฐานข้อมูล
← กลับไปที่ System Design Basics for Backend Developers