Design a Connection Pool
🔌 What is a connection pool? Opening a connection to a database is slow: a network handshake, authentication, maybe TLS. A connection pool opens a few connections once and lends them to the parts of the program that need to run a query. When the caller is done, the connection goes back to the pool for the next caller instead of being closed.
The question comes in a few forms: "design a database connection pool", "design an object pool", "a resource pool with timeouts". They are the same design. A pool has a fixed number of expensive things, lends them out, makes callers wait when they are all busy, and deals with things that break. We design it around database connections and cover the three situations every version has to handle: connections that are free, callers that are blocked waiting for one, and connections that have been closed underneath us.
Requirements
"Design a connection pool for a database client. It should limit how many connections are open and reuse them."
Short, and full of open questions. Each answer below becomes a rule we will code against.
Clarifying Questions
You: Do we open all the connections up front, or when they are needed?
Interviewer: When needed, up to a maximum size. Most of the day the app needs only a few.
You: What happens when every connection is in use and someone asks for one?
Interviewer: The caller waits, but not forever. Each caller says how long it is willing to wait. If nothing frees up in time, it gives up. Some callers do not want to wait at all.
You: If several callers are waiting, who gets the next free connection?
Interviewer: The one that has waited longest. First come, first served.
You: What if a connection dies? The database restarts, or a firewall cuts an idle connection.
Interviewer: The pool must never lend out a dead connection. Whoever discovers a dead connection reports it. If someone is still using it, they find out from their failed query and give it back; the pool throws it away then. If callers are waiting, open a fresh connection for them.
You: Do idle connections stay open forever?
Interviewer: No. A connection that sits unused for longer than an idle timeout should be closed, so a quiet app does not hold resources on the database.
You: How do we model time in this exercise?
Interviewer: Pass a timestamp into every call. We will talk about real threads and real clocks afterwards.
The last answer splits the problem in two. First we design the pool's rules as a plain, single-threaded class where every call says what time it is; that is what the practice tests run. Then we turn it into the real thing, where callers are threads that block, which is the part interviewers push on.
💡 Interview tip: When the interviewer lets you pass time in, take it. You can trace "Carol waits until time 13, a connection frees at time 5" on the whiteboard, which is impossible with real sleeps. Say clearly that the threaded version comes next, so they know you have not forgotten it.
Final Requirements
- The pool opens connections on demand, at most
max_sizeat a time (idle plus in use). Connection ids are1, 2, 3, ...in opening order. acquirelends an idle connection if there is one, or opens a new one if the pool is not full.- If neither is possible, the caller waits in line until its own deadline. A caller with no wait time is refused right away.
- When a connection is released and callers are waiting, the one that has waited longest gets it immediately.
- A dead connection is never lent out. An idle one is dropped as soon as it is reported; one in use is dropped when it is released, and if a caller is waiting, a fresh connection is opened for that caller.
- An idle connection unused for
idle_timeoutor longer is closed. - Anyone can ask for the counts: idle, in use, waiting.
Out of scope
- The database protocol, queries and transactions.
- Keeping a minimum number of warm connections, maximum connection age and leak detection (all in Extensibility).
- Pools that span several databases.