본문 바로가기
DB

[Redis] Redis 개요

by 물고기고기 2023. 2. 21.

이번 개발에 Redis를 사용할 일이 있어 짧게나마 공부중이다

이전에 몽고디비를 사용해본 적이 있어 딕셔너리 타입의 데이터가 익숙하긴 하다

 

우선 Redis는 Remote Dictionary Server 라고해서

  1. 데이터 타입이 딕셔너리 타입으로
  2. 데이터가 메모리에 저장되는 서버이다. (데이터가 기본적으론 휘발된다)

Redis를 서버에서 어떻게 사용할까 이전에 Redis의 문제점에 대해 알고 사용해야 한다.

 

1. Redis는 싱글스레드이기 때문에 한꺼번에 여러개의 요청이 들어온다면 한개씩 처리한다.

그리고 특정 요청이 너무 많은 시간을 잡아먹거나 해당 요청이 실패하면 그 이후의 요청도 전부 실패하게 된다.

그렇기에 너무 오랜시간이 소요되는 요청은 주의해야한다.

Redis에서 최대 시간복잡도는 O(N)이 걸리는데 이와 같은 요청은 되도록 피해서 개발하는 것이 좋다.

  • KEYS : 패턴에 일치하는 모든 키를 반환
  • FLUSH : 모든 데이터를 삭제
  • GET ALL : 모든 데이터를 조회

2. Redis의 메모리 파편화

 

처음엔 메모리 파편화라는 단어만 보고 메모리가 할당되고 취소되는 과정에서 메모리 중간중간 빈공간이 생기고 그걸 재할당하지않아 생기는 문제인줄 알고 해싱 알고리즘을 사용해서 재할당하면 될텐데 라고 생각했으나,

예를 들어 체이닝같은

메모리의 파편화란게 정확히 뭐냐면

1. 데이터끼리 크기가 일정하지 않다라는 전제에서

2. 중간에 데이터가 삭제되었을때

3. 삭제된만큼 메모리가 빌텐데 데이터끼리 크기가 일정하기 않기때문에 메모리가 빈 정도가 듬성듬성해질때

4. 해당 메모리들이 재할당되지 않고 그대로 낭비되는 경우를 말한다.

 

Q.

Why is memory fragmentation occurring?


A.

Memory fragmentation occurs when the free memory available in a system is broken up into small, non-contiguous chunks, which are too small to be effectively utilized by the application or process. This can happen due to a number of reasons, including:

Dynamic memory allocation: Applications that use dynamic memory allocation can contribute to fragmentation because memory is allocated and deallocated as needed. This can result in a situation where there are small blocks of free memory scattered around that are not large enough to be used by the application.

Inefficient memory management: Inefficient memory management techniques, such as memory leaks, can also cause fragmentation by not releasing memory properly.

Large memory allocations: Large memory allocations can also contribute to fragmentation by breaking up the free memory into smaller chunks that are unusable.

Memory allocation algorithms: The memory allocation algorithm used by the operating system or application can also have an impact on fragmentation. Certain algorithms can result in more fragmentation than others, depending on how they manage free memory.

Memory fragmentation can result in decreased performance and increased memory usage. When an application requests memory, the operating system or memory allocator must search for a large enough block of contiguous memory to allocate to the application. If the available memory is fragmented, it can take longer to find a suitable block of memory, leading to slower performance. Additionally, because memory is not being effectively utilized, the system may end up using more memory than necessary.



- Chat GPT

 

데이터가 실제로 차지하는 메모리가 5기가정도여도 레디스 서버로 인해 메모리가 10기가까지 차지하게 될수도 있단 뜻이다. Redis 서버가 올라간 기기의 메모리보다 Redis 서버가 더 큰 메모리를 요청하게 된다면 당연히 Redis 서버가 제대로 작동하지 않게 되는 것이다.(해당 서버가 꺼지거나 데이터 저장에 실패하거나)

 

이에 대해 여러 해결방법이 있는데

  • 레디스를 버전 업하거나(v.4.0 부턴 메모리 관리 방식이 개선 되었다고 한다)
  • 처음부터 메모리 사이즈가 큰 장비를 사용하거나
  • 인메모리 데이터의 최대 장점! 서버를 자주 껐다 켜는 것이다! (진짜 장점임)

마지막으로 인메모리와 캐시가 뭐가 다른 개념이지? 검색해봐도 설명만 장황하지 어떤 것이라고 이해하긴 힘들고

그게 그거 아닌가 싶어서 Chat GPT에게 물어봤다.

A. 인메모리와 캐시는 관련 개념이지만 완전히 동일한 것은 아니다.

인메모리는 하드 드라이브와 같은 2차 저장 장치가 아닌 컴퓨터의 메인 메모리(RAM)에 저장되는 데이터를 말합니다. 메모리 내 데이터는 디스크의 데이터보다 훨씬 빠르게 액세스할 수 있는데, 이는 데이터가 더 느린 저장 장치에서 읽을 필요 없이 RAM에서 직접 액세스할 수 있기 때문입니다.

반면 캐시는 자주 액세스하는 데이터를 저장하는 하드웨어 또는 소프트웨어 구성요소로, 향후 다시 빠르게 액세스할 수 있도록 합니다. 캐시는 메모리, 디스크 또는 다른 유형의 저장 매체에 구현될 수 있습니다. 캐시의 목적은 원래 데이터 원본보다 액세스 속도가 빠른 위치에 데이터 복사본을 저장하여 데이터 액세스에 걸리는 시간을 줄이는 것입니다.

요약하면 인메모리는 데이터가 저장되는 곳을 의미하는 반면 캐시는 자주 접근하는 데이터에 대한 접근을 최적화하는 메커니즘을 의미한다. 캐시는 메모리에 구현될 수 있지만 디스크나 다른 유형의 스토리지 미디어에도 구현될 수 있습니다.

메모리 = 하드웨어 장치

캐시 = 메커니즘 인셈이다.


설치 사이트 : https://redis.io/download/

 

Download

Redis You can download the last Redis source files here. For additional options, see the Redis downloads section below. Stable (7.0) Redis 7.0 includes several new user-facing features, significant performance optimizations, and many other improvements. It

redis.io

 

GUI : https://redis.io/resources/tools/

 

Tools

Tools for managing and deploying Redis

redis.io

 

참조한 글

 

댓글