Cloudflare มีชุดเครื่องมือจัดเก็บข้อมูลที่ครอบคลุมบนแนวขอบ (Edge) ได้แก่ KV (Key-Value Store), Durable Objects และ Queues แต่ละเครื่องมือมีวัตถุประสงค์เฉพาะ ครอบคลุมความต้องการของแอปพลิเคชันที่หลากหลาย ตั้งแต่การเก็บข้อมูล cache ชั่วคราวไปจนถึงการสร้างสถานะ (Stateful) applications
Cloudflare KV — Key-Value Store บนเคลาว์
KV เป็นดาตาสตอร์ Key-Value ที่กระจายอยู่ทั่วโลก ใช้ที่เก็บข้อมูล ข้อมูล Session, Configuration หรือ Cache ของผู้ใช้
- Global Replication: ข้อมูลจำลองไปทั่วโลก
- ความหน่วงต่ำ: อ่านข้อมูลจากตำแหน่งที่ใกล้ที่สุด
- ความทีธรรมชาติ: API ง่ายต่อการใช้งาน
- ข้อมูลเสริม (TTL): ข้อมูลหมดอายุโดยอัตโนมัติ
ตัวอย่างการใช้ KV:
// บันทึกค่า
await env.KV.put('user:123', JSON.stringify({name: 'John', age: 30}));
// ดึงค่า
const user = await env.KV.get('user:123', 'json');
// ลบค่า
await env.KV.delete('user:123');
// ลิสต์กุญแจ
const keys = await env.KV.list();
Durable Objects — Stateful Computing บนเคลาว์
Durable Objects ช่วยให้คุณสามารถสร้าง Applications ที่มีสถานะ (Stateful) ขณะทำงานบน Edge บ่อย ทำให้เหมาะสำหรับ Real-time Applications
- Consistency: ดำเนินการอย่างมีลำดับและปลอดภัย
- Real-time Communication: WebSockets, Multiplayer Games
- Coordination: Distributed Locking, Consensus
- Persistent State: บันทึกสถานะไปยัง Storage
ตัวอย่างการสร้าง Durable Object:
export class Counter {
constructor(state, env) {
this.state = state;
}
async fetch(request) {
const count = (await this.state.get('count')) || 0;
await this.state.put('count', count + 1);
return new Response(count);
}
}
export default {
async fetch(request, env) {
const id = env.COUNTER.idFromName('global');
const obj = env.COUNTER.get(id);
return obj.fetch(request);
}
};
Queues — ระบบคิว (Queue) สำหรับการประมวลผลแบบอะซิงค์
Cloudflare Queues ให้วิธีการส่ง Message ไปยัง Worker ที่ประมวลผลได้โดยอะซิงค์ เหมาะสำหรับงานที่ไม่เร่งด่วน
- Asynchronous Processing: ส่งงาน และ Worker ประมวลผลภายหลัง
- Delivery Guarantee: ข้อความถูกส่งอย่างน้อยครั้งเดียว
- Automatic Retry: พยายามส่งใหม่เมื่อล้มเหลว
- Rate Limiting: ควบคุมจำนวนข้อความต่อวินาที
ตัวอย่างการใช้ Queues:
// Producer Worker
export default {
async fetch(request, env) {
await env.QUEUE.send({action: 'email', user_id: 123});
return new Response('Message sent');
}
};
// Consumer Worker
export default async (batch, env) => {
for (const message of batch.messages) {
const data = message.body;
// ประมวลผลข้อมูล
console.log('Processing:', data);
}
};
การเปรียบเทียบ KV, Durable Objects และ Queues
เลือกตัวเลือกตามความต้องการของคุณ:
- KV: เก็บข้อมูล Cache, Sessions, Configuration
- Durable Objects: Real-time Apps, Stateful Logic, Coordination
- Queues: Async Jobs, Email, Background Processing
- Session Management: เก็บ Session โดยไม่ต้องโปรแกรมบนเซิร์ฟเวอร์
- Rate Limiting: บันทึกจำนวน Requests ต่อ User
- Real-time Notifications: ใช้ Durable Objects สำหรับ WebSocket connections
- Job Processing: ประมวลผลงานยาว ๆ โดยไม่บล็อก Request
สรุป
Cloudflare KV, Durable Objects และ Queues เป็นชุดเครื่องมือที่สมบูรณ์สำหรับสร้างแอปพลิเคชัน Modern บนเคลาว์ KV สำหรับการเก็บข้อมูลอย่างง่าย Durable Objects สำหรับสถานะ และ Queues สำหรับการประมวลผลแบบอะซิงค์ เมื่อรวมกับ Cloudflare Workers และ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง คุณจะได้ระบบที่ปลอดภัย เร็ว และยืดหยุ่น

