여러 아키텍처를 보면 SNS + SQS 조합으로 사용되는 곳이 많다
왜 이렇게 사용하지??
SNS만 사용해도 메시지 유실 없이 잘 사용하고 있는데… ← 진짠가??????
(여기에서는 순서 보장에 대해서는 다루지 않음)
1️⃣ 현재 시스템에서 정말 메시지 유실 없나??
SNS metric 중에 NumberOfNotificationsPublished, NumberOfNotificationsDelivered를 모니터링 해봤는데 다행히 아직까지는 메시지 유실이 없었다
휴…
2️⃣ 소비자가 메시지를 받지 못하는 상태이면!?
SNS retry policy에 의해 재시도 하지만 그래도 실패나면 메시지는 discard됨
만약 SNS > SQS > 소비자 구조라면 SQS에서 DLQ를 설정 가능함
이런 이유로 SNS + SQS(with DLQ) 조합을 사용할 수 있을듯
*참고 : https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html
3️⃣ 소비자에서 에러가 발생하면!? - Lambda case
SNS는 메시지를 async로 Lambda에게 보냄 ← Fanout
결국 SNS + Lambda 조합에서 메시지가 delivered to Lambda 상태라면 Lambda의 async configuration의 retry policy를 따름
방금 Lambda의 async configuration console을 확인해보니… Lambda에도 DLQ 정책이 있다 굳!!
*참고 : https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html
4️⃣ SNS has a no batch function
Lambda에서 SQS 데이터를 가져올 때는 batch가 가능하지만 SNS는 batch를 지원하지 않는다
batch를 이용해 메시지를 reduce하거나 등의 aggregation 작업이 필요하다면 SQS를 이용해야함
*참고 : https://www.rahulpnath.com/blog/amazon-sns-to-lambda-or-sns-sqs-lambda-dotnet
5️⃣ 소비자에도 limit이 있다 - Lambda case
1) SNS + Lambda 조합 대용량 메시지 처리 테스트 결과
10 messages: all successfully processed.
100 messages: all successfully processed.
1,000 messages: only ~90% of messages successfully processed. Remaining messages were sent straight to the Lambda’s Dead Letter Queue.
2) SNS + SQS + Lambda 조합 대용량 메시지 처리 테스트 결과
10 messages: all successfully processed
100 messages: all successfully processed
1,000 messages: all successfully processed
10,000 messages: all successfully processed (took ~30 seconds)
100,000 messages: all successfully processed (took ~90 seconds)
1,000,000 messages: 99.99% successfully processed (took ~35 minutes)
1) 2) 비교…는 그냥 SNS + SQS 조합의 압승!!
저자도 설명하고 있지만 1)과 같은 현상은 Lambda 동시성 최대치가 1000이기에 1000개 메시지부터는 실패하는 메시지가 조금씩 생긴다
*참고 : https://medium.com/@chrislewisdev/load-testing-sns-lambda-in-aws-6bb0bbc90999
😎 결론
소비자가 Lambda인 경우에는 여러가지 선택지가 있는것 같다
트래픽이 많지 않다면 SNS + Lambda 조합으로도 충분히 커버 가능하고, 몰랐지만 Lambda 자체적으로도 DLQ가 있기에 후처리 로직으로 유실 없이 메시지를 소화 가능할 것 같다
하지만 트래픽이 많다면 얘기를 달라짐!!!
무조건 SNS + SQS 조합이 좋다
추가로 트래픽 조건이 아닌 SNS에는 batch 기능이 없다와 같은 제약 조건으로 SNS + SQS 조합을 사용해야할 수도 있다