ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ
ใช้ Durable Objects เพื่อเพิ่มสถานะที่สอดคล้องกันอย่างเคร่งครัดและมีอินสแตนซ์เดียว รวมถึงการประสานงานให้กับสถาปัตยกรรมเอดจ์ที่เดิมไม่มีสถานะ
ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 roomsnewUniqueId()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
idFromNamefor named resources,newUniqueIdfor sessions
They are the missing piece for real-time, consistent, edge-native systems.
คำถามที่พบบ่อย
บทเรียน “ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ”
ใช้ Durable Objects เพื่อเพิ่มสถานะที่สอดคล้องกันอย่างเคร่งครัดและมีอินสแตนซ์เดียว รวมถึงการประสานงานให้กับสถาปัตยกรรมเอดจ์ที่เดิมไม่มีสถานะ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม
ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ไมโครเซอร์วิสใกล้ผู้ใช้
- สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์
- พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น
- ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ