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 limitingINCR + EXPIRE
  • Leaderboard — Sorted Set
  • Pub/Sub messaging — real-time notifications

Redis vs Memcached

ด้านRedisMemcached
Data structuresหลายแบบแค่ string
PersistenceRDB + AOFไม่มี
ThreadingSingle-threadedMulti-threaded
EvictionLRU, LFU, TTLLRU
ReplicationMaster/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

ด้านJedisLettuce
โมเดลการทำงานBlocking / SynchronousNon-blocking / Asynchronous
Thread-safetyไม่ thread-safe — ต้องใช้ connection poolThread-safe — 1 connection share ได้หลาย thread
Connection1 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
  • Memcached — ทางเลือก pure cache, multi-threaded
  • Spring Boot — ใช้ Redis สำหรับ caching บ่อย
  • AWS Services Overview — AWS มี ElastiCache เป็น managed Redis
  • Connection Pooling — จัดการ connection ไป Redis
  • WAL — AOF ของ Redis คล้ายหลักการ WAL