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)
“ใช้ 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 queries | scale writes ข้ามเครื่องยาก (ต้อง shard) |
| Key-value | Redis, DynamoDB | lookup ด้วย key, latency ต่ำมาก | query อื่นนอกจาก key ทำได้จำกัด |
| Document | MongoDB, Firestore | object ที่ schema หลากหลาย อ่านทั้งก้อน | joins และ transactions ข้าม documents จำกัดกว่า |
| Wide-column | Cassandra, Bigtable | writes ปริมาณมหาศาล, time-series | ต้อง design ตาม query ล่วงหน้า |
| Search | Elasticsearch, OpenSearch | full-text search, faceting | ไม่ใช่ source of truth |
| Graph | Neo4j | ความสัมพันธ์หลายชั้น เช่น fraud rings | niche, ทีมต้องมี 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 ล่าสุด |
| Hash | hash(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 ยังไม่ commit | Read Committed ขึ้นไป |
| Non-repeatable read | อ่านแถวเดิมสองครั้งได้ค่าต่างกัน | Repeatable Read / snapshot |
| Phantom | query เดิมสองครั้งได้จำนวนแถวต่างกัน | 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))
แบบทดสอบ
มีข้อมูล 3,200 ล้าน rows กระจายด้วย hash sharding อย่างสม่ำเสมอไป 64 shards แต่ละ shard มีประมาณกี่ล้าน rows