0Pricing
Edge Computing with Cloudflare Workers & Deno · 강의

Durable Objects 및 상태 기반 조정

Durable Objects를 사용하여 본래 상태가 없는 엣지 아키텍처에 강한 일관성을 갖는 단일 인스턴스 상태와 조정 기능을 추가합니다.

Durable Objects 및 상태 기반 조정은(는) CoddyKit의 무료 Edge Computing with Cloudflare Workers & Deno 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Edge Computing with Cloudflare Workers & Deno 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

The Stateless Problem

Workers are stateless by design, each request may hit a different instance anywhere on Earth.

That is great for scale, but hard when you need:

  • A single source of truth
  • Coordination between many clients
  • Strong consistency, not eventual

Durable Objects solve exactly this.

What Is a Durable Object?

A Durable Object (DO) is a single, globally-addressable instance with its own private storage.

For a given object ID, all requests route to the same instance, giving you a consistent place to hold state.

Defining a Durable Object Class

A DO is a class with a fetch() method and access to persistent state.storage.

export class Counter {
  constructor(state, env) {
    this.state = state;
  }
  async fetch(request) {
    let count = (await this.state.storage.get('count')) || 0;
    count++;
    await this.state.storage.put('count', count);
    return new Response(String(count));
  }
}

Binding the Object

Declare the DO class as a binding in wrangler.toml and add a migration so Cloudflare knows about it.

[[durable_objects.bindings]]
name = "COUNTER"
class_name = "Counter"

[[migrations]]
tag = "v1"
new_classes = ["Counter"]

Getting an Object Stub

From a Worker, derive an ID then get a stub to talk to that specific instance.

const id = env.COUNTER.idFromName('global-counter');
const stub = env.COUNTER.get(id);
const res = await stub.fetch('https://do/increment');

idFromName vs newUniqueId

Two ways to get an ID:

  • idFromName('room-42') deterministic, same name always maps to the same object, ideal for named resources like chat rooms
  • newUniqueId() a brand-new unique object, ideal for per-session state

Strong Consistency Guarantee

Because every request for an ID hits the same instance and runs single-threaded, DO operations are strongly consistent, no race conditions on its own storage.

This makes DOs perfect for counters, locks, and leaderboards.

Coordinating Many Clients

A DO is a natural hub. Combined with WebSockets it can broadcast to all connected clients, the basis for collaborative apps and multiplayer rooms.

// Inside the DO: track sockets and fan out
this.sessions.forEach((ws) => ws.send(message));

Storage API Basics

state.storage is a transactional key-value store local to the object.

await this.state.storage.put('user:1', { name: 'Ada' });
const user = await this.state.storage.get('user:1');
await this.state.storage.delete('user:1');

Alarms for Scheduled Logic

Durable Objects support alarms, schedule the object to wake itself up later for cleanup or timeouts.

await this.state.storage.setAlarm(Date.now() + 60000);
// later, the runtime calls:
async alarm() {
  await this.cleanup();
}

When to Use Durable Objects

Reach for DOs when you need:

  • Coordination across requests or clients
  • Strong consistency on a single entity
  • Real-time rooms, locks, rate counters, or queues

For simple cached reads, KV is cheaper. Choose the right tool.

Quick Check

Which method gives you a deterministic Durable Object ID for a named resource like a chat room?

Recap

Durable Objects bring stateful coordination to the edge:

  • One globally-addressable instance per ID
  • Private transactional storage and alarms
  • Strong consistency and single-threaded execution
  • Use idFromName for named resources, newUniqueId for sessions

They are the missing piece for real-time, consistent, edge-native systems.

자주 묻는 질문

“Durable Objects 및 상태 기반 조정” 강의는 무료인가요?

네 — “Durable Objects 및 상태 기반 조정” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Edge Computing with Cloudflare Workers & Deno 강의 전체를 잠금 해제할 수 있습니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.

“Durable Objects 및 상태 기반 조정”에서 뭘 배우나요?

Durable Objects를 사용하여 본래 상태가 없는 엣지 아키텍처에 강한 일관성을 갖는 단일 인스턴스 상태와 조정 기능을 추가합니다. 브라우저에서 직접 실행하는 실습 코드로 Edge Computing with Cloudflare Workers & Deno을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Edge Computing with Cloudflare Workers & Deno을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Edge Computing with Cloudflare Workers & Deno은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“Durable Objects 및 상태 기반 조정” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Edge Computing with Cloudflare Workers & Deno 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Edge Computing with Cloudflare Workers & Deno 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 엣지의 마이크로서비스
  2. 이벤트 기반 아키텍처
  3. 지리적 위치 및 지역화
  4. Durable Objects 및 상태 기반 조정
← Edge Computing with Cloudflare Workers & Deno(으)로 돌아가기