A Ruby-based implementation of the publish-subscribe pattern using AWS SNS (Simple Notification Service) and SQS (Simple Queue Service). This project provides a robust messaging system with support for dead letter queues (DLQ) and message retry capabilities.
This project implements a complete pub-sub messaging system with three main components:
- SNS Publisher (
sender.rb
): Publishes messages to an SNS topic with support for JSON formatting and custom attributes - SQS Consumer (
receiver.rb
): Consumes messages from an SQS queue with support for both direct SQS and SNS-forwarded messages - DLQ Processor (
retry_dlq.rb
): Processes failed messages from a Dead Letter Queue back to their destination queue
- Ruby 2.7 or higher
- AWS Account with access to SNS and SQS services
- Configured AWS credentials (either via AWS CLI or environment variables)
-
Clone the repository:
git clone [repository-url] cd pub-sub-with-sqs
-
Install dependencies:
bundle install
The SNS message publisher supports:
- JSON message formatting
- Custom message attributes
- Command-line interface
- File-based message input
- Logging with sender ID tracking
# Basic usage
./sender.rb --topic-arn "arn:aws:sns:region:account:topic" --message '{"key": "value"}'
# Send message from file
./sender.rb --topic-arn "arn:aws:sns:region:account:topic" --file message.json
# Add custom attributes
./sender.rb --topic-arn "arn:aws:sns:region:account:topic" --file message.json --attribute "priority=high:String"
The SQS message consumer features:
- Long-polling support
- Message attribute handling
- SNS message format detection
- Automated message deletion after processing
- Configurable batch processing
# Basic usage
./receiver.rb --queue-url "https://sqs.region.amazonaws.com/account/queue"
# Configure batch processing
./receiver.rb --queue-url "https://sqs.region.amazonaws.com/account/queue" --max-messages 5 --wait-time 10
The Dead Letter Queue processor supports:
- Message reprocessing from DLQ to destination queue
- Batch processing
- Progress tracking
- Configurable message retention
- Detailed statistics
# Basic usage
./retry_dlq.rb --dlq-url "https://sqs.region.amazonaws.com/account/dlq" --destination-url "https://sqs.region.amazonaws.com/account/queue"
# Process with custom batch size and delay
./retry_dlq.rb --dlq-url "https://sqs.region.amazonaws.com/account/dlq" --destination-url "https://sqs.region.amazonaws.com/account/queue" --batch-size 5 --delay-seconds 30
Messages can be sent in JSON format. Example message.json
:
{
"my-message": "hello baby",
"age": 13
}
--topic-arn ARN
: SNS Topic ARN (required)--id IDENTIFIER
: Sender identifier--region REGION
: AWS Region (default: us-east-1)--file FILE_PATH
: Load message from JSON file--message JSON_STRING
: Provide message as JSON string--key-value KEY=VALUE
: Add key-value pairs to message--attribute NAME=VALUE:TYPE
: Add message attributes
--queue-url URL
: SQS Queue URL (required)--id IDENTIFIER
: Receiver identifier--region REGION
: AWS Region (default: us-east-1)--max-messages NUM
: Maximum messages to receive (default: 10)--wait-time SECONDS
: Long polling wait time (default: 20)--visibility-timeout SECONDS
: Message visibility timeout (default: 30)
--dlq-url URL
: Dead Letter Queue URL (required)--destination-url URL
: Destination Queue URL (required)--batch-size NUM
: Messages per batch (default: 10, max: 10)--delay-seconds NUM
: Visibility delay in destination queue--max-messages NUM
: Maximum messages to process--keep-in-dlq
: Keep messages in DLQ after processing--region REGION
: AWS Region (default: us-east-1)
- Message Format: Always validate JSON messages before sending
- Batch Processing: Use appropriate batch sizes (5-10 messages) for optimal throughput
- Error Handling: Implement DLQ for handling failed message processing
- Monitoring: Track message processing statistics using the built-in logging
- Create an SNS topic and SQS queue
- Subscribe the SQS queue to the SNS topic
- (Optional) Configure a Dead Letter Queue for failed messages
- Ensure proper IAM permissions for SNS publishing and SQS operations
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
[Add your license information here]