63
Redis Tips [email protected]

Redis ndc2013

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Redis ndc2013

Redis Tips

[email protected]

Page 2: Redis ndc2013

• OpenSource Contributor • REDIS • TWEMPROXY

Page 3: Redis ndc2013

Before Presentation

Page 4: Redis ndc2013

Agenda • Dangerous Commands •Memory Policies •Replication •RDB

Page 5: Redis ndc2013

Dangerous Commands •Keys *(pattern)

•FlushAll

Page 6: Redis ndc2013

Dangerous Commands •Why? –Single Thread. –Each command should spend small time.

Page 7: Redis ndc2013

Keys * • Don’t use keys command in production. –“Warning: consider KEYS as a command that should only be used in production environments with extreme care” is written in redis.io keys commands manual.

Page 8: Redis ndc2013

Keys * di = dictGetSafeIterator(c->db->dict); allkeys = (pattern[0] == '*' && pattern[1] == '\0'); while((de = dictNext(di)) != NULL) { …… stringmatchlen(pattern,plen,key,sdslen(key),0) }

Page 9: Redis ndc2013

FlushAll Cache Item Count Time

Memcache 1,000,000 1~2ms

Redis 1,000,000 1000ms(1 second)

Page 10: Redis ndc2013

FlushAll • Redis’s FlushAll is slow and paused.

• But memcache’s flush is really fast.

–memcache doesn’t depend on Item count.

–Redis depends on Item count.

Page 11: Redis ndc2013

FlushAll • What is different from memcache’s flush.

• Memcache just sets time to flush.

• Redis deletes all items.

–It spends 1 second in 1M or 2M items

Page 12: Redis ndc2013

FlushAll-Redis long long emptyDb() { for (int j = 0; j < server.dbnum; j++) { dictEmpty(server.db[j].dict); dictEmpty(server.db[j].expires); } return removed; }

Page 13: Redis ndc2013

FlushAll-Redis

Page 14: Redis ndc2013

FlushAll-Memcache static void process_command(conn *c, char *command) { if (exptime > 0) settings.oldest_live = realtime(exptime) - 1; else /* exptime == 0 */ settings.oldest_live = current_time - 1; }

Page 15: Redis ndc2013

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; }

Page 16: Redis ndc2013

Redis Memory Policy • Volatile-lru • Allkeys-lru • Volatile-random • Allkeys-random • Volatile-ttl • noeviction

Page 17: Redis ndc2013

Redis Memory Policy • In 64bit, Memory Policy doesn’t work,

if you don’t set maxmemory parameter.

• In 32bit, maxmemory is set 4GB as default

Page 18: Redis ndc2013

Default Setting in 64 • There is no memory limitation.

• So If you insert bigger data than memory. It will be slow.

• But But But. Make sure Vm.overcommit_memory property.

Page 19: Redis ndc2013

Memory Policy • If you use Memory-Policy and

replication.

– It can cause some consistency problem.

Master Slave Delete propagation

Delete items because of its own memory policy

Page 20: Redis ndc2013

Replication •Support Chained Replication

•Common mistake

Page 21: Redis ndc2013

Replication •Support Chained Replication

Master

Slave Slave

Slave Slave

Page 22: Redis ndc2013

Replication • Common mistake. –Don’t forget “slaveof no one”

• Slave’s replicationCron keeps track of master’s health.

Page 23: Redis ndc2013

Replication

Master Slave replicationCron

Health check

Page 24: Redis ndc2013

Replication

Master Slave replicationCron

Health check

Page 25: Redis ndc2013

Replication

Master Slave replicationCron

When master reruns, Resync with Master

Page 26: Redis ndc2013

Replication

Master Slave replicationCron

Slave will has no data after resyncing

If master has no data.

Page 27: Redis ndc2013

Initial Replication Step

Page 28: Redis ndc2013

RDB • Good and Bad.

• It causes all of problems.

Page 29: Redis ndc2013

RDB - GOOD • Can store data into persistent layer.

Page 30: Redis ndc2013

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

Page 31: Redis ndc2013

RDB – Large Memory • Performance is relevant to Memory

Size.

17.1 GB 34.2 GB 68.4 GB

117 GB

Page 32: Redis ndc2013

RDB - BAD • Using bgsave in slave.

• Turning off RDB.

• Using save in master or slave.

Page 33: Redis ndc2013

RDB – Twice Memory • fork() and COW(copy on write) Issue

–In Write Heavy System:

Page 34: Redis ndc2013

RDB – Twice Memory

Page 35: Redis ndc2013

RDB – Twice Memory

Page 36: Redis ndc2013

RDB – Twice Memory

Page 37: Redis ndc2013

Real Case Study • Background

–Can’t write to Redis Server

–Sentinel doesn’t find Server’s failure.

Page 38: Redis ndc2013

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.”

Page 39: Redis ndc2013

Real Case Study 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; }

Page 40: Redis ndc2013

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 설정이 가능해짐.

Page 41: Redis ndc2013

SELECT Command • Don’t use “SELECT” Command.

–You will not be able to use “SELECT” in Redis Cluster.

• Redis Proxy can make some troubles.

Page 42: Redis ndc2013

Response Time • Why memcache’s response time is

uniformed? –Difference of Memory Allocator

Memcache Redis - Slab Allocator - Just malloc()

Page 43: Redis ndc2013

Redis VS Memcache • The popular question

–Redis and Memcache: What is better?

Memcache Redis - Uniformed Response

Time - More stable

- Data Structure - Persistent

Page 44: Redis ndc2013

Sentinel

Page 45: Redis ndc2013

Sentinel • Sentinel is Failover Solution for Redis.

Master Slave

Sentinel Sentinel periodically checks Redis Master

Page 46: Redis ndc2013

Sentinel

Master Slave

Sentinel Send “slaveof no one” to slave node

Page 47: Redis ndc2013

Sentinel

Master Slave

Sentinel Notify to client about changing master

Client

Page 48: Redis ndc2013

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"

Page 49: Redis ndc2013

Sharding or Clustering

Page 50: Redis ndc2013

Two Approches.

Page 51: Redis ndc2013

Client Library VS

Server Proxy

Page 52: Redis ndc2013

Client Library

NHN Case Study: http://helloworld.naver.com/helloworld/294797

Craiglist Case Study: http://blog.zawodny.com/2011/02/26/redis-sharding-at-craigslist/

Page 53: Redis ndc2013

Client Library ZooKeeper

or health Checker

Redis Library

Page 54: Redis ndc2013

Craigslist

Page 55: Redis ndc2013

NHN Application Servers

ZooKeeper

Redis Cluster Manager

Redis Shard-1

Redis Shard-2

Redis Shard-3

Page 56: Redis ndc2013

NHN

Page 57: Redis ndc2013

Client Library • Benefits –No hob(faster than server proxy)

• Liabilities

–Consistency Problem(Timing Issue)

Page 58: Redis ndc2013

Server Proxy

Redis Cloud Services

Twemproxy https://github.com/twitter/twemproxy

Page 59: Redis ndc2013

Twemproxy • Simple and fast memcache/redis proxy

Client Client Client Client

Twemproxy

Redis Redis Redis Redis

Page 60: Redis ndc2013

Twemproxy If you use redis as a store, Twemproxy might not be good for your purpose

Page 61: Redis ndc2013

Server Proxy • Benefits –Client doesn’t need any other libraries

• Liabilities

–Hob.(less speed and proxy is also SPOF)

Page 62: Redis ndc2013

Q & A

Page 63: Redis ndc2013

Thank you!