Installationdocker로 Redis 실행docker로 redis.conf 옵션 설정 방법Redis ClusterRedis with sentinelRedis Database(namespace) 구분Redis 자료구조Sorted SetRedis 운용Redis TransactionRedis clicommandsBackupredis-server cliloglevelPub-SubTroubleShooting
Installation
- Centos redis-cli 설치 :
sudo yum install redis
- aws ec2 kernel 6 : sudo yum install redis6 -y
docker로 Redis 실행
sudo docker run \ > -p 6379:6379 \ > -v $PWD/data:/data \ > --name redis \ > -d redis:3.2 redis-server --appendonly yes --requirepass "__@picker-redis"
- 비밀번호 설정하면 기본 유저 이름은
default
임
docker로 redis.conf 옵션 설정 방법
docker run redis:alpine3.17 redis-server --tls-port 6379 ... # 위와 같이 redis-server 뒤에 옵션 값들을 줄줄이 붙여주면 됨 # 혹은 redis.conf 파일 작성해서 volume으로 공유 후 # redis-server /usr/local/etc/redis/redis.conf conf 파일을 명시해주기
redis 서버 tls 로 실행시키기
[ Redis docs ] TLS
[ Redis Docs ] Redis Configuration, Redis Configuration Example
[ Redis Docs ] Create certificates
[ Github ] Redis with TLS Dockerfile
- create certificate 에서 인증서 만들고
- TLS와 Redis Configuration 참고해서 해당 .cert, .key 파일들 명시해서 redis-server 실행시키고
- 만약 클라이언트가 cert 파일 없이 접속하게 하고 싶으면
tls-auth-clients no
옵션 설정해주면 될듯함
Redis Cluster
[ Redis Docs ] Scaling with Redis Cluster
- 데이터셋을 여러 노드로 자동 분할
Redis with sentinel
[ Redis Docs ] High availability with Redis sentinel
Redis Database(namespace) 구분
redis-cli -n 1 // databse 1 번으로 접속. default는 0
redis: host: localhost # window server port: 6379 database: 1 password: xxxxx
@Configuration public class RedisConfig { @Value("${spring.redis.password}") private String redisPassword; @Value("${spring.redis.database}") private int index; @Bean public RedisConnectionFactory redisConnectionFactory(RedisProperties redisProperties) { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration( redisProperties.getHost(), redisProperties.getPort()); configuration.setPassword(redisPassword); configuration.setDatabase(index); return new LettuceConnectionFactory(configuration); } }
Redis 자료구조
[ Blog ] Redis 자료구조 알아보기
Sorted Set
- 운용 사례 : Set 안에 element들을 expire하고 싶을 때, SortedSet으로 해서 timestamp를 score로 주도록 element를 추가하면 주기적으로 오래된 애들 다 지워버릴 수 있음
Redis 운용
Redis Transaction
[ eBook ] Redis in action [2013.6.17] (Locks with timeouts)
[ Redis ] Transactions
- Transaction으로 묶여 있는 명령들은 직렬화되어서 순차적으로 실행됨. 다른 클라이언트에 의해 들어오는 요청은 Transaction 수행 중에는 응답되지 않음 → command가 single isolated operation으로 수행되는 것을 보장
Redis cli
[ Redis 공식문서 ] redis-cli docs
commands
# set에 값 추가 (해당 키가 없으면 해당 키에 집합 만들고 member 추가함) sadd <key> <member> [member ... ] # hash에 필드 추가 hset <key> <field> <value> [<field> <value> ... ] hsetnx # Sets field in the hash stored at key to value , only if field does not yet exist flushdb # current selected database의 key를 지움 flushall # 모든 DB의 key를 지움 # key를 패턴으로 지우기 redis-cli KEYS "prefix:*" | xargs redis-cli DEL # redis cli 로 auth 바로 제공하면서 명령어 실행 # 이때, 특수문자(!@#) 이용시 따옴표 제거 후 백슬래쉬 넣고 실행하면 됨 redis-cli -a techtech1\!\@ PING
- redis-cli INFO : 레디스 서버 정보 확인
redis-cli info clients # Clients connected_clients:27 cluster_connections:0 maxclients:10000 client_recent_max_input_buffer:20480 client_recent_max_output_buffer:20504 blocked_clients:0 tracking_clients:0 pubsub_clients:0 watching_clients:0 clients_in_timeout_table:0 total_watched_keys:0 total_blocking_keys:0 total_blocking_keys_on_nokey:0
Backup
SAVE
: 스냅샷을 동기적으로 처리함 → 스냅샷 만드는 중간에는 연산 수행 불가
BGSAVE
: 스냅샷을 비동기적으로 처리
redis-cli config get dir
: 위의 명령어들을 통해서 생성되는 백업 파일의 저장 위치 정보 알 수 있는 명령어
redis-server cli
loglevel
# This can be one of: # debug (a lot of information, useful for development/testing) # verbose (many rarely useful info, but not a mess like the debug level) # notice (moderately verbose, what you want in production probably) # warning (only very important / critical messages are logged) # nothing (nothing is logged) loglevel notice
- Command 의 로그를 보기위해서… 어떻게 해야 할까 ⇒ MONITOR
- log 로는 Connection에 대한 정보만 거의 나옴
- 실제 커맨드 history 보기 위해서는 MONITOR command 로 확인가능
Pub-Sub
[ Redis Docs ] Redis Pub/Sub

# redis cli commands pubsub channels # 활성화된 채널 리스트 pubsub numsub <channelName> # 채널을 구독중인 클라이언트 수 확인