Role & Architecture Fundamentals
Architecture Fundamentals & Decisions
เขียน architecture ด้วย C4 model ชั่ง trade-off ของ quality attributes อย่าง scalability, security, resilience และ compliance บันทึก decision ด้วย ADR และสร้าง reference architecture ที่ reuse ได้
Architecture คือ ชุด decisions ที่เปลี่ยนทีหลังได้ยาก เช่น จะเก็บ customer data ไว้ที่ไหน ระบบคุยกันแบบ synchronous หรือผ่าน events และจะใช้ model จาก provider รายใด บทนี้ให้เครื่องมือสามอย่างที่ architect ใช้ทุกวัน ได้แก่ views สำหรับสื่อสาร design (C4) กรอบคิดสำหรับชั่ง trade-off (quality attributes) และวิธีบันทึก decision (ADR)
C4 model: zoom in one level at a time
C4 model ของ Simon Brown แบ่งการเล่า design เป็น 4 levels เหมือนแผนที่ที่ zoom เข้าไปเรื่อย ๆ
| Level | แสดงอะไร | Audience หลัก |
|---|---|---|
| 1. System Context | ระบบของเราหนึ่งกล่อง users และ external systems ที่เกี่ยวข้อง | executives, business |
| 2. Container | หน่วยที่ deploy แยกกัน เช่น app, API, database, index | architects, infra, security |
| 3. Component | components ภายใน container หนึ่ง | dev team |
| 4. Code | classes, functions (มักไม่ต้องวาด generate จากโค้ดได้) | developers |
Quality attributes & trade-offs
Requirements ที่ SA คุ้นเคยส่วนใหญ่เป็น functional (ระบบต้องทำอะไร) แต่สิ่งที่กำหนดรูปร่างของ architecture จริง ๆ คือ quality attributes หรือ non-functional requirements (NFR) ว่าระบบต้องทำได้ “ดีแค่ไหน”
| Quality attribute | Tactics | Trade-off |
|---|---|---|
| Scalability | autoscale, cache, queue | cost และ complexity เพิ่ม |
| Resilience | retry, circuit breaker, multi-region | cost ราว 2 เท่า, data sync |
| Security | private network, encryption, least privilege | latency และ ops work เพิ่ม |
| Compliance | data residency, audit log, retention | ตัวเลือก region และ model น้อยลง |
| Latency / cost | model ที่เล็กลง, response cache | quality อาจลดลง |
ไม่มี option ที่ดีทุกด้าน ทุก tactic มี trade-off
กฎสำคัญคือ quality attribute ต้อง measurable ไม่ใช่แค่คำคุณศัพท์
| Vague | Measurable |
|---|---|
| ระบบต้องเร็ว | p95 time-to-first-token ≤ 2 s |
| ระบบต้องรองรับ users มาก | รองรับ 200 requests/s ใน peak hour |
| ระบบต้องเสถียร | availability 99.9% ต่อเดือน |
| ต้อง secure | customer data ไม่ออกนอก region ที่กำหนด และทุก access มี audit log |
เทคนิคที่ใช้ได้ดีคือ quality attribute scenario เขียนเป็นประโยค “เมื่อ [event] เกิดขึ้นกับ [ส่วนใดของระบบ] ระบบต้อง [response อย่างไร] วัดด้วย [metric]” เช่น “เมื่อ LLM provider ล่ม ระบบต้องแจ้งว่ากำลัง transfer ให้ agent ภายใน 5 วินาที และไม่มี request ใดค้าง”
ADR: record your decisions
อีกหนึ่งปี ไม่มีใครจำได้ว่าทำไมทีมเลือก vector database ตัวนี้ Architecture Decision Record (ADR) คือ document สั้น ๆ หนึ่งเรื่องต่อหนึ่ง decision format ที่นิยม (ของ Michael Nygard) มี 5 หัวข้อ
ADR-007: ใช้ hybrid search แทน vector search อย่างเดียว
Status: Accepted (2026-03-14)
Context: คำถามลูกค้ามีเลขบัญชี รหัสผลิตภัณฑ์ และ keyword เฉพาะ
ทดสอบแล้ว vector search อย่างเดียวพลาด exact match 18%
Decision: ใช้ hybrid search (keyword + vector) และ rerank top 20
Consequences: + recall ดีขึ้นใน test set, ใช้ managed service เดิมได้
− index ใหญ่ขึ้น cost เพิ่ม, ต้อง tune analyzer ภาษาไทย
ADR ที่ดี บอก trade-off อย่างตรงไปตรงมา และเมื่อ decision เปลี่ยน ให้เขียน ADR ใหม่ที่ “supersedes” ฉบับเดิม ไม่ลบของเก่า เพื่อให้เห็น history
Reference architecture & reusable components
เมื่อหลายทีมสร้าง chatbot ของตัวเอง แต่ละทีมจะแก้ปัญหาเดิมซ้ำ (authentication, content filtering, logging) และแก้ไม่เหมือนกัน architect จึงสร้าง
- Reference architecture standard design ของ use case ที่พบบ่อย พร้อม ADRs ที่ตัดสินใจไว้แล้ว ทีมใหม่เริ่มจาก design นี้แทนที่จะเริ่มจากศูนย์
- Reusable components / platform services เช่น AI gateway กลางที่ทุกทีมเรียกใช้ ซึ่งคุม quota, logging และ content filtering ไว้ที่จุดเดียว
ผลคือ dev teams ทำงานเร็วขึ้น และ governance ตรวจได้ง่ายขึ้น เพราะทุกระบบผ่าน standard เดียวกัน