System Design for Solution Architects: from Whiteboard to Production
บทที่ 5/12

Data & Caching

Data Stores, Indexing & Transactions

เลือก SQL หรือ NoSQL อย่างมีเหตุผล, B-tree vs LSM index, composite index, sharding strategies, transactions และ isolation anomalies, object storage และ change data capture (CDC)

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

“ใช้ MongoDB หรือ PostgreSQL ดี?” เป็นคำถามที่ผิดถ้ายังไม่รู้ access patterns บทนี้ให้ framework เลือก data store และอธิบายส่วนที่ระบบจริงพังบ่อยที่สุด: indexes, shard keys และ isolation levels

SQL vs NoSQL families

Familyตัวอย่างเหมาะกับข้อจำกัด
Relational (SQL)PostgreSQL, MySQLข้อมูลมี relations, transactions หลาย tables, ad-hoc queriesscale writes ข้ามเครื่องยาก (ต้อง shard)
Key-valueRedis, DynamoDBlookup ด้วย key, latency ต่ำมากquery อื่นนอกจาก key ทำได้จำกัด
DocumentMongoDB, Firestoreobject ที่ schema หลากหลาย อ่านทั้งก้อนjoins และ transactions ข้าม documents จำกัดกว่า
Wide-columnCassandra, Bigtablewrites ปริมาณมหาศาล, time-seriesต้อง design ตาม query ล่วงหน้า
SearchElasticsearch, OpenSearchfull-text search, facetingไม่ใช่ source of truth
GraphNeo4jความสัมพันธ์หลายชั้น เช่น fraud ringsniche, ทีมต้องมี skill

Default ที่ปลอดภัย: เริ่มจาก relational database (PostgreSQL ทำได้ทั้ง JSON, full-text พื้นฐาน และ scale ได้ไกลกว่าที่คนส่วนใหญ่คิด) แล้วเพิ่ม store อื่นเมื่อมี access pattern ที่มันทำไม่ได้ดี

Indexing

  • B-tree (relational ส่วนใหญ่): อ่านและ range query เร็ว แต่ทุก write ต้อง update index ด้วย
  • LSM-tree (Cassandra, RocksDB): writes เร็วมากเพราะเขียนต่อท้ายแล้ว compact ทีหลัง แต่ reads อาจต้องดูหลายไฟล์
  • Composite index (customer_id, created_at) ใช้ได้กับ query ที่เริ่มจาก คอลัมน์ซ้ายสุด (leftmost prefix): ใช้กับ WHERE customer_id = ? หรือ WHERE customer_id = ? AND created_at > ? ได้ แต่ ใช้กับ WHERE created_at > ? อย่างเดียวได้ไม่ดี (บาง DB เช่น PostgreSQL 18 และ MySQL 8 ทำ skip scan ได้ แต่ช้าเมื่อ customer_id มี distinct values มาก ควรมี index ที่ขึ้นต้นด้วย created_at แยก)
  • Covering index: index มีทุกคอลัมน์ที่ query ต้องการ จึงไม่ต้องไปอ่าน table
  • ทุก index มี cost ตอนเขียน อย่าใส่ index ทุกคอลัมน์ “เผื่อไว้”

Sharding

เมื่อ data หรือ writes เกินเครื่องเดียว ให้แบ่ง data ไปหลาย shards สิ่งที่ต้องตัดสินใจคือ shard key

Strategyวิธีระวัง
Rangeแบ่งตามช่วงค่า เช่น วันที่hot shard: writes ใหม่ทั้งหมดลง shard ล่าสุด
Hashhash(key) กระจายสม่ำเสมอrange query ต้องถามทุก shard
Directoryมี lookup table บอกว่า key ไหนอยู่ shard ไหนlookup table ต้อง available เสมอ

Shard key ที่ดี: กระจาย load สม่ำเสมอ และ queries ส่วนใหญ่จบใน shard เดียว เช่น chat ใช้ conversation_id (ข้อความของห้องเดียวกันอยู่ด้วยกัน) ไม่ใช่ timestamp ระวัง cross-shard transactions และ celebrity keys (merchant ใหญ่มากที่ทำให้ shard เดียวร้อน)

Transactions and isolation

ACID transaction ทำให้หลาย statements สำเร็จหรือล้มเหลวพร้อมกัน แต่ isolation level กำหนดว่า transactions ที่วิ่งพร้อมกันเห็นกันแค่ไหน

Anomalyเกิดอะไรป้องกันโดย
Dirty readอ่านข้อมูลที่อีก transaction ยังไม่ commitRead Committed ขึ้นไป
Non-repeatable readอ่านแถวเดิมสองครั้งได้ค่าต่างกันRepeatable Read / snapshot
Phantomquery เดิมสองครั้งได้จำนวนแถวต่างกันSerializable (หรือ snapshot ในบาง DB)
Lost updateสอง transactions อ่านค่าเดียวกันแล้วเขียนทับกันatomic update, SELECT … FOR UPDATE หรือ version check
Write skewสอง transactions ตรวจเงื่อนไขเดียวกันแล้วต่างคนต่างเขียนคนละแถว ผลรวมผิดกฎSerializable หรือ lock ที่ครอบเงื่อนไข

Defaults ต่างกันตาม product: PostgreSQL ใช้ Read Committed, MySQL InnoDB ใช้ Repeatable Read ต้องรู้ default ของ DB ที่ใช้เสมอ

Lost update ที่พบบ่อยที่สุด: ถอนเงินแบบ “อ่านยอด → คำนวณใน app → เขียนกลับ” แก้ด้วย atomic update ที่ตรวจเงื่อนไขในคำสั่งเดียว

UPDATE accounts
SET balance = balance - 100, version = version + 1
WHERE id = 42 AND balance >= 100;
-- 0 rows updated = ยอดไม่พอ ให้ตอบ error

Object storage and CDC

  • Object storage (S3-compatible): เก็บ files, images, backups, data lake ราคาถูก ทนทานสูง ให้ client upload/download ตรงด้วย pre-signed URLs ไม่ต้องผ่าน app servers และตั้ง lifecycle rules ย้ายข้อมูลเก่าไป storage class ถูกลง
  • Change data capture (CDC): อ่าน transaction log ของ database (เช่น WAL ของ PostgreSQL, binlog ของ MySQL) ด้วยเครื่องมืออย่าง Debezium แล้วส่ง changes เป็น events ไป Kafka ใช้ sync search index, invalidate cache, ส่งต่อ data warehouse โดยไม่ต้องให้ app เขียนสองที่ (dual write ซึ่ง inconsistent ได้ ดูบทที่ 7 (Messaging & Event-driven Design))

แบบทดสอบ

มี index (customer_id, created_at) query WHERE created_at หลังวันที่ 2026-01-01 (ไม่มี customer_id) จะใช้ index นี้ได้ดีหรือไม่
Transactions สองตัวอ่านยอดเงิน 1,000 พร้อมกัน ต่างคนถอน 100 แล้วเขียนกลับเป็น 900 ทั้งคู่ เกิด anomaly อะไร
ระบบ chat ควรใช้ shard key อะไร
Isolation level ใดป้องกัน write skew ได้
ต้องการ sync search index ให้ตรงกับ database แบบเกือบ real-time โดยไม่ dual write ควรใช้อะไร

มีข้อมูล 3,200 ล้าน rows กระจายด้วย hash sharding อย่างสม่ำเสมอไป 64 shards แต่ละ shard มีประมาณกี่ล้าน rows