SDS,simple dynamic string

  1. 二进制安全的数据结构
  2. 空间换时间,每次扩容(len+addLen)*2进行内存预分配,当内存达到1M之后,扩容每次只能增加1M
  3. 兼容C数据的函数库

redis中所有的key都是string类型

  1. struct sds{
  2. //redis会根据buf的大小动态的选择sds的类型
  3. int alloct;
  4. int len;
  5. uint8 flags;
  6. char[] buf;
  7. }

Dict

  1. 海量数据的存储:数组,链表
  2. hash(key)->自然数[大]
  3. 数组+链表,头插法

redisDb一共有15个

  1. /* Redis database representation. There are multiple databases identified
  2. * by integers from 0 (the default database) up to the max configured
  3. * database. The database number is the 'id' field in the structure. */
  4. typedef struct redisDb {
  5. dict *dict; /* The keyspace for this DB */
  6. dict *expires; /* Timeout of keys with a timeout set */
  7. dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
  8. dict *ready_keys; /* Blocked keys that received a PUSH */
  9. dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
  10. int id; /* Database ID */
  11. long long avg_ttl; /* Average TTL, just for stats */
  12. } redisDb;
  1. 每个字典里面都有两个dictht,是因为redis里面采用了渐进式的rehash,不会一次性把数据数据全部转移到新的ht当中来
  2. dictEntry
  3. redisObject:表示value中的类型,包括string,hash,set,zset,encoding表示实际存储的类型

    String的编码类型

  4. int

对于int范围之内且能够转换成一个整型值的value,直接通过*ptr保存(8个字节)

  1. embstr

一个redisObject总共16个字节,cacheLine是64字节,sdshdr8加上终结字符’/0’一共是4字节,所以剩余44个字节,只要字符串字节数少于44个字节就可以作为embstr

Bitmap

  1. api
    1. setbit key offset value
    2. getbit key offset
  2. 汉明重量

    list的数据结构

  3. Redis采用quicklist和ziplist的数据结构

34 深入了解Redis数据结构 - 图1
quicklist保存quickNode->zl

Hash的数据结构

  1. 在元素比较少的时候或者单个元素比较小的时候,采用ziplist存储的
  2. 超出阈值之后改为hashtable

    Set的数据结构

  3. 当所有的数据都是整数的时候,使用Intset编码

  4. 当元素大于某个阈值或者元素无法被转成整数的时候,转成hashtable

    Zset的数据结构

  5. 当数据比较少的时候采用ziplist

  6. skiplist+dict

    1. zskiplist:header、tail

      GeoHash

  7. 内部类型:zset

  8. 二分经纬区间获得对应经纬度的编码,把两串编码组合奇数位经度,偶数位维度生成Z阶曲线
  9. 然后利用Base32将获得的编码转成字符串
  10. redis将地图切成26位,经纬度总共52位

    Acl

    redis6.0之后增加用户的概念,用来控制权限