JustJordanT / pizza-byte

A pizza application 🍕: Used for applying different technologies and architectures
Apache License 2.0
0 stars 0 forks source link

Create AWS SNS vs SQS #14

Open JustJordanT opened 8 months ago

JustJordanT commented 8 months ago

https://stackoverflow.com/questions/13681213/what-is-the-difference-between-amazon-sns-and-amazon-sqs

JustJordanT commented 8 months ago
  1. Push vs Poll:

    • SNS: Pushes messages to subscribers once published.
    • SQS: Requires consumers to poll for messages.
  2. Delivery Guarantee:

    • SNS: Attempts delivery but lacks a built-in mechanism to retain undelivered messages.
    • SQS: Retains messages for a configured duration if consumers are unavailable.
  3. Processing Semantics:

    • SNS: Each subscriber gets a copy of the message, good for pub/sub scenarios.
    • SQS: Ensures a message is processed by a single consumer, aiding in exactly-once processing.
  4. Ordering and Deduplication:

    • SNS: Doesn't guarantee message order.
    • SQS: FIFO queues ensure message ordering and exactly-once processing.
  5. Fan-Out Architecture:

    • Useful for notifying multiple services; SNS publishes, SQS or Lambda functions subscribe.
  6. Cost:

    • Cost dynamics may differ; advisable to check AWS pricing.
  7. Complexity and Maintenance:

    • SNS alone could simplify architecture if push mechanism suffices, while combining SNS and SQS provides more robust message handling.
JustJordanT commented 8 months ago

How an SNS message can triggers an HTTPS call to your Orders endpoint to create an order with a specific customer ID, you could follow these steps:

  1. Create an SNS Topic:

    • Start by creating an SNS topic in the AWS Management Console or using the AWS SDK.
  2. Create an HTTPS Subscriber:

    • Subscribe your Orders endpoint to the SNS topic. When subscribing an HTTPS endpoint, AWS SNS will send a confirmation message to the endpoint to ensure it's ready to receive notifications. Your endpoint will need to capture and confirm the subscription by visiting the URL provided in the confirmation message.
  3. Publish Messages to SNS:

    • Once your endpoint has confirmed the subscription, you can publish messages to the SNS topic. The message could be a JSON object containing the customer ID and any other relevant information.
  4. Process SNS Notifications:

    • Your Orders endpoint will receive SNS notifications as HTTPS POST requests. The body of the POST request will contain the message published to the SNS topic.
  5. Parse Customer ID and Create Order:

    • Parse the customer ID from the SNS message, and trigger the order creation logic in your service.
  6. Handle Errors and Retries:

    • Ensure that your Orders endpoint can handle errors gracefully. If the endpoint returns a 2xx status code, SNS considers the delivery successful. If the endpoint returns any other status code, SNS will retry delivering the message based on the retry policy configured for the SNS topic.

Here's a simplified illustration of what the SNS message and your Orders endpoint might look like:

SNS Message:
{
  "Type" : "Notification",
  "MessageId" : "unique-id",
  "TopicArn" : "arn:aws:sns:region:account-id:your-topic",
  "Message" : "{\"customer_id\": 12345}",
  ...
}

Orders Endpoint:
POST /create-order HTTP/1.1
Host: your-orders-endpoint.com
Content-Type: application/json

{
  "customer_id": 12345
}

This setup allows you to send a customer ID to SNS and have it trigger an HTTPS call to your Orders endpoint to create an order with that ID. The decoupling provided by SNS allows for more scalable and maintainable architecture, as your order creation logic is isolated from the publishing service.