<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by malindu ruwantha on Medium]]></title>
        <description><![CDATA[Stories by malindu ruwantha on Medium]]></description>
        <link>https://medium.com/@malinduruwantha?source=rss-84332bddfc9f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rkNBOiz6LvsRWQw0CT1LVg.jpeg</url>
            <title>Stories by malindu ruwantha on Medium</title>
            <link>https://medium.com/@malinduruwantha?source=rss-84332bddfc9f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 19:48:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@malinduruwantha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Messaging Application using Rabbit MQ, Spring Boot, and Spring AMQP]]></title>
            <link>https://malinduruwantha.medium.com/messaging-application-using-rabbit-mq-spring-boot-and-spring-amqp-e2b8fb8bc63a?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2b8fb8bc63a</guid>
            <category><![CDATA[spring-amqp]]></category>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[rabbitmq]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Tue, 25 Apr 2023 16:00:20 GMT</pubDate>
            <atom:updated>2023-04-25T16:00:20.614Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I_v3Mu-llbqCXR-VmXmy7w.jpeg" /></figure><p>Messaging is a secure and reliable way of exchanging information between multiple parties. In this tutorial, we will create a simple data exchange mechanism using Spring Boot, the Spring AMQP protocol, and RabbitMQ.</p><h3><strong>AMQP Protocol</strong></h3><p>AMQP provides a standardized way to send and receive messages, defining a set of patterns and an architecture that can be used for various messaging scenarios, regardless of the platform or technologies used. It also ensures reliable delivery of messages, maintaining the correct order and preventing message loss. Furthermore, AMQP provides secure communication between messaging systems. It can be implemented with messaging systems like RabbitMQ, Microsoft Azure Service Bus, and Apache Qpid, and is supported by languages such as Java, Python, and .NET.</p><h3>AMQP Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GDbToMN6shkQED2ICBO6-g.png" /></figure><h3><strong>1. Environment setup</strong></h3><h4>1.1. Create a Rabbit MQ server locally using docker</h4><ul><li>To get started, download the Docker Desktop application from <a href="https://www.docker.com/products/docker-desktop/">https://www.docker.com/products/docker-desktop/</a>.</li><li>Next, go to <a href="https://hub.docker.com/">https://hub.docker.com/</a> and search for the RabbitMQ image. Copy the name of an image that includes a management tag</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ixZcttQmPAHTtpPjR9wkZQ.png" /></figure><ul><li>To pull the Docker image using the terminal, use the following command, replacing ‘3.10.5-management’ with the image name you selected:</li></ul><pre>docker pull rabbitmq:3.10.5-management</pre><p>It will pull the docker image and you can verify it by going to the image tab in the docker desktop application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G3QMOD5LnPaDR7U8GsXqcA.png" /></figure><ul><li>Then you have to start the docker container. Use the following command</li></ul><pre>docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3.10.5-management</pre><ul><li>You have now started a local RabbitMQ server. You can verify it by navigating to the RabbitMQ management console, which is up and running on port 15672. To access the console, go to <a href="http://localhost:15672/">http://localhost:15672/</a> in your web browser.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NNlnVxf3Sq1zCj5O9jRbGg.png" /></figure><ul><li>Default <strong>UserName</strong> and <strong>Password</strong> is guest</li></ul><h3><strong>2. Setup Spring boot project</strong></h3><ul><li>Download a project using <a href="https://start.spring.io/">https://start.spring.io/</a></li><li>Add the following Dependencies</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*xShGjDWCcm47AlYyZLCHdQ.png" /></figure><h3><strong>3. Creating the Message Producer class</strong></h3><blockquote><em>application.properties</em></blockquote><pre>spring.rabbitmq.host=localhost<br>spring.rabbitmq.port=5672<br>spring.rabbitmq.username=guest<br>spring.rabbitmq.password=guest<br><br><br>rabbitmq.queue.name =simple_messaging_app<br>rabbitmq.exchange.name = simple-messaging-app_exchange<br>rabbitmq.routing.key = simple-messaging-app_routing_key</pre><blockquote>Now we need to create the configuration class to bind the queue with the relevant exchanges. Let’s create a class named RabbitMQConfig.java</blockquote><pre>package com.javaMqGuide.springbootrabbitMqproject.config;<br><br><br>import org.springframework.amqp.core.Binding;<br>import org.springframework.amqp.core.BindingBuilder;<br>import org.springframework.amqp.core.Queue;<br>import org.springframework.amqp.core.TopicExchange;<br>import org.springframework.beans.factory.annotation.Value;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br><br>@Configuration<br>public class RabbitMQConfig {<br><br>    @Value(&quot;${rabbitmq.queue.name}&quot;)<br>//${rabbitmq.queue.name} will get fetched from the application.properties<br>    private String queue;<br><br>    @Value(&quot;${rabbitmq.exchange.name}&quot;)<br>    private String exchange;<br><br>    @Value(&quot;${rabbitmq.routing.key}&quot;)<br>    private String routingkey;<br><br><br>    @Bean<br>    public Queue queue(){<br>        return new Queue(queue);<br>    }<br><br><br>    @Bean<br>    public TopicExchange exchange(){<br>        return new TopicExchange(exchange);<br>    }<br><br>    //binding between queue and exchange using routing key<br>    @Bean<br>    public Binding binding(){<br>        return BindingBuilder.bind(queue())<br>                .to(exchange())<br>                .with(routingkey);<br>    }<br>}</pre><p>In the RabbitMQConfig class, we will create separate beans to return a queue and an exchange. The messaging queue will be bound to the exchange using a routing key.</p><blockquote>Creating the Message Producer<br>RabbitMQProduser.java</blockquote><pre>package com.javaMqGuide.springbootrabbitMqproject.publisher;<br><br>import org.slf4j.Logger;<br>import org.slf4j.LoggerFactory;<br>import org.springframework.amqp.rabbit.core.RabbitTemplate;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.beans.factory.annotation.Value;<br>import org.springframework.stereotype.Service;<br><br>@Service<br>public class RabbitMQProducer {<br><br>    @Value(&quot;${rabbitmq.exchange.name}&quot;)<br>    private String exchange;<br><br>    @Value(&quot;${rabbitmq.routing.key}&quot;)<br>    private String routingkey;<br><br>    private RabbitTemplate rabbitMqTemplate;<br><br>    @Autowired<br>    public RabbitMQProduser(RabbitTemplate rabbitMqTemplate) {<br>        this.rabbitMqTemplate = rabbitMqTemplate;<br>    }<br><br>    public void sendMessage(String message){<br>  <br>        rabbitMqTemplate.convertAndSend(exchange,routingkey,message);<br>    }<br>}</pre><ul><li>{rabbitmq.routing.key} and {rabbitmq.exchange.name} will be fetched from the application.properties file</li></ul><p>Message Producer is responsible for publishing a message to the queue.</p><blockquote>Now, let’s create a REST controller to publish messages. This will create a REST endpoint that uses the RabbitMQProducer class to publish the messages. Let’s create a class named MessageController.java.</blockquote><pre>package com.javaMqGuide.springbootrabbitMqproject.controller;<br><br>import com.javaMqGuide.springbootrabbitMqproject.publisher.RabbitMQProduser;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.ResponseEntity;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web.bind.annotation.RequestParam;<br>import org.springframework.web.bind.annotation.RestController;<br><br>@RestController<br>@RequestMapping(&quot;/api/v1&quot;)<br>public class MessageController {<br><br>    @Autowired<br>    private RabbitMQProduser produser;<br><br>    @GetMapping(&quot;/publish&quot;)<br>    public ResponseEntity&lt;String&gt; sendMessage(@RequestParam(&quot;message&quot;) String message){<br>        produser.sendMessage(message);<br>        return ResponseEntity.ok(&quot;Message send successfullt to RabbitMQ&quot;);<br><br>    }<br><br>}</pre><p>Now you can use <a href="http://localhost:8080/api/v1/publish?message=Test"><strong>http://localhost:8080/api/v1/publish?message=Test</strong></a><strong> Message</strong> url to publish a message to the queue</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_5Z9kuQoSEQlosLrtZnB2A.png" /></figure><p>To verify that the implementation is working as intended, use the RabbitMQ management console. You should see the message get queued from the console.</p><h3><strong>4. Creating the message consumer class</strong></h3><pre>package com.javaMqGuide.springbootrabbitMqproject.consumer;<br><br>import org.slf4j.Logger;<br>import org.slf4j.LoggerFactory;<br>import org.springframework.amqp.rabbit.annotation.RabbitListener;<br>import org.springframework.stereotype.Service;<br><br>import java.util.ArrayList;<br><br>@Service<br>public class RabbitMQConcumer {<br><br>    private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQConcumer.class);<br><br>  <br><br>    @RabbitListener(queues = {&quot;${rabbitmq.queue.name}&quot;})<br>    public void consume(String message){<br>        LOGGER.info(String.format(&quot;Received mesage -&gt; %s&quot;,message));<br>        <br><br>    }<br><br>   <br><br><br><br>}</pre><p>We now need to create a listener for the queue. When a message is published to the queue, the listener will be executed. You can use a logger instance to test the consumer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2b8fb8bc63a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is it difficult to get started with React (day 1)]]></title>
            <link>https://malinduruwantha.medium.com/is-it-difficult-to-get-started-with-react-part-1-795835e87efb?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/795835e87efb</guid>
            <category><![CDATA[jsx]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[create-react-app]]></category>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Sat, 25 Sep 2021 16:12:24 GMT</pubDate>
            <atom:updated>2021-09-25T17:17:57.059Z</atom:updated>
            <content:encoded><![CDATA[<p>Curious? join me, let&#39;s figure it out….…...</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XdcrYecW6uCLwJOOe5048Q.jpeg" /></figure><p>I am a beginner for frontend programming with React, Maybe you are the same, or not 😉 How do we start reading? shall we purchase a crash course or watch some random videos on youtube? For me, I think the best way to get started with a new technology is following the Documentation. But I am still a beginner 🙄. Well, still the answer is the same 🙂.</p><p>You can access the Documentation through the following link</p><p><a href="https://reactjs.org/docs/getting-started.html">Getting Started - React</a></p><p>Great, what should I know before going through this? 🤔<br>Maybe some basics on JavaScript, HTML, CSS.</p><p>First of all, let&#39;s create a react app up and running in our local environment.<br>First, you might need to download and install Node.</p><p>1. Installing NodeJS<br><a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a></p><p>2. Then navigate to the folder where you want to generate the project</p><pre>npx create-react-app my-app<br>cd my-app<br>npm start</pre><p>oh 😶 totally forgot, I am going to use Visual Studio Code as the code editor along with this.</p><p>3. <strong>npm start</strong> will start your local server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EfGdG3DMKRewRKkl5kA1rg.png" /></figure><p>OK, now I am going to try the most common first step for any beginner project. You know that isn&#39;t it?😊</p><p>Let&#39;s print <strong>HELLO WORLD</strong></p><p>first, open your project with VSCode Editor and Navigate to the index.js file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*onFejlF7Tcwdf-qJXhI7BQ.png" /></figure><p>Next, replace</p><pre>&lt;React.StrictMode&gt;</pre><pre>&lt;App /&gt;</pre><pre>&lt;/React.StrictMode&gt;,</pre><pre>with</pre><pre>&lt;h1&gt;Hello, world!&lt;/h1&gt;,</pre><p>until we got to know more about the project structure we are going to use index.js for our code editing purposes.</p><p>but why 🙁 ?? Let&#39;s see, we will get the answers.</p><p>cheers 🍻🍻🍻 we have managed to print some text in the browser. Before having some further hands-on sessions let&#39;s explore the basics, let&#39;s figure out what&#39;s happening underneath. 💡💡💡💡💡💡💡💡</p><h3><strong>Introduction to JSX</strong></h3><pre>const element = &lt;h1&gt;Hello, world!&lt;/h1&gt;;</pre><p>Wait 😳😳😳😳😳</p><p>Are we assigning an HTML tag as a variable?</p><p>Nope, This syntax is called JSX which is used as the template language for React. For the rest of the story, we are going to use JSX.</p><p><strong>JSX supports Embedded Expressions</strong></p><pre>const name = &#39;John Snow&#39;;<br>const element = &lt;h1&gt;Hello, {name}&lt;/h1&gt;;<br>ReactDOM.render(<br>  element,<br>  document.getElementById(&#39;root&#39;)<br>);</pre><p>This will print John Snow as the output</p><p>We can also use expressions as {3+3} or even calling a method inside JSX</p><pre>ReactDOM.render(</pre><pre>&lt;h1&gt;Sum is {calculateSum(3,4)}&lt;/h1&gt;,</pre><pre>document.getElementById(&#39;root&#39;)</pre><pre>);</pre><pre>function calculateSum(number1,number2) {</pre><pre>return number1+number2;</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*o9HZ7xAUStmcxGlJFAxuoA.png" /></figure><p>In the compilation, JSX is converted into JavaScript objects and at the end, we are dealing with JavaScript. So following codes are also possible with JSX</p><pre>ReactDOM.render(</pre><pre>&lt;div&gt;{divide(5,0)}&lt;/div&gt;,</pre><pre>document.getElementById(&#39;root&#39;)</pre><pre>);</pre><pre>function divide(number1,number2) {</pre><pre>if (number2 === 0){</pre><pre>return &lt;h2&gt; cannot divide by Zero&lt;/h2&gt;</pre><pre>} else {</pre><pre>return number1/number2;</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*eDJbbvdtfHTbIz1nzH4kUw.png" /></figure><p><strong>Specifying attributes with JSX</strong></p><pre>ReactDOM.render(</pre><pre>&lt;img src={imagePath}/&gt;,</pre><pre>document.getElementById(&#39;root&#39;)</pre><pre>);</pre><p>According to the Documentation, JSX is converted in to React.createElement()<br>objects. These objects are known as elements and they will construct the DOM.So we are going to stop for today with some questions in mind. We will come up with some more in the next story and some answers to our questions.</p><p>Hope you got some heads up on how to get started with React. From the next story, let us explore more on the basic concepts of React.</p><p>Till then 👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=795835e87efb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with AWS part 3]]></title>
            <link>https://malinduruwantha.medium.com/getting-started-with-aws-part-3-126caea18e5b?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/126caea18e5b</guid>
            <category><![CDATA[cloudfront]]></category>
            <category><![CDATA[aws-region]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[edge-location]]></category>
            <category><![CDATA[availability-zone]]></category>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Mon, 16 Aug 2021 06:44:16 GMT</pubDate>
            <atom:updated>2021-08-16T06:54:00.130Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS Regions</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rpKcFuzCtE8XrmVmFsQMCg.png" /></figure><p>As we discussed earlier one of the drawbacks of using on-premises data centers is it&#39;s being centralized in one geographical location. Several issues came along with this</p><ol><li>If the data centers were subjected to any natural disaster</li><li>All the backups were stored in the same building</li><li>latency to retrieve data from a faraway place from the data center(even the speed of light may delay the data transmission between two distinct geographical locations)</li><li>disaster recovery and data duplication costs will be high with your own infrastructure</li></ol><p>But by using AWS infrastructure you are getting access to all areas throughout the globe</p><ol><li>you can have replications of your data centers where you getting the most traffic.</li><li>In a natural disaster, you can replicate your data in two different regions<br>or in two different availability Zones(will be discussed soon).</li></ol><p>There are several factors that you might need to consider when selecting the AWS region.</p><p><strong>Compliance with data governance and legal requirements</strong></p><p>If your company is located in a country with special data regulation rules, For example, if the data should not leave a specific geographical area as data of country ABC needs to remain inside ABC and cannot transfer to a data center located in country QPR.</p><p><strong>Proximity to your customers</strong></p><p>Select regions near to customers where you are getting the traffic most.</p><p><strong>Available services within a Region</strong></p><p>Sometimes your selected region would not have initiated your service requirements. You should have to reach a region that can satisfy all your AWS services needs.</p><p><strong>Pricing</strong></p><p>pricing can differ across different regions. Due to the tax policies and the infrastructure costs</p><p>By considering the above factors you should need to select the most suitable location to initiate your regions</p><h3><strong>Availability Zones</strong></h3><p>For a single region, there can have multiple availability zones. Availability zone stands for a data center located within a building</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*_-qaAnujCl6dWqRkdEN_6w.jpeg" /></figure><p>Although these availability zones lie to lower the latency rates those are distinct from miles to deal with the disasters. It is recommended to deploy your instances in at least two of these availability zones. So if one got caught up with a disaster service can be delivered continuously from the other.</p><h3><strong>Edge locations</strong></h3><p>what if you have customers all over the world. Is it needed to purchase and deploy your application around all the regions? What if your application is deployed in china and customers accessing data from Canada. data should travel a long way to reach the destination. To overcome this issue AWS has introduced a caching mechanism to replicate a cached copy of your data across different locations which are using the CDN Content delivery Network concept. these cached locations are called edge locations. Edge locations are separately situated from regions and you can push data from the regions to edge locations.</p><p>Amazon web service which is used to deliver content over the network such as videos, applications, data is called Amazon CloudFront.It owns its own version of DNS service as Amazon Route 53 which will redirect customers to the correct location to retrieve data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_XOu3Fuh5TkdtIYL_920Q.png" /></figure><p>Here are some links to explore more about amazon global infrastructure</p><ul><li><a href="https://aws.amazon.com/about-aws/global-infrastructure/">Global Infrastructure</a></li><li><a href="https://www.infrastructure.aws/">Interactive map of the AWS Global Infrastructure</a></li><li><a href="https://aws.amazon.com/about-aws/global-infrastructure/regions_az">Regions and Availability Zones</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=126caea18e5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stepping into the world of Threads]]></title>
            <link>https://malinduruwantha.medium.com/stepping-into-the-world-of-threads-f8d5814a109?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8d5814a109</guid>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Fri, 30 Jul 2021 09:19:58 GMT</pubDate>
            <atom:updated>2021-08-02T03:50:21.581Z</atom:updated>
            <content:encoded><![CDATA[<p>part 2 <strong>Synchronization</strong></p><p><strong>Synchronization</strong></p><p>From the first part, we have discussed creating multiple threads. what will happen if the same thread having the access to the same instance of a class. Both of the threads are changing the states. This will bring some ambiguities to your application. As an example, let&#39;s take a Bank Account object.</p><pre>class BankAccount {</pre><pre>private Integer amount = 100;</pre><pre>public Integer getBankAccountBalance() {</pre><pre>return this.amount;</pre><pre>}</pre><pre>public void setBankAccountBalance(Integer amount) {</pre><pre>this.amount = amount;</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*i_HgSLTME52cMspOS8PTxg.png" /></figure><p>In the Runnable instance. create a new bank account object and implement the withdraw ().</p><pre>public class BankAccountWithdraw implements Runnable {</pre><pre>BankAccount bankAcc = new BankAccount();</pre><pre>@Override</pre><pre>public void run() {</pre><pre>withdraw(100, bankAcc);</pre><pre>}</pre><pre>public boolean isEligibleforWithdraw(Integer requestedAmount, BankAccount bankAcc) {</pre><pre>if (bankAcc.getBankAccountBalance() &gt;= requestedAmount) {</pre><pre>return true;</pre><pre>} else {</pre><pre>return false;</pre><pre>}</pre><pre>}</pre><pre>public void withdraw(Integer withdrawAmount, BankAccount bankAcc) {</pre><pre>if (isEligibleforWithdraw(withdrawAmount, bankAcc)) {</pre><pre>bankAcc.setBankAccountBalance(bankAcc.getBankAccountBalance() - withdrawAmount);</pre><pre>System.out.println(&quot;success&quot;);</pre><pre>} else {</pre><pre>System.out.println(&quot;not enough balance&quot;);</pre><pre>}</pre><pre>}</pre><pre>public static void main(String args[]) {</pre><pre>Runnable r = new BankAccountWithdraw();</pre><pre>Thread person1 = new Thread(r);</pre><pre>Thread person2 = new Thread(r);</pre><pre>person1.start();</pre><pre>person2.start();</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xq3QS6MM5bDY8p_E9Wltvw.png" /></figure><p>then create two thread instances with the same runnable instance</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*-1aNXGbxN5NeDSWGqXxmuQ.png" /></figure><p>Whenever the application runs for person 1, the withdrawal will be successful and for person2 it not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*pq7orZByhievdZJwN6e0xQ.png" /></figure><p>if we illustrate the flow it will be executed as follows.<br>for both instances we are creating a single bank account object with an amount of 100 .whenever person1 withdraw, withdraw() method is executed. It will validate the eligibility and the account would be deducted in the process.<br>in Line 23.</p><p>because of that whenever person 2 is validated, he would not be eligible because the account value is 0 and isEligibleForWithdrawal test will be failed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T4ZpVOrFXu1GolxWB4aYeA.png" /></figure><p>but what will happen when the thread scheduler changes the executing thread from person1 to person 2 whenever person 1 is at Line 23. Now the person 1 has passed the isEligibleForWithdrawaltest but the account is not deducted yet.</p><p>Now, person2 is executing and he can also manage to go through isEligibleForWithdrawaltest because still the account is not deducted for person 1.</p><p>Now the person 2 will successfully withdraw the money and the account balance will be deducted to 0. As the person2 run() is over again, the person2 thread will start its execution from Line 23 and complete the transaction, bringing the bank account amount down to -100.</p><h3>That&#39;s not correct !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</h3><p>Note: You can test this flow by appending a debug pointer at Line 23 and when person 1 thread comes, shifts back to person 2 thread and resume its execution. After finishing person2 shift back to person 1 and resume the execution.</p><p>You can overcome this faulty behavior by making withdraw () synchronized</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*kZbOv7TxtlgleVXVM_wZFA.png" /></figure><p>The synchronized method will not let any other thread use the same method until the currently executing thread finishes its execution.</p><p><strong>Synchronization and how locks work</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*PLJeJ48ARLNQZjzV1Xsz1w.png" /></figure><p>If an object comes with a synchronized method that object will be having a lock. When a thread enters the synchronized method, that lock of the currently executing object instance will be acquired by the executing thread.</p><p>As there is only one lock per object, whenever one thread acquires it another cannot be entered into the synchronized method. when the execution of the synchronized method is finished lock will be released.</p><p>Following is some key facts about synchronized methods</p><ol><li>Only methods can be Synchronized (classes and variables cannot be synchronized)</li><li>One lock for one object</li><li>A class can have both synchronized and non-synchronized methods(Then the different threads can access the non-synchronized methods no locking will happen.)</li><li>If the thread goes sleeps it holds the locks not releasing them</li><li>Thread can acquire multiple locks</li><li>If a thread has acquired the lock it can execute another synchronized method in the same object</li><li>synchronization can be done to a block of code</li></ol><p>8.When using synchronized blocks you need to give the object instance to be locked specifically</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*-VXIvRS8eIt2ZnwPKERCAQ.png" /></figure><p>9.Synchronization can be done at the class level. Think of an instance synchronizing a static method. There is no difference in synchronizing a static method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*pMSlFPd0upYm7OzosNAAZA.png" /></figure><p>10.This can also be done using the static blocks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*kqz32AaXwQNz_grWvPaEGg.png" /></figure><p>the class instance should be given</p><p>IMPORTANT</p><p>if you are having nonstatic synchronized methods in a class they will be blocked only for the same instance. If you invoke them from two different instances it wouldn&#39;t get blocked.</p><h4><strong>Common issues</strong></h4><p>1.<strong>Thread Deadlock</strong><br>Imagine two threads are waiting for each other&#39;s locks. None of them can finish executions until the other releases the lock and no one can release the lock until it is executed.</p><p><strong>2.Thread livelock</strong></p><p>Imagine an occasion a single thread needs two locks for the execution</p><p><strong>Thread A needs Lock Object P and Lock of Object Q<br>Thread B needs Lock Object P and Lock of Object Q</strong></p><p>both Thread A and Thread B start executing parallelly.</p><p>what if Thread A gets lock P and trying to get lock Q, but it is already taken by Thread B.</p><p>Then Thread B whose with lock Q tries to get lock P but it is already taken by Thread A</p><p>Then for B to proceed Thread A will release the lock P and at the same time for A to proceed Thread B will release lock Q and Thread B is getting lock P. then again try to acquire lock Q. Again lock Q is being acquired by Thread A<br>SO now both of the Threads are not making any progress.</p><p><strong>3.Thread Starvation</strong></p><p>This issue happens when one thread holds the lock of a resource for a long time.<br>when other threads are trying to access the shared resource it&#39;s been held. Another reason for Thread starvation is the misdeclaration of thread priorities. If a long-running and highly prioritized thread came in holding a shared resource. It might be again and again selected from the thread pool until the execution is finished. So if the mentioned Thread gets hold of a synchronized shared resource it would be blocked.</p><p><strong>4.Wait() , Notify(), NotifyAll()</strong></p><p>Above are the methods from the object class that helps in thread communication. Three of these methods should be called within a synchronized block or a method.</p><p>a.wait() will be stopping its execution and waits until notify() method is called from the instance of a</p><p>If multiple threads are waiting for a particular Thread notifyall()<br>method will bring all the waiting threads back to the runnable pool.</p><p>So we have covered the basics of Java Threads. Let&#39;s explore more about the concurrency in next story</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8d5814a109" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with AWS]]></title>
            <link>https://malinduruwantha.medium.com/getting-started-with-aws-834c0756d351?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/834c0756d351</guid>
            <category><![CDATA[serveless]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Wed, 21 Jul 2021 08:51:52 GMT</pubDate>
            <atom:updated>2021-07-21T09:01:56.667Z</atom:updated>
            <content:encoded><![CDATA[<p>part 2</p><p>From the first part, we have gone through <br>1. Amazon Elastic compute cloud<br>2 . Advantages of using AWS cloud services<br>3. Deployment models of cloud computing<br>4. Amazon EC2 Instance types<br>5. AMAZON EC2 Auto Scaling Service<br>6. Elastic Load Balancing</p><p>Read the story with the following link<br><a href="https://malinduruwantha.medium.com/getting-started-with-aws-e6a27d189953">Getting Started with AWS part 1</a></p><p>Before going forward let me bring you a simple application build up with several components</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pQzSNO8-cDv2hYCNHP93Tg.png" /></figure><p>For the execution of Application, A requires data from Application B. Application B requires data from Application C.This is an example of a tightly coupled application. what if Application A keeps sending messages and Application C is not available for a certain time. Application B will also fail to process the request from A.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zyabLKG3I4z8AINmjE6gOQ.png" /></figure><p>Then Process runs on Application A will also be failed and the requesting messages will be dropped due to failures. This is one of the drawbacks which prevails in tightly coupled applications.</p><p>what if we can hold the upcoming messages from Application B until Application C comes to the state of AVAILABLE. Let&#39;s use a Queue to store and hold the messages between the components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nwN678jhQ27vpziiKy0thw.png" /></figure><p>The queue will be beholding the incoming messages until the application gets available.AWS service which maps into the message queue service is known as <strong>Amazon Simple Queue Service (Amazon SQS)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*2CTLLmZcpGq_Bvw1EMoAvw.png" /></figure><p><strong>Amazon Simple Queue Service (Amazon SQS)</strong><br>This Service can send store and receive messages. According to the above example, Application B will send the message to the SQS Queue and it will hold the message until Application C will become available. Then the message will be sent to Application C.Whenever it is finished processing message will be deleted from the Queue.</p><p>In the above paragraphs, we talked about the drawbacks of tightly coupled monolithic applications. you can design your application with independent components. Loosely coupled components will prevent the application from falling whenever one component fails. You can achieve the integration between those loosely coupled components via the services like <strong>Amazon SQS and (Amazon SNS)</strong></p><p><strong>Amazon Simple Notification Service (Amazon SNS)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*G4pD18cXu2K7hn0PluzY9A.png" /></figure><p>Amazon SNS can be used for the Notification service between the components. Works according to the publisher and subscriber model. Publisher will publish messages to the subscribing components.</p><p><strong>Serverless Computing</strong></p><p>From the first part, we have discussed the Amazon EC2 instances, the Scaling process, and management. Although you use EC2 instances, server management might be needed. what if you don&#39;t want to interfere with this server management.<br>and what if you can hand over all the responsibilities to another party</p><p>Cool isn&#39;t it?</p><p>Serverless computing concepts let you keep all the headaches of server management aside. Using Serverless, your service provider will take care of the management perspective. Then all you need to worry about is your application. With the word <strong>Serverless, </strong>there might be confusion. Although the name says no servers, There are. The only difference is you don&#39;t need to worry about them.</p><p>AWS is also providing several serverless computing services.</p><p><strong>AWS Lambda</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*SfVGQi_mi7bccFScAo-SUA.png" /></figure><p>Lambda allows you to upload your functions without a provisioned server to a lambda function. you can configure a trigger or an event. when the event fires<br>your function will be executed in a well-managed and highly available environment where you do not want to interfere. Lambda functions will provide high availability. It will scale itself to meet the demand. The specialty is you are not paying for the uptime of a server. It&#39;s only for the instances where your code is running.</p><p>AWS Lambda functions are designed for processes consuming under 15 minutes. So it&#39;s not suitable for long-running processes but quick processes like Rest endpoint for web applications.</p><p>You can easily deploy a lambda function by following the steps.</p><ol><li>Upload your code to lambda</li><li>configure the event or the trigger (eg: HTTP request)</li><li>when the request comes Lambda function will be triggered</li></ol><p>Requirement: you need to convert the media which upload from pc to suit the mobile applications<br>Solution: lambda function for the conversion<br>trigger: whenever a media file uploads to the database</p><p><strong>Container related services</strong></p><p>If you are working with docker containers to package your code and dependencies into a single object. you would have two different options with AWS for the container orchestration. These docker containers will hold your application logic, dependencies, and configurations. Containers will need a host to deploy themselves and in AWS hosts are EC2 instances. Several management processes need to be done to start, restart and manage docker containers. Think of a scenario where your application works across multiple EC2 instances. management will be difficult with the rising amounts of EC2s.<br>Management of these tasks is called <strong>container orchestration.</strong></p><p>Container orchestration tools will be there to deal with the manual orchestration difficulties.</p><p>Amazon provides two main services for container orchestration as ECS and EKS</p><p><strong>Amazon Elastic Container Service (Amazon ECS)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*NSSVfM_iLe3OKQQn1TIBXg.png" /></figure><p><strong>Amazon Elastic Kubernetes Service (Amazon EKS)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*4OWbjzsTcdf_DNpT-Z3L9A.png" /></figure><p>Both these two web services run on top of Amazon EC2 instances .but if you want to give away that headache of managing EC2 instances, AWS provides a serverless computing platform as <strong>AWS Fargate. </strong>this is<strong> </strong>the serverless computing platform used for EKS and ECS.</p><p><strong>AWS Fargate</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H92Vdcrgp1vN6VPphpNF2Q.jpeg" /></figure><p><strong>Summary</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p-1Tgl0hVAF0MqEM2nCtxQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*_0mvUMEYygYQeCNH1sqOBQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=834c0756d351" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stepping into the world of Threads]]></title>
            <link>https://malinduruwantha.medium.com/stepping-into-the-world-of-threads-39e10ea88060?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/39e10ea88060</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[threads]]></category>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Tue, 13 Jul 2021 13:22:32 GMT</pubDate>
            <atom:updated>2021-08-02T03:51:50.797Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 1</h3><h4><strong>Why we need them</strong></h4><p>Before stepping into the world of threads, Let&#39;s have an idea about where we might need them. Think of a situation where you build the Order processing module of an enterprise application.</p><p>The underlying process would be as follows</p><p>1.User Creating and Submitting the order<br>2.Validation of the fields<br>3.Persistence of the data in the database<br>4.changing the status of the order (New Order --&gt;&gt; Submitted Order)<br>5.Extracting data for the reporting tables<br>6.calculation of the net values and net prices</p><p>Imagine you run this application in a single-threaded environment. user waits until all these processes to happen one after another to get the feedback in the UI. It&#39;s not efficient, isn&#39;t it?<br>but what if we can show the user that the order is submitted while doing the other transactional processes in the background. Sounds good, isn&#39;t it?<br>That&#39;s where multithreading comes in handy. It can be utilized to run several processes parallelly.</p><p>Mainly once we launch a Java Application, the main thread is the one that executes. Originated with the main() method in the main stack. If we start another thread of execution, A new stack will be instantiated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yFngKrELKOSmo6vJNBHiMw.png" /></figure><h4><strong>Execution of Threads</strong></h4><p>Although JVM can start a couple of threads at the same time, it&#39;s not guaranteed to run all the threads at the same moment. The number of threads that can be run in parallel is depending on the CPU and is controlled by the thread Scheduler. If the CPU can run only two threads at the same time and there are four runnable threads, the thread scheduler will choose two among them. Others will have to wait. What I wanted to highlight is we don&#39;t have control over the execution of threads.</p><h4><strong>Creating a Thread</strong></h4><p>There are two ways of define and instantiating a thread. <br>1.Extending Thread Class<br>2.Implementing Runnable Interface</p><p>There is no much difference as the Thread class itself is implementing the Runnable interface. The difference lies in the way of starting the thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*8VpjAcJThqPz0P6oYT6TLQ.png" /><figcaption>Extending the Thread Class</figcaption></figure><p>In Java, only one class can be extended. So it is always good to use threads by implementing the Runnable Interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*-EEBfqPHR8cGYcLqr6mTcA.png" /><figcaption>Declare Threads using Runnable</figcaption></figure><p>Although we used the Runnable interface it is mandatory to instantiate a Thread .start() can only be called for an instance of a Thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*ZrFwB0VeQt-9xL4dRZqAoQ.png" /><figcaption>Using lambda to implement Runnable</figcaption></figure><p>Java.Lang.Runnable is a Functional interface so that lambda expressions can be used to implement it.</p><p>After the creation of the Thread instance as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*T_iMaubesUekvcNEYKVOoQ.png" /></figure><p>t1 instance of Thread is in NEW state until the start() method is called. After start() is called upon the thread instance, it moves from the NEW state to Runnable state. Now the Thread can be chosen by the scheduler .whenever the thread is chosen its run() method will be executed. After finishing the execution of the run() method Thread is considered dead and the call stack will be terminated.</p><p><strong>The Thread States and Transitions</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*n4tijsrYZf_3-RUwd9mnSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*3jqI9WoV3-9xoo9nTq8poQ.png" /></figure><p><strong>Sleeping an Executing Thread</strong></p><p>Thread execution can be halted without sending it to the Runnable pool using the sleep () method. It&#39;s a static method of the class Thread. sleep() may throw a checked exception as InteruptedException and need to handle or declare.</p><p>following is a code block for an instance using the sleep()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*ZQtPnEutOY9jGVN5OIHBNQ.png" /></figure><p>parameters for the sleep method are passed as milliseconds. So according to the above example Thread. sleep() will stop the thread execution and after 1000 mile seconds thread will be sent back to the runnable pool. Then the thread scheduler can pick up the thread to continue the execution.</p><p><strong>Thread Priorities</strong></p><p>All the Threads instances have apriority denoted by an integer value from 1–10. The default priority value for the thread is 5 and one can set the priority of a thread explicitly as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*94p--J77uKPL7yPX299BDg.png" /></figure><p>the output of the above application is as follows</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*DIcQ04lQZPWl5fHRlanQww.png" /></figure><p>So whenever there is a thread in the runnable pool with a high priority than the currently executing thread, Scheduler will switch the execution to the high priority thread. If the priority levels are the same execution will be handled normally.</p><p>The <strong>yield()</strong> method will send a running thread to the runnable state, allowing the scheduler to choose another thread with the same priority. but it&#39;s not guaranteed the execution of a separate thread. scheduler might choose the same thread again for the execution. yield() is also a static method from class Thread.</p><p><strong>Join() method</strong></p><p>we have identified two static methods from the Thread class. now let&#39;s move on to a non-static method join().this can be used to join a thread to another.</p><p>As an Example Lets take two threads</p><p><strong>Thread t1 = new Thread();</strong></p><p><strong>t1.start()</strong></p><p>once we execute t1.start() Thread will start its execution.half away through lets initiate another thread as,</p><p>Thread t2= new Thread();<br>Keep in mind at this moment t1 is still executing and we will name it as a currently running thread.</p><p>Then we call the start() for t2 and join().</p><p><strong>t2.start();</strong></p><p><strong>t2.join()</strong></p><p>Currently, the running thread will stop its execution and it joins itself to the end of t2. That means t1 will resume its execution once the t2 finishes its execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*4WEpioUJWU-wpQolR3CkqQ.png" /></figure><p>From the next story, let&#39;s go through Synchronization and some general problems related to threads.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39e10ea88060" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with AWS]]></title>
            <link>https://malinduruwantha.medium.com/getting-started-with-aws-e6a27d189953?source=rss-84332bddfc9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6a27d189953</guid>
            <category><![CDATA[amazon-web-services]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[malindu ruwantha]]></dc:creator>
            <pubDate>Sun, 11 Jul 2021 09:43:07 GMT</pubDate>
            <atom:updated>2021-07-12T03:28:59.134Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 1</h4><p>First, let&#39;s have an understanding of how an online application works. Mainly web applications provide information to the end-users. So there should be two parties as an information requester and the information provider, mapped into the client and the server known by the computing world.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-lU8yqeoa6WYWSQ1z8h5QQ.png" /></figure><p>These web applications will be hosted on distributed servers around the world. In simple terms, AWS provides the needed infrastructure to host your applications.</p><p>In addition to that AWS will fulfill your needs on<br>1.Computing<br>2.Storage<br>3.Network security<br>4.Blockchain<br>and so on…...</p><p>So cloud computing can be described as on-demand consumption of computing power, database, storage, and other IT-related services through a cloud services provider through the internet.</p><p>In the AWS world, the typical server of the client-server model is replaced by <strong>Amazon Elastic Compute Cloud.</strong> This is a virtual server instance also known as the <strong>Amazon EC2 instance</strong>.</p><p>Let&#39;s say you are going to launch an application to the public. Without using a third-party cloud service provider, you will initiate an on-premises data center to fulfill the server and the data storage requirements. Initially, you don&#39;t have a clear idea about the needed storage capacity, User traffic, and the processing power you needed. You just do estimation and then order the needed hardware. when the time goes what if there is a sudden growth of the hits for your application, what if the traffic increases. you might need to scale your infrastructure again which may be costly and a time-consuming process. but if you use AWS instead of your own servers, Scaling can be handled only with a couple of clicks. Either you can increase the EC2 instances that you are using or you can increase the storage and processing power of an EC2 instance.</p><h4><strong>Advantages of using AWS cloud services.</strong></h4><ul><li><strong>No huge initial cost<br></strong>Instead of having to invest heavily in data centers and servers initially, you can pay for the consumption of resources</li><li><strong>Pay as you go prices<br></strong>you can buy or terminate the EC2 instances at any time.</li><li><strong>Do not need to estimate the capacity </strong>when you are setting up your own data centers you might need to estimate the storage and computing needs. but when using AWS you can scale your resources according to your needs.</li><li><strong>No need to worry about the performance issues of the resources</strong></li><li><strong>Go global in minutes<br></strong>As there are data centers all around the world waiting time for a response will be minimum..wherever you access the service responses will be generated from the closest instance of your EC2</li></ul><p>These advantages may be benefit from most cloud service providers.</p><p><strong>Deployment models of cloud computing</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*p15sRnNHt_46Luysw9NElw.png" /></figure><p><strong>Amazon Elastic computer cloud</strong><br>you already know what the EC2 instances replaced. Let&#39;s examine the benefits you gain by using Ec2 instances rather than the typically dedicated hosts.</p><ol><li>you can choose the operating system you need and the other computational specifications</li><li>you are using a shared host that enables cost-effectiveness.</li><li>you can launch an Amazon instance with a couple of clicks and can terminate at any time..you will be charged only for the computational time you used</li><li>can be scaled vertically(Increasing the EC2 instances).</li></ol><p><strong>Amazon EC2 Instance types</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*_8Gv19rBuWXGvGNDl-YILw.png" /></figure><p><strong>Scaling Your Application</strong></p><p>Most of the applications would receive variable demand or the user hits over time. So it is important to design your application architecture to provide its services without any interruption. As an example, if we graph the user traffic against the time it would be looks like follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mxmbc0jznJheTOfWfsGMMw.png" /></figure><p>So how would you allocate resources for this application? Is it to suit the average user demand or for the highest user demand? If you assign resources for the average user demand, the application will be down in the peek times. If you assign for the highest you are paying more. So what will be the solution?<br>It&#39;s non-other than going for on-demand cloud resources.<br>then you can assign resources according to the demand. higher resources for the peak times and you can terminate the resources when the computing demands are low.</p><p>If you want to do this scaling process without any user interaction, you can use <strong>AMAZON EC2 Auto Scaling Service.</strong> then you won&#39;t need to worry about the computing capacity of your application.AWS will handle it on your behalf.</p><p>With the Amazon Auto Scaling service, it will append more Amazon EC2 instances to meet the computing requirements for your application.</p><p>mainly there are two approaches</p><ol><li>Dynamic Scaling (Respond to changing demands)</li><li>Predictive Scaling (scheduling the resources based on the predictions)</li></ol><p>Note: You can configure AMAZON EC2 Auto Scaling by providing the minimum and a maximum number of instances to be provisioned with the varying demand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*BQckI8jCVqo3_DJygcXepg.png" /></figure><p>but when the user traffic increases Amazon will automatically provision another EC2 instance to meet the requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*XWaRJmNsm74NgUn8u4AneA.png" /></figure><p><strong>Load Balancing with AWS</strong></p><p>With the Amazon Auto Scaling feature, we can handle the varying user traffic. How can we distribute the incoming requests among the instances?<br>Yes, That&#39;s where we need to have a load balancing mechanism to control the distribution of traffic among the instances.</p><p>The load balancer will act as a single point that inputs the incoming user traffic. Then it will distribute the requests evenly within the alive instances. This is an instance where single-point failures might erose. Because of that load balancers should be highly available. With Amazon web services you can use several load balancers and also AWS provide their own as <strong>Elastic Load Balancing.</strong></p><p>Now we can alter our architectural diagram as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*U3KRnzp74GBeB24-EM69Hg.png" /></figure><p>Amazon elastic Load balancing service and Amazon EC2 Autoscaling service will be together handling the user traffic by routing the load across the instances and scaling the number of instances to deliver high performance for your application.</p><p>Now with the above-discussed services your overall architecture will be as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NChBzu266OpQmTpobYAoYQ.png" /></figure><p>In the next part let&#39;s further investigate some Amazon web services.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6a27d189953" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>