본문으로 건너뛰기
김신건의 로그

[AWS] SQS: managed queue, FIFO, DLQ

· 수정 · 📖 약 1분 · 349자/단어 #aws #sqs #queue #cloud #messaging
SQS, Simple Queue Service, FIFO Queue, Standard Queue, DLQ, visibility timeout, long polling

정의

SQS = AWS 의 완전 관리형 메시지 큐. infinite scale, no provisioning, pay-per-request.

Standard vs FIFO

StandardFIFO
처리량거의 무한300 msg/s (batching 시 3000)
순서best-effortstrict (group 안)
중복at-least-once (드물게 중복)exactly-once (5분 dedup)
가격cheap약간 비쌈

흐름

sequenceDiagram
    P->>SQS: SendMessage
    SQS-->>P: messageId
    C->>SQS: ReceiveMessage (long poll)
    SQS-->>C: message + receiptHandle
    Note over SQS: message *invisible* (visibility timeout)
    C->>C: 처리
    C->>SQS: DeleteMessage(receiptHandle)

Visibility Timeout

flowchart LR
    Recv[Receive] -->|invisible 30s 기본| Hidden[다른 consumer 안 보임]
    Hidden -->|delete 받음| Removed[제거]
    Hidden -->|timeout 만료| Visible[다시 보임 = retry]

처리 시간 + 안전 마진 으로 설정. 짧으면 중복 처리, 길면 재시도 지연.

DLQ (Dead Letter Queue)

RedrivePolicy:
  deadLetterTargetArn: arn:aws:sqs:...:my-dlq
  maxReceiveCount: 5

5회 처리 실패 → DLQ 로. 운영자 검사 후 redrive.

Long Polling

sqs.receive_message(
    QueueUrl=q,
    WaitTimeSeconds=20,   # long poll (max 20s)
    MaxNumberOfMessages=10,
)
  • 메시지 없으면 20초 기다림. 짧은 polling 보다 API call 절감 + latency 낮음.
  • 거의 항상 사용.

SQS + Lambda

EventSourceMapping:
  EventSourceArn: arn:aws:sqs:...
  FunctionName: my-func
  BatchSize: 10
  MaximumBatchingWindowInSeconds: 5

AWS 가 자동으로 batch poll + Lambda 호출 + delete.

FIFO + MessageGroupId

sqs.send_message(
    QueueUrl=q,
    MessageBody='...',
    MessageGroupId='user-42',          # 같은 group = 순서 보장
    MessageDeduplicationId='unique-id'
)
  • 같은 group = 한 consumer 가 순서대로.
  • 다른 group = 병렬.

Standard vs Kafka

SQSKafka
운영0 (managed)
처리량무한 (auto)노드 수 비례
Retention최대 14일무제한 (디스크)
ReplayDLQ 만자유
Fan-outSNS 와 결합consumer group
비용사용량 비례인프라 비례

AWS only + 단순 큐 = SQS. 영속 + 재처리 + 큰 throughput = Kafka (또는 MSK).

흔한 함정

WARNING

  1. At-least-once 중복 = consumer idempotent 필요. idempotency-keys.
  2. Visibility timeout 짧음 = 처리 도중 message 가 다른 consumer 에게 다시. 처리 시간 모니터링.
  3. DLQ 미설정 = 실패 message 영원히 재시도.
  4. FIFO 의 group 부재 = 모든 message 가 한 group 으로 = 단일 consumer = 처리량 30 msg/s.

관련 위키

이 글의 용어 (6개)
[AWS] EventBridge: 이벤트 버스, 스케줄러, partner sourcescloud
정의 EventBridge = AWS 의 통합 event bus. 옛 CloudWatch Events 의 후계. AWS service 이벤트 + custom 이벤트 + SaaS …
[AWS] Lambda: 서버리스 함수, 트리거, 동시성cloud
정의 AWS Lambda = 서버리스 함수 실행. 이벤트 트리거 → 함수 실행 → 결과 / 비동기 처리. 서버 관리 0. 트리거 종류 | 트리거 | 모델 | |---|---| |…
[AWS] SNS: pub-sub fan-outcloud
정의 SNS (Simple Notification Service) = pub-sub fan-out. 한 message → 여러 subscriber 에 동시 배포. 구조 Subsc…
[Distributed] Kafka: 분산 로그, partition, consumer groupdistributed-systems
정의 Apache Kafka = 분산 commit log. 고처리량 (수백만 msg/s), 영속, 수평 확장. event-driven 아키텍처 의 de facto. 핵심 개념: …
[Distributed] Message Broker 비교: Kafka / RabbitMQ / NATS / SQS / Redis Streamsdistributed-systems
결정 트리 6가지 broker 매트릭스 | 항목 | Kafka | RabbitMQ | NATS | SQS | Redis Streams | Google Pub/Sub | |---|…
[Pattern] Idempotency Keys: 중복 요청 안전 처리distributed-systems
정의 Idempotency = 같은 요청을 N번 보내도 결과가 1번과 동일. 분산 시스템 / 결제 / API 의 안전망. [!IMPORTANT] 네트워크는 항상 timeout /…

💬 댓글

사이트 검색 / 명령어

검색

스크롤 = 확대/축소 · 드래그 = 이동 · 0 = 원래 크기 · ESC = 닫기