01Architecture สำหรับ Go Backend

รูปแบบเหมาะกับTrade-off
Simple / Package-oriented

Service เล็ก CRUD, ทีมเล็ก และ domain ตรงไปตรงมา

เริ่มเร็ว แต่ต้องแยก package ตาม capability เมื่อระบบโต

Layered

ระบบธุรกิจทั่วไป แยก handler, service และ repository

เข้าใจง่าย แต่อาจกลายเป็นชั้นบาง ๆ และ dependency ไหลผิดทิศ

Clean / Hexagonal

Domain สำคัญและต้องสลับ database, queue หรือ transport

ทดสอบง่าย แต่ interface/adapter มากเกินจำเป็นได้

DDD

กฎธุรกิจซับซ้อน มี bounded context และภาษาธุรกิจเฉพาะ

ต้องลงทุนกับ domain model และความเข้าใจร่วมกับผู้เชี่ยวชาญ

Microservices

ต้อง deploy/scale แยก และมี ownership ชัดระหว่างทีม

เพิ่ม network failure, observability และ consistency cost

Go ไม่บังคับ architecture ระดับ application เอกสารทางการเน้น package, module, internal และ cmd สำหรับ server มากกว่า framework structure ดังนั้นค่าเริ่มต้นที่ใช้งานได้ดีในปัจจุบัน คือ modular monolith แบบ package/feature-oriented แล้วใช้แนวคิด Layered หรือ Ports & Adapters เฉพาะ boundary ที่มีแนวโน้มเปลี่ยน

อ่านเอกสารทางการ: Go — Organizing a Module ↗

02Architecture สำหรับ Node.js Backend

รูปแบบเหมาะกับTrade-off
MVC

เว็บหรือ API ขนาดเล็กที่ผูกกับ request/response ชัดเจน

คุ้นเคยและเร็ว แต่ business logic มักไหลเข้า controller/model

Layered

REST API ทั่วไปที่ต้องแยก controller, service และ data access

onboard ง่าย แต่อาจ coupling ตาม technical layer ทั้งระบบ

Feature Modules / NestJS

ทีมกลาง–ใหญ่ ต้องการ DI, convention และขอบเขต feature ชัด

โครงสร้างสม่ำเสมอ แต่มี framework abstraction และ decorator

Clean / Hexagonal

business rule สำคัญและมีหลาย delivery/infrastructure adapter

ควบคุม dependency ได้ดี แต่ต้องรักษา mapping และ boundary

Event-driven / Microservices

workflow async, scale/deploy แยก หรือทีมมี service ownership

ต้องรับมือ retry, duplicate, ordering, tracing และ partial failure

สำหรับ Node.js ไม่มีแบบเดียวที่นิยมที่สุดในทุกบริบท แต่ feature-based modular monolith เป็นค่าเริ่มต้นที่ปลอดภัยสำหรับระบบธุรกิจส่วนใหญ่: Express/Fastify อาจใช้ router–service–repository แบบเบา ส่วน NestJS มีโมดูลเป็นหน่วย encapsulation โดยตรง ควรแยก microservice เมื่อมีเหตุผลด้านทีม การ deploy หรือ scaling จริง ไม่ใช่เพราะขนาด code เพียงอย่างเดียว

อ่านเอกสารทางการ: NestJS Modules ↗
อ่านเอกสารทางการ: NestJS Microservices ↗

03เกณฑ์เลือกก่อนตั้งชื่อ Pattern

  1. 1
    Domain complexity

    CRUD เป็นหลัก หรือมีกฎ/invariant ที่ต้องรักษาหลายขั้นตอน?

  2. 2
    Change boundaries

    ส่วนใดเปลี่ยนพร้อมกัน และส่วนใดต้อง deploy หรือ scale แยก?

  3. 3
    Team topology

    ใครเป็นเจ้าของ capability และทีมสื่อสารข้าม boundary แค่ไหน?

  4. 4
    Operational maturity

    รองรับ tracing, retry, deployment และ incident หลาย service ไหวหรือไม่?

  5. 5
    Evolution cost

    เริ่มแบบง่าย แล้วมี seam ที่แยกภายหลังได้โดยไม่ rewrite หรือไม่?

04โจทย์และแนวคำตอบ

โจทย์ 1

Go มี Backend Architecture แบบใดบ้าง แต่ละแบบเหมาะกับงานใด และปัจจุบันนิยมแบบไหน?

แนวคำตอบ

มีตั้งแต่ package-oriented, Layered, Clean/Hexagonal, DDD ไปจนถึง Microservices ครับ ผมเลือกตามความซับซ้อนและ boundary: CRUD เล็กเริ่มจาก package เรียบง่าย, ระบบธุรกิจทั่วไปใช้ feature-oriented layered, domain ซับซ้อนจึงเพิ่ม DDD และ ports, ส่วน microservices ใช้เมื่อจำเป็นต้อง deploy/scale/own แยกจริง

ถ้าถามค่าเริ่มต้นที่พบได้ทั่วไป ผมเลือก modular monolith แบ่ง package ตาม business capability และรักษา dependency เข้าหา domain มากกว่านำ Clean Architecture เต็มรูปแบบมาใช้ทุกโปรเจกต์ เพราะขยายต่อได้โดยไม่จ่าย distributed systems cost ก่อนเวลา

โจทย์ 2

Node.js มี Backend Architecture แบบใดบ้าง มีเกณฑ์เลือกอย่างไร และแบบใดนิยมมากที่สุด?

แนวคำตอบ

ตัวเลือกหลักคือ MVC, Layered, Feature Modules แบบ NestJS, Clean/Hexagonal และ Event-driven/Microservices ครับ ระบบเล็กใช้ Express/Fastify แบบ router–service–repository ได้ ส่วนทีมใหญ่ที่ ต้องการ convention และ DI มักเหมาะกับ NestJS feature modules

ผมไม่เรียกแบบใดชนะทุกกรณี แต่ modular monolith แบ่งตาม feature เป็น baseline ที่ใช้ได้กว้างที่สุด จากนั้นพิจารณา domain complexity, team ownership, throughput, failure isolation และ operational maturity ก่อนแยก microservice