diff --git a/src/replication.c b/src/replication.c index ca3287491..97e01b64d 100644 --- a/src/replication.c +++ b/src/replication.c @@ -418,13 +418,13 @@ void feedReplicationBuffer(char *s, size_t len) { } if (add_new_block) { createReplicationBacklogIndex(listLast(server.repl_buffer_blocks)); + + /* It is important to trim after adding replication data to keep the backlog size close to + * repl_backlog_size in the common case. We wait until we add a new block to avoid repeated + * unnecessary trimming attempts when small amounts of data are added. See comments in + * freeMemoryGetNotCountedMemory() for details on replication backlog memory tracking. */ + incrementalTrimReplicationBacklog(REPL_BACKLOG_TRIM_BLOCKS_PER_CALL); } - /* Try to trim replication backlog since replication backlog may exceed - * our setting when we add replication stream. Note that it is important to - * try to trim at least one node since in the common case this is where one - * new backlog node is added and one should be removed. See also comments - * in freeMemoryGetNotCountedMemory for details. */ - incrementalTrimReplicationBacklog(REPL_BACKLOG_TRIM_BLOCKS_PER_CALL); } }