40
REDIS [email protected] 장애관리

Redis edu 3

Embed Size (px)

Citation preview

REDIS

[email protected]

장애관리

First of All.

Single Thread

Single Thread 의 의미

Don‘t execute Long Tasks.

O(n) Commands

O(n) Commands

KEYS

FLUSHALL/FLUSHDB

DEL big size of collections

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)

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

Del Collections

Item Count Time

list 1,000,000 1000ms(1 second) set

Sorted set

hash

RDB

RDB is Redis Snapshot

RDB

Fork(), Cow(Copy on Write)

Heavy Write 시에 메모리 이슈가 발생할 수 있음.

Redis의 Replication은 무조건 Fork()를 하므로 주의

RDB

17.1 GB

34.2 GB

68.4 GB

117 GB

메모리 사이즈에 따라서 생성 속도가 다름.

클 수록 느림…

Twice Memory

Twice Memory

Twice Memory

Write Error Case

읽기는 가능한데, 쓰기만 실패

Write Error Case

stop-writes-on-bgsave-error

RDB 생성 옵션이 켜져 있을 때, RDB 생성이 어떤 이유로 실패하면, 위의 옵션으로 인해서 Write 작업이 금지됨.

Info는 되지만, Ping은 실패함

Write Error Case

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

Write Error Case 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; }

Write Error Case config set stop-writes-on-bgsave-error no

Redis.conf에서 옵션 off(2.6.12 이상)

AOF

AOF is Appended only File

AOF *3 $3 Set $1 A $3 abc ……

AOF Rewrite - AOF 파일의 상태가 특정 조건(파일 사이즈가 얼마 이상일 경우)일 때 - AOF 파일을 현재 상태에 맞춰서 새롭게 생성 - AOF Rewrite도 Fork() 사용하므로 RDB 와 같은 이슈가 발생

메모리 파편화

Redis 는 매번 메모리 할당을 하므로, 부하가 많을 경우, 튀는 경우가 발생.

실제로 Redis -> Arcus 는 전환시에 별 문제가 없었지만, Arcus -> Redis 는 전환시에 튀는 경우가 발생

Slab Allocator Slab 0 Slab 1 Slab 2 … … Slab N

1mb 1mb 1mb 1mb 1mb 1mb 1mb 1mb

Slab Page List

Free Item List

LRU List Slab 0 Slab 1 Slab 2 … … Slab N Head

Slab 0 Slab 1 Slab 2 … … Slab N Tail

Item

Item

Item 밑에 있는 아이템일 수록 사용한지 오래됨.

Eviction Memcached는 slab별 LRU 만 지원

Redis 는 LRU, Random 등을 지원

Twitter에서 사용하는 Twemcache 의 경우는 Eviction 전략만 추가한 Memcached의 변종?

LRU 관리 Memcached는 이전에는 LRU 관리에 이슈가 있었음.

LRU List를 뒤에서 부터 검색해서 메모리를 확보함

다양한 Expire 값을 설정하면, Expire가 짧은 아이템이 앞으로가, 뒤에서 찾지못하고 메모리 할당의 가능성이 큼.

LRU 관리 Arcus는 뒤에서 부터 LRU List를 검색하더라도, 처음까지 전부 체크함.

모두 한번에 체크하는 것은 아니라, 매 텀마다, 이전 기록을 바탕으로 검색

LRU 관리 최근 버전의 Memcache 는 LRU_Crawler라는 스레드가 추가됨.

Memcached는 독립 스레드에서, Arcus 는 해당 LRU동작을 확장

LRU 관리 Redis 도 매 커맨드가 실행되기 전에 약 100여개 정도 랜덤하게 Eviction 정책에 따라서 Key를 지움.

메모리 파편화 Memcached는 메모리 관리를 직접하므로 문제가 덜함.

Redis 는 Jemalloc등을 사용하지만, 메모리 할당을 직접 처리할 수 없으므로 메모리 파편화가 심해질 수 있다.

메모리 파편화

메모리 파편화 Memcache는 메모리를 자신이 관리하므로, 실제 메모리 부족 시, 기존 데이터를 지울 수 있다.

Redis는 Jemalloc에서는 메모리 Max를 알지 못하므로, 그냥 새로운 Chunk를 할당하게 된다.

Thank you.