Redis Tutorial Home

Redis is an open-source in-memory key-value store that can be used as a database, cache, and message broker. It supports multiple data structures, delivers extremely high performance, and is an essential component in modern web applications.

What is Redis?

Redis stands for REmote DIctionary Server, developed by Salvatore Sanfilippo in 2009. It stores data in memory, providing blazing-fast read and write speeds capable of handling over 100,000 operations per second.

Core Features of Redis

Feature Description
In-memory storage Data is stored in memory for extremely fast read and write speeds
Persistence Supports RDB snapshots and AOF logging
Rich data types Supports strings, hashes, lists, sets, sorted sets, and more
Atomic operations All operations are atomic, with transaction support
Pub/Sub Built-in publish/subscribe messaging system
High performance Single-instance QPS can exceed 100k, with master-replica replication and cluster support
💡 Why is it called Redis? Redis = REmote DIctionary Server, because it is essentially a remotely accessible dictionary (key-value) service.

What Can Redis Do?

1. Caching System

This is the most common use case. Store database query results, API responses, page fragments, etc., in Redis to dramatically boost application performance.

TEXT
User Request → Check Redis Cache → Hit → Return
                               ↓ Miss
                              Query DB → Store in Redis → Return

2. Session Storage

Store user login state, shopping carts, temporary data, etc., in Redis to support session sharing across distributed environments.

3. Message Queue

Use Redis Lists and Pub/Sub to implement lightweight message queues.

4. Leaderboards

Leverage the sorting capabilities of Sorted Sets for gaming leaderboards, trending lists, and more.

5. Counters

Use the INCR command on strings for article views, likes, visit statistics, and more.

6. Social Features

Use Set intersection and union operations to implement mutual friends, people you may know, and other social features.

Redis vs Other Databases

Redis vs Memcached

Aspect Redis Memcached
Data types Rich (5 basic types) Strings only
Persistence Supported Not supported
Clustering Native support Requires client-side sharding
Memory management More flexible Simpler
Performance Extremely high Extremely high
⚠️ How to choose? If you only need simple key-value caching, Memcached is sufficient. If you need complex data structures, persistence, or clustering, choose Redis.

Redis vs Relational Databases (MySQL)

Aspect Redis MySQL
Storage Memory Disk
Data model Key-value Relational tables
Query capability Simple Powerful (SQL)
Transaction support Simple transactions Full ACID
Performance Extremely high High
Persistence Optional Default
💡 Best practice: Use Redis + MySQL together — Redis for caching and hot data, MySQL for durable storage.

Redis vs MongoDB

Aspect Redis MongoDB
Data model Key-value Document (JSON)
Query language Command-based SQL-like
Memory usage All in-memory Memory + disk
Use cases Cache, sessions, queues Document storage, content management

Redis Use Cases

Used by Major Internet Companies

Typical Application Scenarios

Scenario Data Type Used Description
Caching String Store cached data with expiration
Session storage String Store user session info
Shopping cart Hash Store product IDs and quantities
Message queue List LPUSH/RPOP for queuing
Leaderboard Sorted Set Auto-sorting, real-time updates
Social relationships Set Friend lists, intersection calculations
Counter String INCR atomic increment
Rate limiting String + TTL Track access counts

Redis Version History

Version Release Date Key Features
1.0 2009 Initial release
2.0 2010 Virtual memory, Pub/Sub
2.6 2012 Lua scripting, improved expiration precision
2.8 2013 Partial sync, config rewrite
3.0 2015 Official cluster support
3.2 2016 GEO, bitmap optimizations
4.0 2017 Module system, mixed persistence
5.0 2018 Streams data type
6.0 2020 ACL access control, multi-threaded I/O
7.0 2022 Functions, multi-part AOF
💡 Recommended version: Use Redis 6.0 or later for better performance and more features.

Who Is This Tutorial For?

Prerequisites

Tutorial Contents

This tutorial is divided into 4 parts:

  1. Redis Basics: Installation, configuration, basic operations
  2. Data Types in Depth: Strings, hashes, lists, sets, sorted sets
  3. Advanced Features: Transactions, persistence, security, performance optimization
  4. Practical Applications: Using Redis with Python, Java, Node.js

❓ FAQ

Q Redis stores data in memory — is data lost on power failure?
A Redis supports persistence, saving in-memory data to disk. You can use RDB snapshots, AOF logging, or both.
Q How much data can Redis store?
A It depends on available memory. A single Redis instance can store several GB of data, and clustering can scale to TB levels.
Q Is Redis single-threaded?
A Redis core command execution is single-threaded, but version 6.0 introduced multi-threaded I/O for network reads and writes. The single-threaded design avoids lock contention, actually improving performance.
Q What's the difference between Redis and a traditional database?
A Redis is a NoSQL database that stores data in memory, ideal for caching and hot data. Traditional databases store data on disk for durable storage. They are typically used together.

📖 Summary

📝 Exercises

  1. Think: In your projects, which scenarios are suitable for Redis?
  2. Research: Look at websites or apps you use regularly — what might they be using Redis for?
  3. Compare: What are the pros and cons of Redis vs Memcached? How would you choose?

Next Lesson

In the next lesson, we will learn Redis Installation, covering installation on Linux, Windows, and Mac.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏