---
title: "Cloudflare Durable Objects - Stateful Serverless Functions"
description: "Build real-time & multiplayer apps without distributed systems complexity. Stateful serverless functions with WebSockets, embedded SQLite, and global coordination."
url: "https://www.cloudflare.com/products/durable-objects"
---

# Durable Objects

> Ship multiplayer systems, chat services, session stores and other coordination-heavy apps using Durable Objects.

## Key Features

- Single-instance strong consistency
- Built-in WebSocket support
- Transactional SQLite storage
- Hibernation for cost efficiency
- Alarms for scheduled wake-ups
- Automatic geographic placement
- Millions of objects per account

## Benefits

### Stateful serverless

Durable Objects are stateful serverless functions: they run for as long as you need them, can compute in the background, and handle multiple requests concurrently.

### WebSockets included

Every Durable Object is also a WebSocket server and client. Broadcast and coordinate state in real-time with just a few lines of code.

### Like a micro-VM

Think of every Durable Object as a micro-VM: create thousands (or millions) of them to do work, and throw them away when you're done.

### Embedded SQL database

Every Durable Object has a built-in, embedded SQLite database. Serverless doesn't have to be stateless.

### Schedule Work

Every Durable Object can do work in the background, periodically poll an API, and programmatically execute code in the future with the Alarms API (it's built-in).

### @cloudflare/actors

The @cloudflare/actors library provides a set of powerful abstractions over Durable Objects: the container to Durable Object's micro-VM.

## Use Cases

### Build chat systems and messaging apps

One object per chat room handles all messages, user presence, and room state with global consistency. No Redis clusters or message queues required. [Try it!](https://github.com/cloudflare/templates/tree/main/durable-chat-template)

### Create collaborative editing experiences

One object per document coordinates real-time edits from multiple users without distributed systems expertise. Think Figma or Google Docs architecture, simplified.

### Power multiplayer games and interactive experiences

One object per game session manages player state, game logic, and real-time updates close to users. Each game room scales independently without infrastructure overhead. [Try it!](https://github.com/cloudflare/templates/tree/main/multiplayer-globe-template)

### Coordinate live dashboards and real-time analytics

Objects aggregate and push real-time data updates to connected clients for monitoring and live events. Real-time data without the coordination nightmare.

## Code Examples

### Global co-ordination without the infrastructure hassle

Coordinate compute and state across thousands (or millions) of clients: route users to the same Durable Object, no matter where they are in the world.

```typescript
import { DurableObject } from 'cloudflare:workers';

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const counterName = url.searchParams.get('name');
    if (!counterName) return new Response('missing ?name', { status: 400 });

    const counterStub = env.COUNTERS.getByName(counterName);

    let count;
    switch (url.pathname) {
      case '/increment':
        count = await counterStub.increment();
        break;
      case '/decrement':
        count = await counterStub.decrement();
        break;
      case '/':
        count = await counterStub.get();
        break;
      default:
        return new Response('not found', { status: 404 });
    }
    return new Response(`${count}`);
  },
};

export class Counter extends DurableObject {
  async get() {
    return (await this.ctx.storage.get('value')) ?? 0;
  }
  async increment(amount = 1) {
    const newValue = (await this.get()) + amount;
    await this.ctx.storage.put('value', newValue);
    return newValue;
  }
  async decrement(amount = 1) {
    const newValue = (await this.get()) - amount;
    await this.ctx.storage.put('value', newValue);
    return newValue;
  }
}
```

### Fan-out, fan-in

Durable Objects can speak WebSockets: connect thousands of clients per object and create millions of objects to broadcast real time events.

```typescript
import { DurableObject } from "cloudflare:workers";

export class WebSocketServer extends DurableObject {
  async fetch() {
    const webSocketPair = new WebSocketPair();
    const [client, server] = Object.values(webSocketPair);
    this.ctx.acceptWebSocket(server);

    return new Response(null, {
      status: 101,
      webSocket: client,
    });
  }

  async webSocketMessage(ws, message) {
    ws.send(
      `[Durable Object] message: ${message}, connections: ${this.ctx.getWebSockets().length}`,
    );
  }
}
```

### An embedded SQL database in every Durable Object

Every Durable Object has a built-in, [zero-latency SQLite database](https://blog.cloudflare.com/sqlite-in-durable-objects/): store per-user state, buffer events, and/or persist message histories without having to scale out another database.

```typescript
import { DurableObject } from "cloudflare:workers";

export class UserChatHistory extends DurableObject {
  sql: SqlStorage;
  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);
    this.sql = ctx.storage.sql;

    this.sql.exec(`CREATE TABLE IF NOT EXISTS history(
      roomId    INTEGER PRIMARY KEY,
      roomName  TEXT,
      message   TEXT,
      timestamp TIMESTAMP)`
    );
  }

  async getHistory(roomId: string) {
    return this.sql.exec("SELECT * FROM history WHERE roomId = ?;", roomId).one()
  }
}
```

## Resources

- [Full Documentation](https://developers.cloudflare.com/durable-objects): Complete technical documentation
- [Get Started](https://dash.cloudflare.com/sign-up): Sign up and start building
- [Pricing](/plans.md): See pricing details

## Related Products

- [Browser Run](/products/browser-rendering.md): Automated browsers
- [Cloudflare Pages](/products/pages.md): Build & deploy frontend sites
- [Containers](/products/containers.md): Any language, anywhere
- [Email Service](/products/email-service.md): Send and receive email

---

*This is a markdown version of [https://www.cloudflare.com/products/durable-objects](https://www.cloudflare.com/products/durable-objects) for AI/LLM consumption.*
