Durable Objects — Part 5. A state machine that runs on alarms

Chat rooms react to time as well as events: archive after quiet hours, close after days, fire scheduled notices. One DO alarm is that clock — no separate cron fleet required.

setAlarm is singular per instance (it replaces). Persist a queue of due work in SQLite; point the alarm at the nearest time — the same idea as Agents job queues.

Two pillars

State in SQLite (phase, lastActiveAt). Due times via alarms. alarm() reads, guards, writes, reschedules. Alarms wake hibernated objects.

In a chat room

Each message bumps lastActiveAt and resets a 24h idle alarm. On fire, move to archive and maybe snapshot to L2. Closing clears alarms and data by policy.

Input/output gates already reduce races — keep transitions short. Reserve blockConcurrencyWhile for brief schema init.

Series close: named servers (1), one per room (2), sleep with the door open (3), tiered memory (4), alarm-driven state (5). That is a chat room that can nap with the chat bar still open.