تجميع اتصالات قاعدة البيانات
تعلّم كيف تلغي تجمعات الاتصالات تكلفة فتح اتصال بقاعدة البيانات لكل طلب، وكيف تحدد حجم التجمع وتضبطه لتحسين الأداء.
تجميع اتصالات قاعدة البيانات درس مجاني في System Design Basics for Backend Developers على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 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/7) وفتح باقي دورة System Design Basics for Backend Developers، انتقل إلى CoddyKit PRO. تتضمن دورة System Design Basics for Backend Developers 4 دروس في المجموع.
ماذا ستتعلم في «تجميع اتصالات قاعدة البيانات»؟
تعلّم كيف تلغي تجمعات الاتصالات تكلفة فتح اتصال بقاعدة البيانات لكل طلب، وكيف تحدد حجم التجمع وتضبطه لتحسين الأداء. تتمرن على System Design Basics for Backend Developers مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحسين زمن الاستجابة ومعدل النقل
- التزامن والتنفيذ المتوازي
- اختبار الأداء وتحليله
- تجميع اتصالات قاعدة البيانات