1. Redis Introduction
What is Redis?
Redis (Remote Dictionary Server) is an open-source, in-memory key-value store database. If traditional databases are like a library, then Redis is the sticky note on your desk — instantly accessible and blazingly fast.
Its core features:
- Blazing fast: Data is stored in memory, with read/write speeds reaching 100K+ QPS
- Rich data structures: Not just simple Key-Value — also supports List, Hash, Set, Sorted Set, and more
- Persistence: Data can be saved to disk and survives restarts
- Atomic operations: All operations are atomic, with built-in concurrency safety
- Multi-purpose: Caching, message queues, leaderboards, counters — it handles them all
Why Choose Redis?
Imagine you run a bubble tea shop:
- 🗄️ MySQL is like your warehouse — it can store a lot, but every time you need an ingredient, you have to go dig through the warehouse
- ⚡ Redis is like the shelf on your workbench — commonly used ingredients are within arm's reach, making service 10x faster
Redis doesn't replace MySQL — it's the "acceleration partner." Hot data goes in Redis, cold data goes in MySQL, each serving its purpose.
Typical Use Cases
- 🔔 Caching: The most classic use case, reducing database load
- 🏆 Leaderboards: Easily implement real-time rankings with Sorted Set
- 📊 Counters: Article view counts, likes, UV statistics
- 💬 Message queues: Implement async tasks with List or Stream
- 🔒 Distributed locks: SETNX for cross-service mutual exclusion
- ⏱️ Session caching: Store user sessions with expiration times
- 📍 Geospatial: Nearby people, nearby shops
- 🔄 Rate limiting: API frequency limits to prevent malicious requests