انتقل إلى المحتوى
أمين أبوزيد
MID-017

تجميع الاتصالات (Connection Pooling)

  • 5 درس
  • 2.5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • HikariCP
  • تحديد حجم المجمّع
  • الامتلاء (saturation)
  • المهلات

تعلّم أولًا

2.5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Explain why pools exist and how HikariCP manages connections.
  2. Size a pool from database capacity and request concurrency, not by guesswork.
  3. Configure timeouts and diagnose pool exhaustion from metrics and thread dumps.
  4. Explain why "more connections" often makes things slower.

الدروس

  1. 01كلفة الاتصال بقاعدة البيانات
  2. 02البنية الداخلية لـ HikariCP
  3. 03تحديد حجم المجمّع
  4. 04المهلات وكشف التسرّب
  5. 05عرض عملي: استنزاف المجمّع تحت الحِمل

المختبر والتمارين

Lab

MID-017-LAB1 · Pool tuning

Outcomes: 1–4 · Time: ~2 h Load-test Shop's checkout with pool sizes 5, 10, 50, and 200 (same test, same database). Record throughput, p95 latency, pending threads, and database CPU, and explain the results. Acceptance: a chart plus a written pool-size recommendation with reasoning.

Exercises (assessment: sizing scenarios)

IDOutcomeTask
MID-017-EX011List four costs of opening a new PostgreSQL connection per request.
MID-017-EX021Explain what happens when all HikariCP connections are in use and a thread asks for another.
MID-017-EX032A service handles 300 req/s with 20 ms average connection hold time and runs on 4 instances. Estimate the pool size per instance.
MID-017-EX042The database allows 100 connections; you have 12 instances. Choose a pool size and justify it, leaving room for admin and migrations.
MID-017-EX053Configure connectionTimeout, leakDetectionThreshold, and statement_timeout, and say what each protects against.
MID-017-EX063Given a thread dump excerpt with 40 threads in getConnection, list the next three diagnostic steps.
MID-017-EX074Explain in a paragraph why a pool of 200 can give lower throughput than a pool of 10.
أظهر الحلول
  • EX01 TCP handshake, TLS, authentication, backend process creation and server memory.
  • EX02 The thread waits up to connectionTimeout, then gets an exception; the pool's pending count rises.
  • EX03 300 × 0.02 = 6 connections in use in total, about 1.5 per instance on average; use 3–5 per instance for headroom and bursts.
  • EX04 About 5–6 per instance (60–72 total), leaving room for admin sessions and migrations; check whether the database can actually serve that concurrency.
  • EX05 Timeout fails fast instead of piling up threads; leak detection logs holder stack traces; statement timeout stops runaway queries.
  • EX06 Look at the holders' stacks (what are they doing), pool metrics (active/pending), and slow queries or remote calls inside transactions.
  • EX07 Too many concurrent queries cause CPU contention, context switches, lock contention, and cache pressure; a small pool queues work cheaply in the app.
  • Outcome 1 is also covered in the quiz.

الاختبار

Outcomes: O1 why pools exist, HikariCP · O2 sizing · O3 timeouts and diagnosis · O4 more is not faster.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A connection pool mainly saves:O1
  2. 2Calling close() on a pooled connection:O1
  3. 3Little's law for a pool: connections needed ≈O2
  4. 4With 8 app instances of pool size 20, the database sees up to:O2
  5. 5Best first step when the pool is exhausted:O2
  6. 6connectionTimeout controls:O3
  7. 7Thread dumps showing many threads in HikariPool.getConnection indicate:O3
  8. 8leakDetectionThreshold helps to find:O3
  9. 9Why can more connections slow the database?O4
  10. 10A pool of 200 for a 4-core database is:O4

الجزء العملي

Sizing scenarios: three cases with request rates, hold times, instance counts, and a database limit; choose pool sizes and explain. Pass: quiz 8/10, scenarios within reasonable bounds with correct reasoning, and the lab chart and recommendation.

أخطاء شائعة

Pools bigger than the database can serve; long transactions holding connections.

التقييم

Sizing scenarios.

الصلة بالمسار الوظيفي

Advanced troubleshooting (SRC-015).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.