The client
The data structure
|
|
At first glance, I found that wiping a client data structure was really much ah. It’s a distraction. It’s okay. You don’t have to remember them all. Just a few important ones.
The configuration file
Client command
|
|
- Client list-> View information about the client
Among them
- Flags indicates the client type N- indicates normal client, M- indicates master, and so on
|
|
- Obl stands for the length of the fixed buffer
- Oll stands for the length of the dynamic buffer list
- Omem indicates the number of bytes used
- Events represents the event type (R/W)
- CMD records the command executed for the last time
See function for details
|
|
- Info Clients — > View all clients
- Connected_clients: indicates the number of client connections on the current Redis node. This parameter needs to be monitored carefully. If the number exceeds maxClients, new client connections will be rejected.
- Client_longest_output_list: specifies the maximum number of queue objects in all output buffers.
- Client_biggest_input_buf: specifies the maximum capacity occupied by all input buffers.
- Blocked_clients: Number of clients executing blocking commands such as BLPOP, brPOP, brpoplpush
Client Shutdown
- Invoke the client kill command
- The command does not conform to the protocol format
- Client Timeout
- The input buffer exceeds the threshold 1 gb
- Output buffer exceeds threshold (soft limit and hard limit, client shuts down immediately after hard limit, shuts down after soft limit lasts for specified second)
The format for the client – the output buffer – limit
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
In the command, class indicates the client type. If the client exceeds the hard limit, the client is shut down immediately. If the client exceeds the soft limit and lasts for soft seconds, the client is shut down again.
The class values:
normal -> normal clients including MONITOR clients
slave -> slave clients
pubsub -> clients subscribed to at least one pubsub channel or pattern
|
|
Because Redis is single-threaded, it cannot loop through the client all the time. Client timeout detection is performed in clientsCron of serverCron, which is a periodic function that is executed every 100ms. Server. hz Indicates the frequency of calling the serverCron function. The default value is 10. In the clientsCron function, to loop all clients once every second, iterations are set to numclients/ server.Hz. If there are too many clients, it may cause the redis main thread to block. Therefore, 5.0 introduced dynamic Hz, as shown in the configuration file dynamic-Hz, which is turned on by default.
The service side
The database
The database is stored in the redisDb structure, while the server redisServer structure holds redisDb objects and numbers, which can be updated in the configuration file (see dbnum in the configuration file).
The data structure
|
|
RedisServer contains redisDb data structures. Indicates which DB is currently in use.
|
|
Database switching
Database switching is performed using SELECT. C ->db = &server.db[id]; c->db = &server.db[id];
|
|
Key space
Redis can have multiple databases, and different databases do not affect each other. How to do that? Key space can be understood as a C++ namespace for isolation. The data is stored in a dict object in redisDb. So you’re basically operating on keys based on key space.
|
|
Expired keys
-
In Redis, expiration keys are stored in the redisDb expires variable, which is a dict pointer type.
-
storage
-
Key holds the Key object of the database
-
Value holds the expiration time of the database key object, which is a long integer Unix time.
|
|
- Setting expiration Time
Relative mode: expire, in seconds; Pexpire, in milliseconds
Absolute mode: expireat, unit: second; Pexpireat < timestamp >, in milliseconds. All other commands are converted to pexpireat
Expire command: setExpire(c,c->db,key,when) — >de = dictFind(db->dict,key-> PTR) — >dictAddOrFind(db->expires,dictGetKey(de))
- Deleting expiration Time
Persist (c->db,c->argv[1]) — >dictDelete(db->expires,key-> PTR)
- Check the remaining time of the key
View expiration time:
TTL, a unit of seconds; PTTL, millisecond
LookupKeyReadWithFlags – > getExpire — – > TTL = expire – mstime ();
- Key expiration policy
|
|
-
Periodic deletion is divided into two modes, which are called in different scenarios. So the ending judgment is different.
In databasesCron in the scheduled task serverCron, it is ACTIVE_EXPIRE_CYCLE_SLOW, which means we can spend more time processing. In the beforeSleep function before the event loop, it is ACTIVE_EXPIRE_CYCLE_FAST, because the processing of the event cannot be affected. So, we did an optimization
12345678Copy the code
if (type == ACTIVE_EXPIRE_CYCLE_FAST) { /* Don't start a fast cycle if the previous cycle did not exit * for time limit. Also don't repeat a fast cycle for the same period * as the fast cycle total duration itself. */ if(! timelimit_exit)return; if (start < last_fast_cycle + ACTIVE_EXPIRE_CYCLE_FAST_DURATION*2) return; last_fast_cycle = start; }Copy the code
- REDIS_EXPIRELOOKUPS_TIME_PERC is the percentage of CPU allocated to the activeExpireCycle function in a specified period of time. The default value is 25.
- Up to 16 libraries per loop.
- Each library requires to find up to 5 expired keys (it should be noted that, because each library is randomly selected a key, so the amount of data should not be too small, too little random effect is not good).
- In each library, 20 keys are randomly selected at a time. Check whether it is expired key, expired count once. Check every 16 times to see if the time limit is exceeded. If so, exit the loop. No further search.
Database notification
Database notifications take advantage of the publisk-subscribe model supported in Redis, which allows clients to see key changes in the database by subscribing to a given channel or pattern (stored in client and Server pubsub_Channels and Pubsub_Patterns, but stored differently). This function is classified into two types: key-space notification and event notification. To enable this function, you need to set parameters in the configuration file. See notify keyspace – events
Key space notification – What command was executed by a key
keyspace@:
Event notification – which commands an event is executed by
keyevent@:
|
|
Parameters that
To enable the configuration, you must set it in the configuration file as follows: notify-keyspace-events Elg. To enable the configuration K and E, you must enable at least one of them.