APIs, Cloud-native & Integration
APIs, Microservices & Legacy Integration
API-first design ผ่าน API gateway, microservices vs modular monolith, synchronous vs event-driven และการ integrate กับ core banking ด้วย anti-corruption layer, transactional outbox, saga, strangler fig และ idempotency key
ระบบ AI ในองค์กรไม่ได้อยู่ลำพัง AI assistant ที่ดีต้องอ่าน data จาก core banking, create ticket ใน CRM และ publish events ให้ระบบอื่น ส่วนที่ยากที่สุดของหลาย projects จึงไม่ใช่ model แต่เป็น integration ซึ่งเป็นจุดที่ประสบการณ์ SA เรื่อง API และ interface spec มีค่ามาก
API-first & API gateway
API-first คือ design และตกลง API contract (เช่น OpenAPI spec) ก่อนเขียนโค้ด ทีม consumer และ provider จึงทำงานคู่ขนานได้ และ contract เป็น document ที่ test ได้
API gateway เป็น single entry point ที่ทุก request ต้องผ่าน รวม cross-cutting concerns ที่ทุก API ต้องทำไว้ที่เดียว
- Authentication และ authorization (เช่น validate OAuth token)
- Rate limiting และ quota ต่อ app
- Routing และ versioning (
/v1กับ/v2อยู่คู่กันได้ระหว่าง migration) - Centralized logging และ metrics
บน Azure คือ Azure API Management (APIM) ซึ่งทำหน้าที่ AI gateway หน้า Azure OpenAI ได้ด้วย เช่น token limit ต่อ app, load balancing ไปหลาย deployments และ log token usage (บทที่ 7 (Cloud-native on Azure))
Microservices vs modular monolith
| Modular monolith | Microservices | |
|---|---|---|
| Deployment | หน่วยเดียว | deploy แยกอิสระทีละ service |
| Pros | simple, debug ง่าย, transaction ใน database เดียว | ทีมอิสระ, scale แยกส่วน, เลือก tech stack ต่างกันได้ |
| Cons | ทีมใหญ่ชนกันง่าย, scale ทั้งก้อน | network failures, distributed data, ต้องมี observability และ platform ที่แข็งแรง |
| เหมาะกับ | ทีมเดียวหรือไม่กี่ทีม, domain ยังเปลี่ยนบ่อย | หลายทีม, domain boundaries ชัด, ต้อง scale หรือ release ต่างจังหวะกัน |
Synchronous vs event-driven
- Synchronous (REST, gRPC) caller รอ response เหมาะเมื่อต้องการผลทันที เช่น ดูยอดเงิน แต่ถ้า downstream service ล่ม caller ก็ล้มตาม
- Asynchronous (events, queues) producer publish ว่า “เกิดอะไรขึ้น” แล้วทำงานต่อ consumers มารับเอง เหมาะกับงานที่ไม่ต้องรอ เช่น “chat จบแล้ว ให้ summarize และบันทึกลง CRM” ระบบ loosely coupled และทนต่อ downstream ที่ล่มชั่วคราว
บน Azure เช่น Service Bus (queues และ topics สำหรับ business messages), Event Grid (event distribution) และ Event Hubs (high-volume streaming)
Patterns for distributed data
Transactional outbox ปัญหาคลาสสิก: write ลง database สำเร็จ แต่ publish event ไม่สำเร็จ (หรือกลับกัน) วิธีแก้คือเขียน event ลง outbox table ใน transaction เดียวกัน กับ business data แล้วมี relay process อ่าน outbox และ publish ภายหลัง
Saga งานที่ข้ามหลาย services ทำ transaction เดียวไม่ได้ จึงแบ่งเป็น steps แต่ละ step มี compensating action ถ้า step หลัง fail เช่น reserve วงเงินแล้วแต่ issue บัตรไม่สำเร็จ ให้ release วงเงินคืน
Idempotency key network อาจทำให้ request เดียวถูกส่งซ้ำ caller จึงแนบ unique key ไปกับ request, provider จำ key ไว้ ถ้าได้ key เดิมอีกครั้งให้ส่ง response เดิมกลับโดยไม่ทำซ้ำ สำคัญมากเมื่อ agent เรียก write tools เพราะ agent อาจ retry เอง
POST /v1/tickets
Idempotency-Key: 6f1c0e9a-2b7d-4c55-9a51-0d8f3e2b7a14
{ "customerRef": "from-session", "category": "card-dispute", "summary": "..." }
Integrating with legacy systems
Anti-corruption layer (ACL) translation layer ที่กั้นระหว่างระบบใหม่กับ core banking ระบบใหม่เห็นแค่ data model ที่ clean เช่น CardStatus { active, blocked, expired } ส่วน ACL แปลง status codes แบบเก่า, fixed-length fields หรือ message format ของ legacy system ถ้าวันหนึ่ง core เปลี่ยน แก้ที่ ACL ที่เดียว
Strangler fig migrate legacy system ทีละส่วน วาง gateway ไว้หน้า legacy system แล้วค่อย ๆ เปลี่ยน route ทีละ function ไปยัง service ใหม่ จนระบบเดิมไม่เหลืองาน ลด risk เมื่อเทียบกับ big-bang migration