Redis replication gap tuning

Redis manages replication offset between master and slaves. If you type in “INFO REPLICATION” at redis-cli, the following output comes out.

...
# Replication
role:master
connected_slaves:1
slave0:ip=x.x.x.x,port=6379,state=online,offset=813086613,lag=1
master_replid:4e1056aefd16c7b7b3c7a7a6db27ad6440bc9766
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:813086613
...

For the above result, offset number can vary depending on each environment. But slave0’s offset should be equal or similar to master_repl_offset.

The gap between master_repl_offset and slave’s offset is the amount which is not replicated (or not acked) in bytes. If the number is big, partial data loss can happen in case of abnormal master shutdown. Therefore, it is the tuning point to reduce the gap.

Replication data is sent from master to slaves asynchronously, and slaves send back acks periodically.

– Sending ack from a slave (replication.c)

void replicationCron(void) {
...
    if (server.masterhost && server.master &&
        !(server.master->flags & CLIENT_PRE_PSYNC))
        replicationSendAck();
...
}

– Calling replicationCron() (server.c)

int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
...
    run_with_period(1000) replicationCron();
...
}

– macro define (server.h)

#define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))

The main logic to send ack to master is inside replicationSendAck() and it's called by run_with_period(1000) checking interval.

From my test, the shorter is the interval, the smaller is the replication offset gap. To shorten the interval, you need to change server.hz whose value is from config file’s “hz” option. (default is “10”)

To sum up, by reducing “hz” value, you can decrease the replication gap.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.