Step 1 · The pattern
Borrow a connection, give it back
Opening a connection per command is slow. The app keeps a small pool of open connections. Each request borrows one, sends its command, reads its reply and hands the connection back.
- request
- correct reply
In short
Pooled connection mix-ups: how it works and how it breaks
Opening a connection for every command is slow, so clients keep a pool of open connections and lend one to each request. On a Redis connection, replies carry no label saying which request they answer. They simply come back in the order the commands were sent, and the client matches them by position. That works as long as every command sent is followed by exactly one reply read. A request that is cancelled or times out after sending, but before reading, breaks the count. If the connection goes back into the pool, its unread reply is still waiting. The next caller reads it as its own, and every caller after that is one reply behind. Usually that produces errors. Occasionally the stray reply looks valid, and one user is handed another user's data. A connection in an unknown state must be closed, never reused.
How this comes up in interviews
"A user reports seeing another user's data, and the only shared component is the cache client. How could a connection pool cause that?"
Then they ask
- What should the client do with a connection when a request times out or is cancelled?
- Why does this usually show up as errors and only occasionally as wrong data?
- What second line of defence would you add in case a reply is still crossed?
A strong answer covers
- Replies on a Redis connection carry no label. The client matches them to commands by order.
- That holds only while every command sent is followed by exactly one reply read.
- A request cancelled after sending and before reading leaves its reply behind. Later callers on that connection are one reply behind.
- A connection in an unknown state must be closed, never returned to the pool.
- Also check that data read from the cache belongs to the user who asked for it.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Redis docs: pipelining and the request/response protocol ↗ · redis-py issue #2624: cancelled async command leaves connection unsafe ↗ · OpenAI: March 20 ChatGPT outage ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.