Under Construction
Redis10 min131 views

Redis

Minh Khoa

Minh Khoa

Author

1️⃣ What is Redis?

Redis = super-fast temporary memory, used specifically for short-term data storage.

It is like RAM for the server .

Instead of every time the client asks the server → the server has to “dig” into the database,

Redis keeps the result ready above RAM → next time it responds extremely fast.

For example:

  • A user has just viewed product A → cache product A in Redis for 1 hour.
  • The next time someone views it → get it directly from Redis, no DB query needed.

2️⃣ How does Redis store data?

Redis stores data in the form of key – value:

Key Value user:1 {"name":"Na","age":22} token:xyz "abc123" leaderboard player1:100, player2:200

It is like a multi-compartment locker, each compartment is one key, and inside it contains a value.


3️⃣ Why is Redis fast?

Because it:

  • Stores directly on RAM (not on disk).
  • Runs a single thread, no complicated locking needed.
  • All operations such as GET, SET, INCR, LPUSH… only take a few microseconds.

4️⃣ What is Redis used for?

💨 1. Cache (speeding up)

Store temporary data → no need to query the database repeatedly.

# Lần đầu
GET user:123   →  ❌ Không có
→ Query DB
→ SET user:123 '{"name":"Na"}'

# Lần sau
GET user:123   →  ✅ Có cache, trả liền

🔐 2. Session (store login)

Instead of storing the session in a file, Redis keeps the user login in RAM:

SET session:abc123 { "userId": 5, "role": "admin" } EX 3600

→ When the user sends a request, the backend only needs to read session:abc123.


🧾 3. Queue (job queue)

For example, a user uploads 1 image → the backend pushes a job into Redis:

LPUSH job_queue {"file":"avatar.jpg","user":123}

The Worker in the background will RPOP take the job out for processing.


📢 4. Pub/Sub (realtime)

Redis can send realtime messages between multiple servers:

PUBLISH chat:room1 "Na: Hello!"
SUBSCRIBE chat:room1

→ Build realtime chat systems, notifications, or game events.


🏆 5. Leaderboard / Counter

Use Sorted Set to store scores:

ZADD leaderboard 100 player1
ZADD leaderboard 250 player2
ZREVRANGE leaderboard 0 1 WITHSCORES

→ Return the top 1 player as fast as lightning.


5️⃣ Can Redis store data for a long time?

Yes, Redis still writes data to disk in 2 ways:

  • RDB: snapshot (captures the entire RAM every X minutes).
  • AOF: log every command SET, INCR, DEL

But Redis does not replace the database, it is temporary cache.

If the power goes out → some data may be lost.


6️⃣ Does Redis expire automatically

Redis has TTL – Time To Live:

SET user:1 "Na" EX 600

→ Automatically deletes after 10 minutes.

Very useful for cache or login tokens.


7️⃣ Redis has cluster (multiple servers)

When the app gets big, Redis can run multiple nodes:

  • 1 master node (master)

  • some replica nodes (replica)

    → Automatically replicate data and scale across multiple machines.


8️⃣ Redis is not only for web

Redis is also used for:

  • Game server (store leaderboards, player sessions).
  • IoT (store device state).
  • Machine learning (cache model or inference results).
  • Analytics (count views, logins…).