Things to know about Event-Driven Architecture
Tags: aws, serverless
Published: 2021-09-13 04:39:40

<!-- wp:paragraph -->
<p>Event-driven architecture provides developers with increased availability, elasticity, scalability, and cost optimization. With the rise of microservices and the cloud, event-driven architecture is becoming increasingly important for developers to understand.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This blog explains what serverless architecture is, when it should be used, and how to use AWS serverless services like API Gateway, Lambda, SQS, SNS, and SES.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4><strong><em>Q.What do you mean by Serverless?</em></strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Deploying an application usually requires a server infrastructure. This means either a virtual or physical server needs to be managed, along with its os and prerequisites. Using a virtual server from a cloud provider such as Amazon does eliminate physical constraints, although it still requires some level of customization for the operating system and its software processes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The serverless architecture is a way to build and run applications and services without having to manage the infrastructure behind it.  <strong>With a serverless architecture, your application still runs on servers, of course, but the server management is done by AWS. So you focus purely on the individual functions in your application code. </strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>AWS defines its serverless services as “the native architecture of the cloud that enables you to shift more of your operational responsibilities to AWS, increasing your agility and innovation.”&nbsp; The diagram below presents the serverless architecture you can deploy in minutes using the solution's implementation guide.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://i1.wp.com/d1.awsstatic.com/product-marketing/Serverless/diagram_serverless-computing_weather-app.216b17d18ea1b3ed6e6e24b2cd9b58264b52058d.png?resize=1100%2C405&amp;ssl=1" alt=""/><figcaption>Credits: https://i1.wp.com/d1.awsstatic.com/product-marketing/Serverless/diagram_serverless-computing_weather-app.216b17d18ea1b3ed6e6e24b2cd9b58264b52058d.png?resize=1100%2C405&amp;ssl=1</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>It is also known as Function-as-a-Service (FaaS) is a serverless way to execute modular pieces of code, which can be executed in response to an event, such as a user clicking on an element in an application. This makes it easy to develop and deploy microservices with ease. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As each function can be scaled automatically which is a very cost-effective way of paying for computing resources. You only pay for the times that your functions get called, rather than paying to have your application always on and waiting for requests on so many different instances.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4><strong><em>Q.When and why should you consider serverless</em></strong> ?</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You should consider using a serverless provider if you have a small number of functions that you need to be deployed. It may make more sense to migrate small pieces of the application into serverless functions over time, as migrating an entire application might be very complex with system architecture.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Cost-effective, a cost is based on the CPU Usage time.</li><li><strong>Simplified scalability</strong>, as the  vendor handles all of the scaling on demand.</li><li><strong>Event driven,</strong> so no need for a complicated deploy process to roll out bug fixes and new features, developers can add and modify code for particular events.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://hexaware.com/wp-content/uploads/2020/04/characteristics-of-Serverless-Computing.png" alt=""/><figcaption>Credits: https://hexaware.com/wp-content/uploads/2020/04/characteristics-of-Serverless-Computing.png</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading {"textAlign":"center","level":3} -->
<h3 class="has-text-align-center"><strong><em>A Few of AWS Serverless services</em></strong>:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong><em>A.Amazon API Gateway: </em></strong>The AWS API Gateway is a managed service for building, deploying, and scaling various types of APIs. An API Gateway can remove some overhead in managing multiple APIs by monitoring, logging, throttling, caching, and other infrastructural requirements.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>With a few clicks in the AWS Management Console, you can create REST and Web Socket APIs that act as a front door for your applications to access data, business logic, or other AWS services from your back-end services. Some of its other benefits are,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>You can run multiple versions of the same API simultaneously with API Gateway. </li><li>You can monitor performance of metrics and information - on API calls, data latency, error rates - from the API Gateway dashboard.</li><li>You can decrease your costs based on the number of API requests you make, per Region, across your AWS accounts. </li><li>You can authorize access to your APIs with AWS Identity and Access Management, or IAM, as well as Amazon Cognito.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://docs.aws.amazon.com/whitepapers/latest/microservices-on-aws/images/image3.png" alt=""/><figcaption>Credits: https://docs.aws.amazon.com/whitepapers/latest/microservices-on-aws/images/image3.png</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For more information regarding the implementation of Amazon API Gateway.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-from-example.html" target="_blank">https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-from-example.html</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>B.AWS Lambda:</strong> AWS Lambda is a serverless compute service that has been designed to allow you to run your application code without having to manage and provision your own EC2 instances. This saves you from having to maintain and administer an additional layer of technical responsibility within your solution. Instead, that responsibility is passed over to AWS to manage for you.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Just upload your code, and Lambda takes care of everything required to run and scale your code with high availability and cost-effectiveness as, you pay only for the requests served and the compute time required to run your code for each of those requests.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/fc074d501302eb2b93e2554793fcaf50b3bf7291/2019/06/27/Screen-Shot-2019-06-27-at-2.23.51-PM-1024x510.png" alt=""/><figcaption>Credits: https://aws.amazon.com/blogs/architecture/understanding-the-different-ways-to-invoke-lambda-functions/</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>A Lambda function on its own doesn't do much. It needs to be executed. We call that triggers. There are many AWS services that can trigger a Lambda function. When your Lambda function gets executed, Lambda needs to know what piece of your code it needs to execute. That is the entry point for your code(handler). This tells Lambda in which file or class the function to execute is, as well as its name. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Lambda will use that handler to execute your Lambda from the trigger is received, but your code probably needs to receive some more information about what just triggered it. That information is passed to the handler via what we call an "event payload." </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>C.Simple Queue Service (SQS)</em></strong> is a fully managed service offered by AWS that works with serverless systems, or any distributed architecture.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's simply a queueing service for messages between components, It has the capability of sending, storing, and receiving these messages at scale. It is also possible to configure the service using the AWS Management Console, the AWS CLI, or using the AWS SDKs. </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The producer component of your architecture is responsible for sending messages to your queue. </li><li>The SQS service stores the message across a number of SQS servers for resiliency within the specified queue. </li><li>Consumers are responsible for processing the messages within your queue.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","width":532,"height":241,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://knowledgehills.com/images/aws/AWS-SQS-Flow.jpg" alt="" width="532" height="241"/><figcaption>Credits: https://knowledgehills.com/images/aws/AWS-SQS-Flow.jpg</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>When the consumer element of your architecture is ready to process the message from the queue, the message is retrieved and is then marked as being processed by activating the visibility timeout on the message. <strong>This timeout ensures that the same message will not be read and processed by another consumer. When the message has been processed, the consumer then deletes the message from the queue. </strong>During this period, the consumer processes the message. If it fails to process a message, perhaps due to a communication error, the consumer will not send a delete message request back to SQS. As a result, if the visibility timeout expires and it doesn't receive the request to delete the message, the message will become available again in the queue for other consumers to process.  There are two queues which can be used,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Standard queues, you have unlimited throughput, at-least-once delivery, and best-effort ordering. </li><li>First-in, first-out queues, you have high throughput, first-in, first-out delivery, and exactly-once processing.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>A dead-letter queue differs from the standard and FIFA queues as this dead-letter queue is not used as a source queue to hold messages submitted by producers.<strong> Instead, the dead-letter queue is used by the source queue to send messages that fail to process for one reason or another. </strong>A dead-letter queue must be configured as the same queue type as the source is used against.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>D. Simple Notification Service  (SNS)</em></strong> is used as a publishing and subscribe messaging service. It is centered around topics (group for collecting messages). </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Users over endpoints can then subscribe to this topic and messages or events are then published to that particular topic. </li><li>When a message is published, all subscribers to that topic receive a notification of that message. </li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>SNS is a managed service and highly scalable, allowing you to distribute messages automatically to all subscribers across your environment, including mobile devices. It can be configured with the AWS management console, the CLI, or the AWS SDK. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/1b6453892473a467d07372d45eb05abc2031647a/2017/11/20/introducing_sns_message_filtering_image_4.png" alt=""/><figcaption>Credits: https://aws.amazon.com/blogs/compute/simplify-pubsub-messaging-with-amazon-sns-message-filtering/</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>SNS uses the concept of publishers and subscribers, similar to SQS. </strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The producers, or publishers, send messages to a topic, which is used as a central communication control point.</li><li>The consumers, or subscribers of the topic, are then notified of this message by HTTP, HTTPS, Email, Email-JSON, etc ... . </li><li>SNS offers methods of controlling specific access to your topics through a topic policy. The policy themselves follow the same format as IAM policies.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>By integrating SNS and SQS with each other, together, a solution can be designed to send messages to subscribers through a push method.  To do this, you'll need to have your SQS queue subscribed to the SNS topic. </strong> Integration with  AWS Lambda allows SNS notifications to invoke existing Lambda functions. Like SQS, the Lambda function has to be subscribed to the topic. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>For more information on SNS,</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="https://aws.amazon.com/blogs/mobile/invoking-aws-lambda-functions-via-amazon-sns/" target="_blank" rel="noreferrer noopener">https://aws.amazon.com/blogs/mobile/invoking-aws-lambda-functions-via-amazon-sns/</a></li><li><a href="https://aws.amazon.com/blogs/compute/simplify-pubsub-messaging-with-amazon-sns-message-filtering/" target="_blank" rel="noreferrer noopener">https://aws.amazon.com/blogs/compute/simplify-pubsub-messaging-with-amazon-sns-message-filtering/</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>E. Simple Email Service (SES)</em></strong> makes it possible to handle an automated email system to communicate with your customers. </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>When receiving emails, you can architect your application to respond automatically to incoming emails, such as a request to unsubscribe from newsletter.</li><li>When sending emails, you can architect your application to emails automatically for an event, such as email confirmation. </li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>AWS will automatically scan for spam and viruses, and reject any messages from untrusted sources. When receiving an email, there are two ways to configure SES to instruct it as to what to do with the email. </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Recipient-based control, you set up configurations to direct the email based on the recipient. These recipient lists are classed as conditions. Receipt rules are used to control what action is taken when a condition is met. </li><li>IP address-based control defines what happens to your email based on its source IP address. This method allows you to select if email should be accepted or rejected based on its originating IP address. </li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/632667547e7cd3e0466547863e1207a8c0c0c549/2020/11/18/blogses_diagram.jpg" alt=""/><figcaption>Credits: https://aws.amazon.com/blogs/messaging-and-targeting/auto-reply-to-incoming-emails-using-amazon-simple-email-service-ses/</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For more information on event-driven architecture,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong><a rel="noreferrer noopener" href="https://aws.amazon.com/event-driven-architecture/" target="_blank">https://aws.amazon.com/event-driven-architecture/</a></strong></li></ol>
<!-- /wp:list -->