다양한 캐시 패턴이 있다.
데이터베이스가 원본 데이터 소스라고 가정하고 설명하겠다.
write-back, write-behind
participant Application
participant Cache
database Database
Application -[#0a0]> Cache: write 요청
note over Cache
저장
end note
Application <[#0a0]- Cache: 성공 응답
note over Cache, Database
비동기
end note
Cache -[#0a0]> Database: write 요청
note over Database
저장
end note
Cache <[#0a0]- Database: 성공 응답
Application -[#00a]> Cache: read 요청
Application <[#00a]- Cache: 캐시 데이터 반환
write-through
일반적으로 다음과 같은 구조를 write-through 패턴이라고 부름
캐시가 데이터 소스에 직접 write할 수 있어야 가능한 구조임
ex) Apache HTTPd proxy를 cache로 사용하는 경우에 이렇게 동작함
participant Application
participant Cache
database Database
Application -[#0a0]> Cache: write 요청
note over Cache
저장
end note
Cache -[#0a0]> Database: write 요청
note over Database
저장
end note
Cache <[#0a0]- Database: 성공 응답
Application <[#0a0]- Cache: 성공 응답
그러나 다음과 같은 구조를 write-through라고 부르기도 함.
여기서 캐시는 또 다른 (그리고 더 빠른) 데이터 저장소에 불과함
participant Application
participant Cache
database Database
Application -[#0a0]> Database: write 요청
note over Database
저장
end note
Application <[#0a0]- Database: 성공 응답
Application -[#0a0]> Cache: write 요청
note over Cache
저장
end note
Application <[#0a0]- Cache: 성공 응답
Application -[#00a]> Cache: read 요청
Application <[#00a]- Cache: 캐시 데이터 반환
이 두 가지 패턴의 공통점은 데이터가 데이터 소스와 캐시에 저장되는 것이 보장된다는 점임. 그리고 이 저장은 동기적으로 이뤄져야 일관성이 보장됨. 그렇기 때문에 write-back과의 차별성이 생김.
write-around
participant Application
participant Cache
database Database
Application -[#0a0]> Database: write 요청
note over Database
저장
end note
Application <[#0a0]- Database: 성공 응답
Application -[#00a]> Cache: read 요청
Application <[#00a]- Cache: 캐시 데이터가 있으면 반환
Application -[#00a]> Database: 캐시 데이터가 없으면 read 요청
Application <[#00a]- Database: 데이터 반환
Application -[#0a0]> Cache: 캐시에 데이터 write 요청
note over Cache
저장
end note
Application <[#0a0]- Cache: 성공 응답
데이터 소스와 캐시를 동시에 사용하는 것임.
쓰기 작업이 캐시를 우회함으로써 쓰기 부담을 줄이고 읽기 빈도가 높은 데이터에만 캐싱을 적용하는 패턴임. 읽기 빈도가 낮은 데이터는 캐싱의 필요성을 줄임.
cache-aside (lazy loading)
participant Application
participant Cache
database Database
Application -[#00a]> Cache: read 요청
Application <[#00a]- Cache: 캐시 데이터가 있으면 반환
Application -[#00a]> Database: 캐시 데이터가 없으면 read 요청
Application <[#00a]- Database: 데이터 반환
Application -[#0a0]> Cache: 캐시에 데이터 write 요청
note over Cache
저장
end note
Application <[#0a0]- Cache: 성공 응답
읽기 요청이 있을 때 캐시를 확인하고 캐시에 없으면 데이터 소스에서 데이터를 가져와 캐시에 저장하는 방식임.
read-through
participant Application
participant Cache
database Database
Application -[#00a]> Cache: read 요청
Application <[#00a]- Cache: 캐시 데이터 반환
Cache -[#00a]> Database: read 요청
Cache <[#00a]- Database: 데이터 반환
note over Cache
저장
end note
읽기 요청 시, 캐시를 먼저 확인하고 없으면 캐시가 데이터 소스에서 데이터를 가져와 캐시에 저장한 후 반환하는 방식임