Redis: what one SET command touches

One thread, one event loop: how a command gets in, does its work, and reaches the disk and the replicas afterwards.

Source
redis/redis @ fd7710c
Checked
12 boxes, 14 arrows, 31 source lines
Drawn
, by an AI agent
Press play to watch the page being drawn. The video has no sound.

What it shows

Redis runs commands on one thread. The diagram shows why that is enough: what a command does is short, and what is slow happens somewhere else.

In. The event loop in ae.c waits until a socket is readable, then calls the handler registered for it. For a client connection that handler is readQueryFromClient, which reads the bytes and parses them into commands. A complete command goes to processCommand in server.c, which checks it and calls the command's own function: setCommand for SET.

The work. SET stores the value in the keyspace with setKeyByLink and queues +OK with addReply. Nothing is written to the socket at that moment. The reply is queued on the client and written out later by writeToClient.

After. A command that changed data is passed on to two places: the buffer of the append-only file and the stream that replicas read. Snapshots take another route. A periodic task decides one is due and forks. The child process writes dump.rdb from the memory it inherited at that instant, so the parent keeps serving.

Where every box and arrow comes from

Before this drawing was accepted, a script looked up each box and each arrow in redis/redis at commit fd7710c (19 November 2026) and failed if one was missing. These are the lines it found.

Arrows
From, toProof in the source
Client → Event looprequest
  • src/ae.c:403the loop waits until a socket has something to readnumevents = aeApiPoll(eventLoop, tvp);
  • src/ae.c:440then calls the handler registered for that socketfe->rfileProc(eventLoop,fd,fe->clientData,mask);
Event loop → Networkingreadable
  • src/socket.c:288a readable socket runs the connection's read handlerif (!callHandler(conn, conn->read_handler)) return;
  • src/networking.c:137which for a client connection is readQueryFromClientconnSetReadHandler(conn, readQueryFromClient);
Networking → Server corecommand
Server core → String commandscall
String commands → Keyspaceset key
  • src/t_string.c:183SET stores the value in the keyspacesetKeyByLink(c, c->db, key, valref, setkey_flags, &link);
String commands → NetworkingaddReply
  • src/t_string.c:219then queues the +OK reply for the clientaddReply(c, ok_reply ? ok_reply : shared.ok);
Networking → Clientreply
  • src/networking.c:2994queued replies are sent when the socket can take themint ret = _writeToClientNonSlave(c, &nwritten);
  • src/networking.c:2868by writing them to the client's connection*nwritten = connWrite(c->conn, c->buf + c->sentlen, c->bufpos - c->sentlen);
Server core → Snapshotsave point
  • src/server.c:1767the periodic task starts a snapshot once enough keys have changedrdbSaveBackground(SLAVE_REQ_NONE,server.rdb_filename,rsiptr,RDBFLAGS_NONE);
Server core → Append-only filepropagate
  • src/server.c:3881a command that changed data is passed to the append-only filefeedAppendOnlyFile(dbid,argv,argc,duration);
Server core → Replication
  • src/server.c:3883and to replicationreplicationFeedSlaves(server.slaves,dbid,argv,argc);
Keyspace → Snapshotfork
  • src/rdb.c:2361the snapshot is written by a forked child, which sees the keyspace as it was at that instantif ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {
  • src/rdb.c:2367the child saves every keyretval = rdbSave(req, filename,rsi,rdbflags);
Snapshot → dump.rdbwrite
  • src/rdb.c:2245the snapshot is written to a fileFILE *fp = fopen(filename,"w");
Append-only file → appendonly.aofappend
  • src/aof.c:1514queued commands are appended to the filenwritten = aofWrite(server.aof_fd,server.aof_buf,sdslen(server.aof_buf));
Replication → Replicastream
  • src/replication.c:730the command is added to the buffer streamed to replicasfeedReplicationBuffer(selectcmd->ptr, sdslen(selectcmd->ptr));
Boxes
BoxProof in the source
Client

Any Redis client: it is not part of the server's source.

Event loop
  • src/ae.c:497the loop that waits for events and handles them one at a timevoid aeMain(aeEventLoop *eventLoop) {
Networking
Server core
String commands
Replica

Another Redis server that follows this one.

Replication
  • src/replication.c:673sends a write command to every replicavoid replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
Append-only file
  • src/aof.c:1705queues a write command for the append-only filevoid feedAppendOnlyFile(int dictid, robj **argv, int argc, long long duration) {
Snapshot
  • src/rdb.c:2352saves a snapshot from a forked child processint rdbSaveBackground(int req, char *filename, rdbSaveInfo *rsi, int rdbflags) {
Keyspace
  • src/db.c:807stores a value under a keyvoid setKeyByLink(client *c, redisDb *db, robj *key, robj **valref, int flags, dictEntryLink *plink) {
  • src/db.c:480adds a new key to the databasekvobj *dbAdd(redisDb *db, robj *key, robj **valref) {
appendonly.aof

The append-only file on disk.

dump.rdb

The snapshot file on disk.

How it was made

An AI agent read the project's source at the commit above and drew this page on a WireMagic canvas through Agent Bridge, the same tools any agent gets when you connect it to the app. The result is an ordinary WireMagic file: download it, open it, and move things around.

You can have your own agent do this for a codebase you know. Connect it, then ask it to draw the architecture of the repository.