Redis Begins
charsyam@naver.com
Redis?
Redis?
Redis is an open source.
http://redis.io
Redis?
Redis is an open source.
BSD licensed.
http://redis.io
Redis?
Redis is an open source.
BSD licensed.
advanced key-value store.
http://redis.io
Redis?
Redis.io
http://redis.io
Why Redis?
Why Redis?
Simple.
Performance.
Build
$ make
But
You can meet some errors in Linux
Solution:
$> make distclean; make;
Why?
Redis saves settings to .make-* files
And they disturb normal building.
Argenda
Single Thread.
Collections.
Persistent.
Replication.
The one thing
you should know.
Redis is
Single Threaded!
Single Thread
Means!
Don’t execute
long task.
Example:
Keys command
Flushall/flushdb command
O(n)
Keys *
di = dictGetSafeIterator(c->db->dict);
allkeys = (pattern[0] == '*' && pattern[1] == '0');
while((de = dictNext(di)) != NULL) {
……
stringmatchlen(pattern,plen,key,sdslen(key),0)
}
FlushAll
Cache Item Count Time
Memcache 1,000,000 1~2ms
Redis 1,000,000 1000ms(1 second)
Memcache’s flush
is faster than
Redis’s flush.
Memcache’s flush
is faster than
Redis’s flush?
FlushAll-Redis
for (i = 0; i < ht->size && ht->used > 0; i++) {
dictEntry *he, *nextHe;
if ((he = ht->table[i]) == NULL) continue;
while(he) {
nextHe = he->next;
dictFreeKey(d, he);
dictFreeVal(d, he);
zfree(he);
ht->used--;
he = nextHe;
}
}
FlushAll-Memcache
if (exptime > 0)
settings.oldest_live = realtime(exptime) - 1;
else /* exptime == 0 */
settings.oldest_live = current_time - 1;
FlushAll-Memcache
if (settings.oldest_live != 0 &&
settings.oldest_live <= current_time &&
it->time <= settings.oldest_live) {
do_item_unlink(it, hv);
do_item_remove(it);
it = NULL;
}
Argenda
Single Thread.
Collections.
Persistent.
Replication.
Collections
Memcached supports just K-V
List
Set
Sorted Set
Hash
Key:Value
$> set key value
$> get key
Key:Value
sql> insert into userinfo (name,
email) values(‘charsyam’,
‘test@abc.com’);
Key:Value
$> set id:name “charsyam”
$> set id:email test@abc.com
$> mget id:name id:email
1) “charsyam”
2) test@abc.com”
List
$> rpush listname a --- (a)
$> rpush listname b --- (a, b)
$> lpush listname c --- (c, a, b)
$> rpop listname(or lpop listname)
List - when
When you need job queue or stack.
Set
$> sadd setname id1
$> sadd setname id2
$> smember setname
1) “id2”
2) “id1”
set - when
When you need follwers ids or
group members id
Sorted Set
$> zadd zsetname 1 “one”
$> zadd zsetname 2 “two”
$> zadd zsetname 3 “three”
Sorted Set
$> zrange zsetname 0 -1
1) “one”
2) “two”
3) “three”
zset - when
When you need ranking
Sorted Set
$> zrange zsetname 1 3
1) “two”
2) “three”
Hash
sql> insert into userinfo (name,
email) values(‘charsyam’,
‘test@abc.com’);
Hash$> hmset id name “charsyam”
email test@abc.com
Hash$> hgetall
1) “name”
2) “charsyam”
3) “email”
4) “test@abc.com”
Hash$> hset id email abc@abc.com
$> hgetall
1) “name”
2) “charsyam”
3) “email”
4) “abc@abc.com”
Remember!!!!!
The one thing
you should know.
Redis is
Single Threaded!
Don’t insert too
many items into
collections.
Delete collections
Item Count Time
list 1,000,000 1000ms(1 second)
set
Sorted set
hash
Argenda
Single Thread.
Collections.
Persistent.
Replication.
Redis can store
its memory
snapshot.
RDB
RDB is not
Relation DBMS.
RDB is redis
snapshot name.
Fork()
RDB - BAD
• Bad Performance in Large memory.
• Twice memory problem.
• Denying write problem when storing
RDB fails.
• If you just want Cache. Turn off RDB
RDB – Large Memory
• Performance is relevant to Memory
Size.
17.1 GB
34.2 GB
68.4 GB
117 GB
RDB – Twice Memory
• fork() and COW(copy on write) Issue
–In Write Heavy System:
RDB – Twice Memory
RDB – Twice Memory
RDB – Twice Memory
Real Case Study
• Background
–Can’t write to Redis Server
–Sentinel doesn’t find Server’s failure.
Real Case Study
• Reason
–If redis fails to save RDB, Redis basically
denies write operations from client.
–“MISCONF Redis is configured to save RDB
snapshots, but is currently not able to
persist on disk. Commands that may modify
the data set are disabled. Please check Redis
logs for details about the error.”
Real Case Study
• Reason
if (server.stop_writes_on_bgsave_err &&
server.saveparamslen > 0
&& server.lastbgsave_status == REDIS_ERR &&
c->cmd->flags & REDIS_CMD_WRITE)
{
flagTransaction(c);
addReply(c, shared.bgsaveerr);
return REDIS_OK;
}
Real Case Study
• Solution #1
• Solution #2
config set stop-writes-on-bgsave-error no
Turn off RDB Setting
2.6.12 부터 conf 에서 stop-writes-on-bgsave-error
설정이 가능해짐.
AOF
AOF is append
only file.
AOF memorizes
all requests.
AOF*3rn$3rnsetrn$1rnA
rn$3rn123rn*3rn$3rn
setrn$1rnBrn$3rn123
rn*3rn$3rnsetrn$1r
nCrn$3rn123rn
AOF*3
$3
Set
$1
A
……
AOF Rewrite
When size of AOF grows than redis
rewrite memory state to AOF.
Argenda
Single Thread.
Collections.
Persistent.
Replication.
Redis support
master/slave
replication
Replication
•Support Chained Replication
Master 1st Slave 2nd Slave
1st slave is master of 2nd slave
Replication
Master Slave
replicationCron
Health check
Replication
Master Slave
replicationCron
Health check
Replication
Master Slave
replicationCron
When master reruns, Resync with Master
Mistake: Replication
Master Slave
replicationCron
Slave will has no data after resyncing
If master has no data.
Initial Replication Step
Replication
Don’t forget
“slave of no one”
Sentinel
Sentinel
• Sentinel is Failover Solution for Redis.
Master Slave
Sentinel
Sentinel periodically checks Redis Master
Sentinel
Master Slave
Sentinel
Send “slaveof no one” to slave node
Sentinel
Master Slave
Sentinel
Notify to client about changing master
Client
Sentinel
redis 127.0.0.1:2003> psubscribe *
Reading messages... (press Ctrl-C to quit)
1) "pmessage"
2) "*"
3) "+switch-master"
4) "resque 127.0.0.1 1999 127.0.0.1 2002"
Sentinel
• Sentinel will connect correct master
even if you set slave’s ip in conf
Master Slave
Sentinel
Slave’s ip in sentinel.conf
Sentinel
• INFO
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
……
Sentinel
• Reconnect to correct master using
master_host and master_port.
Master Slave
Sentinel
Reconnect to master of slave.
Sentinel.conf
port 26379
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 30000
sentinel can-failover mymaster yes
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
Wrap Up
Redis is
Single Threaded!
Q & A
Thank you!

Redis begins