<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asad Ullah</title>
    <description>The latest articles on DEV Community by Asad Ullah (@assadbintahir).</description>
    <link>https://dev.to/assadbintahir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F107831%2F1da4fed5-dc7e-408b-8bbc-2c5ae6e2d45f.jpg</url>
      <title>DEV Community: Asad Ullah</title>
      <link>https://dev.to/assadbintahir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/assadbintahir"/>
    <language>en</language>
    <item>
      <title>How to solve LeetCode problem about finding all duplicates in an array</title>
      <dc:creator>Asad Ullah</dc:creator>
      <pubDate>Sun, 30 Apr 2023 10:29:00 +0000</pubDate>
      <link>https://dev.to/assadbintahir/how-to-solve-leetcode-problem-about-finding-all-duplicates-in-an-array-4kad</link>
      <guid>https://dev.to/assadbintahir/how-to-solve-leetcode-problem-about-finding-all-duplicates-in-an-array-4kad</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, we will discuss the solution to &lt;a href="https://leetcode.com/problems/find-all-duplicates-in-an-array/" rel="noopener noreferrer"&gt;&lt;code&gt;LeetCode&lt;/code&gt; problem #422&lt;/a&gt; — "Find All Duplicates in an Array" using JavaScript. The problem statement requires us to find all the elements that appear twice in a given array. The constraints require us to solve the problem in O(n) time and use only constant extra space. We will start by discussing the problem in detail and then move on to the approach we can take to solve it efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; of length n where all the integers of &lt;code&gt;nums&lt;/code&gt; are in the range &lt;code&gt;[1, n]&lt;/code&gt; and each integer appears once or twice, return an array of all the integers that appear twice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = &lt;code&gt;[4,3,2,7,8,2,3,1]&lt;/code&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[2,3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = &lt;code&gt;[1,1,2]&lt;/code&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = &lt;code&gt;[1]&lt;/code&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Constraints:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;n == nums.length&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 105&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= n&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each element in &lt;code&gt;nums&lt;/code&gt; appears &lt;strong&gt;once&lt;/strong&gt; or &lt;strong&gt;twice&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Approach:
&lt;/h2&gt;

&lt;p&gt;To detect duplicates, we just need to track the items which have already occurred. There can be two solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;One can be brute force with a nested loop. We can start from the first element in the iteration, and then compare it against all the entries in the array to see if it has a duplicated value or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This approach will have a time complexity of O(n^2).&lt;/li&gt;
&lt;li&gt;If we revisit our understanding of the data structures, we know that array has an O(n) time complexity of lookup operation, hence each iteration of the first loop will do a lookup which will result in O(n^2).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;We need to somehow eliminate the second loop. There is another data structure in the form of Objects/Key-value pairs in JS which have some characteristics of a hash map primarily a constant time lookup operation. That is what we need to eliminate nested iteration. Hence we use an object to track the duplicate and its constant lookup operation gives us an O(n) time complexity for the solution.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To solve the given problem, we will create a hash map to store the frequency of elements of the input array. We will iterate through each element in the input array and add its frequency to the hash map. If the frequency is already present in the hash map, then the element is a duplicate and we can add it to the duplicates array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
* returns duplicates from an array
*/&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findDuplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inputHashMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dupplicates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;inputHashMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;dupplicates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;inputHashMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;dupplicates&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;testData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="c1"&gt;// invoking solution for each test data item&lt;/span&gt;
&lt;span class="nx"&gt;testData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;findDuplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: Concerning the discussion on&lt;/em&gt; &lt;a href="https://twitter.com/AssadBinTahir/status/1650830073325551616?s=20" rel="noopener noreferrer"&gt;&lt;em&gt;tweet&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, updated the if condition of checking the existence of an element of the array inside the hashmap.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The function &lt;code&gt;findDuplicates&lt;/code&gt; takes an input array as an argument and returns an array containing all the duplicate elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We start by initializing an empty &lt;code&gt;duplicates&lt;/code&gt; array to store the duplicates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We then iterate through each element in the input array using a for loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We see if the current element is part of &lt;code&gt;inputHashMap&lt;/code&gt;. If it is found, then we add the current element to the &lt;code&gt;duplicates&lt;/code&gt; array since it is a duplicate. If the element is not found in the &lt;code&gt;inputHashMap&lt;/code&gt;, we add it to the &lt;code&gt;inputHashMap&lt;/code&gt; since it is a new element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At the end of the loop, we return the &lt;code&gt;duplicates&lt;/code&gt; array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate the &lt;code&gt;testData&lt;/code&gt; to invoke each test case against our solution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Time Complexity
&lt;/h3&gt;

&lt;p&gt;The time complexity of the above algorithm is O(n) since we iterate through the input array only once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ending Notes:
&lt;/h2&gt;

&lt;p&gt;For those who prefer a visual guide, you can also check out the video walkthrough for this problem on YouTube using this URL: &lt;a href="https://www.youtube.com/watch?v=R79s8KY8Kd4" rel="noopener noreferrer"&gt;Developer Log 1 | LeetCode 442 | Find All Duplicates in an Array | Code with Asad&lt;/a&gt;. This video will walk you through the problem and navigate through this problem in detail. With this article and the video, you will be well-equipped to solve the problem on LeetCode using JavaScript.&lt;/p&gt;

&lt;p&gt;In case you want to see the code used in this article or YT video, please see this &lt;a href="https://github.com/Assadbintahir/dsa-problems/tree/master/1.%20find_duplicates_from_array" rel="noopener noreferrer"&gt;GitHub URL&lt;/a&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
      <category>dsa</category>
    </item>
    <item>
      <title>AWS AZ vs Region Scoped Services</title>
      <dc:creator>Asad Ullah</dc:creator>
      <pubDate>Sun, 30 Apr 2023 10:19:23 +0000</pubDate>
      <link>https://dev.to/assadbintahir/aws-az-vs-region-scoped-services-1e3n</link>
      <guid>https://dev.to/assadbintahir/aws-az-vs-region-scoped-services-1e3n</guid>
      <description>&lt;p&gt;A common perception in cloud development is that there isn't much to do when we work with AWS managed services. However depending on the AWS service you use, your resources are either deployed at the AZ, Region, or Global level. Each service is different, so you must understand how the scope of a service might affect your application architecture.&lt;/p&gt;

&lt;p&gt;When you operate a Region-scoped service, you only need to select the Region you want to use. If you are not asked to specify an individual AZ to deploy the service in, this is an indicator that the service operates on a Region-scope level. For Region-scoped services, AWS automatically performs actions to increase data durability and availability. On the other hand, some services ask you to specify an AZ. With these services, you are often responsible for increasing the data durability and high availability of these resources.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;well-known best practice&lt;/strong&gt; for cloud architecture is to use Region-scoped, managed services. These services come with availability and resiliency built in. When that is not possible, make sure your workload is replicated across multiple AZs. At a minimum, you should use two AZs. That way, if an AZ fails, your application will have infrastructure up and running in a second AZ to take over the traffic.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;key concept&lt;/strong&gt; about whether you are using managed services or managing the infra yourself, is that AWS's customers maintain complete control of their data and are responsible for managing the security related to their content.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloudskills</category>
      <category>architecture</category>
      <category>devops</category>
    </item>
    <item>
      <title>Do we need AWS RDS?</title>
      <dc:creator>Asad Ullah</dc:creator>
      <pubDate>Wed, 09 Feb 2022 15:17:23 +0000</pubDate>
      <link>https://dev.to/assadbintahir/do-we-need-aws-rds-5hc5</link>
      <guid>https://dev.to/assadbintahir/do-we-need-aws-rds-5hc5</guid>
      <description>&lt;p&gt;Like many AWS services, AWS RDS is built on top of AWS EC2. This leads to the question that why would we opt for RDS when we can run our own DB in a memory optimized EC2 instance with high performance volume attached? Using EC2 and our custom DB install will save us extra cost of RDS, right?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Yes&lt;/strong&gt;, when our system is not sensitive to high availability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No&lt;/strong&gt;, when our system is sensitive to high availability and performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RDS provides many critical capabilities required for an highly available architecture. With RDS, we don't need to worry about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated provisioning&lt;/li&gt;
&lt;li&gt;Database engine &amp;amp; OS patching&lt;/li&gt;
&lt;li&gt;Continuous backup and restore in exact point in time&lt;/li&gt;
&lt;li&gt;Read replicas for increased read performance&lt;/li&gt;
&lt;li&gt;Monitoring dashboards&lt;/li&gt;
&lt;li&gt;Multi AZ setup for disaster recovery&lt;/li&gt;
&lt;li&gt;Vertical and horizontal scalability.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>cloudnative</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Building In AWS - Things To Consider</title>
      <dc:creator>Asad Ullah</dc:creator>
      <pubDate>Thu, 03 Feb 2022 10:57:36 +0000</pubDate>
      <link>https://dev.to/assadbintahir/building-in-aws-things-to-consider-13p4</link>
      <guid>https://dev.to/assadbintahir/building-in-aws-things-to-consider-13p4</guid>
      <description>&lt;p&gt;AWS is the leading cloud provider in present times and enable us to build and deliver products faster. There are several things which AWS services provides by default i.e. elasticity, auto scaling etc. AWS provides services in almost all the areas of software development. Be it infrastructure as a service i.e. EC2, or managed services like AWS Lambda &amp;amp; DynamoDB etc. All of such brilliant tools make AWS a default choice for building software. &lt;/p&gt;

&lt;p&gt;There are several points which we should consider prior to building in AWS. It can haunt our system later on if we are not very mindful of these. There are some points and hidden gotchas which are important and we can learn these from the experiences of others. We can summarize these points as following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Availability of services (region wise)&lt;/li&gt;
&lt;li&gt;Availability of services (distribution wise)&lt;/li&gt;
&lt;li&gt;Cost &amp;amp; Pricing implications&lt;/li&gt;
&lt;li&gt;Dealing with compliance&lt;/li&gt;
&lt;li&gt;Limits or quotas&lt;/li&gt;
&lt;li&gt;IAM capabilities of services&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll look at these points one by one and try to identify the things we should be careful about.&lt;/p&gt;

&lt;h3&gt;
  
  
  Availability of services (region wise)
&lt;/h3&gt;

&lt;p&gt;We should be mindful of presence of service in our target region before we commit the architecture. This can not be changed later on easily. Newly established region don't have all the services. It is also possible that a new service is not available in all regions. &lt;/p&gt;

&lt;p&gt;There are scenarios when although a service is available in a region, certain features of that service is not available. Hence it is a good habit to read the documentation of a service thoroughly prior to deciding on services.&lt;/p&gt;

&lt;p&gt;Hence the things which we should look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our targeted service is available in our desired region&lt;/li&gt;
&lt;li&gt;All the features which we intend to use are available in the desired region&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although AWS documentation mentions these things to some extent, &lt;a href="https://awsservices.info/?regionOption=null&amp;amp;serviceOption=null&amp;amp;radioButtonOption=allServices&amp;amp;hideGov=true" rel="noopener noreferrer"&gt;this table from trek10&lt;/a&gt; has granular details comparatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Availability of services (distribution wise)
&lt;/h3&gt;

&lt;p&gt;In AWS, services are distributed area wise as well. Some of the services create/provision resources globally. Some of them do at regional level and some at availability zone level. We can categorize it as following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Global services&lt;/strong&gt;: which includes Route53, DynamoDB Global Index, IAM etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regional Services&lt;/strong&gt;: which includes S3 bucket, SNS, SQS etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zonal Services&lt;/strong&gt;: which includes EC2 instances, RDS instances, EBS volumes etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a service is distributed zonal wise and our system requires regional availability, our architecture needs to take care of that replication. Similarly, if a service is distributed region wise and we need multi religion (partially global) replication, our architecture is supposed to take care of that. This is the case when the service itself doesn't provide replication. For example, RDS provides the capability of replication into different AZs.&lt;/p&gt;

&lt;p&gt;We must consider our service of choice and the requirement in order to add components in our architecture which will take care of such replication. For example we are planning to use EC2 instances which is distributed at availability zone level. We can use auto scaling groups (ASGs) for regional availability because ASGs spreads EC2 instances in multiple AZs.&lt;/p&gt;

&lt;p&gt;Following is an &lt;a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-benefits.html#arch-AutoScalingMultiAZ" rel="noopener noreferrer"&gt;example from AWS docs&lt;/a&gt; for instance distribution using ASGs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.aws.amazon.com%2Fautoscaling%2Fec2%2Fuserguide%2Fimages%2Fsample-3-tier-architecture-with-azs-diagram.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.aws.amazon.com%2Fautoscaling%2Fec2%2Fuserguide%2Fimages%2Fsample-3-tier-architecture-with-azs-diagram.png" alt="instance replication using ASGs" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Cost &amp;amp; pricing (cost models)
&lt;/h3&gt;

&lt;p&gt;Different AWS services have different pricing models. It will be a mistake on our part to assume that pricing model of one service will apply to another service. As we know that &lt;em&gt;we pay as we use&lt;/em&gt; in cloud. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EC2 is priced for time it runs in hours and seconds. We will be priced for a running EC2 even if it is not doing anything.&lt;/li&gt;
&lt;li&gt;Fargate containers and Lambdas are only priced when they run and are in use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence whenever we are designing a system in AWS, we must ask our client/product team:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What are the cost calculations and budget for the system to be built?&lt;/li&gt;
&lt;li&gt;What are the traffic estimates for the system to be built?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both of these questions will allow to make appropriate decisions regarding the services we use. &lt;/p&gt;

&lt;p&gt;Whenever we are designing in AWS, &lt;strong&gt;&lt;em&gt;Network Traffic&lt;/em&gt;&lt;/strong&gt; is always important to be considered. The golden rule is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't forget the traffic and cost drivers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, we are priced whenever traffic is moved from region to another region or from one availability zone to another availability zone in EC2. Another example of cost model is the use of NAT gateway. Not only we pay for NAT gateway, but we also pay for internet gateway.&lt;/p&gt;

&lt;p&gt;Hence we should always try to identify the cost drivers in our architecture. In some architectures, heavy data load is the cost driver. For example in a webhook architecture, we should be mindful of following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event size&lt;/li&gt;
&lt;li&gt;Number of events&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cost driver here will be outbound traffic. As the number of webhook endpoints of clients increases, we have to dispatch more events from our system to internet. Hence the outbound traffic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dealing with compliance
&lt;/h3&gt;

&lt;p&gt;Whenever we are designing a system, we should also be careful about the compliance which is required. Sometimes the client companies require certain compliance and sometimes governments require some standards to be followed. For example, PCI-DSS, HIPPA/HITECH etc.&lt;/p&gt;

&lt;p&gt;We see in &lt;a href="https://aws.amazon.com/compliance/" rel="noopener noreferrer"&gt;AWS description&lt;/a&gt; and documentation that it is compliant to certain standards. But in reality, this is an half answer. Instead of the whole AWS cloud, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Certain aws services, regions and edge locations are compliant to a standard or framework. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hence, when we are making a choice of region or service, we need to check if that region or service is compliant to our target framework or standard.&lt;/p&gt;

&lt;p&gt;For example, elasticache or memcached are not scoped in china region. Some edge locations are not scoped or covered in compliance. Hence if we are using AWS cloudfront, we are not sure which edge locations will be used and whether those edge locations are scoped in compliance framework or not.&lt;/p&gt;

&lt;p&gt;These are the points related to compliance which we must consider before finalizing the architecture, or presenting the audit report to audit authority.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limits or quotas
&lt;/h3&gt;

&lt;p&gt;A limit is the constraint which protects us from excessive use of a services - protecting us against huge bills. Most of the quotas can be increased by requesting to AWS support. There are some limits which can't be increased. Common upper limits can easily be found in AWS documentation. If some limits are not clear, we can reach out to AWS support team.&lt;/p&gt;

&lt;p&gt;All the AWS services are limited in some ways. For example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can't spin 100 EC2 instances at once.&lt;/li&gt;
&lt;li&gt;By default port 25 of SMTP is blocked for EC2. It is not visible in security group either. However, we can request AWS to remove this filter.&lt;/li&gt;
&lt;li&gt;SNS has different throughput limits per different region. It will be higher in &lt;code&gt;us-east-1&lt;/code&gt; as compared to &lt;em&gt;Frankfurt&lt;/em&gt; region. Hence some services behave differently depending on the region. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of the limits are documented in a very subtle way. It is very easy to overlook them. Hence we must do following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read the docs carefully regarding limits&lt;/li&gt;
&lt;li&gt;Read the service FAQs carefully and identify if there are any limits.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  IAM capabilities of services
&lt;/h3&gt;

&lt;p&gt;All AWS services use IAM to control access to services using policies. It is very often that we rely on IAM to achieve certain architectural goals. For example tenant isolation using separate VPC per client.&lt;/p&gt;

&lt;p&gt;We must consider that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some services can have different IAM featres. For example IAM features are only at resource level like start or stoping EC2 containers.&lt;/li&gt;
&lt;li&gt;Some AWS services allow access on the basis of tags and policies. For example all the resources having a particular tag.&lt;/li&gt;
&lt;li&gt;Some other services don't allow conditions based access or policies. Or don't allow resource level access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence we shouldn't take resource based access or policy based access for granted and shouldn't assume that it will be available for all services in a similar way. We should always consider whether the access capability we desire is available in our target service. &lt;a href="https://docs.aws.amazon.com/service-authorization/latest/reference/reference.html" rel="noopener noreferrer"&gt;AWS IAM documentation&lt;/a&gt; is great for this purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Since we have already discussed all the gotchas in detail, hence whenever developing an architecture in AWS,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gather knowledge of services at broad level to get an idea of what is possible.&lt;/li&gt;
&lt;li&gt;Once the services are selected, go into deeper details of the services to find constraints and limitations&lt;/li&gt;
&lt;li&gt;Go through the checklist which is also discussed in this article to avoid common pitfalls.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note: I took help from my experience and existing resources for this article. Any mistake or factual inaccuracy can be reported in comments. I will try my best to maintain &amp;amp; update this with time to time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Reoucrces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/compliance/services-in-scope/" rel="noopener noreferrer"&gt;AWS Compliance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/" rel="noopener noreferrer"&gt;AWS services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloudonaut.io/page/1/" rel="noopener noreferrer"&gt;Blogs from  Cloudonaut&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>architecture</category>
    </item>
    <item>
      <title>How to do serverless in AWS?</title>
      <dc:creator>Asad Ullah</dc:creator>
      <pubDate>Mon, 10 Jan 2022 09:12:44 +0000</pubDate>
      <link>https://dev.to/assadbintahir/how-to-do-serverless-in-aws-4p6k</link>
      <guid>https://dev.to/assadbintahir/how-to-do-serverless-in-aws-4p6k</guid>
      <description>&lt;p&gt;AWS serverless ecosystem consists of a lot of tools and services. Although there are couple of guides on the implementation details of individual services, there is not enough material highlighting the principles involved in choosing an AWS service. A wrong choice can be a costly mistake when our serverless system hits scale. This article is more of a &lt;strong&gt;survey of serverless fundamentals in AWS&lt;/strong&gt;. It will briefly touch upon composition paradigms and the services which can be used for any of these paradigms. Common use cases of AWS services and their comparison will also be discussed.&lt;/p&gt;

&lt;p&gt;Before we proceed to architect a serverless system, we must understand what serverless systems offer and what are the common use cases for serverless systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why serverless?
&lt;/h3&gt;

&lt;p&gt;Serverless is no silver bullet and it promises certain things which makes it useful for certain scenarios.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows us to prototype things faster and thus enables us to move from ideas to market.&lt;/li&gt;
&lt;li&gt;It makes us focus more on our product instead of spending time on infrastructure management.&lt;/li&gt;
&lt;li&gt;It is a low cost solution because it follows the &lt;em&gt;Pay as you use&lt;/em&gt; model.&lt;/li&gt;
&lt;li&gt;Scalability and high availability are built into the AWS services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Considering the promises serverless do, there are some use cases for which serverless makes more sense.&lt;/p&gt;

&lt;h3&gt;
  
  
  Popular Use Cases
&lt;/h3&gt;

&lt;p&gt;Serverless architecture is best used to perform short-lived tasks and manage workloads that experience infrequent or unpredictable traffic. Some of the use cases are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Trigger Based Tasks:&lt;/strong&gt; Any user activity that triggers an event or a series of events is a good candidate for serverless architecture.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Building Restful APIs:&lt;/strong&gt; You can leverage Amazon API Gateway with serverless functions to build REST APIs that scale with demand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous Processing:&lt;/strong&gt; Serverless functions can handle behind-the-scenes application tasks, such as rendering product information or transcoding videos after upload, without interrupting the flow of the application or adding user-facing latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web Applications:&lt;/strong&gt; Evolving nature of SaaS products and web applications make them suitable candidate for serverless.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to design serverless architecture?
&lt;/h3&gt;

&lt;p&gt;Now we understand the benefits and popular use cases for serverless, we will jump to the fundamentals of serverless systems. &lt;/p&gt;

&lt;p&gt;Whenever we are designing serverless or microservices, we must define the events in our system, bounded context of our APIs and the composition paradigm we will be choosing for our system.&lt;/p&gt;

&lt;h4&gt;
  
  
  Events &amp;amp; Bounded Context
&lt;/h4&gt;

&lt;p&gt;It is better to divide large business operations to small bounded contexts. This enables us to clearly define the responsibility of any given part of our system. We can define this process to three steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Be explicit about the inter-relationship of our bounded contexts (small broken pieces of our large business operation).&lt;/li&gt;
&lt;li&gt;Define events and service boundaries for these bounded contexts.&lt;/li&gt;
&lt;li&gt;Group events by reducing data dependencies.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://www.infoq.com/presentations/microservices-ddd-bounded-contexts/" rel="noopener noreferrer"&gt;This infoq article&lt;/a&gt; has a good guide on bounded contexts.&lt;/p&gt;

&lt;p&gt;A bounded context can be any part of our system clearly depicting the flow. One example of a bounded context in a serverless system can be seen in the following diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnebxtn3miicchejhl83q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnebxtn3miicchejhl83q.jpeg" alt="Bounded context example" width="651" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll discuss the design decisions about these services later in the article.&lt;/p&gt;

&lt;h4&gt;
  
  
  Composition
&lt;/h4&gt;

&lt;p&gt;There are two paradigms to compose serverless systems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Orchestration&lt;/li&gt;
&lt;li&gt;Choreography&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each of these paradigms have different characteristics which enable us to make the choice for our use case. &lt;/p&gt;

&lt;h4&gt;
  
  
  Orchestration
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Synchronous - Request Response paradigm&lt;/li&gt;
&lt;li&gt;Services are controlled by a centralized function or process.&lt;/li&gt;
&lt;li&gt;The controller function needs to wait for each downstream process to complete and return a response.&lt;/li&gt;
&lt;li&gt;Services are very tightly coupled&lt;/li&gt;
&lt;li&gt;Failure could stop the entire process / flow&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Choreography
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;An asynchronous or event driven paradigm&lt;/li&gt;
&lt;li&gt;Services work independently and are decoupled from each other&lt;/li&gt;
&lt;li&gt;Failures and retries are easier to manage&lt;/li&gt;
&lt;li&gt;Difficult to implement timeouts.&lt;/li&gt;
&lt;li&gt;End-to-end monitoring and reporting are comparatively difficult.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since we understand bounded contexts and composition paradigms, we will move on to implementation of these principles in AWS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serverless tools in AWS
&lt;/h3&gt;

&lt;p&gt;There are several serverless services in AWS. Some of them are listed down below in their respective category.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compute&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;AWS Lambda&lt;/li&gt;
&lt;li&gt;AWS Fargate&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Application Integration&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;AWS Event Bridge&lt;/li&gt;
&lt;li&gt;AWS SQS&lt;/li&gt;
&lt;li&gt;AWS SNS&lt;/li&gt;
&lt;li&gt;AWS Step Functions&lt;/li&gt;
&lt;li&gt;AWS API Gateway&lt;/li&gt;
&lt;li&gt;AWS AppSync&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Data Store&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;AWS S3&lt;/li&gt;
&lt;li&gt;AWS DynamoDB&lt;/li&gt;
&lt;li&gt;AWS RDS Proxy&lt;/li&gt;
&lt;li&gt;AWS Aurora Serverless
AWS Kinesis, AWS Cognito and AWS CloudWatch are also used often in serverless systems.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Serverless Orchestration in AWS
&lt;/h3&gt;

&lt;p&gt;AWS step functions are used for serverless orchestration in AWS. Step functions are all about modelling business transactions through series of state transitions. Step functions enable us to do end to end monitoring and reporting via audit histories. The order flow is modeled and source controlled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8mgcsi4i14ht61dsc4x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8mgcsi4i14ht61dsc4x0.png" alt="step function state machine" width="250" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above is one of the example of a step function flow. We can clearly see it as state machine. There are two types of step functions workflows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Standard (Workflow which takes more than 5 minutes to complete)&lt;/li&gt;
&lt;li&gt;Express (Workflow taking less than 5 minutes and when we need high execution volume)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Some of the common use cases for AWS step functions are given below&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Microservices orchestration&lt;/li&gt;
&lt;li&gt;IT Automation&lt;/li&gt;
&lt;li&gt;Data processing and ETL orchestration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implementation details and extended flow examples can be studied at &lt;a href="https://aws.amazon.com/step-functions/use-cases/" rel="noopener noreferrer"&gt;AWS official examples page&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serverless Choreography in AWS
&lt;/h3&gt;

&lt;p&gt;Choreography in AWS allows us to modify each step of the flow independently. It also enables us to scale the different parts of our system independently of each other. Since we have already discussed bounded contexts and events, events allows us to extend functionality in choreography comparatively easily. &lt;/p&gt;

&lt;p&gt;There are two considerations which we must discuss before proceeding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;End to end monitoring and reporting is comparatively difficult.&lt;/li&gt;
&lt;li&gt;Difficult to implement timeout logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's discuss the example choreography architecture given below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ufemyctz74d8erffc27.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ufemyctz74d8erffc27.jpeg" alt="choreography in aws" width="582" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is no centralized service controlling everything. Instead each part of the system relies on the incoming event, does its part and forwards it to a destination. The whole system is driven by events. For this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A client makes request to API which is powered by AWS API Gateway&lt;/li&gt;
&lt;li&gt;API Gateway integrates with AWS Lambda Function in an asynchronous way (more on this later.)&lt;/li&gt;
&lt;li&gt;AWS Lambda does its processing and pushes the event to AWS EventBridge's event bus. &lt;/li&gt;
&lt;li&gt;EventBridge have rules and it notifies the matching rule destinations.&lt;/li&gt;
&lt;li&gt;A Lambda and an SNS topic can also be destination for an event from event bus.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we saw that the question of choosing SNS &amp;amp; EventBridge at their respective places is still not answered. At this stage, there should be more questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When to use AWS SQS and when to use AWS SNS?&lt;/li&gt;
&lt;li&gt;When to use an AWS SNS and when to use AWS EventBridge?&lt;/li&gt;
&lt;li&gt;What is synchronous and asynchronous invocation of a Lambda?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By a quick read about these services, we find out that some services appears to do very similar things like SQS, SNS and EventBridge. In order to have the correct mental model for these services, we must understand how these services integrate with the compute part of our serverless system i.e. Lambda.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lambda Integration Models
&lt;/h3&gt;

&lt;p&gt;There are three ways in which Lambda integrates with the other AWS services.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Poll based integration (Synchronous)&lt;/li&gt;
&lt;li&gt;Push based integration (Synchronous)&lt;/li&gt;
&lt;li&gt;Event based integration (Asynchronous)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before we proceed, lets discuss the synchronous and asynchronous invocation of lambda functions. &lt;/p&gt;

&lt;h4&gt;
  
  
  Synchronous Invocations
&lt;/h4&gt;

&lt;p&gt;In synchronous invocation, we wait for the function to process the event and return a response.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lambda runs the function and waits for a response&lt;/li&gt;
&lt;li&gt;Lambda returns the response from the function's code with additional data, such as the version of the function that was invoked&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0nm0dqxzkz4xo31gcc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0nm0dqxzkz4xo31gcc2.png" alt="synchronous invocation" width="412" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Asynchronous Invocations
&lt;/h4&gt;

&lt;p&gt;In asynchronous invocation, Lambda queues the event for processing and returns a response immediately.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lambda places the event in a queue and returns a success response without additional information&lt;/li&gt;
&lt;li&gt;A separate process reads events from the queue and sends them to your function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F321yszha3gkzlx3nfrn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F321yszha3gkzlx3nfrn4.png" alt="asynchronous invocation" width="627" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AWS has very detailed &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; for both kind of invocations. Let's move on to the lambda integration models.&lt;/p&gt;

&lt;h4&gt;
  
  
  Poll Based Integration
&lt;/h4&gt;

&lt;p&gt;Lambda is a service which contains many resources such as our lambda function, event queue, event resource mapping etc. In this kind of integration, event resource mapping is responsible for polling the target service and fetching data when available.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lambda polls the records from stream or queue using &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventsourcemapping.html" rel="noopener noreferrer"&gt;event resource mapping&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Resource mapping then invokes your function synchronously based on BatchSize or BatchWindow&lt;/li&gt;
&lt;li&gt;Each event that your function processes can contain hundreds or thousands of items&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below are the services which do poll based integration with AWS Lambda:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS DynamoDB Streams&lt;/li&gt;
&lt;li&gt;AWS Kinesis Streams&lt;/li&gt;
&lt;li&gt;AWS SQS&lt;/li&gt;
&lt;li&gt;AWS MQ&lt;/li&gt;
&lt;li&gt;Apache Kafka (managed and self-managed both)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is an example of this kind of integration from AWS Docs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6vyj7aelcjxqcuklado.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6vyj7aelcjxqcuklado.png" alt="poll based integration" width="654" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Push Based Integration
&lt;/h4&gt;

&lt;p&gt;In this kind of integration, services themselves are responsible for pushing the event to Lambda Service.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The services pushes an event to Lambda&lt;/li&gt;
&lt;li&gt;Lambda then invokes your function and return a response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWS API Gateway, AWS Cognito &amp;amp; AWS CloudFront do this kind of integration with AWS Lambda. The example diagram in choreography section can be seen as an example of API Gateway's integration with Lambda.&lt;/p&gt;

&lt;p&gt;For the API Gateway scenario, there is a catch here. Since our Lambda function is invoked synchronously by lambda service, it returns a response. There is a way to configure Lambda so that it integrates with API Gateway in an asynchronous way. &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html" rel="noopener noreferrer"&gt;Official docs&lt;/a&gt; has a very good guide for it. &lt;/p&gt;

&lt;h4&gt;
  
  
  Event Based Integration
&lt;/h4&gt;

&lt;p&gt;In this kind of invocation, services invokes the lambda and immediately gets success response after handing over the event. Lambda then processes the event in the background.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Services hand off the event to Lambda and Lambda handles the rest&lt;/li&gt;
&lt;li&gt;Lambda manages the function's asynchronous event queue&lt;/li&gt;
&lt;li&gt;We can configure Lambda to send an invocation record to another service

&lt;ul&gt;
&lt;li&gt;SQS Queue&lt;/li&gt;
&lt;li&gt;SNS Topic&lt;/li&gt;
&lt;li&gt;Lambda Function&lt;/li&gt;
&lt;li&gt;EventBridge event bus&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;S3, SNS, EventBridge &amp;amp; CloudWatch do this kind of integration with lambda. Below is an example where SNS integrates with Lambda in an asynchronous way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4qk74erl6lq9r27cjp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4qk74erl6lq9r27cjp7.png" alt="event based integration" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of the times, we are dealing with poll based integration and event based integration. Below is a short visual summary of all three types of integrations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh472bsisqwyc48frrss7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh472bsisqwyc48frrss7.png" alt="lambda integration summary" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we understand different composition paradigms, how they are implemented in AWS and how Lambda integrates with the serverless services in AWS. Let's discuss briefly the design principle and use cases for some common services.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use AWS SQS and AWS SNS?
&lt;/h3&gt;

&lt;p&gt;SQS and SNS are used to decouple our applications in cloud. But both of these services have some very distinct characteristics making them suitable for different purposes. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnte8bc20t3ew3iiciwdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnte8bc20t3ew3iiciwdw.png" alt="sns vs sqs" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  SQS
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;SQS is basically for &lt;strong&gt;1:1 reliable asynchronous communication&lt;/strong&gt; between two entities.&lt;/li&gt;
&lt;li&gt;SQS &lt;strong&gt;temporarily holds messages&lt;/strong&gt; and is owned by the consumer. Its job of consumer to get the message from SQS. (REMEMBER: Lambda polling) Lambda does this for us automatically.&lt;/li&gt;
&lt;li&gt;It is used to decouple our services in a way where we want to process the messages &lt;strong&gt;as per the capacity of consumer&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  SNS
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;SNS is for &lt;strong&gt;1:N message/event publishing (pub/sub model)&lt;/strong&gt;. It is also called “Fanning out the events”&lt;/li&gt;
&lt;li&gt;It is used for application where we need &lt;strong&gt;high throughput&lt;/strong&gt;. Which means we want to fan out events as soon as they arrive. It is the job of SNS to publish the event to subscribers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to use SNS &amp;amp; EventBridge?
&lt;/h3&gt;

&lt;p&gt;SNS &amp;amp; EventBridge are both used to distribute events. EventBridge is under the hood same as AWS CloudWatch Events API but it provides more features like integrating with external services. SNS is more useable for fanning out events to different services while EventBridge's intended use is for Application Integration.&lt;/p&gt;

&lt;h4&gt;
  
  
  SNS
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;SNS is for &lt;strong&gt;1:N message/event publishing (pub/sub model)&lt;/strong&gt;. It is also called “Fanning out the events”&lt;/li&gt;
&lt;li&gt;It is used for application where we need &lt;strong&gt;high throughput&lt;/strong&gt;. Which means we want to fan out events as soon as they arrive. It is the job of SNS to publish the event to subscribers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  EventBridge
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;EventBridge is for &lt;strong&gt;1:N ((N:N sometimes) for message distribution&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Recommended to be used for &lt;strong&gt;application integration like SaaS or third party integration&lt;/strong&gt; like datadog, shopify etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhsinq9gyez2tgn9d8kd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhsinq9gyez2tgn9d8kd.png" alt="sns vs eventbridge" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes people face difficulty in the limit of 5 target on a matching rule when they try to use EventBridge in place of SNS. What we can do is that to have an SNS topic as a destination of matching rule and route the event to SNS for fanning out. AWS &lt;strong&gt;EventBridge is primarily meant for routing event in our system&lt;/strong&gt;. The convention is to have a separate rule against each service to which we want to route an event. This is good for separation of concerns. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We discussed bounded contexts and composition paradigms, how to implement them in AWS and mental models for some of the services central to event driven systems. After having the knowledge of all these things, we should be able to make an educated choice for our system on the basis of our budget, our use case, our team size and the project timelines. There is no right or wrong for most of the people and systems. But these design decisions and principles become important when we are dealing with serverless at scale. &lt;/p&gt;

&lt;p&gt;This whole article is meant to serve as a mental model for designing serverless in AWS. For the implementation details of individual services, AWS Docs are the best place to go.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: I took help from my experience and existing resources for this article. Any mistake or factual inaccuracy can be reported in comments. I will try my best to maintain &amp;amp; update this with time to time.&lt;/em&gt;     &lt;/p&gt;

</description>
      <category>serverless</category>
      <category>aws</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
