
Message Queue 관련 용어
Message
- 애플리케이션에서 다른 애플리케이션이 이용할 수 있도록 생성하는 데이터 패킷 [IBM]
- 발신자와 수신자 애플리케이션 간에 전송되는 데이터 [CloudAMQP]
Queue
- FIFO(First In First Out, 선입선출)의 특징을 가지는 자료구조
Message Broker
- 메시지 큐가 정의되어 있는 메시지 지향 미들웨어(MOM)의 소프트웨어 모듈 [IBM]
- 메시지 큐에서 더 확장된 기능(라우팅, 필터링 등)을 가짐
- 많은 곳에서 Message Queue와 특별한 구분 없이 사용
메시지 지향 미들웨어 (Message Oriented Middleware)
메시지 큐를 알아보기 전에 메시지 지향 미들웨어(MOM)를 알아보자.
미들웨어 : 운영체제와 응용 프로그램 사이, 또는 서로 다른 응용 프로그램이 서로 통신하는데 사용되는 소프트웨어

MOM 시스템은 메시지를 기반으로 한 미들웨어이며, 비동기 메시지를 통해 두 소프트웨어의 통신을 중개한다. MOM 시스템의 기본 요소는 클라이언트, 메시지, MOM 공급자이다.
특징
- 비동기 방식으로 메시지 전달
- 메시지 발행하는 Producer와 소비하는 Consumer로 구성
장점
- 지속성 : 메시지의 백업을 유지
- 라우팅 : 미들웨어 계층이 직접 메시지 라우팅 가능. 하나의 메시지를 여러 수신자에게 배포 가능(브로드캐스트/멀티캐스트)
- 변환 : 송신 측과 수신 측의 요구에 따라 메시지의 변환이 가능. 라우팅, 브로드캐스트/멀티캐스트와 같이 사용한다면 메시지를 자신의 형식으로 발송하고 여러 개의 수신 응용 프로그램 고유의 형식으로 수신하는 것도 가능함.
단점
- 아키텍처에 추가 구성 요소(메시지 전송 에이전트)가 필요함.
- 전체적인 시스템 구조가 복잡해짐.
- 복잡한 구조로 인한 오버헤드 발생.
메시지 큐

개념
- 프로세스(프로그램) 간 데이터를 교환할 때 사용하는 통신 방법 중 하나
- MOM의 구현을 의미
- broker와 혼용해 사용되기도 함
Message의 생명 주기

- Producer가 Message를 Queue에 보낸다. Queue는 Consumer가 사용할 수 있을 때까지 Message를 유지 및 보관한다.

- Consumer가 Message를 처리할 준비가 되면 Queue에서 Message를 검색 및 소비한다. Queue는 Message를 즉시 삭제하지 않고 Consumer가 처리를 완료할 때까지 유지한다.

- Consumer가 Message를 처리하면 Queue에서 Message가 사라진다.
메시징 패턴 (Messaging Pattern)
Point-to-Point Messaging (One-way Messaging)

- Producer는 Consumer가 어떠한 시점에 Message를 검색하고 처리할 것을 기대하고 Queue에 Message를 보냄
- Consumer는 Queue에서 Message를 검색 후 처리하며 Producer는 Consumer의 존재, 처리 방법을 알지 못함 ⇒ Producer가 Consumer의 응답에 의존적이지 않음
Request/Response messaging

- 해당 패턴의 경우 Consumer가 Response Message를 보낼 Reply Queue가 필요함
- Producer는 Request Queue에 Message를 보낸 후, Reply Queue로부터 Consumer의 Response를 기다림.
- Consumer는 Message를 처리 후 Reply Queue에 Response Message를 전달
- 만약 Response가 정해진 시간 안에 반환되지 않는다면 Producer는 두 가지 선택을 할 수 있다.
Pub/Sub(Publish, Subscribe)

- Producer는 Message를 Queue에 보내고, 다수의 Consumer는 Copy된 Message를 소비할 수 있음
- Consumer는 Message에 대해 경쟁하지 않음
- Consumer는 오직 Topic에만 관심이 있으며, Message를 보낸 Producer에 의존하지 않음
- Producer도 어떤 Consumer가 Message를 소비하는지 관심이 없고 의존하지 않음
- Consumer들이 공통으로 반응해야 하는 event를 전달할 때 사용됨
메시지 큐의 장점
- 비동기(Asynchronous)
- 생산된 메시지의 저장, 전송에 대해 동기화 처리를 하지 않고 큐에 넣으므로 나중에 처리 가능
- 동기화 방식에서 병목 현상을 방지할 수 있음
- 낮은 결합도(Decoupling)
- 생산자 서비스와 소비자 서비스가 독립적으로 행동하게 됨으로써 서비스 간 결합도가 낮아짐
- 확장성(Scalable)
- 생산자 서비스 or 소비자 서비스를 원하는 대로 확장할 수 있음
- 여러 Consumer에게 Message Process를 분산(로드 밸런싱)하여 애플리케이션이 확장해도 부담을 줄일 수 있음

- 트래픽 급증(spike)를 완화하는 버퍼, 부하분산(로드 레벨링)의 역할을 할 수 있음

- 탄력성(Resilience)
- Consumer가 실패하더라도 응답에 의존적이지 않기 때문에 Message를 안정적으로 교환 가능
- 소비되지 않은 Message는 Queue에 남아 있기 때문에 Consumer가 복구되면 바로 처리 가능
- 보장성(Guarantees)
- At-least-once processing : 어떤 상황에서도 Message Queue 인프라 내에서 Message가 Queue에 보내진다면 Consumer에게 전달되었는지, 손실되었는지 확인함
- 결국 Message Queue에 보관되는 모든 Message는 Consumer에게 한 번은 전달됨을 보장
- Exactly-once processing : Producer가 Message를 Queue에 보내는 도중 네트워크 문제로 다른 Producer가 똑같은 Message를 보내는 경우 동일한 Message가 두 번 Queue에 들어갈 수 있음
- Message ID를 기반으로 이미 전달된 Message가 들어온 경우 삭제할 수 있음
- Consumer에게 정확히 한 번만 Message가 전달되도록 할 수 있음
References
https://medium.com/event-driven-utopia/the-stuff-that-every-developer-should-know-about-message-queues-a9452ac9c9d
https://www.cloudamqp.com/blog/what-is-message-queuing.html
https://www.ibm.com/kr-ko/topics/message-queues
https://docs.oracle.com/cd/E19148-01/820-0532/6nc919fai/index.html
https://tecoble.techcourse.co.kr/post/2021-09-19-message-queue/
https://goyunji.tistory.com/125
https://www.icelancer.com/2016/12/message-queue.html
https://12bme.tistory.com/176
https://colevelup.tistory.com/3
https://www.geeksforgeeks.org/what-is-message-oriented-middleware-mom/
https://bbo-blog.tistory.com/47