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

[#184] RefreshToken DB -> Redis 마이그레이션 작업 #189

Merged
merged 4 commits into from
Nov 22, 2023

Conversation

Hchanghyeon
Copy link
Member

👨‍💻 작업 사항

📑 PR 개요

  • DB에 저장하던 RefreshToken을 Redis로 마이그레이션 한다.
  • 마이그레이션 필요성
    • 클라이언트가 RefreshToken이 만료되었을 때 로그아웃 API를 서버로 날리지 못하기 때문에 DB에 계속 무한정으로 남아있는 현상
      • Redis의 TTL 기능으로 클라이언트가 RefreshToken이 만료되었을 때 자동으로 Redis에서 삭제 가능
    • 로컬 캐시로도 가능하지만 Prod 환경에서 2대의 서버가 돌아가기 때문에 정합성 일치 문제로 사용 불가능하여 별도의 Redis 서버를 구축

✅ 작업 목록

  • build.gradle에 Redis 의존성 추가
  • CacheConfig에 Redis 연결 설정 추가
  • RefreshToken Redis에 저장하는 기능 구현
  • 테스트 코드 수정 및 사용하지 않는 코드 삭제

🙏 리뷰어에게

  • 환경 별 Redis 서버 사용

    • prod: AWS ElasticCache
    • dev, local, test: NAS Docker Redis
  • 회원 로그인 관련된 테스트 코드를 실행할 때마다 Redis에 RegisterToken이 생성되는데 TTL로 설정되어있어서 크게 무리가 가지는 않을 것 같습니다!


테이블 명

  • refresh_token 테이블 삭제 예정

API endpoint

  • POST /members
  • GET /auth/login
  • POST /auth/refresh
  • DELETE /auth/logout

Prefix

PR 코멘트를 작성할 때 항상 Prefix를 붙여주세요.

  • P1: 꼭 반영해주세요 (Request changes)
  • P2: 적극적으로 고려해주세요 (Request changes)
  • P3: 웬만하면 반영해 주세요 (Comment)
  • P4: 반영해도 좋고 넘어가도 좋습니다 (Approve)
  • P5: 그냥 사소한 의견입니다 (Approve)

@Hchanghyeon Hchanghyeon added 신규 기능 [feat] 새로운 기능을 추가한다. 리팩토링 [refactor] 실행 결과는 동일하고, 코드 구조만 변경한다. labels Nov 22, 2023
@Hchanghyeon Hchanghyeon added this to the 5차 스프린트 milestone Nov 22, 2023
@Hchanghyeon Hchanghyeon requested a review from a team November 22, 2023 16:03
@Hchanghyeon Hchanghyeon self-assigned this Nov 22, 2023
@Hchanghyeon Hchanghyeon requested review from kylekim2123 and jay-so and removed request for a team November 22, 2023 16:03
@Hchanghyeon Hchanghyeon linked an issue Nov 22, 2023 that may be closed by this pull request
4 tasks
import org.springframework.stereotype.Repository;

@Repository
public class RedisRepository {
Copy link
Member Author

Choose a reason for hiding this comment

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

별도의 RedisRepository를 만들어서 어디서든지 공통으로 사용할 수 있도록 구현했습니다.

Comment on lines +71 to +80
final PolymorphicTypeValidator typeValidator = BasicPolymorphicTypeValidator.builder()
.allowIfBaseType(Object.class)
.build();

final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.activateDefaultTyping(typeValidator, ObjectMapper.DefaultTyping.NON_FINAL);

final GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(
objectMapper);
Copy link
Member Author

Choose a reason for hiding this comment

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

Redis에 LocalDateTime의 형태를 저장할 때 직렬화, 역직렬화하게 되면 오류가 발생합니다.
위와 같이 ObjectMapper에 JavaTimeModule을 추가하면 문제 없이 사용할 수 있습니다.

@Hchanghyeon Hchanghyeon changed the title [회원] RefreshToken DB -> Redis 마이그레이션 작업 [#184] RefreshToken DB -> Redis 마이그레이션 작업 Nov 22, 2023
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

72.7% 72.7% Coverage
0.0% 0.0% Duplication

@Hchanghyeon Hchanghyeon marked this pull request as ready for review November 22, 2023 17:15
@Hchanghyeon Hchanghyeon merged commit ccd1913 into dev Nov 22, 2023
@Hchanghyeon Hchanghyeon deleted the feature/#184 branch November 22, 2023 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
리팩토링 [refactor] 실행 결과는 동일하고, 코드 구조만 변경한다. 신규 기능 [feat] 새로운 기능을 추가한다.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[회원] RefreshToken DB -> Redis 마이그레이션 작업
2 participants