it

Edge Computing คืออะไร? สอนสร้าง Edge Application ด้วย Cloudflare Workers 2026

edge computing cloudflare workers guide
Edge Computing คืออะไร? สอนสร้าง Edge Application ด้วย Cloudflare Workers 2026

ในยุคที่ผู้ใช้คาดหวังว่าเว็บไซต์จะโหลดเร็วภายในไม่กี่มิลลิวินาที การส่ง request ข้ามทวีปไปยัง origin server แล้วรอ response กลับมาเริ่มไม่เพียงพออีกต่อไป Edge Computing คือคำตอบที่ย้ายการประมวลผลไปอยู่ใกล้ผู้ใช้มากที่สุด ณ จุดเดียวกับ CDN ทำให้ latency ลดลงอย่างมหาศาล

บทความนี้จะอธิบาย Edge Computing ตั้งแต่แนวคิดพื้นฐาน เจาะลึก Cloudflare Workers ซึ่งเป็น platform ที่ได้รับความนิยมสูงสุดในปี 2026 ครอบคลุมทุก product ตั้งแต่ KV, Durable Objects, D1, R2 จนถึงการสร้าง full application ที่ edge พร้อมเปรียบเทียบกับ Vercel Edge Functions, AWS Lambda@Edge และ Deno Deploy อย่างละเอียด

อ่านเพิ่ม: React Server Components คืออะไร? เข้าใจ RSC และ use server/u · อ่านเพิ่ม: Worker Threads คืออะไร? สอนใช้ Multi-threading ใน Node.js สำ · อ่านเพิ่ม: Service Mesh คืออะไร? เปรียบเทียบ Istio vs Linkerd สำหรับ Ku

Edge Computing คืออะไร?

Edge Computing คืออะไร? สอนสร้าง Edge Application ด้วย Cloudflare Workers 2026

Edge Computing คือแนวคิดที่ย้ายการประมวลผลจาก centralized server (data center ที่อยู่ไกล) ไปยังจุดที่อยู่ใกล้ผู้ใช้มากที่สุด ซึ่งก็คือ edge node ของ CDN network ที่กระจายอยู่ทั่วโลก

แนวคิดหลัก

ลองนึกภาพว่าคุณมี API server อยู่ที่ US-East (Virginia) แต่ผู้ใช้อยู่ที่กรุงเทพ ทุก request ต้องเดินทางไปกลับข้ามมหาสมุทรแปซิฟิก ใช้เวลาอย่างน้อย 200-300ms เฉพาะค่า network round trip ไม่นับรวม processing time แต่ถ้าคุณ deploy code ไว้ที่ edge node ในสิงคโปร์หรือกรุงเทพเลย latency จะเหลือแค่ 5-20ms

Edge vs Cloud vs On-Premise

ลักษณะOn-PremiseCloud (Region)Edge Computing
ตำแหน่งในออฟฟิศ/Data center ตัวเองCloud region (เช่น ap-southeast-1)CDN edge ทั่วโลก (300+ locations)
Latencyขึ้นกับตำแหน่ง50-300ms (ข้าม region)5-30ms (ใกล้ผู้ใช้)
Scaleจำกัดยืดหยุ่นสูงอัตโนมัติทั่วโลก
ค่าใช้จ่ายFixed cost สูงPay-per-usePay-per-request (ถูกมาก)
Compute Powerไม่จำกัดไม่จำกัดจำกัด (CPU time, memory)
เหมาะกับLegacy, complianceงาน heavy processingLatency-sensitive, global users

Cloudflare Workers — V8 Isolates ไม่ใช่ Containers

สิ่งที่ทำให้ Cloudflare Workers ต่างจาก serverless platform อื่นคือเทคโนโลยีที่ใช้ ในขณะที่ AWS Lambda ใช้ container (Firecracker microVM) ซึ่งมี cold start 100-500ms Cloudflare Workers ใช้ V8 Isolates ซึ่งเป็น JavaScript engine ตัวเดียวกับ Chrome browser ทำให้

  • Cold start แทบไม่มี: เริ่มทำงานภายใน 0-5ms เทียบกับ Lambda ที่ 100-500ms
  • Memory footprint ต่ำมาก: แต่ละ isolate ใช้ memory น้อยมาก ทำให้รันได้หลายพัน isolate พร้อมกัน
  • Security: แต่ละ request ทำงานใน isolate ที่แยกจากกัน ไม่สามารถเข้าถึงข้อมูลของ request อื่นได้
  • Global deployment: Deploy ครั้งเดียว ทำงานใน 300+ locations ทั่วโลกทันที

Workers Runtime — Service Workers API

Cloudflare Workers ใช้ API ที่คล้ายกับ Service Workers ใน browser ทำให้นักพัฒนาที่เคยเขียน JavaScript/TypeScript สามารถเริ่มได้ทันที

เนื้อหาเกี่ยวข้อง — AWS Step Functions Freelance IT Career

Cloudflare Storage Solutions

KV (Key-Value Storage)

KV เป็น eventually consistent key-value store ที่กระจายอยู่ทั่วโลก เหมาะสำหรับข้อมูลที่อ่านบ่อยแต่เขียนไม่บ่อย เช่น configuration, feature flags, cached data

Durable Objects — Consistency ที่ Edge

Durable Objects แก้ปัญหาที่ KV ทำไม่ได้ คือ strong consistency แต่ละ Durable Object เป็น singleton ที่มี state ของตัวเอง เหมาะสำหรับ real-time collaboration, rate limiting, WebSocket rooms

D1 — SQLite ที่ Edge

D1 คือ SQLite database ที่ทำงานบน edge network ของ Cloudflare ทำให้คุณมี relational database ที่ latency ต่ำมาก โดยไม่ต้อง provision server

แนะนำเพิ่มเติม — หนังสือเทรดที่ SiamCafeBook

R2 — S3-Compatible Object Storage

R2 เป็น object storage ที่เข้ากันได้กับ S3 API แต่ไม่มีค่า egress (ค่าโอนข้อมูลออก) ทำให้ประหยัดมากสำหรับ application ที่ serve static files หรือ user uploads

Queues และ AI Workers

Queues ช่วยให้คุณส่งงานจาก Worker หนึ่งไปยังอีก Worker หนึ่งแบบ asynchronous เหมาะสำหรับงานที่ไม่ต้องตอบ user ทันที เช่น ส่ง email, process image, update analytics

AI Workers (Workers AI) ให้คุณรัน AI model ที่ edge ได้โดยตรง รองรับทั้ง text generation, image generation, embeddings, speech-to-text โดยใช้ GPU ที่ Cloudflare มี

Wrangler CLI — เครื่องมือจัดการ Workers

# ติดตั้ง

npm install -g wrangler



# Login

wrangler login



# สร้างโปรเจกต์ใหม่

wrangler init my-worker

cd my-worker



# Development (local)

wrangler dev



# Deploy

wrangler deploy



# ดู Logs

wrangler tail



# จัดการ KV

wrangler kv namespace create MY_KV

wrangler kv key put --binding MY_KV "key" "value"



# จัดการ D1

wrangler d1 create my-db

wrangler d1 execute my-db --command "SELECT * FROM users"



# จัดการ R2

wrangler r2 bucket create my-bucket

Workers กับ Hono Framework

Hono เป็น web framework ที่ออกแบบมาสำหรับ edge computing โดยเฉพาะ เร็ว เบา และทำงานได้ทุก runtime (Cloudflare Workers, Deno, Bun, Node.js) การใช้ Hono กับ Workers ทำให้เขียน API ได้เร็วและสะอาดมากขึ้น

เนื้อหาเกี่ยวข้อง — if else javascript คือ

ทำไม Hono? Hono มี router ที่เร็วมาก (RegExpRouter) ขนาดเล็กมาก (~14KB) มี middleware ครบ (CORS, JWT, Logger, Rate Limit) และ type-safe ด้วย TypeScript ทำให้เป็น framework ที่ดีที่สุดสำหรับ Workers ในปี 2026

Workers กับ Remix — Full-Stack ที่ Edge

Cloudflare Pages รองรับ Remix ทำให้สร้าง full-stack application ที่ทำงานทั้ง server-side rendering และ API ที่ edge ได้ทั้งหมด

# สร้างโปรเจกต์ Remix บน Cloudflare

npx create-remix@latest --template remix-run/remix/templates/cloudflare



# โครงสร้าง

my-remix-app/

├── app/

│   ├── routes/

│   │   ├── _index.tsx

│   │   └── api.users.tsx

│   └── root.tsx

├── functions/

├── public/

└── wrangler.toml

Edge Middleware Patterns

A/B Testing ที่ Edge

export default {

  async fetch(request, env) {

    const url = new URL(request.url);



    // กำหนด variant จาก cookie หรือ random

    let variant = getCookie(request, "ab-variant");

    if (!variant) {

      variant = Math.random() < 0.5 ? "A" : "B";

    }



    // Rewrite URL ตาม variant

    if (variant === "B") {

      url.pathname = "/new-landing" + url.pathname;

    }



    const response = await fetch(url.toString(), request);

    const newResponse = new Response(response.body, response);

    newResponse.headers.set("Set-Cookie", `ab-variant=${variant}; Path=/; Max-Age=86400`);

    return newResponse;

  },

};

Image Transformation ที่ Edge

export default {

  async fetch(request, env) {

    const url = new URL(request.url);



    // /images/photo.jpg?w=400&h=300&fit=cover

    if (url.pathname.startsWith("/images/")) {

      const width = url.searchParams.get("w") || "800";

      const height = url.searchParams.get("h") || "600";

      const fit = url.searchParams.get("fit") || "cover";



      return fetch(request, {

        cf: {

          image: {

            width: parseInt(width),

            height: parseInt(height),

            fit: fit,

            quality: 80,

            format: "webp",

          },

        },

      });

    }



    return fetch(request);

  },

};

เปรียบเทียบ Edge Platforms 2026

Edge Computing คืออะไร? สอนสร้าง Edge Application ด้วย Cloudflare Workers 2026
ฟีเจอร์Cloudflare WorkersVercel Edge FunctionsAWS Lambda@EdgeDeno Deploy
RuntimeV8 IsolatesV8 Isolates (Edge Runtime)Container (Node.js/Python)V8 Isolates (Deno)
Cold Start0-5ms0-5ms100-500ms0-5ms
Locations300+~30 regions13 CloudFront locations35+ regions
Max Execution30s (paid) / 10ms (free)25s5s (viewer) / 30s (origin)50ms (free) / unlim (paid)
DatabaseD1, KV, Durable ObjectsVercel KV, PostgresDynamoDB (แยก)Deno KV
Object StorageR2 (no egress fee)Vercel BlobS3ไม่มี built-in
Free Tier100K req/day500K/month1M req/month100K req/day
TypeScriptรองรับรองรับผ่าน build stepNative
WebSocketรองรับ (Durable Objects)ไม่รองรับไม่รองรับรองรับ
AIWorkers AI (built-in)AI SDKBedrock (แยก)ไม่มี built-in

Pricing เปรียบเทียบ

PlanCloudflare WorkersVercel EdgeAWS Lambda@Edge
Free100K req/day, 10ms CPU500K req/month1M req/month
Paid (per 1M req)$0.50$2.00 (Pro plan)$0.60
Base Price$5/mo (Workers Paid)$20/mo (Pro)Free (pay per use)
Egressฟรีคิดตาม bandwidth$0.085/GB
Cloudflare ถูกกว่าอย่างชัดเจน: โดยเฉพาะเรื่อง egress ที่ฟรี ถ้าคุณ serve รูปภาพหรือ static content มาก R2 + Workers จะประหยัดกว่า S3 + CloudFront อย่างมหาศาล การวางแผนต้นทุนที่ดีคือหัวใจของการพัฒนาระบบที่ยั่งยืน

Edge Computing Use Cases ที่ใช้งานจริง

1. Personalization ที่ Edge

ปรับเปลี่ยนเนื้อหาตาม geolocation, device, หรือ user preferences โดยไม่ต้อง round trip ไป origin เหมาะสำหรับเว็บ e-commerce ที่ต้องแสดงราคา สกุลเงิน และ shipping options ตามประเทศ

2. Auth ที่ Edge

ตรวจสอบ JWT token หรือ API key ที่ edge ก่อนที่ request จะถึง origin ช่วยลด load ของ backend server และ block request ที่ไม่ถูกต้องได้ทันที นักพัฒนาที่ทำงานกับระบบการเงินออนไลน์จะได้ประโยชน์จาก pattern นี้มาก

3. A/B Testing

ตัดสินใจที่ edge ว่าจะแสดง variant ไหนให้ user เห็น ทำได้เร็วกว่าการ redirect ผ่าน origin และไม่กระทบ performance

แนะนำเพิ่มเติม — ระบบเทรดของ iCafeForex

4. Rate Limiting

จำกัดจำนวน request ต่อ IP หรือ API key ที่ edge ป้องกัน DDoS และ abuse ก่อนที่จะกระทบ backend

5. Image/Video Transformation

Resize, crop, convert format ของรูปภาพที่ edge ส่ง optimized image ให้ user ตาม device และ network conditions

เนื้อหาเกี่ยวข้อง — ทำความเข้าใจ double top and double bottom chart patterns

6. Geo-routing และ Localization

Route request ไปยัง origin server ที่ใกล้ที่สุด หรือแสดงเนื้อหาภาษาที่ถูกต้องโดยอัตโนมัติ ระบบที่ต้องให้บริการผู้ใช้หลายประเทศ เช่น แพลตฟอร์มบริการออนไลน์จะได้ประโยชน์อย่างมาก

Limitations — ข้อจำกัดที่ต้องรู้

CPU Time

Workers Free plan จำกัดที่ 10ms CPU time ต่อ request ส่วน Paid plan ได้ 30 seconds ฟังดูน้อยแต่เนื่องจากไม่นับ I/O wait time (เช่น fetch, database query) จึงเพียงพอสำหรับงานส่วนใหญ่ แต่ไม่เหมาะกับ CPU-intensive tasks เช่น image processing จำนวนมากหรือ complex computation

Memory

Workers มี memory limit ที่ 128MB ซึ่งเพียงพอสำหรับ API ทั่วไป แต่ไม่เหมาะกับงานที่ต้องใช้ memory มาก เช่น processing ไฟล์ขนาดใหญ่

Runtime Compatibility

Workers ไม่ใช่ Node.js เต็มรูปแบบ ไม่รองรับ Node.js APIs บางตัว เช่น fs, child_process, net npm packages ที่พึ่งพา Node.js-specific APIs จะใช้ไม่ได้ แม้จะมี compatibility layer เพิ่มขึ้นเรื่อยๆ ก็ตาม

Database Limitations

D1 เป็น SQLite ดังนั้นมีข้อจำกัดเรื่อง concurrent writes KV เป็น eventually consistent ไม่เหมาะกับข้อมูลที่ต้อง consistent ทันที ต้องเลือกใช้ Durable Objects ถ้าต้องการ strong consistency

สร้าง Full Application ที่ Edge

มาลองสร้าง application จริงที่ทำงานทั้งหมดที่ edge ตั้งแต่ frontend จนถึง database

// wrangler.toml

name = "my-edge-app"

main = "src/index.ts"

compatibility_date = "2026-01-01"



[[d1_databases]]

binding = "DB"

database_name = "my-app-db"

database_id = "xxx"



[[kv_namespaces]]

binding = "CACHE"

id = "yyy"



[[r2_buckets]]

binding = "UPLOADS"

bucket_name = "my-uploads"
Full Stack at Edge: ด้วย Hono + D1 + KV + R2 คุณสร้าง full application ที่ทำงานทั้งหมดที่ edge ได้ ไม่ต้องมี origin server เลย ค่าใช้จ่ายต่ำมาก performance สูงมาก เหมาะกับ startup หรือ side project ที่ต้องการ global availability โดยไม่ต้องจัดการ infrastructure

Cloudflare Pages — Static + Edge Functions

Cloudflare Pages เป็น platform สำหรับ deploy frontend framework (React, Vue, Svelte, Astro) พร้อม edge functions ที่ทำงานใน Workers runtime ทำให้ได้ full-stack application ที่ deploy ง่ายมาก

เนื้อหาเกี่ยวข้อง — บทความที่เกี่ยวข้อง: swift playground คืออะไร

# Deploy static site + functions

npx wrangler pages deploy ./dist



# โครงสร้าง

my-site/

├── src/                 # Frontend code

├── functions/           # Edge functions (auto-mapped to routes)

│   └── api/

│       └── users.ts     # → /api/users

├── public/              # Static assets

└── wrangler.toml

เทคนิคขั้นสูง

แนวโน้มของ Edge Computing ในอนาคต

Edge Computing กำลังเติบโตอย่างรวดเร็วและจะเปลี่ยนวิธีที่เราสร้าง web application ไปอย่างสิ้นเชิง เทรนด์ที่น่าจับตาในปี 2026 และต่อไป ได้แก่

  • AI at Edge: การรัน AI model ที่ edge จะกลายเป็นเรื่องปกติ ลด latency สำหรับ AI-powered features
  • Edge Databases: database ที่ออกแบบมาสำหรับ edge เช่น D1, Turso, Neon จะเสถียรมากขึ้น
  • WebAssembly at Edge: การรัน Wasm modules ที่ edge จะเปิดทางให้ใช้ภาษาอื่นนอกจาก JavaScript เช่น Rust สำหรับ backend ที่สามารถ compile ไป Wasm แล้วรันที่ edge ได้
  • Edge-first Frameworks: framework ใหม่ๆ จะออกแบบมาสำหรับ edge เป็นหลัก ไม่ใช่ adapter

การเรียนรู้ edge computing ตั้งแต่ตอนนี้จะเป็นข้อได้เปรียบในอนาคต สำหรับนักพัฒนาไทยที่ต้องการติดตามเทคโนโลยีใหม่ๆ การอ่านแหล่งข้อมูลเทคโนโลยี | SiamCafe Book | iCafe Cloudเป็นประจำจะช่วยให้ไม่พลาดเทรนด์สำคัญ

สรุป

Edge Computing ไม่ใช่เทรนด์ชั่วคราว แต่เป็นวิวัฒนาการของ web infrastructure ที่จะเปลี่ยนวิธีที่เราสร้างและ deploy application ไปตลอดกาล Cloudflare Workers เป็น platform ที่ครบเครื่องที่สุดในปี 2026 ด้วย ecosystem ที่ประกอบด้วย KV, Durable Objects, D1, R2, Queues และ AI Workers ทำให้สร้าง full application ที่ edge ได้โดยไม่ต้องมี traditional server เลย

ข้อแนะนำสำหรับผู้เริ่มต้น ให้เริ่มจากการสร้าง Worker ง่ายๆ ที่ respond กับ request แล้วค่อยเพิ่ม D1 สำหรับ database และ KV สำหรับ caching ใช้ Hono เป็น framework เพราะเขียนง่ายและ type-safe จากนั้นค่อยขยายไปใช้ R2 สำหรับ file storage และ Durable Objects สำหรับ real-time features

สิ่งสำคัญคือต้องเข้าใจข้อจำกัดของ edge computing เช่น CPU time limit, memory limit และ runtime compatibility จะได้เลือกใช้ได้ถูกต้องกับ use case ของคุณ Edge computing เหมาะมากสำหรับ latency-sensitive applications แต่สำหรับ heavy computation ยังคงต้องใช้ traditional cloud server อยู่ ความเข้าใจทั้งสองโลกจะทำให้คุณเป็นนักพัฒนาที่ครบเครื่องมากขึ้น

XM Legend · เทรดเดอร์ & ผู้สอน Forex 13 ปี

ผู้ก่อตั้ง SiamCafe ตั้งแต่ปี 1997 · เทรดเดอร์สาย Forex มากกว่า 13 ปี ได้รับการยกย่องเป็น XM Legend · แบ่งปันความรู้ Forex, ไอที, AI และการเทรด จากประสบการณ์จริงในตลาดจริง