Redis
In-memory data store — เร็วมากเพราะข้อมูลอยู่ใน RAM (~100k ops/sec ต่อ instance)
Architecture
- In-memory first — primary storage = RAM
- Single-threaded — handle commands ทีละตัว → ไม่ต้อง lock → simple, predictable
- ยกเว้น: I/O threading + persistence threads (ตั้งแต่ 6.0+)
Persistence Options
- RDB snapshot — periodic full snapshot (เร็ว แต่อาจหายระหว่าง snapshot)
- AOF (Append-Only File) — log ทุก write command → replay ตอน restart
- AOF + RDB — ปลอดภัยที่สุด (แนะนำสำหรับ production)
Data Types
- String — ใช้ทั่วไป, counter (
INCR) - List — queue/stack
- Set — unique members
- Hash — field-value pairs (เหมือน object)
- Sorted Set — leaderboard, ranking
- Stream (v5+) — คล้าย Kafka
- HyperLogLog — cardinality estimation
- Bitmap — bit operations
- Geospatial — location-based queries
Use Cases
- Cache — cache-aside, write-through pattern
- Session store — stateless app servers
- Rate limiting —
INCR+EXPIRE - Leaderboard — Sorted Set
- Pub/Sub messaging — real-time notifications
Redis vs Memcached
| ด้าน | Redis | Memcached |
|---|---|---|
| Data structures | หลายแบบ | แค่ string |
| Persistence | RDB + AOF | ไม่มี |
| Threading | Single-threaded | Multi-threaded |
| Eviction | LRU, LFU, TTL | LRU |
| Replication | Master/replica + cluster | ไม่มี |
ใช้ Redis เมื่อต้องการ data structures, persistence, pub/sub — ใช้ Memcached เมื่อต้องการ pure cache + multi-threaded throughput สูง
Client Libraries สำหรับ Java
- Jedis — Redis client แบบดั้งเดิม
- Lettuce — reactive/async client (นิยมใน Spring Boot ยุคใหม่)
Jedis vs Lettuce
| ด้าน | Jedis | Lettuce |
|---|---|---|
| โมเดลการทำงาน | Blocking / Synchronous | Non-blocking / Asynchronous |
| Thread-safety | ไม่ thread-safe — ต้องใช้ connection pool | Thread-safe — 1 connection share ได้หลาย thread |
| Connection | 1 thread = 1 connection | แชร์ connection ได้ (Netty-based) |
| Reactive | ไม่รองรับ | รองรับ (Project Reactor) |
| Cluster support | รองรับ แต่ manual มากกว่า | รองรับเต็มรูปแบบ auto-reconnect |
| ใช้ resource | กินเยอะกว่า (pool connections) | กินน้อยกว่า |
| เหมาะกับ | app แบบดั้งเดิม, traffic ไม่สูงมาก | app สมัยใหม่, high concurrency, reactive stack |
| Default ใน Spring Boot | ก่อน v2.0 | ตั้งแต่ v2.0 เป็นต้นมา |
Jedis (Synchronous) Lettuce (Asynchronous)
───────────────────── ─────────────────────
Thread 1 → Conn 1 → Redis Thread 1 ─┐
Thread 2 → Conn 2 → Redis Thread 2 ─┼→ Conn (shared) → Redis
Thread 3 → Conn 3 → Redis Thread 3 ─┘
(ต้องมี pool) (1 connection พอ)
สรุป: โปรเจกต์ใหม่ควรใช้ Lettuce เพราะ thread-safe, reactive, และเป็น default ของ Spring Boot ยุคใหม่
Key Points
- Single-threaded → ไม่ต้อง lock, ~100k ops/sec
- Persistence: RDB (snapshot) + AOF (append log) — ใช้คู่กันปลอดภัยสุด
- Data types หลากหลาย — ไม่ใช่แค่ key-value string เหมือน Memcached
- ใน Spring Boot ยุคใหม่ ใช้ Lettuce เป็น default client
Related
- Memcached — ทางเลือก pure cache, multi-threaded
- Spring Boot — ใช้ Redis สำหรับ caching บ่อย
- AWS Services Overview — AWS มี ElastiCache เป็น managed Redis
- Connection Pooling — จัดการ connection ไป Redis
- WAL — AOF ของ Redis คล้ายหลักการ WAL