Hospital ER Triage & Bed Allocation — Part A: Design
Definitions
Triage
- In plain English
- Triage is sorting patients by how urgently they need care.
- Think of it like
- It is like sorting post into urgent and standard trays.
Acuity level
- In plain English
- An acuity level is a number from 1 (most urgent) to 5 (least urgent) assigned at triage.
- Think of it like
- It is like a severity rating on a support ticket.
The brief
In er-triage, patients need scarce, compatible beds. This teaching model makes scheduling choices explicit; it is not a clinical protocol.
What it must do
- Register patients with acuity from 1 to 5.
- Order waiting patients by acuity, then arrival time; preserve registration order for exact ties.
- Level 1 requires resuscitation; children under 16 prefer paediatric beds, then general beds.
- Other patients use general beds; alert doctors when a level-1 patient arrives.
- Move Waiting → InBed → Discharged or Admitted; release the ER bed at either exit.
How well it must do it
- Prevent double booking when several nurses allocate concurrently.
- Use O(log n) heap insertion and removal; bed matching also scans the small bed pool.
- Change matching rules without changing the allocator loop.
Out of scope
- Medical records and clinical advice.
- Staff rostering and billing.
Design walkthrough
Entities and responsibilities
| Type | Responsibility |
|---|---|
| Patient | Store acuity, arrival and lifecycle state. |
| Bed and BedType | Describe one resource and its compatibility. |
| WaitingList | Hide the priority queue. |
| BedPool | Reserve and release beds safely. |
| MatchingRule | Rank compatible bed types. |
| Allocator | Coordinate arrivals, allocation and discharge. |
| AlertSink | Notify doctors about level-1 arrivals. |
Choose strict head-of-line ordering: if the highest-priority patient has no compatible bed, allocation stops. Lower-priority patients remain waiting too.
An alternative can bypass blocked patients, but then “always the most urgent first” needs qualification. We keep that trade-off visible.
Class diagram
Allocator uses WaitingList, BedPool, MatchingRule and AlertSink; the collections hold patients and beds.
The allocator lock protects queue and patient changes. The bed pool lock protects reservation. Always take the allocator lock before the pool lock.
Core flow: arrival to allocation
Arrival registers a patient and may alert doctors; allocation either reserves a compatible bed or leaves the queue unchanged.
Patient lifecycle
A waiting patient enters a bed before discharge or admission to a ward.
Admitted means transferred out of the ER in this model. Both exit transitions release the ER bed.
Type skeleton
package triage
import "time"
type State string
const (
Waiting State = "waiting"
InBed State = "in-bed"
Discharged State = "discharged"
Admitted State = "admitted"
)
type Patient struct {
ID string
Age, Acuity int
Arrived time.Time
State State
}
type WaitingList struct{ patients []*Patient }
A bed is one resource. Its type is a separate value, so rules can choose compatible types.
package triage
import "sync"
type BedType string
const (
Resus BedType = "resuscitation"
General BedType = "general"
Paediatric BedType = "paediatric"
)
type Bed struct {
ID string
Type BedType
PatientID string
}
type BedPool struct {
mu sync.Mutex
beds []Bed
}
type AlertSink interface{ LevelOne(string) }
type Allocator struct {
mu sync.Mutex
waiting WaitingList
pool *BedPool
rule MatchingRule
alerts AlertSink
}
Preference order matters: a child should receive a paediatric bed before a general bed when both are free.
package triage
type MatchingRule interface{ Types(*Patient) []BedType }
type Standard struct{}
Design decisions
How is the waiting list ordered?
Our choice
Hide container/heap behind WaitingList methods; compare acuity, then arrival.
Why not the alternatives
- Sort the whole slice after every arrivalSorting costs O(n log n) per arrival instead of O(log n) heap insertion.
How do we stop two nurses taking one bed?
Our choice
Reserve under the pool mutex while the allocator holds its coordination lock.
Why not the alternatives
- Check availability before lockingAnother nurse could reserve the bed between the check and assignment.
How do we choose a bed type?
Our choice
Use a MatchingRule strategy.
Why not the alternatives
- Hard-code matches inside AllocateNew matching policies would change the core scheduling code.
How are illegal transitions prevented?
Our choice
Make checked Patient methods control lifecycle changes.
Why not the alternatives
- Assign state strings from callersA waiting patient could be discharged without occupying a bed.
How are doctors alerted?
Our choice
Call AlertSink after registering the patient and unlocking.
Why not the alternatives
- Print under the allocator lockA slow output operation would block all allocations.
Trade-offs
Pros
- Urgency and tie-breaking are explicit.
- Beds cannot be double-booked.
- Matching rules can be replaced.
Cons
- One allocator lock limits throughput.
- Head-of-line blocking can leave other beds idle.
- Lower-priority patients can wait indefinitely.
Use when
- Prioritised allocation of scarce resources.
- A small pool with clear compatibility rules.
Avoid when
- Pure first-come-first-served work.
- A real clinical system without clinical review.
Quick check
Takeaways
- Use urgency first, then arrival time.
- Protect check-and-assign as one action.
- State and Strategy keep transitions and matching rules explicit.