AI Solution Architect: from System Analyst to AI Architect
บทที่ 6/12

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

⏱ ประมาณ 30 นาที

ระบบ AI ในองค์กรไม่ได้อยู่ลำพัง AI assistant ที่ดีต้องอ่าน data จาก core banking, create ticket ใน CRM และ publish events ให้ระบบอื่น ส่วนที่ยากที่สุดของหลาย projects จึงไม่ใช่ model แต่เป็น integration ซึ่งเป็นจุดที่ประสบการณ์ SA เรื่อง API และ interface spec มีค่ามาก

System integration: channels (mobile app, web, call center) ส่ง requests ผ่าน HTTPS เข้า API Gateway ที่ทำ authentication และ rate limit แล้ว route ไปยัง AI Orchestrator, Customer service และ Payment service โดย Customer service เชื่อม Core banking ซึ่งเป็น legacy system ผ่าน anti-corruption layer ที่ translate data models และ Payment service ส่ง events แบบ async ไปยัง event bus
ทุก channel เข้าทางเดียว services คุยกันผ่าน APIs และ events และ legacy system ถูกครอบด้วย anti-corruption layer

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 monolithMicroservices
Deploymentหน่วยเดียวdeploy แยกอิสระทีละ service
Prossimple, 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

แบบทดสอบ

Pattern ใดแก้ปัญหา “write ลง database สำเร็จแต่ publish event ไม่สำเร็จ”
Agent retry การ create ticket ทำให้เกิด ticket ซ้ำสองใบ ควรใช้กลไกใดป้องกัน
Layer ใดทำหน้าที่แปลง status codes และ data format ของ core banking ให้ระบบใหม่
Dev team มี 6 คน ระบบใหม่และ requirements ยังเปลี่ยนบ่อย ควรเริ่มด้วย architecture แบบใด
ต้องการ migrate legacy system ไป service ใหม่ทีละ function โดย users ไม่รู้สึก ควรใช้ pattern ใด