การเก็บข้อมูลบน Edge Network เป็นสิ่งที่ Cloudflare ให้ความสำคัญมากขึ้นเรื่อยๆ Cloudflare D1 คือ Serverless SQL Database ที่ใช้ SQLite ทำงานร่วมกับ Cloudflare Workers และ Pages ทำให้คุณสร้าง Full-Stack Application บน Edge ได้โดยไม่ต้องจัดการ Database Server เอง เหมาะสำหรับนักพัฒนาที่ใช้ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง และต้องการข้อมูลบางส่วนอยู่ใกล้ User
Cloudflare D1 คืออะไร?
D1 เป็น Serverless SQLite Database บน Cloudflare Edge ที่ใช้ SQLite Engine แผ่สัก Primary Database ไปยัง Edge Location ทั่วโลก ทำให้การ Query มี Latency ต่ำมาก
ประโยชน์หลัก อาทิ:
- Zero Cold Start — ทำงานร่วมกับ Workers ไม่มี Cold Start
- SQL Syntax — ใช้คำสั่ง SQL เหมือนปกติ
- Automatic Replication — D1 สำเนา Read Replica ไปยัง Edge อัตโนมัติ
- Free Tier — Free Plan รองรับ 5 GB Storage และ 5 ล้าน Row Reads/วัน
การสร้างและใช้งาน D1 Database
สร้าง Database
# สร้าง D1 Database ใหม่
wrangler d1 create my-database
# ดู Database ทั้งหมด
wrangler d1 list
# รัน SQL ผ่าน CLI
wrangler d1 execute my-database --command="SELECT * FROM users LIMIT 10"
ตั้งค่าใน wrangler.toml
name = "my-worker"
main = "src/index.js"
compatibility_date = "2024-01-01"
# เชื่อม D1 กับ Worker
[[d1_databases]]
binding = "DB"
database_name = "my-database"
database_id = "xxxx-xxxx-xxxx-xxxx"
การใช้งาน D1 ใน Worker
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === '/api/users') {
// ดึงข้อมูลจาก D1
const { results } = await env.DB.prepare(
'SELECT id, name, email FROM users ORDER BY created_at DESC LIMIT 10'
).all();
return new Response(JSON.stringify(results), {
headers: { 'Content-Type': 'application/json' }
});
}
if (request.method === 'POST' && url.pathname === '/api/users') {
const { name, email } = await request.json();
// INSERT ข้อมูลเข้า D1
await env.DB.prepare(
'INSERT INTO users (name, email, created_at) VALUES (?, ?, datetime(\'now\'))'
).bind(name, email).run();
return new Response(JSON.stringify({ success: true }), {
headers: { 'Content-Type': 'application/json' }
});
}
return new Response('Not Found', { status: 404 });
},
};
D1 Migration: การจัดการ Schema
-- migrations/0001_create_users.sql
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- รัน Migration
$ wrangler d1 migrations apply my-database
D1 vs ฐานข้อมูลทั่วไป
| คุณสมบัติ | Cloudflare D1 | Traditional Database |
|---|---|---|
| Latency | ต่ำ (ทำงานบน Edge) | ช้ากว่า (อยู่ Datacenter เดียว) |
| Management | Serverless, ไม่ต้อง Manage | ต้อง Manage Server |
| Syntax | SQLite (SQL Standard) | MySQL, PostgreSQL, MSSQL |
| Scaling | Automatic | ต้อง Config |
| Cost | Free Tier + Pay-as-you-go | ค่า Server คงที่ |
| Transaction | รองรับ ACID | รองรับ ACID |
เมื่อไหรควรใช้ D1?
ควรใช้ D1 สำหรับ:
- ข้อมูลที่ต้องการ Low Latency เช่น User Profile, Session, Config
- Application ที่สร้างด้วย Workers หรือ Pages Functions
- ข้อมูล Read-heavy ที่ไม่มีการ Concurrent Write สูง
- Prototype หรือ MVP ที่ต้องการความเร็วในการสร้าง
ควรใช้ Cloud VPS โดยตรง สำหรับ:
- Database ที่ต้องการ Concurrent Write สูง (MySQL, PostgreSQL)
- Transaction ที่ซับซ้อนหรือมีผลต่อเนื่องสูง
- ข้อมูลที่ต้องการ Custom Extension หรือ Advanced Features
สรุป
Cloudflare D1 เป็นตัวเลือกที่น่าสนใจสำหรับนักพัฒนาที่ใช้ Cloudflare Workers และต้องการ Database กับ Low Latency แต่สำหรับ Application แบบ Enterprise ที่ต้องการ Database แบบเต็มรูปแบบ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง ที่ติดตั้ง MySQL หรือ PostgreSQL ยังเป็นทางเลือกที่ดีกว่าอยู่เสมอ

