Post

Redis

Why Redis is fast

I love the comments under https://zhuanlan.zhihu.com/p/160157573 :)

Todo

  • learn the flow of main function
  • figure out how replication works such that it does not return stale data?
  • figure out how atomicity is maintained. read after write works?
  • figure out persistence works?

What happens for BRPOP?

Recently, I am investigating celery issues that uses redis as the message broker. It uses BRPOP underneath to consume messages. I then wonder if a redis client calls BRPOP and then dies before it receives the response, what will happen?

BRPOP is handled by this function, It then comes to these lines of code, copied below.

1
2
3
4
5
        addReplyArrayLen(c,2);
        addReplyBulk(c,key);
        addReplyBulk(c,value);
        decrRefCount(value);
        listElementsRemoved(c,key,where,o,1,1,NULL);

As you can see, it adds the reply to the write buffer, and then remove list elements. The reply will be sent to client at next epoll cycle. See more details from this post. Ok, now we know how that the BRPOP actually pops the element before the reply is sent to client, so it is possible that redis client crashes and the data is gone too.

This post is licensed under CC BY 4.0 by the author.