Redis 개요
Cache란
- 요청이 올 결과를 미리 저장해두었다가 빠르게 서비스하는 것을 의미
- 일반적인 CPU 메모리 구조에서의 Disk가 아닌 Memory 영역에서 캐싱을 하게 된다.
Cahce 구조
- Look aside Cache
- 클라이언트가 요청한 데이터가 캐시에 있는지 먼저 확인하여 응답
- 캐시에 데이터가 없다면 DB를 조회하고 캐시에 적재한 뒤 응답
- Write Back가
- 서버가 모든 데이터를 캐시에만 저장
- 캐시의 데이터를 일정 주기마다 DB에 모두 반영
- DB에 저장한 데이터를 캐시에서 제거
- 굉장히 많은 쓰기 작업(
INSERT
쿼리)이 있을 때 배치 처리를 하기 위해 사용
Redis란
- Remote Dictionary Server
- In-Memory Data Structure Store
- key-value 형태의 데이터 구조를 제공
Redis 특징
- 빠른 성능
- Redis는 서버의 주 메모리에 데이터를 상주시킨다.
- 디스크 엑세스가 필요 없기 때문에 CPU를 적게 사용하고 간단한 알고리즘으로 데이터에 엑세스할 수 있다.
- 인 메모리 데이터 구조
- 다양한 데이터 유형에 매핑되는 키를 저장할 수 있다.
- 기본 데이터 유형은 String, 최대 데이터 크기는 512MB
- 다양성과 편의성
- 채팅과 메시징 시스템에 매우 적합
- 데이터를 일정 기간이 지난 후 자동 삭제하는 Time To Live 값을 가질 수 있다.
- 싱글 스레드이기에 원자적으로 경합 상태가 없는 일관성 없는 결과를 만들지 않도록 한다.
- 복제 및 지속성
- 마스터 - 슬레이브 아키텍처를 통한 비동기식 복제를 지원
- 특정 시점 스냅샷 기능이나 데이터 변경 시 이를 디스크에 저장하는 Append Only File(AOF) 생성을 지원
- 다양한 개발 언어 지원
- 백 개가 넘는 오픈 소스 클라이언트를 사용 가능
- Java, Python, PHP, C, JavaScript, Ruby, R, Go 등 다수 언어 지원
Redis 사용 사례
- 캐싱
- 빠른 성능으로 엑세스 지연 시간을 줄이고 처리량을 늘릴 수 있다.
- 관계형 DB의 부담을 줄여준다.
- 세션 관리
- 세션 키에 대한 적절한 TTL(Time To Live)과 함께 빠른 키 스토어로 활용 가능
- 실시간 순위표
- Redis Sorted Set 데이터 구조를 사용하면 손쉽게 동적 순위표를 생성할 수 있다.
- ex) 좋아요를 가장 많은 메시지, 선두에 누가 있는지 등
- 속도 제한
- 클라이언트 API 키에 연결된 Redis 카운터를 사용하여 특정 기간 동안 엑세스 요청 수를 세고 한도 초과 시 조치를 취할 수 있다.
- 대기열
- Redis List 데이터 구조를 사용해 영구 대기열을 구현할 수 있다.
- 채팅 및 메시징
- 패턴 매칭과 더불어 PUB/SUB 표준을 지원
- 고성능 채팅방, 실시간 코멘트 스트림, 서버 상호 통신 지원
- 이벤트를 기반으로 작업을 트리거 가능
https://aws.amazon.com/ko/elasticache/what-is-redis/
https://youtu.be/mPB2CZiAkKM