Redis practical tutorial
Are you still complaining about slow web application queries, occasional lost sessions, or even e-commerce oversales and oversubscriptions? Today I will take you from "zero installation" to "implementation of Python high-frequency scenarios", using a lightweight and efficient solution to solve these problems!
What is Redis?
Redis (Remote Dictionary Server) is an open source memory-first data structure storage system that can be used as a cache, semi-persistent database, and lightweight message middleware at the same time. Its biggest feature is that it supports many more structured operations than ordinary key values, and can help us quickly implement complex logic using Redis native commands without having to write a bunch of code at the application layer.
Let’s briefly list a few commonly used features at the production level:
- Millisecond response: full memory storage, single node read and write up to 100,000/second+
- Data half-implementation: RDB (scheduled snapshot) + AOF (incremental log) dual persistence, taking into account both speed and security
- Full coverage of master-slave/sentinel/cluster: from read-write separation to high availability, to distributed horizontal expansion
- Lua script atomic operation: an artifact to solve oversold and counting consistency
1. Quickly install Redis in 3 steps
1.1 Recommended solution: Docker one-click startup
Whether you are on Windows, Mac or Linux, Docker can avoid environment dependency problems:
1.2 Alternate: WSL/Ubuntu native
If you are not used to Docker, install it with WSL 2 Ubuntu/Debian:
2. A quick overview of high-frequency Redis data types (with code)
The core of Redis is not "storing key values", but using corresponding data structures to solve corresponding problems! Only the types used in 80% of production scenarios are listed below:
2.1 String: Tiger Balm
It can be used to store single values, counters, and temporary tokens:
2.2 Hash: Storing objects saves memory
Replacing hashed string keys like "user:1000:name=xx" saves about 30-50% of memory:
2.3 List: lightweight queue/stack
Use LPUSH/RPUSH+BRPOP/BLPOP to implement a blocking message queue without introducing heavyweight middleware such as RabbitMQ/Kafka:
2.4 Sorted Set: Automatic Ranking
Automatically sort by score (such as number of likes, score, timestamp) without application layer operations:
3. Seamless integration of Python and Redis
For production environments, it is recommended to use officially maintained ones.redisLibrary, supports synchronous/async:
3.1 Installation and basic connection
3.2 High-frequency Python scenario 1: interface caching
By writing a decorator, you can add Redis cache to any synchronous/asynchronous interface to avoid frequent database checks:
3.3 High-frequency Python scenario 2: oversold atomic lock
Use Lua script + SET NX EX to solve the overselling problem of e-commerce flash sales and reservation registration to ensure atomicity:
4. Redis tips to avoid pitfalls
4.1 Key naming convention
Unified team standards to avoid confusion, recommended业务:模块:资源IDThe colon layering:
4.2 Slow query monitoring
For production environmentSLOWLOG GET 10Check the last 10 commands that exceed the specified time. By default, the records exceed 10000 microseconds (10ms):
Summarize
Redis is a lightweight but powerful tool. As long as you choose the right data structure, you can solve the most difficult performance problems with the least amount of code. Today we learned:
- Docker starts the development environment with one click
- Usage of 4 high-frequency data structures
- The complete code of Python plus interface cache and oversold atomic lock
- Tips for avoiding pitfalls in production
If you want to learn more, you can read the "Sentinel Mode/Cluster Mode" and "Memory Optimization" content later!

