Database/Redis

[Redis] 복제 Replication 이중화 방법, 위험성, 이중화정보 확인 방법

꽁담 2020. 3. 8. 03:37

Redis 의 복제 (Replication 이중화)


이중화 개념으로 데이터를 거의 실시간(비동기 방식)으로 다른 레디스 노드에 복사하는 방식입니다.

서비스를 제공하던 마스터 노드가 다운되더라도, 복제 노드에서 서비스를 계속해서 진행할 수 있습니다.


기본적으로 복제서버는 읽기만 가능합니다.

이 옵션은 redis.conf 파일의 replica-read-only 파라미터로 쓰기가 가능하게 변경할 수 있습니다.



복제(Replication 이중화)가 없을 때의 문제점


1. 단일로 운영되던 Redis 가 어떤 이슈로 인해 다운된 경우 서비스를 할 수 없습니다.

2. Redis 를 다시 구동하기 위해 AOF 파일을 읽을때도, 만약 파일이 크다면 읽는 시간이 오래 소요됩니다.

3. 하드웨어 장애로 인해 AOF 나 RDB 파일이 유실된 경우 복구할 수 없습니다.



Replication 복제 특징


1. 비동기 복제를 합니다.

2. 마스터를 여러 복제서버를 가질 수 있습니다.

3. 복제서버는 다시 복제서버를 가질 수 있습니다.

4. 마스터의 데이터를 복제 서버로 보내는 작업은 자식프로세스(RDB 파일 생성하는 프로세스)가 처리합니다.

5. 자동 Failover 가 되지 않습니다.



복제 구성 방법


1. 복제서버의 redis.conf 파일에서 replilcaof 파라미터에 마스터 IP 와 PORT 를 넣어줍니다.

2. 복제서버를 시작합니다.

3. 복제가 완료되었습니다.


* 마스터 노드에서는 추가 작업이 필요하지 않습니다.

* 마스터 서버와 복제 노드간에는 네트워크 통신이 허용되어 있어야 합니다.



복제 구성 예제 - conf, 구동, 로그


Redis 의 conf 파일

서버의 conf 파일을 아래 사진처럼 작성한 뒤 서버를 구동합니다.

- 6379 포트를 사용하는 Redis 는 마스터서버 입니다.

- 6380, 6381 포트를 사용하는 Redis는 6379 포트를 사용하는 Redis 서버의 복제서버입니다.

1
2
3
4
5
6
7
8
9
10
$ cat redis_6379.conf | grep -e ^port -e ^replicaof
port 6379
 
$ cat redis_6380.conf | grep -e ^port -e ^replicaof
port 6380
replicaof 127.0.0.1 6379
 
$ cat redis_6381.conf | grep -e ^port -e ^replicaof
port 6381
replicaof 127.0.0.1 6379
cs


Redis 서버 구동

1
2
3
$ redis-server redis_6379.conf
$ redis-server redis_6380.conf
$ redis-server redis_6381.conf
cs


Redis 마스터 서버 로그

Line 1 - 8 : 복제서버로부터 Replica 요청을 받은 후 동기화 작업을 시작합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2970:M 08 Mar 2020 03:30:31.690 * Replica 127.0.0.1:6380 asks for synchronization
2970:M 08 Mar 2020 03:30:31.690 * Full resync requested by replica 127.0.0.1:6380
2970:M 08 Mar 2020 03:30:31.690 * Starting BGSAVE for SYNC with target: disk
2970:M 08 Mar 2020 03:30:31.690 * Background saving started by pid 2979
2979:C 08 Mar 2020 03:30:31.699 * DB saved on disk
2979:C 08 Mar 2020 03:30:31.699 * RDB: 0 MB of memory used by copy-on-write
2970:M 08 Mar 2020 03:30:31.776 * Background saving terminated with success
2970:M 08 Mar 2020 03:30:31.776 * Synchronization with replica 127.0.0.1:6380 succeeded
 
2970:M 08 Mar 2020 03:30:32.068 * Replica 127.0.0.1:6381 asks for synchronization
2970:M 08 Mar 2020 03:30:32.068 * Full resync requested by replica 127.0.0.1:6381
2970:M 08 Mar 2020 03:30:32.068 * Starting BGSAVE for SYNC with target: disk
2970:M 08 Mar 2020 03:30:32.068 * Background saving started by pid 2993
2993:C 08 Mar 2020 03:30:32.070 * DB saved on disk
2993:C 08 Mar 2020 03:30:32.070 * RDB: 0 MB of memory used by copy-on-write
2970:M 08 Mar 2020 03:30:32.107 * Background saving terminated with success
2970:M 08 Mar 2020 03:30:32.107 * Synchronization with replica 127.0.0.1:6381 succeeded
cs


Redis 복제 서버 로그

Line 1 - 4 : 마스터서버로 동기화를 시작합니다.

Line 5 - 6 : 부분 혹은 전체 동기화 중 하나를 선택합니다.

Line 7 - 19 : 동기화 작업을 완료합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2975:S 08 Mar 2020 03:30:31.690 * Connecting to MASTER 127.0.0.1:6379
2975:S 08 Mar 2020 03:30:31.690 * MASTER <-> REPLICA sync started
2975:S 08 Mar 2020 03:30:31.690 * Non blocking connect for SYNC fired the event.
2975:S 08 Mar 2020 03:30:31.690 * Master replied to PING, replication can continue...
2975:S 08 Mar 2020 03:30:31.690 * Partial resynchronization not possible (no cached master)
2975:S 08 Mar 2020 03:30:31.690 * Full resync from master: f83b0cbda3649c1accff2ff337ae2d1e8db68609:0
2975:S 08 Mar 2020 03:30:31.776 * MASTER <-> REPLICA sync: receiving 239 bytes from master
2975:S 08 Mar 2020 03:30:31.776 * MASTER <-> REPLICA sync: Flushing old data
2975:S 08 Mar 2020 03:30:31.798 * MASTER <-> REPLICA sync: Loading DB in memory
2975:S 08 Mar 2020 03:30:31.798 * MASTER <-> REPLICA sync: Finished with success
2975:S 08 Mar 2020 03:30:31.798 * Background append only file rewriting started by pid 2987
2975:S 08 Mar 2020 03:30:31.830 * AOF rewrite child asks to stop sending diffs.
2987:C 08 Mar 2020 03:30:31.830 * Parent agreed to stop sending diffs. Finalizing AOF...
2987:C 08 Mar 2020 03:30:31.830 * Concatenating 0.00 MB of AOF diff received from parent.
2987:C 08 Mar 2020 03:30:31.832 * SYNC append only file rewrite performed
2987:C 08 Mar 2020 03:30:31.832 * AOF rewrite: 0 MB of memory used by copy-on-write
2975:S 08 Mar 2020 03:30:31.907 * Background AOF rewrite terminated with success
2975:S 08 Mar 2020 03:30:31.907 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
2975:S 08 Mar 2020 03:30:31.907 * Background AOF rewrite finished successfully
cs



Redis 서버에서 replilcation 정보 확인


모든 파라미터 값을 설명하지는 않습니다.


마스터 서버의 info replication 확인

Line 4 : 마스터 노드를 나타냅니다.

Line 5 : 복제 서버의 수를 나타냅니다.

Line 6, 7 : 복제서버의 IP, PORT 정보를 나타냅니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ redis-cli -6379 
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=154,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=154,lag=1
master_replid:32607ab72ed190a74692498981ac96419f33bca2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154
cs


복제 서버의 info replication 확인

Line 4 : 복제 노드를 나타냅니다.

Line 5, 6 : 마스터의 IP, PORT 를 확인할 수 있습니다.

Line 13 : 복제 서버에 연결된 복제 서버를 나타냅니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ redis-cli -6380
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:196
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:32607ab72ed190a74692498981ac96419f33bca2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ redis-cli -6381
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:11
master_sync_in_progress:0
slave_repl_offset:210
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:32607ab72ed190a74692498981ac96419f33bca2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:210
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:210
cs



복제 구성 시 마스터 서버에서 백업을 받아야만 하는 이유


마스터 서버에서 백업을 받지 않는경우 마스터와 복제 서버에서 데이터를 모두 유실할 수 있는 가능성이 있습니다.

왜 그럴까요?


마스터 서버에서 AOF 나 RDB 파일이 없이 복제서버만 구성 후 운영 중인 상태입니다.

1. 마스터 서버가 다운되었습니다. ( AOF, RDB 파일이 없음 )

2. Failover 가 발생하기 전 마스터 서버가 빠르게 구동되었습니다. AOF 나 RDB 파일이 없기 때문에 데이터가 없는 상태로 구동됩니다.

3. 마스터 서버에서 복제 서버로 동기화가 일어납니다.

4. 복제 서버는 데이터가 없어지게 됩니다.

5. 복제 서버에서 AOF 백업을 받는다고 하지만, rewrite 기능으로 AOF 파일에서도 데이터가 사라지게 됩니다.