Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NDD-235]: Redis를 이용한 비디오 URL 해싱 (6h / 5h) #90

Merged
merged 10 commits into from
Nov 23, 2023

Conversation

quiet-honey
Copy link
Collaborator

@quiet-honey quiet-honey commented Nov 22, 2023

NDD-235 Powered by Pull Request Badge

Why

리뷰어님들과의 미팅 중 원래 구현사항이었던 양방향 암호화를 통한 비디오 URL 암호화는 근본적인 해결 방법이 아니고, 그런 부분까지 신경 쓰기보다는 클라이언트 측에서 다운로드를 방지하는 방법이 더 나을 것이라는 리뷰어님의 의견이 나와서 팀 내부 회의 결과 양방향 암호화/복호화 처리를 포기하고, Redis에 (해시값 : 실제 URL)을 저장하여 단방향 해시로 진행하기로 결정되어서 이를 API에 반영하기 위해 진행하였다.

Nest.js에서 Redis를 사용하기 위해 관련 모듈을 찾아보았다.
image
위 사진처럼 ioredis가 가장 많은 다운로드 수를 보여주고 있었고, 실제로 검색해봤을 때도 가장 사용법도 많이 나와서 가장 안정적이고 손쉽게 사용할 수 있겠다는 생각이 들어 이를 사용하였다.

원래 "nestjs redis 적용"이라고 구글에 치면 가장 많이 나오는 @liaoliaots/nestjs-redis를 사용하여 쉽게 redis를 사용해보려고 하였으나, nestjs v10 이상에서는 아직 적용이 불가능한 것으로 판단되어 어쩔 수 없이 ioredis 자체만으로 구현을 해보았다. 그리고 버전 문제를 어떻게든 해결해보려고 시간을 쏟다보니 예상 시간보다 1시간 이상 더 지체되었다.

How

아래와 같은 과정을 통해 구현을 하였다.

기본 설정

  • redis 사용을 위한 ioredis 모듈 추가
  • ioredis 모듈을 사용해 Redis 클라이언트 생성

비디오 토글 시 해시값 저장/삭제

  • 해시 로직 구현
  • Redis에 key:value 저장, 삭제 로직 구현
  • 비디오 토글 시 해시값:원본 URL 저장/삭제 구현

해시값으로 비디오 세부 정보 조회

  • 해시값으로 Redis에 저장되어 있는 비디오 원본 URL 조회

Result

비디오 상태 토글

  • true로 토글
    image

  • false로 토글
    image

해시로 비디오 조회

image

Prize

@liaoliaots/nestjs-redis를 사용하기 위한 1~2시간의 시간 소비 후 특정 모듈에만 의존하고 다른 블로그 등에서 올려놓은 글만 참조하려고 해서는 안된다라는 배움을 얻음
Nest.js에서 Redis의 저장, 삭제, 조회 등 단순한 사용법에 대해 학습 진행

Reference

https://remarkablemark.medium.com/how-to-use-redis-with-node-js-ioredis-368a8b754574

@quiet-honey quiet-honey added BE 백엔드 코드 변경사항 feature 새로운 기능이 추가 된 경우 test 테스트코드가 변경된 경우 labels Nov 22, 2023
@quiet-honey quiet-honey self-assigned this Nov 22, 2023
redisInstance = new Redis(redisUrl);
}
return redisInstance;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문이 중첩되는 경우를 해제하기 위해서

fucntion getRedisInstance() {
  if(redisInstance) return redisInstance;

  const redisUrl: string = process.env.REDIS_URL as string;

  if(redisUrl) {
    redisInstance = new Redis(redisUrl);
    return redisInstance;
  }

  throw new Error('REDIS_URL environment variable is not defined.');
}

이런 로직은 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

cloudflare-workers-and-pages bot commented Nov 22, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 702fe2d
Status: ✅  Deploy successful!
Preview URL: https://7fe72d93.gomterview.pages.dev
Branch Preview URL: https://feature-ndd-235.gomterview.pages.dev

View logs

Copy link
Collaborator

@JangAJang JangAJang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!! Docs도 잊지 말아주세요!!

@quiet-honey quiet-honey merged commit c30e748 into dev Nov 23, 2023
1 check passed
@delete-merged-branch delete-merged-branch bot deleted the feature/NDD-235 branch November 23, 2023 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BE 백엔드 코드 변경사항 feature 새로운 기능이 추가 된 경우 test 테스트코드가 변경된 경우
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants