没有合适的资源?快使用搜索试试~ 我知道了~
redis内存存储结构分析
5星 · 超过95%的资源 需积分: 10 12 下载量 181 浏览量
2012-03-05
13:52:15
上传
评论
收藏 2.19MB PDF 举报
温馨提示
试读
20页
redis缓存解决方案和基本命令,redisredis内存存储结构分析.Redis的数据全部放在内存带来了高速的性能,但是也带来一些不合理之处。比如一个中型网站有100万注册用户,如果这些资料要用Redis来存储,内存的容量必须能够容纳这100万用户。但是业务实际情况是100万用户只有5万活跃用户,1周来访问过1次的也只有15万用户,因此全部100万用户的数据都放在内存有不合理之处,RAM需要为冷数据买单。
资源推荐
资源详情
资源评论
Redis 内存存储结构分析
五竹,20110418
Redis: A persistent key-value database with built-in net interface written in ANSI-C
for Posix systems
1 Redis 内存存储结构
本文是基于 Redis-v2.2.4 版本进行分析.
1.1 Redis 内存存储总体结构
Redis 是支持多 key-value 数据库(表)的,并用 RedisDb 来表示一个 key-value
数据库(表). redisServer 中有一个 redisDb *db; 成员变量, RedisServer 在初始化
时,会根据配置文件的 db 数量来创建一个 redisDb 数组. 客户端在连接后,通过
SELECT 指令来选择一个 reidsDb,如果不指定,则缺省是 redisDb 数组的第 1 个
(即下标是 0 ) redisDb. 一个客户端在选择 redisDb 后,其后续操作都是在此
redisDb 上进行的. 下面会详细介绍一下 redisDb 的内存结构.
redis 的内存存储结构示意图
redisDb 的定义:
typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
dict *io_keys; /* Keys with clients waiting for VM I/O */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id;
} redisDb;
struct redisDb 中 ,dict 成员是与实际存储数据相关的. dict 的定义如下:
typedef struct dictEntry
{
void *key;
void *val;
struct dictEntry *next;
} dictEntry;
typedef struct dictType
{
unsigned int (*hashFunction)(const void *key);
void *(*keyDup)(void *privdata, const void *key);
void *(*valDup)(void *privdata, const void *obj);
int (*keyCompare)(void *privdata, const void *key1, const void *key2);
void (*keyDestructor)(void *privdata, void *key);
void (*valDestructor)(void *privdata, void *obj);
} dictType;
/* This is our hash table structure. Every dictionary has two of this as we
* implement incremental rehashing, for the old to the new table. */
typedef struct dictht
{
dictEntry **table;
unsigned long size;
unsigned long sizemask;
unsigned long used;
} dictht;
typedef struct dict
{
dictType *type;
void *privdata;
dictht ht[2];
int rehashidx; /* rehashing not in progress if rehashidx == -1 */
int iterators; /* number of iterators currently running */
} dict;
dict 是主要是由 struct dictht 的 哈唏表构成的, 之所以定义成长度为 2 的
( dictht ht[2] ) 哈唏表数组,是因为 redis 采用渐进的 rehash,即当需要 rehash 时,
每次像 hset,hget 等操作前,先执行 N 步 rehash. 这样就把原来一次性的 rehash
过程拆散到进行, 防止一次性 rehash 期间 redis 服务能力大幅下降. 这种渐进
的 rehash 需要一个额外的 struct dictht 结构来保存.
struct dictht 主要是由一个 struct dictEntry 指针数组组成的, hash 表的冲突
是通过链表法来解决的.
struct dictEntry 中的 key 指针指向用 sds 类型表示的 key 字符串, val 指
针指向一个 struct redisObject 结构体, 其定义如下:
typedef struct redisObject
{
unsigned type:4;
unsigned storage:2;
/* REDIS_VM_MEMORY or REDIS_VM_SWAPPING */
unsigned encoding:4;
unsigned lru:22; /* lru time (relative to server.lruclock) */
int refcount;
void *ptr;
/* VM fields are only allocated if VM is active, otherwise the
* object allocation function will just allocate
* sizeof(redisObjct) minus sizeof(redisObjectVM), so using
* Redis without VM active will not have any overhead. */
} robj;
type 占 4 bit,用来表示 key-value 中 value 值的类型,目前 redis 支持:
string, list, set,zset,hash 5 种类型的值.
/* Object types */
#define REDIS_STRING 0
#define REDIS_LIST 1
#define REDIS_SET 2
#define REDIS_ZSET 3
#define REDIS_HASH 4
#define REDIS_VMPOINTER 8
storage 占 2 bit ,表示 此值是在 内存中,还是 swap 在硬盘上.
encoding 占 4 bit ,表示值的编码类型,目前有 8 种类型:
/* Objects encoding. Some kind of objects like Strings and Hashes can be
* internally represented in multiple ways. The 'encoding' field of the object
* is set to one of this fields for this object. */
#define REDIS_ENCODING_RAW 0 /* Raw representation */
#define REDIS_ENCODING_INT 1 /* Encoded as integer */
#define REDIS_ENCODING_HT 2 /* Encoded as hash table */
#define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
#define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked
list */
#define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
#define REDIS_ENCODING_INTSET 6 /* Encoded as intset */
#define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
如 type 是 REDIS_STRING 类型的,则其值如果是数字,就可以编码
成 REDIS_ENCODING_INT,以节约内存.
如 type 是 REDIS_HASH 类 型 的 , 如 果 其 entry 小 于 配 置 值 :
hash-max-zipmap-entries 或 value 字 符 串 的 长 度 小 于
剩余19页未读,继续阅读
资源评论
- popkiler2012-07-03文档总结的很全
37degreemiss
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功