System Design Academy

Saving…

Lesson 4 of 7 in Module 50 of 45 lessons complete

Hospital ER Triage & Bed Allocation — Part A: Design

Module 5, lesson 4Case study15 min

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

TypeResponsibility
PatientStore acuity, arrival and lifecycle state.
Bed and BedTypeDescribe one resource and its compatibility.
WaitingListHide the priority queue.
BedPoolReserve and release beds safely.
MatchingRuleRank compatible bed types.
AllocatorCoordinate arrivals, allocation and discharge.
AlertSinkNotify 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 coordinates two protected collections.

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.

An unavailable bed leaves the patient waiting.

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.

Only an in-bed patient can leave the ER.

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

patient.go — Store clinical acuity separately from scheduling changes.
Go
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.

bed.go — Separate resource reservation from scheduling.
Go
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.

matching.go — The strategy returns bed types in preference order.
Go
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.
Builds onEncapsulation

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.
Builds onState

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

Two level-2 patients wait. Who is allocated first?
Why does Allocate check and assign inside one critical section?

Takeaways

  • Use urgency first, then arrival time.
  • Protect check-and-assign as one action.
  • State and Strategy keep transitions and matching rules explicit.

Try the quick check above whenever you feel ready.

Previous: Restaurant Kitchen Order Router — Part B: Build