Lewati ke konten utama
menjadi.dev
System Design

Redis

In-memory data store yang super cepat — untuk caching, session, pub/sub, dan queue.

Redis (REmote DIctionary Server) adalah in-memory data store yang menyimpan data di RAM — membuatnya sangat cepat (microsecond latency). Redis bukan cuma cache — dia juga: Key-Value Store (strings, numbers, JSON), Data Structures (lists, sets, sorted sets, hashes), Pub/Sub (real-time messaging), Streams (log data structure — seperti Kafka ringan), dan Rate Limiting (counter dengan expiry). Redis persisten opsional (RDB snapshot, AOF log). Use case: Session store, Cache dengan TTL, Real-time leaderboard (sorted sets), Rate limiting counter, Job queue, dan Real-time analytics. Di production, Redis biasanya dijalankan dengan Redis Sentinel (high availability) atau Redis Cluster (sharding horizontal).

Contoh Kode

# Redis commands
SET user:123 "{\"name\": \"Budi\"}" EX 300  # Set dengan TTL 5 menit
GET user:123
LPUSH queue:emails "email-data"           # Push ke queue
BLPOP queue:emails 5                    # Pop dari queue (blocking)
PUBLISH channel:news "New article!"     # Pub/Sub
ZADD leaderboard 1500 "player1"         # Sorted set

Istilah Terkait