Queues
Q what is the difference between SNS & SQS & Event Bridge?
- Abbreviation
- SNS stands for simple notification service.
- SQS stands for simple Queue service.
- Usage
- SNS uses a Publisher Subscriber system, we own a topic and we publish to that topic and subscribers get notified of events that are delivered to that topic.
- One to many fan out.
- High throughput.
- Many subscribers.
- SQS is a queuing service for message processing. SQS can be a subscriber to SNS. When somebody publishes a message to SNS our SQS will get a message that may be processed at a later time.
- Allows application owner to publish messages to a queue and be decouple application from one another.
- One of the oldest service from AWS.
- SQS has a synchronous communication.
- Temporary message holding pool.
- Ordered message processing.
- Event Bridge is an improved version of SNS. Event Bridge provides third-party integration, such as Shopify, Pager duty, data docs, etc.
- One too many.
- AWS, SQS, third party application integration.
- Components
- SQS has following components.
- Queues
- Queues can be created from console, CLI or infrastructure code.
- We publish messages to queue.
- They are temporary holding pools.
- A queue can be a FIFO queue or regular queue.
- By default SQS does not respect order of messages. We need to create FIFO queue for it.
- Messages
- Messages are a raw data, Json blob.
- We can subscribe messages one by one or in groups.
- As a message is subscribed by consumer. It is hidden from other consumers.
- Polling
- Polling is a methodology via which application which wants to receive the message interacts with queue and processes them.
- SNS has following components.
- Topics
- Topics are not holding pools for messages.
- As soon as the message is published, in a topic, the topic delivers an identical copy of messages to all the subscribers.
- There may be a failure scenario if one of the service is down this can be handled by adding a SQS between SNS, and receiving service.
- once a message is delivered, it disappears from the topic.
- Messages
- Messages are same as in SQS I.e json blobs.
- Publish/Subscribe(PubSub)
- owners publishes data into topic and receiver who has subscribed to topic receive data.
- Event bridge has following components.
- Message bus
- Message bus is same as topic, we publish the event bus.
- Message filtering allows us to filter messages. For example, we may have a target that receives delivered messages.
- Events
- Events can be pending, shipped , or delivered events.
- Can be constructed by applications, or emitted by AWS services like EC2.
- We can integrate events with third-party providers like SAP, etc.
- Rules
- Rules, match the coming events and match them to the corresponding targets.
- For a particular rule we can have maximum of five targets.
- Targets
- Targets are destination, endpoints what will be invoked on an event, etc.
- Framework
- Publishing a message to a topic can deliver to many subscribers. It uses fanout framework. The topic may be delivered to different types of services like SQS, lambda, email.
- In SQS The system must poll the Queue to discover new events. So whenever an event is delivered to Queue there is no notification. There should be a mechanism of pulling the queue for new events, process the events and deleting the events. This can be done by a separate thread.
- Consumers
- Messages in an SQS queue are typically processed by a single consumer or a single service with a narrow responsibility and different services if they care about same events they can have their own separate queues.
- Messages in SNS are broadcasted to all those consumers Who have subscribed to topic.
- When to use
- If other systems care about event we should use SNS.
- If our system gives about event we should use SQS.
- Use case
- When we buy something online our transaction processing service may send event to SNS.
- The SNS invokes lambda which sends an email back to the customer.
- The SNS may store order success info in Dynamo DB.
- We may publish same event in analytics queue which is SQS.
- Then our SQS will be polled by a service.
- So if a message is sent from SNS to SQS it will be there guaranteed and we will not loose it.
- Example
- SQS
- Auto service creates/updates, orders, and publishes them to Queue.
- Analytics service may poll the queue at regular intervals to subscribe messages and check the status of the order.
- SNS
- Order service publishes message to the topic in queue.
- All the services who have subscribed to topic in queue, get the message, such as accounting service, analytics service, order dashboard service, etc.
Comments
Post a Comment