-
캐시 전략 (Cache Aside, Write-through, Write-back) 에 대해 알아보자!Server 2025. 4. 18. 17:27728x90반응형
캐시 전략 (Cache Strategies)
1. Cache Aside (Lazy Load)
설명: Cache Aside 전략은 애플리케이션이 데이터를 캐시에 직접 관리하는 방식입니다. 캐시가 필요한 데이터를 먼저 데이터베이스에서 읽고, 그 데이터를 캐시에 저장하는 방식입니다.
- 장점: 캐시 미스 시 실제 데이터베이스에서 데이터를 로드하므로 데이터 일관성이 보장됩니다.
- 단점: 캐시가 비어 있을 때 첫 번째 요청은 성능상 불리할 수 있습니다.
2. Write-through 캐시
설명: Write-through 캐시는 데이터를 먼저 캐시에 쓰고, 그 후에 데이터베이스에 기록하는 전략입니다. 모든 쓰기 작업은 캐시와 데이터베이스에 동시에 적용됩니다.
- 장점: 데이터 일관성을 유지할 수 있습니다.
- 단점: 모든 쓰기 작업이 캐시와 데이터베이스에 동시에 발생하여 성능이 저하될 수 있습니다.
3. Write-back 캐시
설명: Write-back 캐시는 데이터를 먼저 캐시에 쓰고, 일정 시간이 지나거나 데이터가 만료되면 캐시에서 데이터베이스로 기록하는 방식입니다.
- 장점: 데이터베이스에 대한 쓰기 작업을 줄여 성능을 향상시킬 수 있습니다.
- 단점: 데이터 일관성 문제 발생 가능성 및 캐시 미스가 발생할 수 있습니다.
1. Cache Aside (Lazy Load)
설명: Cache Aside 전략은 애플리케이션이 데이터를 캐시에 직접 관리하는 방식입니다. 캐시가 비어 있으면 데이터베이스에서 데이터를 로드하여 캐시에 저장하고, 이후의 요청은 캐시에서 데이터를 가져옵니다.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.EnableCaching; import org.springframework.stereotype.Service; @Service @EnableCaching public class ProductService { @Autowired private ProductRepository productRepository; @Cacheable(value = "products", key = "#id") public Product getProductById(Long id) { // 데이터베이스에서 데이터를 읽어옵니다. return productRepository.findById(id).orElseThrow(() -> new RuntimeException("Product not found")); } }
2. Write-through 캐시
설명: Write-through 전략은 데이터를 캐시와 데이터베이스에 동시에 기록하는 방식입니다. 모든 쓰기 작업은 캐시와 데이터베이스에 동시에 적용됩니다.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.EnableCaching; import org.springframework.stereotype.Service; @Service @EnableCaching public class ProductService { @Autowired private ProductRepository productRepository; @CachePut(value = "products", key = "#product.id") public Product updateProduct(Product product) { // 데이터베이스에 데이터를 업데이트하고, 동시에 캐시에도 업데이트합니다. return productRepository.save(product); } }
3. Write-back 캐시
설명: Write-back 전략은 데이터를 캐시에서만 수정하고, 일정 시간이 지난 후에 캐시에서 데이터베이스로 데이터를 쓰는 방식입니다.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service @EnableCaching public class ProductService { @Autowired private ProductRepository productRepository; @Cacheable(value = "products", key = "#id") public Product getProductById(Long id) { // 데이터베이스에서 데이터를 읽어옵니다. return productRepository.findById(id).orElseThrow(() -> new RuntimeException("Product not found")); } @CacheEvict(value = "products", key = "#product.id") public void updateProduct(Product product) { // 데이터베이스에 쓰지 않고 캐시에서만 업데이트 productRepository.save(product); } }
728x90반응형'Server' 카테고리의 다른 글
JVM과 Node.js 런타임 환경 비교 (1) 2025.04.28 java 어플리케이션에서 시스템 명령어 사용하려면? (0) 2025.04.23 API Gateway의 역할과 장단점을 알아보자 (0) 2025.04.18 모놀리식 아키텍처 vs 마이크로 서비스 비교 (0) 2025.04.18 웹서버? WAS 서버? 에 대해 알아보자 (1) 2025.04.18