Skip to content
Ameen Abozaid
Planned lessonMID-017-L01

The cost of a database connection

  • Lesson 1 of 5
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

explain why applications reuse connections.

Planned topics

  • Opening a PostgreSQL connection costs a TCP handshake, authentication, optional TLS, and a new server process (PostgreSQL uses one process per connection).
  • Each connection uses memory on the server; many idle connections waste it.
  • A pool keeps a fixed set of open connections and lends them out per transaction.
  • The database does useful work only on as many queries as it has CPU cores and disks; extra connections queue inside it.
Draft outline details

Demonstration

a diagram: 200 request threads, a pool of 10 connections, one database; requests wait in the pool queue rather than inside the database.

Check your understanding

Why not open a new connection per request?

Show answer

Setup is slow and each connection costs server memory and a process.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.