Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
구현내용
1. 포스트 좋아요/취소
Likes 테이블 생성했습니다.
post_id, user_id, is_deleted 등이 column으로 있습니다.
특정 유저가 특정 포스트에 좋아요와 취소를 반복할 때, DB에 여러 row가 생겼다가 없어지는 것이 싫어서 is_deleted를 추가했습니다.
각 상황별 동작은 아래와 같습니다.
2. 포스트 조회 시 좋아요 개수 추가
포스트 조회 시 좋아요 개수도 함께 조회되도록 했습니다.
좋아요 개수는 일단 likes 테이블에서 select count(*)하여 가져옵니다. 후에 post 테이블의 like_count를 만들어 like_count만으로 가져올 생각입니다.
현재 구현되어 있는 방식은 아래와 같습니다.
2-1. 캐싱되어 있다면 그대로 리턴
2-2. 캐싱되어 있지않다면 DB에서 select count(*) 하여 조회 후 PostLikeCountCacheMissEvent 발생시켜 Redis에 해당 post의 좋아요 개수 추가
3. 포스트 조회 시 현재 로그인한 유저가 좋아요 눌렀는지 여부 추가
좋아요 눌렀는지 여부를
"likedByCurrentUser": false
와 같이 내려줬습니다.고민사항
1. 좋아요 개수 조회시 캐시miss 났을 때, DB 접근 횟수
현재 좋아요 개수 조회시 Redis Cache miss가 났을 때, post 여러개 조회중이었다면 DB에 여러번 접근합니다.
Cache miss가 난 포스트들을 모아서 한번에 DB에 접근하는게 더 나을지 고민입니다.
2. 좋아요 개수 post의 like_count
좋아요 개수는 현재 likes 테이블에서 select count(*)로 조회되고 있지만, post테이블에 like_count를 만들어서 개수를 세도록 변경할 예정입니다.
이때 좋아요 개수가 하나 변할때마다 DB에 접근하지 않고 모아서 몇초마다 한번 씩 반영하는 식으로 만들려고 합니다.
저번에 ConcurrentHashMap을 이야기해주셨는데, 서버가 여러대인 환경에서 같은 post의 좋아요도 각서버별로 저장했다가 여러번 DB에 반영하게 되는 것은 아닐지 고민입니다.
이 부분도 Redis를 사용해서 모든 서버가 하나의 Redis에 좋아요 개수를 관리하고 한번에 업데이트할까 고민했는데, 그러면 서버가 여러대인 경우에 어떤 서버가 좋아요 개수를 DB에 업데이트해야하는지도 잘 모르겠어서 고민입니다.