English | 中文版
[TOC]
struct redisCommand { /* Redis command */
char *name; /* command name */
redisCommandProc *proc; /* command callback */
int arity; /* number of arguments */
char *sflags; /* string flags (human readable) */
int flags; /* parsed binary flags */
redisGetKeysProc *getkeys_proc; /* function to extract key arguments from argv */
int firstkey; /* position of first key (0 = no keys) */
int lastkey; /* position of last key (may be negative: argc+lastkey) */
int keystep; /* step between first and last key */
long long microseconds, calls; /* total microseconds spent and call count */
};-
name: command name (lookup is case-insensitive) -
proc: pointer to the implementation function -
arity: used to validate argument count (negative value -N indicates >= N args) -
sflags: human-readable flagsflag binary flag constant meaning example commands wREDIS_CMD_WRITEwrite command (may modify dataset) SET,RPUSH,DELrREDIS_CMD_READONLYread-only command GET,STRLEN,EXISTSmREDIS_CMD_DENYOOMmay use a lot of memory; check OOM before executing SET,APPEND,RPUSH,LPUSH,SADDaREDIS_CMD_ADMINadministrative command SAVE,BGSAVE,SHUTDOWNpREDIS_CMD_PUBSUBpub/sub related command PUBLISH,SUBSCRIBE,PUBSUBsREDIS_CMD_NOSCRIPTnot allowed inside Lua scripts BRPOP,BLPOP,SPOPRREDIS_CMD_RANDOMnon-deterministic: same args on same dataset may return different result SPOP,SRANDMEMBER,RANDOMKEYSREDIS_CMD_SORT_FOR_SCRIPTsort output when used in Lua scripts to make results deterministic SINTER,SMEMBERS,KEYSlREDIS_CMD_LOADINGallowed while server is loading data INFO,SHUTDOWN,PUBLISHtREDIS_CMD_STALEallowed on replicas with stale data SLAVEOF,PING,INFOMREDIS_CMD_SKIP_MONITORnot auto-propagated in MONITORmodeEXECkREDIS_CMD_ASKING(internal use) FREDIS_CMD_FAST(internal use) -
flags: binary flags derived fromsflags -
getkeys_proc: function to extract keys from argv -
firstkey,lastkey,keystep: describe key positions -
calls,microseconds: command statistics
serverCron runs every 100ms by default and manages server resources and maintenance tasks.
graph TD
updateCachedTime(Update cached time) --> getLRUClock(Update LRU clock)
getLRUClock --> zmalloc_user_memory(Update memory usage stats)
zmalloc_user_memory --> prepareForShutdown(Check graceful shutdown)
prepareForShutdown --> clientCron(Handle clients)
clientCron --> databasesCron(Handle DB tasks)
databasesCron --> rewriteAppendOnlyFileBackground(Schedule BGREWRITEAOF)
rewriteAppendOnlyFileBackground --> checkPersistence(Check background persistence)
checkPersistence --> flushAppendOnlyFile(Flush AOF buffer to disk)
flushAppendOnlyFile --> freeClientsInAsyncFreeQueue(Free clients in async queue)
freeClientsInAsyncFreeQueue --> incrCronloops(Increment server.cronloops)
Main tasks include:
- Update cached server time
- Update LRU clock
- Update commands-per-second counters
- Update memory peak records
- Handle SIGTERM
- Manage client resources
- Manage database resources
- Run deferred BGREWRITEAOF if scheduled
- Check background persistence status (
rdb_child_pid != -1 || aof_child_pid != -1) - Flush AOF buffer to AOF file
- Close clients scheduled for asynchronous close
- Increment
server.cronloops
Decision flow for persistence tasks:
no_persistent=>operation: No background persistence running
is_delay=>condition: Is BGREWRITEAOF delayed?
is_autosave=>condition: Are autosave conditions met?
exec_bgrewriteaof=>operation: Execute BGREWRITEAOF
exec_bgsave=>operation: Execute BGSAVE
is_aofrewrite=>condition: Is AOF rewrite needed?
do_nothing=>operation: Do nothing
no_persistent->is_delay
is_delay(yes)->exec_bgrewriteaof
is_delay(no)->is_autosave
is_autosave(yes)->exec_bgsave
is_autosave(no)->is_aofrewrite
is_aofrewrite(yes)->exec_bgrewriteaof
is_aofrewrite(no)->do_nothing
- Initialize server state structure
- Load configuration options
- Initialize internal data structures
- Restore dataset state (load RDB/AOF as configured)
- Start the event loop
[1] Huang Jianhong. Redis Design and Implementation
[2] Redis5 design and source analysis — Chapter 9: command processing lifecycle