비전공자도 이해할 수 있는 Redis 입문/실전
performance-comparison-before-after-redis-spring-boot
🧑🏻
성능 개선을 할 땐 반드시 수치를 측정하면서 비교 해야 한다. 성능 개선을 할 때 ‘느린 것 같아요’, ‘빨라진 것 같아요’라고 얘기하는 건 잘못된 습관이다. 성능 개선을 할 때는 실제로 얼마나 느린지를 측정해야 하고, 개선을 한 뒤에 얼마나 빨라졌는 지를 측정해야 한다. 즉, 정확한 수치로 얘기할 수 있어야 한다.
✅ Redis 적용 전 BoardService
@Service
public class BoardService {
...
// @Cacheable(cacheNames = "getBoards", key = "'boards:page:' + #page + ':size:' + #size", cacheManager = "boardCacheManager")
public List<Board> getBoards(int page, int size) {
Pageable pageable = PageRequest.of(page - 1, size);
Page<Board> pageOfBoards = boardRepository.findAllByOrderByCreatedAtDesc(pageable);
return pageOfBoards.getContent();
}
}캐싱을 적용시키는 어노테이션을 주석 처리한 뒤 Postman으로 테스트를 해보자.
여러번 요청을 보내보니 평균적으로 200ms 정도의 속도가 나오는 걸 확인할 수 있다.
(개인이 가지고 있는 컴퓨터의 성능에 따라 수치는 다를 수 있다.)
✅ Redis 적용 후 @Service
public class BoardService {
...
@Cacheable(cacheNames = "getBoards", key = "'boards:page:' + #page + ':size:' + #size", cacheManager = "boardCacheManager")
public List<Board> getBoards(int page, int size) {
Pageable pageable = PageRequest.of(page - 1, size);
Page<Board> pageOfBoards = boardRepository.findAllByOrderByCreatedAtDesc(pageable);
return pageOfBoards.getContent();
}
}주석 처리를 해제한 뒤에 다시 한 번 테스트를 해보자.
여러번 요청을 보내보니 평균적으로 20ms 정도의 속도가 나오는 걸 확인할 수 있다. 10배 정도 속도가 향상됐다.