โ Back to Connection pooling
๐ Connection pooling: cheat sheet
๐จ The analogyA taxi rank outside a station: riders take a waiting taxi, and it comes back to the rank afterwards.
๐ The regular explanationKeeping a small set of open database connections that application threads borrow and return, so requests skip the expensive connection setup and the database is protected from more sessions than it can serve.
- Ana needs to reach the hotel. There's no rank, so a brand-new taxi has to be found, and it first drives over to say hello.
- The driver checks the hotel's badge, and the two agree on a secret signal so nobody can listen in.
- The driver gives the password, and the hotel sets up a parking bay and a helper just for this one taxi.
- Only now does the real trip happen, and it's over in a moment.
- Then the taxi is sent away for good, and the hotel packs up its helper. The next rider has to start all over again.
- When the day starts, three taxis are found and signed in at the hotel once. They wait at the rank with engines running.
- Ana walks up and takes the first free taxi. No badge check, no password, it's ready right away.
- Ana rides to the hotel and back. Meanwhile Ben takes Taxi 2 for his own trip.
- Ana is done, and Taxi 1 goes back to the rank instead of being sent away. The marshal tidies it for the next rider.
- This rank has two taxis. Ana and Ben each take one.
- Ana asks her taxi to wait outside a shop while she browses for half an hour. Ben never sends his taxi back at all.
- Cleo arrives and the rank is empty, so she joins the queue.
- After the set waiting time, Cleo gives up and goes home upset, even though the hotel itself was quiet the whole time.
- The marshal notices Taxi 2 has been gone far too long, and writes down who took it, so the station can fix the problem.
- Two stations each keep forty taxis at their rank. The hotel's street has room for a hundred, so eighty taxis crowd in.
- A third station opens with forty taxis of its own. The street fills up, and the last taxis are turned away.
- The hotel only has a few porters, so most of those taxis were just sitting in the jam anyway. Each station switches to a small rank of ten.
- One day a thousand riders flood into Station A at once. They queue calmly at the station, and the hotel's street stays clear.
- Taxi 1 waits at the rank, its lane to the hotel open. The barrier guard closes any lane where nothing has moved for an hour.
- An hour passes. The guard quietly closes the lane, and nobody tells the taxi. It still looks fine at the rank.
- Ana asks for Taxi 1. The marshal checks it first, gets no answer, and throws it out. Ana waits while a fresh taxi is found.
- The better plan is to retire every taxi after a shift shorter than the guard's limit. A fresh taxi takes over before any lane is closed.
- While it waits, the fresh taxi also takes a short drive now and then, so the guard sees it moving and leaves its lane open.
- Taxis from all three stations now stop at the shuttle desk. The desk has just a few carts that go the last stretch to the hotel.
- A rider from Station A gets a cart for one trip. When the trip is done, the cart is free again.
- The same cart then carries a rider from Station B. A few carts serve every station.
- A rider from Station C left a bag in a cart on the last trip and expects it now. But this trip gets a different cart, and the bag isn't there.
๐งฉ Key questions
- Why does the station keep a taxi rank instead of finding a brand-new taxi for every rider?
- Because getting and signing in a new taxi takes far longer than most trips. The rank does the slow checks once, so each rider just takes a taxi that's ready.
- Cleo finds the rank empty and gives up after the waiting time, yet the hotel was quiet. What's the most likely cause?
- Riders are keeping taxis waiting outside shops, or never sending them back. The taxis were out but not driving. Get them back quickly, and the marshal's watch shows who kept one.
- Three stations share one hotel street with room for 100 taxis. A fourth station opens. What should each station's rank look like?
- Small, so all four ranks together fit the street with room to spare. The street is shared, and more taxis than porters only jam it.
- A barrier guard quietly closes any lane where nothing has moved for 30 minutes. How long should each taxi's shift be?
- A bit shorter than 30 minutes, so taxis retire before any lane closes. Fresh taxis take over before the guard can close a lane, and short drives keep waiting taxis moving.
- At the hotel's shuttle desk, a rider leaves a bag in a cart and expects it on the next trip. Why is it missing?
- Each trip may get a different cart. Carts are shared between riders, so bring what you need on every trip.