Ch04 Data Structure

10/22/2023 redis

# Advanced

# SDS

  • Redis定义了多种SDS来存放字符串(不同长度用不同的结构体)

image-20231022225056755

image-20231022225655621

# IntSet

  • 在数据量不大的时候,存数字

    image-20231022230159657

    image-20231022230322489

  • 在添加一个放不下的元素之后升级扩容image-20231022230948052

  • 会确保元素的唯一,有序

  • 底层采用二分查找、

# Dict

image-20231022232851627

image-20231022231944400

  • used有可能大于size的大小,因为可能哈希冲突,一个key对应多个value(*next)
  • union表示v可以是这四种类型中任意一种
  • 为什么需要sizemask?hash的时候对size求余运算 == 和size -1 求与运算 【必须在size是2的n次方的情况下】

image-20231022232706521

image-20231022232927033

  • 每次冲突的时候往队首加就可以,否则要遍历好多次

  • rehashimage-20231022234048876

  • image-20231022234210142

image-20231023000923250

  • 由于rehash可能数据量过大,主线程会很慢,所以采用渐进式image-20231023001401729

# ZipList

image-20231023110714164

image-20231023111327107

image-20231023112419553

  • 连锁更新问题image-20231023112835283

image-20231023113002227

# QuickList

image-20231023113646216

image-20231023113826362

image-20231023113914170

image-20231023114207740

# SkipList

image-20231023120806383

image-20231023120904268

image-20231023120944768

# RedisObject

image-20231023121303177

  • 编码方式就是用上面哪个数据结构来实现image-20231023121558062
  • image-20231023121723854

# String

image-20231023140052403

  • EMBSTR:最好字符串不要超过44字节,这样就一共分配64字节,正好是redis申请内存的一个分片大小

image-20231023135958893

# List

image-20231023140538722

image-20231023140908766

# Set

image-20231023142648721

image-20231023143218870

image-20231023143453805

# ZSet

image-20231023144109371

image-20231023144311886

  • 但是内存占用太高了

image-20231023145938382

# Hash

image-20231023150330077

image-20231023150714988

# 过期策略

image-20231024152511167

image-20231024154117403

  • 用了另一个dict来存,key和之前的一样,val存的是过期时间

image-20231024154639276

image-20231024154618214

image-20231024155228722

image-20231024160646479

# 淘汰策略

image-20231024161105148

image-20231024161357971

image-20231024161854294

image-20231024162501570

# 分片

image-20231024172420853

# 缓存

image-20231024172845892

# 为什么不用redis做消息队列

# image-20231024175654217

image-20231024173410144

# redis备份

AOF

image-20231024173704538

image-20231024173815591

# 主从

image-20231024174855942

# 容灾

image-20231024175224816

image-20231102190157109