Redis 源码阅读

缘起

在成都面试小米二面的时候,跟面试官聊起了Sqlite数据库的二进制机制,提到过Sqlite底层为了节省磁盘的空间,采取的一些措施,在我讲完之后,面试官提到过Redis和另外一个不记得的库,这次跑完招聘会之后,回到学校突然兴致来了就顺带查了下Redis的使用

搜集的Redis的资料

  1. 用Clion阅读源码
  2. 大师一样使用Redis这篇文章中提到比较好的一个观点是,像专家一样使用Redis并不是说记住Reids的API,而是了解Redis的机制,使得现实场景下的问题模型,刚好适合Reids提供的机制,从而最大化Redis的威力,

Before we start, you might have noticed that Redis’ API isn’t like most. Rather than having 4 generic CRUD methods, Redis has a number of specialized methods. So far we’ve only looked at a small percentage of them. Our application will only make use of a handful. This is a pretty common usage pattern. Some commands you might never use, some commands make you think wow, that’s exactly what I need when you happen to be browsing through the online reference. Mastering Redis isn’t about memorizing all the commands (not that there’s an insane amount). It’s about (a) understanding the 5 data structures, (b) understanding how to model data and query it using Redis and (c) combining a and b to easily tell whether Redis is a good fit.

  1. 使用Redis 做一个短网址网站
  2. Redis最大能有多少个Key?单个节点可以处理2.5亿个很多时候,都是受制于内存大小,当内存超出的时候,会出现swap情况,造成性能下降

To give you a few examples (all obtained using 64-bit instances):

An empty instance uses ~ 3MB of memory. 1 Million small Keys -> String Value pairs use ~ 85MB of memory. 1 Million Keys -> Hash value, representing an object with 5 fields, use ~ 160 MB of memory. To test your use case is trivial using the redis-benchmark utility to generate random data sets and check with the INFO memory command the space used.

64-bit systems will use considerably more memory than 32-bit systems to store the same keys, especially if the keys and values are small. This is because pointers take 8 bytes in 64-bit systems. But of course the advantage is that you can have a lot of memory in 64-bit systems, so in order to run large Redis servers a 64-bit system is more or less required. The alternative is sharding