SQS & SNS
SQS¶
General Info¶
- Description
-
- Message queueing service
- SQS Queue = buffer between the app components that receive data & those that process data
- Features
- Ensures delivery of each message at least once
- Supports multiple readers/writers on the same queue
- Limitations
- Design your system to be IDEMPOTENT
- FIFO not guaranteed (use FIFO queues)
- Data stored within SQS is not encrypted by AWS (user has to encrypt before pushing to SQS)
- Message Lifecycle
-
- Component 1 (
C1
) sends message A (MA
) to a queue - When
C2
is ready to process a message, it retrieves it from the queue - While
MA
is being processed, it remains in the queue & is not returned for the duration of the visibility timeout C2
deletesMA
from the queue once it is done processing it
- Component 1 (
Message Identifiers
Identifier | Description |
---|---|
QUEUE URL |
Includes QUEUE NAME , provided at creation time and unique in the scope of all your queues |
MESSAGE ID |
|
RECEIPT HANDLE |
|
Sending¶
Parameters
Message Group ID |
|
Message Deduplication ID |
Specific to each <issue, user> |
- Retry
-
- If the producer detects a failed
SendMessage
action, it can retry sending as many times as necessary, using the same message deduplication ID - Assuming that the producer receives at least one acknowledgement before the deduplication interval expires, multiple retries neither affect the ordering of messages nor introduce duplicates
- If the producer detects a failed
- Delay Queue
-
- Let you postpone the delivery of new messages to a queue for a number of seconds
- Similar to visibility timeouts because both make messages unavailable to consumers for a specific period of time
- The difference is that, for delay queues, a message is hidden when it is first added to queue
- Whereas for visibility timeouts a message is hidden only after it is consumed from the queue
- To set delay seconds on individual messages, rather than on an entire queue, use message timers to allow SQS to use the message timer's
DelaySeconds
value instead of the delay queue'sDelaySeconds
value - Message timers let you specify an initial invisibility period for a message added to a queue
- Let you postpone the delivery of new messages to a queue for a number of seconds
- Dead-Letter Queue
-
- Queues, which other queues (source queues) can target for messages that can't be processed (consumed) successfully
- The redrive policy specifies the source queue, the dead-letter queue, and the conditions under which Amazon SQS moves messages from the former to the latter if the consumer of the source queue fails to process a message a specified number of times
- When the
ReceiveCount
for a message exceeds themaxReceiveCount
for a queue, SQS moves the message to a dead-letter queue (with its original message ID) - Configure an alarm for any messages delivered to a dead-letter queue
Receiving¶
Parameters
Visibility Timeout |
|
MaxNumberOfMessages |
|
- Return
-
- When you receive a message with a
message group ID
, no more messages for the samemessage group ID
are returned unless you delete the message or it becomes visible - SQS FIFO does not guarantee only once delivery when used as a Lambda trigger. If only once delivery is important in your serverless application, it's recommended to make your function idempotent.
- When you receive a message with a
- Long Polling
-
ReceiveMessage
check for existence of a message in the queue & returns IMMEDIATELY (with or without a message)- Long polling sends
WaitTimeSeconds
(<=20secs) argument toReceiveMessage
- If no message in the queue → call will wait
WaitTimeSeconds
for a message to appear before returning
SNS¶
- Description
-
- Web service for mobile & messaging to manage notifications
- PUB/SUB Paradigm
- Notifications pushed to clients, eliminating need to poll periodically
- Allows for parallel async processing
- FANOUT = SNS message sent to a topic & then replicated & pushed to multiple SQS queues/HTTP endpoints/email addresses
Components
PUBLISHERS |
Communicate with SUBSCRIBERS ASYNC by sending a message to a TOPIC |
SUBSCRIBERS |
|
TOPIC |
|
DELIVERY METHOD |
SQS, HTTP/HTTPS, email, SMS, Lambda |