Per-Conversation Ordered Message Processing on a Shared Thread Pool
Last updated: November 29, 2026
Quick Overview
Messages arrive for many conversations concurrently. Implement an executor that processes messages for the same conversation strictly one at a time in arrival order, while different conversations run in parallel on a fixed thread pool.
Sierra
November 29, 20260
0
0 solved
Messages arrive for many conversations concurrently. Implement an executor that processes messages for the same conversation strictly one at a time in arrival order, while different conversations run in parallel on a fixed thread pool.
An agent must never handle a customer's second message before the first, or it may answer out of context or run a tool twice, yet Sierra serves huge numbers of conversations at once. The interviewer checks lock discipline, avoiding a thread per conversation, and subtle races around queue creation and cleanup.
What the Interviewer Expects
- Keep one queue per active conversation with a single drainer
- Use one lock to guard the queue map and avoid check-then-act races
- Run user callbacks outside the lock
- Clean up idle conversation queues to avoid unbounded memory
- Isolate failures so one bad message does not stall a conversation
- Provide a way to wait for quiescence in tests
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you bound queue length per conversation and apply backpressure?
- How does this change when messages for one conversation can arrive on different machines?
- How would you avoid one very chatty conversation hogging a worker forever?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
submit(conv_id, fn, *args) is non-blocking. Tasks for the same conv_id run sequentially in submit order and never overlap. Tasks for different ids may run concurrently, bounded by the pool size. A...
High-Level Design
Keep a dict conv_id -> deque. The presence of a deque means a drainer is scheduled or running for that id. submit appends to an existing deque, or creates one and schedules a drainer on the pool. ...