TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† 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.
Opening a connection from scratchStep 5 of 5
๐ŸงRider Ana๐Ÿš•Brand-new taxigone๐ŸจHotel
  1. 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.
  2. The driver checks the hotel's badge, and the two agree on a secret signal so nobody can listen in.
  3. The driver gives the password, and the hotel sets up a parking bay and a helper just for this one taxi.
  4. Only now does the real trip happen, and it's over in a moment.
  5. Then the taxi is sent away for good, and the hotel packs up its helper. The next rider has to start all over again.
Borrow and returnStep 4 of 4
๐Ÿš Taxi rankopenopenopen๐ŸงRider Ana๐Ÿงโ€โ™‚๏ธRider Ben๐Ÿš•Taxi 1free๐Ÿš•Taxi 2busy๐Ÿš•Taxi 3free๐ŸจHotel
  1. When the day starts, three taxis are found and signed in at the hotel once. They wait at the rank with engines running.
  2. Ana walks up and takes the first free taxi. No badge check, no password, it's ready right away.
  3. Ana rides to the hotel and back. Meanwhile Ben takes Taxi 2 for his own trip.
  4. Ana is done, and Taxi 1 goes back to the rank instead of being sent away. The marshal tidies it for the next rider.
When the pool runs dryStep 5 of 5
๐Ÿš Taxi rank๐ŸงRider Ana๐Ÿงโ€โ™‚๏ธRider Benborrowed it๐Ÿงโ€โ™€๏ธRider Cleogave up๐Ÿš•Taxi 1held, idle๐Ÿš•Taxi 2gone too long๐ŸจHotel
  1. This rank has two taxis. Ana and Ben each take one.
  2. Ana asks her taxi to wait outside a shop while she browses for half an hour. Ben never sends his taxi back at all.
  3. Cleo arrives and the rank is empty, so she joins the queue.
  4. After the set waiting time, Cleo gives up and goes home upset, even though the hotel itself was quiet the whole time.
  5. The marshal notices Taxi 2 has been gone far too long, and writes down who took it, so the station can fix the problem.
Sizing pools against one databaseStep 4 of 4
๐Ÿš‰Station A1,000 waiting๐Ÿš‰Station Bpool 10๐Ÿš‰Station Cpool 10๐ŸจHotel30 of 100
  1. Two stations each keep forty taxis at their rank. The hotel's street has room for a hundred, so eighty taxis crowd in.
  2. A third station opens with forty taxis of its own. The street fills up, and the last taxis are turned away.
  3. 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.
  4. One day a thousand riders flood into Station A at once. They queue calmly at the station, and the hotel's street stays clear.
Stale connections and maxLifetimeStep 5 of 5
๐Ÿš Taxi rankopen๐ŸงRider Ana๐Ÿš•Fresh taximax 25 min๐Ÿ’‚Barrier guardkept open๐ŸจHotel
  1. 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.
  2. An hour passes. The guard quietly closes the lane, and nobody tells the taxi. It still looks fine at the rank.
  3. 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.
  4. 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.
  5. 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.
A pooler in front of the databaseStep 4 of 4
shared๐Ÿš‰Station A๐Ÿš‰Station B๐Ÿš‰Station Csetting lost๐Ÿ›Ž๏ธShuttle deskshared๐ŸจHotel
  1. 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.
  2. A rider from Station A gets a cart for one trip. When the trip is done, the cart is free again.
  3. The same cart then carries a rider from Station B. A few carts serve every station.
  4. 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.