Reids practice foundation


1、 Reids practice foundation

1. Cache system and redis

Cache system:

① Request = (2) business calculation = (3) read / write data (CACHE < = > dB) (4) template rendering = (5) output
Redis – relieve or even take over database pressure

  • If you can check the database, you can’t check the database
  • Store common data
  • View database processshow processlist
  • Caching common data with complex query conditions into redis can significantly reduce the reading and writing of the database, thus reducing the pressure on the database
  • Operation is atomic
  • Atomic operation: ensure the reliability of data, rollback of operation errors, and support the implementation of competitive business

Advantages of redis:

  • Superior performance of resident memory reading and writing, far better than hard disk IO
  • Support multiple data structures to meet multiple business needs
  • It can automatically save data to the hard disk service and restore the service to be stable and durable when the service is restarted
Daemonize yes background start
Port 6379 port number
pidfile /var/

Redis cli 6379 shutdown redis client exit

Client connection redis
Install the corresponding extension
Operate key value data
Set get del exists setnx
Key names are generally designed from large to small modules, separated by colons

Hash type data
hset hget hdel hexists hsetnx
To delete a hash type key, you need to delete each field one by one
Caching is independent of the database, not all data accept latency, always pay attention to data consistency

Data of type list
lset llen lrange ltrim lpop rpush lpop rpop
Example: recording and reading of operation log

Data of type set
sadd smembers scard spop sdiff

2. Redis cache case

Cache weather
For general caching, when setting key weaher: City: Date query, first check whether the key exists, and then return directly. If there is no first request interface, and then cache the key, you can set a synchronization interface, which is called every 10 minutes, to keep the synchronization with the remote interface data
Incrby auto increment
PV auto increment: use string key value to set key interface to call incrby plus 1 once to get ($key)
Uvset: use set to set key calling interface, use IP address, add IP address to set, and calculate the number of set elements

Script synchronization data: synchronize the data from Mysql to redis for the first time, and prevent the loss of redis data in the later stage. Consider adding incremental calculation to MySQL, and accumulate one operation multiple times

Paging Optimization: paging based on ID area rather than limit

3. Redis message queue

Introduction: sequence collection of messages when message queuing
Scenario: PV statistics and viewing on the homepage of the website
The traditional direct update table is easy to lock in case of large concurrency,
Redis message queue: redis requests rpush script to process pvlog set PV at a constant speed to view get PV

Coping with peak flow
Asynchronous consumption (variable speed insertion, production and uniform processing, consumption)
Decoupling application (production from different sources and consumption from different directions)

There are many ways to implement message queuing. The main advantage of using redis is that it is simple and fast
There are two ways to implement message queuing in redis:

  • Based on list
  • Based on publish / subscribe

Redis implements message queuing
Implementation of message queue based on list

  • The capacity of redis determines the service carrying capacity
  • Each message can only be consumed by one consumer
  • We usually consume all the information
  • Fault tolerance in handling failed messages

Implementation of message queue based on publish / subscribe
Implementation of PV statistics based on publish and subscribe
In subscription mode, you can only perform subscribe, psubscribe and other subscription related operations. To perform other operations, you need to re instantiate a redis instance

There are many ways to implement message queuing
The main advantage of using redis is simple and fast
There are two ways to implement message queuing in redis:
Based on list 
Based on subscribe / publish

2、 Redis cache introduction

Redis is completely free and complies with BSD protocol. It is a high-performance key value database

  • Simple key value storage, high performance
  • Redis has more data structures and supports more data structures
  • Redis supports data persistence and data recovery
  • All redis operations are atomic
  • The server supports auth password authentication

Redis field type

  • String string
  • Hash / hash
  • List list
  • Set unordered set
  • Zset ordered set

String type

Most common data types
Can be any type of string, such as binary, JSON object
Maximum capacity 512M
Generally used as cache

Hash type

  • Similar to PHP array
  • Multiple key values can be saved, and each K-V is a string type
  • Up to 2 ^ 32-1 fields

Generally used to store personal information cache

List type

  • The implementation mode is bidirectional linked list
  • Used to store an ordered list of strings
  • Add and pop elements from both ends of the queue
  • Especially suitable for message queuing
Set up seckill activity
 1. Set seckill status MS? Status 1 description end 0 not on
 2. Seckill queue list
 3. Judge the queue length. If the queue length is exceeded, set the seckill status to end
 4. To process the queue, set the queue status to stop killing before processing

Set type

  • Each element in the collection is different
  • Elements up to 2 ^ 32-1
  • Element has no order
Difference set sdiff
 Union Sunion

Zset set

  • Set is ordered
  • It supports inserting, deleting and judging whether the element exists
  • Get the top n elements with the highest / lowest score

3、 Redis operation and maintenance

  1. Persistence

Save data snapshot at the time interval specified by RDB
Suitable for backup
Fork sends out the child process for backup. The main process has no IO operation
Fast recovery of large data sets
Persistent under specific conditions, easy to lose data
When large data is stored, there will be performance problems

Aof first appends the command to the end of the operation log to save all the history records

The data is very complete and less data is lost in fault recovery
Historical operations can be processed
Large file size
Slower than RDB and slower recovery

  1. Rational use of redis

Prevent full memory
Set timeout
Do not store too large documents
Do not store infrequently used documents
Improve efficiency
Reasonable use of different data structure types
Be careful with regular processing or batch operation of hash, set, etc

3、Redis Cluster

  • Multiple redis instances run in collaboration
  • When slot is used to segment data, CRC16 and 16384 will be dispersed after modulus taking
  • Master slave structure and election algorithm to ensure the reliability of each node
  • The client can connect to any node for operation


  • All redis nodes are interconnected with each other, and binary protocol is used internally to optimize transmission speed and bandwidth
  • The failure of a node is only effective when more than half of the nodes in the cluster detect failure
  • The client is directly connected to the redis node, and does not need the intermediate proxy layer. The client does not need to connect to all the nodes in the cluster, just any available node in the cluster
  • Redis cluster maps all physical nodes to 0-16383slot. Cluster is responsible for maintaining node < – > slot < – > value

matters needing attention:

Batch operation is not fully supported
Things cannot be supported by cross-border points
Multiple instances are not supported
Key is the minimum granularity
At least 6 clusters can ensure a complete and highly available cluster