단일 서버
사용자의 요청이 처리되는 과정
- 도메인 이름을 이용하여 웹사이트에 접속
- DNS(Domain Name Servie)에서 IP 주소로 변환
- IP 주소로 HTTP 요청이 전달
- 웹 서버가 HTML/JSON 형태의 응답을 반환
데이터베이스
RDB
NoSQL
- key-value store
- graph store
- collumn stroe
- document store
NoSQL이 적합한 경우
- 아주 낮은 응답 latency가 요구됨
- 다루는 데이터가 비정형
- 데이터를 직렬화/역직렬화만 하면 된다
- 아주 많은 양의 데이터를 저장
수직적 규모 확장(scale up) vs 수평적 규모 확장(scale out)
scale up
트래픽 양이 적을 경우 수직적 확장, 단순함
단점
- 한계가 있다 (무한대로 증설 불가)
- 장애에 대한 자동복구/다중화 방안 제시x
- 장애 발생 시 서비스 중단
로드 밸런서
부하 분산 집합에 속한 웹 서버들에게 트래픽 부하를 고르게 분산하는 역할
- 사용자: 로드밸런서의 공개 IP 주소로 접속
- 서버 간 통신에 사설 IP wnth dldyd
장점
- 장애 자동복구 해소, 가용성 향상
- 웹 사이트 전체 다운x
- 트래픽이 증가할 때 유연하게 대처
데이터베이스 다중화
서버 사이에 master-slave 관계 설정, 데이터 원본: 주 서버에, 사본: 부 서버에 저장
대부분의 애플리케이션은 읽기 연산의 비중이 쓰기 연산보다 높다
이점
- 더 나은 성능: 병렬로 처리될 수 있는 질의의 수가 늘어난다
- 안정성: 재해 복구 가능 - 데이터를 여러 장소에 다중화
- 가용성
장애 발생 상황
- 부 서버 다운
- 읽기 연산이 주 서버로 전달
- 새로운 부 서버가 생성 후 부 서버를 대체
- 부 서버가 여러 대인 경우 부 서버로 연산이 분산
- 주 서버 다운
- 부 서버가 주 서버가 됨
- 새로운 부 서버가 추가
- production 환경
- 부 서버에 보관된 데이터가 최신 상태가 아닐 수 있음: 복구 스크립트
- 다중 마스터/원형 다중화로 대처 가능
캐시
값비싼 연산 결과, 자주 참조되는 데이터를 메모리 안에 두고, 뒤 이은 요청이 보다 빨리 처리될 수 있도록 하는 저장소
캐시 계층
데이터가 잠시 보관되는 곳, 데이터베이스보다 빠르다
캐시 사용 시 유의할 점
- 캐시는 어떤 상황에 바람직한가?
- 데이터 갱신은 자주 일어나지 않지만 참조는 빈번하게 일어나는 경우
- 어떤 데이터를 캐시에 두어야 하는가?
- 영속적으로 보관할 데이터는 x
- 캐시에 보관된 데이터는 어떻게 만료되는가?
- 만료 기한을 적절히 정할 것
- 일관성은 어떻게 유지되는가?
- 일관성: 데이터 저장소의 원본과 캐시 내의 사본이 같은지 여부
- Scaling Memcache at Facebook
- 장애 대처?
- 캐시 서버: 단일 장애 지점 → 여러 지역에 걸쳐 캐시 서버를 분산시켜야 한다
- 캐시 메모리
- 너무 작을 경우: eviction → 캐시 성능이 떨어짐
- overprovision
- 데이터 방출(eviction) 정책
- 캐시가 꽉 찬 후에 데이터를 넣어야 할 경우 → LRU(Least Recently Used), LFU(Least Frequently Used), FIFO(First In First Out)
콘텐츠 전송 네트워크 (CDN)
CDN(Content Delivery Network): 정적 콘텐츠를 전송하는 데 쓰이는, 지리적으로 분산된 서버의 네트워크
사용자에게 가장 가까운 CDN 서버가 정적 콘텐츠 전달
CDN 사용 시 고려해야 할 사항
- 비용
- 데이터 전송 양에 따라 요금을 낸다
- 적절한 만료 시한 설정
- time-sensitive 콘텐츠: 만료 시점을 잘 정해야 한다
- CDN 장애에 대한 대처 방안
- 문제가 생기면 원본 서버로부터 직접 콘텐츠를 가져온다
- 콘텐츠 무효화 방법
- CDN 서비스 사업자가 제공하는 API를 이용하여 무효화
- 콘텐츠의 다른 버전을 서비스하도록 오브젝트 버저닝 이용
무상태(stateless) 웹 계층
상태 정보를 웹 계층에서 제거
- 관계형 데이터베이스/NoSQL 같은 지속성 젓장소에 보관하고 필요할 때 가져온다
상태 정보 의존적인 아키텍처
문제: 같은 클라이언트로부터의 요청은 항상 같은 서버로 전송되어야 한다
- 대부분의 로드밸런서: sticky session 기능 제공 - 로드밸런서에 부담
무상태 아키텍처
상태 정보가 필요할 경우 공유 저장소로부터 데이터를 가져옴
데이터 센터
두 개의 데이터 센터를 이용하는 사례
사용자는 가장 가까운 데이터 센터로 안내 - 지리적 라우팅
데이터 센터 중 하나에 장애 → 장애가 없는 데이터 센터로 전송
다중 데이터센터 아키텍처를 만들 때의 고려 사항
- 트래픽 우회
- 올바른 데이터 센터로 트래픽을 보내는 효과적인 방법을 찾아야 한다
- ex) GeoDNS: 사용자에게서 가장 가까운 데이터센터로 트래픽을 보낸다
- 데이터 동기화
- 데이터를 여러 데이터 센터에 걸쳐 다중화
- 넷플릭스: https://netflixtechblog.com/active-active-for-multi-regional-resiliency-c47719f6685b
- 테스트와 배포
- 웹 사이트/애플리케이션을 여러 위치에서 테스트
- 자동화된 배포 도구
메시지 큐
메시지의 무손실을 보장하는 비동기 통신을 지원하는 컴포넌트
장점: 서비스/서버 간 결합이 느슨해져 규모 확장성이 보장되어야 하는 안정적 애플리케이션 구성에 좋다
로그, 메트릭 그리고 자동화
로그
에러 로그 모니터링
로그를 단일 서비스로 모아주는 도구 활용
메트릭
사업 현황에 관한 유용한 정보를 얻는다
유용한 메트릭
- 호스트 단위 메트릭: CPU, 메모리, 디스크 I/O
- 종합 메트릭: 데이터베이스 계층의 성능, 캐시 계층의 성능
- 핵심 비즈니스 메트릭: 일별 능동 사용자, 수익, 재방문
자동화
- CI(continuous integration) 도구
- 빌드, 테스트, 배포 등의 절차 자동화
- 메시지 큐 도입: 컴포넌트가 보다 느슨히 결합
- 로그, 모니터링, 메트릭, 자동화 도구 추가
데이터베이스 규모 확장
수직적 확장
기존 서버에 공성능의 자원 증설
약점
- 하드웨어에 한계가 있다
- SPOF(Single Point of Failure)로 인한 위험성
- 비용이 많이 든다
수평적 확장
= 샤딩(sharding)
더 많은 서버를 추가
샤딩: 대규모 데이터베이스를 샤드라고 부르는 작은 단위로 분할
모든 샤드는 같은 스키마를 쓰지만, 샤드에 보관되는 데이터 사이에는 중복이 없다
샤딩 전략: 샤딩 키
- 데이터를 고르게 분할할 수 있도록 하는 것이 중요
샤딩 도입 문제점
- 데이터의 재 샤딩
- 데이터가 많아져서 하나의 샤드로는 감당하기 어렵다
- 샤드 간 데이터 분포가 균등하지 못하다 (샤드 소진이 빨리 일어난다)
- 유명인사 문제: 핫스팟 키 문제, 특정 샤드에 질의가 집중되어 서버에 과부하
- 조인과 비정규화: 여러 샤드에 걸친 데이터를 조인하기 힘들어진다
백만 사용자, 그리고 그 이상
요약
- 웹 계층은 무상태 계층으로
- 모든 계층에 다중화 도입
- 가능한 한 많은 데이터를 캐시할 것
- 여러 데이터 센터를 지원할 것
- 정적 콘텐츠는 CDN을 통해 서비스할 것
- 데이터 계층은 샤딩을 통해 그 규모를 확장할 것
- 각 계층은 독립적 서비스로 분할할 것
- 시스템을 지속적으로 모니터링하고, 자동화 도구들을 활용할 것
'System Design Interview' 카테고리의 다른 글
[대규모 시스템 설계 기초] 2장 - 개략적인 규모 추정, 3장 - 효과적 면접을 위한 4단계 접근법 (1) | 2023.12.10 |
---|