// Glossary

WebSocket Hibernation

Last updated ·

WebSocket hibernation is a feature of Cloudflare Durable Objects that lets an object be evicted from memory while its WebSocket clients stay connected. The connections remain open on Cloudflare's network. When a message arrives, the runtime re-creates the object, runs its constructor again and delivers the message to the object's handler.

Why does hibernation change the economics of real-time features?

Presence indicators, live notifications and inboxes hold connections open for hours while messages are sparse. Without hibernation, the Durable Object serving those sockets stays resident the whole time. Cloudflare's guide to WebSockets in Durable Objects states that "Billable Duration (GB-s) charges do not accrue during hibernation". For a product with many mostly idle connections, hibernation removes the cost of keeping objects in memory between messages.

How does EOXLABS build with hibernating WebSockets?

The rules below come from the platform's documented behavior, and EOXLABS applies them to every hibernating object it designs:

  1. Accept connections with ctx.acceptWebSocket(), not ws.accept(). Only the former allows the object to hibernate.
  2. Keep per-connection state small and store it with serializeAttachment(), which Cloudflare caps at 16,384 bytes. Larger data goes to the object's storage, with only its key attached to the socket.
  3. Keep the constructor light, because it runs again after every wake-up.
  4. Avoid setTimeout and setInterval for idle housekeeping. Cloudflare lists timers, alarms and incoming requests among the events that keep an object awake.

The engineering note on a real-time inbox built with WebSocket hibernation puts these rules to work.

What does it look like in practice?

A support team keeps an inbox tab open all day, and a new ticket arrives every few minutes. Between tickets, the Durable Object holding the team's sockets hibernates. Protocol-level ping frames, defined in RFC 6455, are answered by the runtime without waking the object. A new ticket wakes it, the object reads each socket's attachment to learn who is connected, and it pushes the ticket to the right agents. The wake-up adds a little work to the first message after a quiet period, which can show up in tail latency.

What is hibernation often confused with?

  • Disconnection. Clients see no close event during hibernation. If either side does close the connection, its attachment is lost.
  • Persistence of memory. In-memory state is reset on eviction. Anything held only in variables is gone when the object wakes.
  • Outbound connections. Cloudflare states that hibernation "is only supported when a Durable Object acts as a WebSocket server" and that "Outgoing WebSockets do not hibernate." An object holding an outbound connection to a speech or streaming provider does not hibernate; Cloudflare notes that an open outbound WebSocket "prevents eviction for up to 15 minutes", which matters for real-time voice systems.

The platform underneath is described on the Durable Objects stack page, and the glossary defines related terms.

→

Related

+

More glossary

Have a system that can’t go dark?

Start a project →