<?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>Forem: Kamil Bugno</title>
    <description>The latest articles on Forem by Kamil Bugno (@kamilbugnokrk).</description>
    <link>https://forem.com/kamilbugnokrk</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%2F468814%2Fa56ca7d5-c7f9-481c-84f2-3356695e9390.jpg</url>
      <title>Forem: Kamil Bugno</title>
      <link>https://forem.com/kamilbugnokrk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kamilbugnokrk"/>
    <language>en</language>
    <item>
      <title>Zero Framework Overhead with Solid.js</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Tue, 16 Aug 2022 16:46:00 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/zero-framework-overhead-with-solidjs-op2</link>
      <guid>https://forem.com/kamilbugnokrk/zero-framework-overhead-with-solidjs-op2</guid>
      <description>&lt;p&gt;Would you like to learn about a new JavaScript Framework that boasts best in class performance metrics? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=Bubv8wlLIxo"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kDuDU8mU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wbblf30557ouev43f9c.png" alt="Image description" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidjs</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Parallel programming in C#</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Wed, 04 May 2022 09:15:16 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/parallel-programming-in-c-53eg</link>
      <guid>https://forem.com/kamilbugnokrk/parallel-programming-in-c-53eg</guid>
      <description>&lt;p&gt;Do you know what parallel programming is, how it differs from asynchronous programming and why it can enhance your application? If not, let's delve into this incredibly interesting topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is parallel programming?
&lt;/h2&gt;

&lt;p&gt;As the name suggests it means that certain pieces of code can run at the same time. Parallelism is something that you should even know from your everyday life. If this morning you prepared a coffee or a tea, probably you didn't wait until the water boiled to get the cup out of the cupboard. Most likely you did several things at once: you poured water into the kettle, turned it on, and while it was boiling you prepared a cup and poured in the right amount of coffee/tea. Thanks to it the process of preparing your morning drink was faster and you saved time.&lt;/p&gt;

&lt;p&gt;If you want to translate the above situation into the programming world, we will use terms such as CPU and threads. Look at this picture:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fn6k3p2rt2w43l91wl1mv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fn6k3p2rt2w43l91wl1mv.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we have two CPUs, and some threads (black rectangular elements). At the same time, we do two operations: boil the water and prepare the cup. It is good to notice, that making a coffee/tea in a parallel way assumes that we use more than one available CPU to execute our tasks simultaneously.&lt;/p&gt;

&lt;p&gt;It is easy to confuse parallelism with asynchronism because these things seem to be similar, but there is a huge difference between them - let's explore it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parallelism vs. asynchronism
&lt;/h2&gt;

&lt;p&gt;Parallelism means that we can do two or more things at the same time, and you need at least two CPUs for that, while asynchronism can be achieved with one CPU and means that your threads don't actively wait for the end of some external operation. Also, async programming is used more frequently for some IO operations such as connecting to the database, access the file or call the API, whereas parallel programming is more focused of heavy CPU operations such as complex calculations. What is more, asynchronism can be achieved without parallelism: JavaScript is able to use asynchronous operations, but it is impossible to do some parallel stuff (since there is only a single-core). If you are interested in the details of asynchronous programming, read &lt;a href="https://dev.to/kamilbugnokrk/asynchronous-programming-in-c-2jh2"&gt;one of my posts that explains it&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prons and cons of the parallel programming
&lt;/h2&gt;

&lt;p&gt;Parallel programming is not something magical that always have only benefits for our application. Like most things it has its advantages and disadvantages. Let's take a look at them:&lt;br&gt;
✅ As we mentioned earlier, parallelism can reduce the time of our operations because we are able to execute several actions at the same time. &lt;br&gt;
✅ We can use our resources more efficient when we write a code with parallelism in mind.&lt;br&gt;
❌ Parallel code is usually more complex to write, read, and debug.&lt;br&gt;
❌ Parallelism has a strong connection to the hardware, there can be some differences in the execution of our code depending on the underlying hardware.&lt;/p&gt;

&lt;p&gt;You should already understand the basic concept of parallelism, so let's go straight to how it can be achieved in C#.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parallel programming in C#
&lt;/h2&gt;

&lt;p&gt;Let's assume that we have several heavy computations that we would like to conduct. In our scenario, I prepared a special method for it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Start: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&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="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1_500&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="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;30&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="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;120&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End: "&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ElapsedMilliseconds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;The math operations simulate our heavy computation. Without the parallel programming, when we have several computation actions, we can run them in a sequence:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"E"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"All: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ElapsedMilliseconds&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;The output is following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Start: A
End: A 106
Start: B
End: B 106
Start: C
End: C 102
Start: D
End: D 110
Start: E
End: E 114
All: 575


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Thanks to the example, we can see that &lt;code&gt;All&lt;/code&gt; actions take similar or more time than the sum of the times of each action: All &amp;gt;= A + B + C + D + E.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;Parallel.Invoke&lt;/code&gt; method
&lt;/h3&gt;

&lt;p&gt;There is a way of improving the performance of it. We can simply use Task Parallel Library. Our &lt;code&gt;HeavyComputation&lt;/code&gt; method remains unchanged, but we modify the way of calling it by using &lt;code&gt;Parallel.Invoke&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"E"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"All: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ElapsedMilliseconds&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;The output is following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Start: B
Start: A
Start: D
Start: C
Start: E
End: B 103
End: C 109
End: E 111
End: A 112
End: D 112
All: 200


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Based on the input, there are several things that I would like to discuss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;time - as you can see, in the above example &lt;code&gt;All&lt;/code&gt; operations take less time than the sum of each action: All &amp;lt; A + B + C + D + E. The conclusion is one - our computation tasks were run at the same time. It is important that &lt;code&gt;Parallel.Invoke&lt;/code&gt; doesn't &lt;em&gt;guarantee&lt;/em&gt; the parallel execution, because, as we mentioned earlier, it is dependent on other things such as hardware, but it &lt;em&gt;enables&lt;/em&gt; your code to run at once. &lt;/li&gt;
&lt;li&gt;order of operations - &lt;code&gt;Parallel.Invoke&lt;/code&gt; doesn't provide the same execution order as we specified in the method, so this is the reason why our actions were executed in a different order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is also good to know that there are three more useful methods that can help us with executing our code in parallel:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Parallel.For&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Parallel.ForEach&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Parallel.ForEachAsync&lt;/code&gt; - it was added in .NET 6&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Parallelism with asynchronism
&lt;/h3&gt;

&lt;p&gt;Even though the parallel solution may seem to be correct, it can be improved even further, because it has one drawback: it is executed in a synchronous way. What exactly does it mean? I think that visual representation can clarify it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;our current solution (with some simplification) is the following
&lt;img src="https://media.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%2Fsjybpobyy4f7ugp11108.png" alt="Image description"&gt; 
The first CPU run the &lt;code&gt;Main&lt;/code&gt; method and execute our computations (A-E) on separate CPUs. It is important that during the computations, the calling thread is actively waiting for the end of the execution of all computations. &lt;/li&gt;
&lt;li&gt;in async way the picture will be as follows:
&lt;img src="https://media.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%2Fmuaorykaexxvw2e9fwg5.png" alt="Image description"&gt;
During the heavy computation, the first CPU is free and can be used as needed. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How to write a code that will use parallel and asynchronism? The answer in not very complicated: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"E"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"All: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ElapsedMilliseconds&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;We just wrapped the &lt;code&gt;Parallel.Invoke&lt;/code&gt; call inside &lt;code&gt;Task.Run&lt;/code&gt;. Thanks to it we can use the await/async keywords and enhance the solution with asynchronous programming. Although everything looks fine now, there is still one thing to focus on: our computation returns some value, but so far, we haven't used it. Let's change that!&lt;/p&gt;

&lt;h3&gt;
  
  
  Save values from computations
&lt;/h3&gt;

&lt;p&gt;In C# we have special collections designed for concurrent use. One of them is &lt;code&gt;ConcurrentBag&amp;lt;T&amp;gt;&lt;/code&gt; that is thread-safe and can store unordered collection of objects. Our final solution will look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ConcurrentBag&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"E"&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="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"All: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ElapsedMilliseconds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myData&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;We simply add new values to our &lt;code&gt;ConcurrentBag&lt;/code&gt; collection and display it. Besides &lt;code&gt;ConcurrentBag&lt;/code&gt;, there are more thread-safe entities that can store our data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ConcurrentStack&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ConcurrentQueue&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ConcurrentDictionary&amp;lt;TKey,TValue&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is good to know that from the performance perspective, there is one situation when they are slower than their nonconcurrent equivalent: when our solution doesn't use a lot of parallelism. So we have the most to gain from using them if our code is indeed heavily concurrent. &lt;/p&gt;

&lt;h3&gt;
  
  
  Exception handling
&lt;/h3&gt;

&lt;p&gt;Let's modify our code to throw an exception at some point:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&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="p"&gt;=&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"E"&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="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Exception: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&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;I added try/catch block and raised an exception for &lt;code&gt;B&lt;/code&gt; operation - all the rest remains unchanged. There are two things to keep in mind when it comes to exceptions:&lt;br&gt;
a) When one parallel action raises an exception, it doesn't mean, that other operations will be stopped. They will continue their execution despite the fact of the exception. The output from the above code is the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Start: C
Start: A
Starting B
Start: E
Start: D
End: C 125
...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It shows that after the failure of &lt;code&gt;B&lt;/code&gt; operation, the system still executed operation &lt;code&gt;E&lt;/code&gt; and &lt;code&gt;D&lt;/code&gt;. Taking this into consideration, you can assume that parallel operations are separated when it comes to the exception perspective. &lt;br&gt;
b) When we catch the exception, you can see that we are dealing with &lt;code&gt;AggregateException&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://media.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%2Fa04cty7kfn0tkorh9qqr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fa04cty7kfn0tkorh9qqr.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The &lt;code&gt;AggregateException&lt;/code&gt; contain list of all exceptions throw by our parallel operations (in our case it is only 1) and the details of them (in our case it is "B"). It is important that thanks to &lt;code&gt;AggregateException&lt;/code&gt; no exception will be lost, even when they will be raised at the same time. &lt;/p&gt;

&lt;p&gt;There are two ineresting methods for handling &lt;code&gt;AggregateException&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Flatten()&lt;/code&gt; - sometimes there is a need to have a parallel operation that run some nested parallel operations. As a name suggests, &lt;code&gt;Flatten&lt;/code&gt; helps with converting all nested exceptions to one-level exceptions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Handle()&lt;/code&gt; - it provides you with the ability to invoke a handler on each of your exceptions. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see a full example that uses both &lt;code&gt;Flatten()&lt;/code&gt; and &lt;code&gt;Handle()&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&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="p"&gt;=&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;//some nested operations&lt;/span&gt;
                &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
                        &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting B1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidTimeZoneException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                            &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B1"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B2"&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="p"&gt;=&amp;gt;&lt;/span&gt;
                        &lt;span class="p"&gt;{&lt;/span&gt;

                            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting B3"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B3"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                            &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B3"&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="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BadImageFormatException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D"&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="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"E"&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="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AggregateException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Flatten&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myException&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myException&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;InvalidTimeZoneException&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HandleInvalidTimeZoneException&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;ArgumentNullException&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HandleArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;BadImageFormatException&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;HandleBadImageFormatException&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above, we throw three types of exceptions: &lt;code&gt;BadImageFormatException&lt;/code&gt;, &lt;code&gt;ArgumentNullException&lt;/code&gt;, and &lt;code&gt;InvalidTimeZoneException&lt;/code&gt;. Some of the parallel operations are nested, so in the catch block we use &lt;code&gt;Flatten()&lt;/code&gt;. Using &lt;code&gt;Handle&lt;/code&gt; helps us to deal independently with each of the exceptions. There are three helpers methods:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;HandleInvalidTimeZoneException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HandleInvalidTimeZoneException"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;HandleArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HandleArgumentNullException"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;HandleBadImageFormatException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HandleBadImageFormatException"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&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;Each of them handles the exact type of the exceptions and returns true. For &lt;code&gt;Handle&lt;/code&gt; method value true means that the exception was handled correctly and should not be rethrown. This is the reason why we have this operation in the &lt;code&gt;switch&lt;/code&gt; statement: &lt;code&gt;_ =&amp;gt; false&lt;/code&gt; - an exception that is not &lt;code&gt;BadImageFormatException&lt;/code&gt;, &lt;code&gt;ArgumentNullException&lt;/code&gt;, or &lt;code&gt;InvalidTimeZoneException&lt;/code&gt; will be rethrown.&lt;/p&gt;

&lt;p&gt;So far, our example was not complex. In real life scenarios, it may be different, and we will need some synchronization. Let's take a look at it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Synchronization
&lt;/h3&gt;

&lt;p&gt;Synchronization is a response to a situation when a lot of threads access the same data. Why might this situation be problematic? Let's assume that we want to perform some computation in parallel and store the sum of the results in a variable:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;finalResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;For&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
         &lt;span class="n"&gt;finalResult&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We used the &lt;code&gt;Parallel.For&lt;/code&gt; method that is a loop whose iterations are executed at the same time. The first argument is the starting index (in our case 0), second is the final index (in our case 20) and third is an action that contains actual index (in our case &lt;code&gt;i&lt;/code&gt; variable). Every parallel operation adds its result to the &lt;code&gt;finalResult&lt;/code&gt; variable, and here is the issue: we end up with race condition.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is race condition?
&lt;/h3&gt;

&lt;p&gt;Race condition usually occurs when a lot of threads share the same data and want to modify them. Let's see how it can work in our case assuming that HeavyComputation returns value 10 all the time:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Thread X enters the line &lt;code&gt;finalResult += HeavyComputation(i.ToString());&lt;/code&gt;. It checks the &lt;code&gt;finalResult&lt;/code&gt; value and currently it is zero.&lt;/li&gt;
&lt;li&gt;Thread Y enters the same line, checks the &lt;code&gt;finalResult&lt;/code&gt; value that is 0, conduct the computations and modify the value to 10.&lt;/li&gt;
&lt;li&gt;Thread X execute the computation and add ten to the value of &lt;code&gt;finalResult&lt;/code&gt;. Because it checked the &lt;code&gt;finalResult&lt;/code&gt; before it was modified by thread Y, the final value is ten instead of twenty. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Synchronization can deal with the race conditions by, among other things, using locks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Locks
&lt;/h3&gt;

&lt;p&gt;Locks are part of exclusive locking constructs. It means that only one thread can be in a section of the code that is protected by the &lt;code&gt;lock&lt;/code&gt;. All other threads are blocked until the section is free and a new one can enter. Because of it, you should always &lt;code&gt;lock&lt;/code&gt; a code that execute quickly to avoid bottlenecks. Let's see how it can be implemented in C#:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;finalResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;syncRoot&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Parallel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;For&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;localResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="k"&gt;lock&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;syncRoot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//one thread at the same time&lt;/span&gt;
            &lt;span class="n"&gt;finalResult&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;localResult&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="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We used &lt;code&gt;lock&lt;/code&gt; keyword that introduces the section where only one thread can be at the same time. As you can see, the section contains an operation of adding the value to &lt;code&gt;finalResults&lt;/code&gt; - the computation is done before and, thanks to this, it can still be done in parallel. As we mentioned earlier, the code inside &lt;code&gt;lock&lt;/code&gt; shouldn't be time consuming, because we will have a bottleneck and won't really use the power of parallelism.  &lt;/p&gt;

&lt;p&gt;One of the risks of using &lt;code&gt;lock&lt;/code&gt; is dead lock. It is a situation when all threads are waiting (usually for each other) and the application cannot continue working. To avoid dead lock, it is good to have a separate &lt;code&gt;syncRoot&lt;/code&gt; for every &lt;code&gt;lock&lt;/code&gt; and minimizing the nesting of the locks. &lt;/p&gt;

&lt;p&gt;It is worth mentioning that under the hood of &lt;code&gt;lock&lt;/code&gt; we have a &lt;code&gt;Monitor.Enter&lt;/code&gt; and &lt;code&gt;Monitor.Exit&lt;/code&gt; operations. Let's see how the above code looks in Intermediate Language (IL):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;HeavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;syncRoot&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;lockTaken&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Monitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Enter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;lockTaken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;finalResult&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;finally&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="n"&gt;lockTaken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Monitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When we execute &lt;code&gt;Monitor.Enter&lt;/code&gt; without any exception, the value of &lt;code&gt;lockTaken&lt;/code&gt; will be changed to true. Later our code will be run (in this case &lt;code&gt;finalResult += num;&lt;/code&gt;) and in the &lt;code&gt;finally&lt;/code&gt; block we exit the monitor. &lt;code&gt;finally&lt;/code&gt; section is extremely important because it protects us from the situation that our exception resulted in having a thread that enter the &lt;code&gt;lock&lt;/code&gt; block but doesn't exit.  &lt;/p&gt;

&lt;p&gt;It is good to know that inside &lt;code&gt;lock&lt;/code&gt; block we are not allowed to use async operations - otherwise you get an error: &lt;em&gt;cannot await in the body of a lock statement&lt;/em&gt;. Why is there this limitation? Because from the thread perspective &lt;code&gt;await&lt;/code&gt; keyword can create a new thread, but inside the &lt;code&gt;lock&lt;/code&gt; only one thread is allowed. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this post I tried to explain fundamentals of parallel computing: we learned why it is important, what are the advantages and disadvantages of parallelism and how to write correct concurrent code in C# using Task Parallel Library and locks. I hope that now you know all the essentials to start your journey into the parallelism! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Demystifying package.json</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Mon, 25 Apr 2022 11:15:27 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/demystifying-packagejson-1amh</link>
      <guid>https://forem.com/kamilbugnokrk/demystifying-packagejson-1amh</guid>
      <description>&lt;p&gt;If you work with a frontend, your project usually contains &lt;em&gt;package.json&lt;/em&gt; file. Do you know what it's for and why this file is so important? Let's take a closer look at &lt;em&gt;package.json&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In earlier times, when you met a representative of some company, you were likely to receive a business card. Small piece of paper contained all necessary information: name of the person, contact, company details, etc. File called &lt;em&gt;package.json&lt;/em&gt; is something similar to a business card, but it describes your project. Let's see how the file can look.&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="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-modal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3.15.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Accessible modal dialog component for React.JS&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;main&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./lib/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;module&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./lib/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repository&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;git&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://github.com/reactjs/react-modal.git&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;homepage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://github.com/reactjs/react-modal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bugs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://github.com/reactjs/react-modal/issues&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;directories&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;example&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;examples&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;authors&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ryan Florence&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;license&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MIT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;devDependencies&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dependencies&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;peerDependencies&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tags&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;keywords&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;engines&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;gt;=8&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above file comes from &lt;em&gt;react-modal&lt;/em&gt;, an open-source library. As you can see, it contains some data about the project (&lt;code&gt;name&lt;/code&gt;, &lt;code&gt;version&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt;, etc.) and lists all external projects that were used to build the library (&lt;code&gt;devDependencies&lt;/code&gt;, &lt;code&gt;dependencies&lt;/code&gt;, &lt;code&gt;peerDependencies&lt;/code&gt;). &lt;/p&gt;

&lt;h3&gt;
  
  
  Dependencies
&lt;/h3&gt;

&lt;p&gt;Currently, software projects aren't created in a vacuum. Otherwise, a development of new software would not be effective because you would need to reinvent the wheel repeatedly. In reality, most projects use previously developed solutions and build new user functionalities on top of them. This is called dependencies. &lt;/p&gt;

&lt;h3&gt;
  
  
  Versions of dependencies
&lt;/h3&gt;

&lt;p&gt;Each dependency contains a name and a version: &lt;code&gt;"react-dom": "^16.13.1"&lt;/code&gt;. As you can see, the version is not just a number, but it has some characters. There is a special logic behind this and we will take a look at it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;16&lt;/code&gt; - in our example, it is a major version. Usually, number that represent the major part is changed when incompatible or really big, new functionalities are added the project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;13&lt;/code&gt; - it is a minor version. When small, new functionalities are added to the project, the minor version is usually incremented.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; - it is a patch version. If the code contains some bugs, after the deployment of fixes, the patch number is changed. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; - it means that only minor and path number can be increased. For example, &lt;code&gt;"17.10.2"&lt;/code&gt; wouldn't comply this whereas &lt;code&gt;"16.14.2"&lt;/code&gt; is ok and can be used by our project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are more options for special characters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt; - it means that all versions higher than the given can be used. For example, &lt;code&gt;"17.10.2"&lt;/code&gt; is ok, but &lt;code&gt;"15.11.3"&lt;/code&gt; isn't - the number is lower than our initial version &lt;code&gt;"16.13.1"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;~&lt;/code&gt; - it means that only patch number can be increased. For example, &lt;code&gt;"16.14.2"&lt;/code&gt; is not correct, because minor version is changed, but &lt;code&gt;"16.13.2"&lt;/code&gt; is ok - the difference is only for the path part.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;no character&lt;/code&gt; - it means that only the exact version is valid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to to play around with the versions, I recommend this site: &lt;a href="https://semver.npmjs.com/" rel="noopener noreferrer"&gt;https://semver.npmjs.com/&lt;/a&gt; &lt;br&gt;
&lt;a href="https://media.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%2F4yrf1whege8ie68qkd56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4yrf1whege8ie68qkd56.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing dependencies
&lt;/h3&gt;

&lt;p&gt;Let's assume that we have a &lt;em&gt;package.json&lt;/em&gt; file and we want to run our project. One of the necessary actions that we need to take is to install all dependencies. It can be done by &lt;code&gt;npm install&lt;/code&gt;. As a result, a new &lt;em&gt;node_module&lt;/em&gt; folder will be created, and it will contain all packages from the section &lt;code&gt;dependiencies&lt;/code&gt; of &lt;em&gt;package.json&lt;/em&gt;. If your dependencies are dependent on other packages, they also will be downloaded and placed in &lt;em&gt;node_module&lt;/em&gt; folder, even though they are not explicitly listed in your &lt;em&gt;package.json&lt;/em&gt; - it is good to know that installing packages from &lt;code&gt;dependencies&lt;/code&gt; section is transitive.&lt;/p&gt;

&lt;p&gt;Sometimes when we use a lot of external packages, &lt;em&gt;node_module&lt;/em&gt; folder can have an immense size. Because it is easy to recreate that folder (by installing the dependencies), you shouldn't include it to the repository - all team members are able to execute &lt;code&gt;npm install&lt;/code&gt; to download necessary dependencies. &lt;/p&gt;

&lt;p&gt;You can also notice that there is a new file added in your solution: &lt;em&gt;package-lock.json&lt;/em&gt;. In the &lt;code&gt;dependiencies&lt;/code&gt; section of that file, all dependant packages will be listed (even those that are indirectly dependent). I will explain &lt;em&gt;package-lock.json&lt;/em&gt; more deeply later in this post. &lt;/p&gt;
&lt;h3&gt;
  
  
  Types of dependencies
&lt;/h3&gt;

&lt;p&gt;In our example, &lt;em&gt;react-modal&lt;/em&gt; has three types of dependencies: &lt;code&gt;devDependencies&lt;/code&gt;, &lt;code&gt;dependencies&lt;/code&gt;, and &lt;code&gt;peerDependencies&lt;/code&gt;. Let's clarify the differences between them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dependencies&lt;/code&gt; - as we mentioned earlier, all packages from &lt;code&gt;dependencies&lt;/code&gt; section are installed along with its dependencies. If I want to install package X (as either &lt;code&gt;dependencies&lt;/code&gt; or &lt;code&gt;devDependencies&lt;/code&gt;) that has Y as &lt;code&gt;dependencies&lt;/code&gt;, and Y has package Z as &lt;code&gt;dependencies&lt;/code&gt;, all three packages (X, Y, and Z) will be installed and placed in &lt;em&gt;node_modules&lt;/em&gt;.
&lt;img src="https://media.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%2F95xmrbmobirjch04p40u.png" alt="Image description"&gt; 
If you want to add a new package to your solution, you can use &lt;code&gt;npm install NameOfThePackage&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;devDependencies&lt;/code&gt; - while &lt;code&gt;dependencies&lt;/code&gt; are transitive, &lt;code&gt;devDependencies&lt;/code&gt; aren't. In the above example, when X is defined as &lt;code&gt;dependencies&lt;/code&gt; or &lt;code&gt;devDependencies&lt;/code&gt; in your project and both Y and Z packages are defined as &lt;code&gt;devDependencies&lt;/code&gt;, they will not be added to the solution - only X will be downloaded. 
&lt;img src="https://media.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%2F8gqfvs92j6bvs1z0cr7m.png" alt="Image description"&gt;
If you want to add a new package as a &lt;code&gt;devDependencies&lt;/code&gt; to your solution, you can use &lt;code&gt;npm install --save-dev NameOfThePackage&lt;/code&gt;. Development dependencies as a name suggest are helpful when it comes to develop the project locally. When it is deployed to the production, they are not useful and won't be added to &lt;em&gt;node_module&lt;/em&gt;. So when you use a command &lt;code&gt;npm install --production&lt;/code&gt;, and X is defined as &lt;code&gt;devDependencies&lt;/code&gt; it won't be downloaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the above dependency classification, the question may arise of what type to use. There is a lot of opposite views on this topic, so I would like to discuss it more deeply. Let's assume that you want to build a React application and you used Create React App (CRA) tool for a configuration. When you open the &lt;em&gt;package.json&lt;/em&gt; file, you can notice that there are only &lt;code&gt;dependencies&lt;/code&gt;, not &lt;code&gt;devDependencies&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@testing-library/jest-dom"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.16.4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@testing-library/react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^13.1.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@testing-library/user-event"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^13.5.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react-dom"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react-scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"5.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"web-vitals"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.1.4"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some of the Internet suggestions say that you should not use &lt;code&gt;dependencies&lt;/code&gt; for packages that are focused on local development activities, such as testing libraries, code formatters, etc. But as you can see above, &lt;em&gt;@testing-library/jest-dom&lt;/em&gt; that is helpful for writing tests is added as &lt;code&gt;dependencies&lt;/code&gt;. To complicate matters further, when you visit, for example, &lt;em&gt;prettier&lt;/em&gt; or &lt;em&gt;eslint&lt;/em&gt; projects, you can see that on &lt;a href="https://www.npmjs.com" rel="noopener noreferrer"&gt;https://www.npmjs.com&lt;/a&gt; (official npm package repository) the instruction for installation is the following: &lt;code&gt;npm i eslint&lt;/code&gt;. However, the &lt;em&gt;eslint&lt;/em&gt; documentation says that you should use &lt;code&gt;npm install eslint --save-dev&lt;/code&gt;. So, which one is better to use?&lt;br&gt;
Before answering the question, let's separate the types of applications that you can build with the help of &lt;em&gt;package.json&lt;/em&gt; file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;user applications: when you use, for example, React and your application is accessible for users by the browser, we can say that it is a user application. &lt;/li&gt;
&lt;li&gt;developer application: when you build a piece of code that will be used by other developers, we can assume that you are working on a developer application. For example, &lt;em&gt;react-modal&lt;/em&gt; is a developer application, because the main goal of it is to reuse its functionalities in external projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having this distinction in mind, we can finally answer our question. When you build a developer application, you should add all packages which your app actively uses as an imported external functionality as &lt;code&gt;dependencies&lt;/code&gt;. So, when you build a modal library that uses on its foundation &lt;em&gt;react-modal&lt;/em&gt;, &lt;em&gt;react-modal&lt;/em&gt; should be placed in &lt;code&gt;dependencies&lt;/code&gt; section. But when you use &lt;em&gt;prettier&lt;/em&gt; to format your code, you should download it as &lt;code&gt;devDependencies&lt;/code&gt;. The above suggestions are valid when you build a developer application, for user applications there are some changes: it doesn't matter what type of dependencies you use. This is the reason why CRA placed all external packages in &lt;code&gt;dependencies&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;If you're observant, you probably noticed that we haven't discussed one type of dependencies yet: &lt;code&gt;peerDependencies&lt;/code&gt;. I deliberately delayed explaining this because we need a distinction of application types which has just been introduced. &lt;code&gt;peerDependencies&lt;/code&gt; are used for developer applications. Let's assume that our project uses package X and there is a high probability that other developers that will download our package also use X:&lt;br&gt;
&lt;a href="https://media.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%2Fjc99ilnbgo90gopuekhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fjc99ilnbgo90gopuekhc.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
In that case, we can require a specific version of X package from developers who use our code. We can do it by writing: &lt;code&gt;"X": "^2.14.0&lt;/code&gt; in the &lt;code&gt;peerDependencies&lt;/code&gt; section. As a result, the project that uses our package won't be able to download X with a lower version. If you, for example, tried to download X in &lt;code&gt;"1.10.2"&lt;/code&gt; version you will receive the error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Found: x@1.10.2
node_modules/x
   x@"1.10.2" from the root project
Could not resolve dependency:
peer x@"^2.14.0" from yourProject@1.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why do we need &lt;em&gt;package-lock.json&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;As we mentioned earlier, when you run &lt;code&gt;npm i&lt;/code&gt; you can see that &lt;em&gt;package-lock.json&lt;/em&gt; file is added to your project. The main goal of this file is to store information about all dependencies and versions that your project uses. The file is able to contain nested dependencies (for example, our project is dependent on X, X on Y, and Y on Z - in &lt;em&gt;package-lock.json&lt;/em&gt; all three dependencies, X, Y, and Z will be added). To understand why &lt;em&gt;package-lock.json&lt;/em&gt; is so important take a look at the picture:&lt;br&gt;
&lt;a href="https://media.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%2Far8sibtl8whhtvjjoiir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Far8sibtl8whhtvjjoiir.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We have a project that has several dependencies. If you remember the version principles, you should know that, for example, Z package can have not only &lt;code&gt;"7.8.9"&lt;/code&gt; version but also all versions with minor number increased. So, let's assume, that there is a release of a new version for Z package: &lt;code&gt;"7.12.3"&lt;/code&gt;. The same situation can be for our direct dependant: A package. When one user run &lt;code&gt;npm i&lt;/code&gt; before a release of an updated version, the up-to-date version won't be installed. As a result, we can end up with a situation, when we use different versions of dependencies for our project (depending on when &lt;code&gt;npm i&lt;/code&gt; took place):&lt;br&gt;
&lt;a href="https://media.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%2Fas3ee1jg8zf8h0s5cb78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fas3ee1jg8zf8h0s5cb78.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Why might this situation be a problem? Let's assume that package A has a new functionality in version &lt;code&gt;"10.23.8"&lt;/code&gt; that wasn't there before. Dorothy decided to use this new feature in a code and merged her changes to a repository. When Adam tried to run the code, there is an error - he used version &lt;code&gt;"10.11.12"&lt;/code&gt;. &lt;em&gt;package-lock.json&lt;/em&gt; as a name suggests, lock all versions of used dependencies so that we are sure that the entire team will use the same version of external packages. It is important that &lt;em&gt;package-lock.json&lt;/em&gt; should be included in our repository to use the power of locking dependencies' versions.&lt;/p&gt;
&lt;h3&gt;
  
  
  Scripts
&lt;/h3&gt;

&lt;p&gt;Another thing that is specified in &lt;em&gt;package.json&lt;/em&gt; file is a section called &lt;code&gt;scripts&lt;/code&gt;. When you create a React application using CRA, you can see the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"eject"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts eject"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you, for example, run &lt;code&gt;npm start&lt;/code&gt; the command will be translated to &lt;code&gt;react-scripts start&lt;/code&gt; because of the section &lt;code&gt;scripts&lt;/code&gt;. There are a huge number of script's names that are supported by npm, but you are also free to specify your own names. Thanks to it, all team members that download your project with &lt;em&gt;package.json&lt;/em&gt; file will be able to run the script with the custom name by using &lt;code&gt;npm run CustomNameOfTheScript&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;As you can see, one &lt;em&gt;package.json&lt;/em&gt; contains many important things for your project. I hope that this post has made you more aware of the intricacies that may lie behind this file.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Asynchronous programming in C#</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Fri, 15 Apr 2022 10:16:52 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/asynchronous-programming-in-c-2jh2</link>
      <guid>https://forem.com/kamilbugnokrk/asynchronous-programming-in-c-2jh2</guid>
      <description>&lt;p&gt;Would you like to find out why asynchronous programming is important, how to use it in C#, and what advantages asynchronous code has? Let's get into it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should we use asynchronous programming?
&lt;/h2&gt;

&lt;p&gt;Most people who describe usage of asynchronism explain it by the example of a desktop app, so I will focus on this topic first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Desktop app
&lt;/h3&gt;

&lt;p&gt;Let's assume that we have a button called 'Process' in our desktop app, and when a user clicks it some computation that require access to external resources will take place (for example, API or database). If we don't use asynchronism, the entire app will be frozen during the processing time - our user won't be able to interact with the app. The different situation is with the power of asynchronous code. A main thread that is responsible for displaying the app view won't be blocked, and no frozen time will appear.&lt;/p&gt;

&lt;p&gt;Let's visualize this situation in a detailed way:&lt;br&gt;
1) On the left we have our desktop app. On the right there are details related to CPU and threads. When the app was rendering a view (UI) for the first time, a thread used some resources to do it (black rectangular element). Later, we can observe that there isn't any other action, and the app is idle. Nothing will happen until a user interact with the UI. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fld0xs8ys95kv03tzve9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fld0xs8ys95kv03tzve9y.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2) Let's imagine that the user clicked the button. The app started to be active and a thread was used to handle user interaction.&lt;br&gt;
&lt;a href="https://media.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%2Fnukdrqd15o570kds7ild.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fnukdrqd15o570kds7ild.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3) If you remember, we assumed that an action behind this button is to access external API or database. Let's see what will happen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fez6h9g6cnm8pyhudolqx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fez6h9g6cnm8pyhudolqx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4) And here is the most crucial point: when we don't use asynchronous programming, our thread will be actively wait until the end of the API call. During this time, the application will be frozen, and the user won't be able to interact with it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F4qiw2v1gzc138a21evza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4qiw2v1gzc138a21evza.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5) In an alternative situation with asynchronous programming, you can notice that during external call our CPU is free and the user can easily interact with the app without the frozen time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ft6yu5g9d7lopu9fqc9j4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ft6yu5g9d7lopu9fqc9j4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above situation was described for a desktop app, but let's be honest - this type is not as popular as it used to be. Currently, we usually create some web apps with a backend. So, what effect on it will asynchronous programming have?&lt;/p&gt;

&lt;h3&gt;
  
  
  Web server app
&lt;/h3&gt;

&lt;p&gt;Let's assume that we have an API written in C#, and one of its endpoints accesses the database to retrieve some data. If we don't use asynchronous programming the thread will be waiting during the database retrieval operation. As a result, when a new request is come to our API, we couldn't process it effectively. To make matters worse, our system can create some additional threads in the background to handle new requests. Why this situation is not good? Because creating new threads is consuming, and when the CPU is busy with our first request, newly created threads won't be scheduled and unfortunately, they will be waiting. &lt;/p&gt;

&lt;p&gt;Let's observe this situation on the images:&lt;br&gt;
1) At the beginning we see that a thread was used. The main goal for it was to start the web server, do some start up operations, etc. After that, the server is idle because there aren't any user requests:&lt;br&gt;
&lt;a href="https://media.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%2Fu22jiz0fdba7iy2iyiro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fu22jiz0fdba7iy2iyiro.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2) Our API received one HTTP request. A thread will take care of handling this request:&lt;br&gt;
&lt;a href="https://media.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%2F24ktubgrfpq6sapr7uk7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F24ktubgrfpq6sapr7uk7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3) During that request we want to get some data from external resource. When we reach the C# code responsible for retrieving data from the database, the situation will be the following: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fnon11znq8lg2mh9rftsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fnon11znq8lg2mh9rftsz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4) Without asynchronism we end up with the thread that will do nothing except for waiting for the database query's result. New requests cannot be easily processed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fqrcomfaccaai9in96p0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqrcomfaccaai9in96p0m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5) With asynchronous programming we will have a free CPU that can handle new requests, so our API is more effective - it can process more requests than in the previous situation. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F9xijpuyqm79nkhbecgn3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F9xijpuyqm79nkhbecgn3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The web server explanation assumed that we had only one CPU, because I want to keep it as simple as possible, but the situation with several CPU will be quite similar. &lt;/p&gt;

&lt;p&gt;Currently, you should know what the benefits of asynchronous programming are. Let's see how we can use it in C#.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async and await
&lt;/h2&gt;

&lt;p&gt;If we want to use the power of asynchronism, we can easily do it in C# by two keywords: async and await. It is good to know that there are some rules related with these words - let's dig into this.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Method that uses await should be async
&lt;/h3&gt;

&lt;p&gt;If we assume that we want to make some HTTP request, we can do it in that way:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;DoSth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Please note that &lt;code&gt;GetAsync&lt;/code&gt; is an asynchronous method and thus should be called with an await keyword. Every method that uses await must be marked as async. Without having async in the method declaration, we will receive an error and the code won't compile correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. It is possible to run async operation without await
&lt;/h3&gt;

&lt;p&gt;The following code will compile even if there is no await keyword:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;DoSth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What is more, we can also remove the async word, because there is no await usage in the body of the method. This process is called 'Eliding async and await'. It is good to know, that it has some big consequences and is only recommended in a special situation. &lt;/p&gt;

&lt;h4&gt;
  
  
  Consequences
&lt;/h4&gt;

&lt;p&gt;The code will simply continue working without taking care of the async operation. It may seem to be an advantage, but there are some issues related to it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;using statement: if our code is in a using statement, the object can be disposed before the operation ends, and, as a result, the operation won't succeed.&lt;/li&gt;
&lt;li&gt;try/catch block: it may happen that the execution reached the end of try/catch block before the operation ends, and, as a result, we will not be able to properly handle the exception.&lt;/li&gt;
&lt;li&gt;using 'Eliding async and await' incorrectly can even cause a termination of the entire process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Use Case
&lt;/h4&gt;

&lt;p&gt;Let's assume that we have two nested methods A and B (B is called inside A and contains some async operations). Both methods A and B use async/await word. In that scenario, we can skip async/await in the inner method (B) because it will be eventually awaited in A method. This is the only situation, when considering 'Eliding async and await' might be a good idea.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;HttpResponseMessage&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&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;h3&gt;
  
  
  3. Async method can return &lt;code&gt;Task&lt;/code&gt;, &lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;ValueTask&amp;lt;T&amp;gt;&lt;/code&gt;, or be &lt;code&gt;void&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The async method can return several types, and now we will focus on the most common ones:&lt;/p&gt;
&lt;h4&gt;
  
  
  a) &lt;code&gt;Task&lt;/code&gt; vs &lt;code&gt;void&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Task&lt;/code&gt; is usually used when we don't want to return any real value. Why don't use &lt;code&gt;void&lt;/code&gt;? It is true that in non-asynchronous world in this scenario we will use &lt;code&gt;void&lt;/code&gt; without a second thought, but in async code it is not valid anymore. &lt;code&gt;Task&lt;/code&gt; object contains some additional information about the operation and can be awaitable, so this is the reason why most often you will see &lt;code&gt;Task&lt;/code&gt; instead of &lt;code&gt;void&lt;/code&gt;. What is more, it is impossible to use await word to call async method that returns &lt;code&gt;void&lt;/code&gt;. &lt;/p&gt;
&lt;h4&gt;
  
  
  b) &lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt; vs &lt;code&gt;ValueTask&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;When we want to return some value, we have two options &lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code&gt;ValueTask&amp;lt;T&amp;gt;&lt;/code&gt;. What is the difference? The former is a reference type whereas the latter is a value type. &lt;code&gt;ValueTask&amp;lt;T&amp;gt;&lt;/code&gt; is in some cases better when it comes to the performance than &lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;. There is one special scenario for using it: when the execution path can retrieve data in both synchronous and asynchronous way. Sounds complicated? Let's see an example:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;ValueTask&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetPageVersion&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="n"&gt;_cachedValue&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="n"&gt;_cachedValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Version&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&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="n"&gt;_cachedValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&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;As you can see, the method &lt;code&gt;GetPageVersion&lt;/code&gt; behaves differently based on the cached value. It is likely that most of the time, the asynchronous code will not be run. In that case &lt;code&gt;ValueTask&amp;lt;T&amp;gt;&lt;/code&gt; is a better choice. In any other scenarios you should consider &lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Async method can be called in a sync way
&lt;/h3&gt;

&lt;p&gt;It is quite important to be able to distinguish a situation when we have a synchronous and asynchronous code.&lt;br&gt;
Let's see an example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;GetPageVersion&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;GetAwaiter&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;GetResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Version&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&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;We run &lt;code&gt;client.GetAsync&lt;/code&gt; method, and as a name suggest, it is asynchronous method. But the way of calling this method matters: when we use &lt;code&gt;GetAwaiter&lt;/code&gt; and &lt;code&gt;GetResult&lt;/code&gt; the code will be executed synchronously. It is good to know that using it can be dangerous - we can even end up with deadlocks.&lt;/p&gt;

&lt;p&gt;Now, you should know the basics of using async and await in your C# code. But do we really know how it works? Let's explore this topic more deeply.&lt;/p&gt;

&lt;h2&gt;
  
  
  Under the hood of asynchronism
&lt;/h2&gt;

&lt;p&gt;To be able to go under the hood we will need some Intermediate Language (IL) viewer. When we build the project, the .NET environment generates IL code from our source code and thanks to it we can really see what is going here.&lt;/p&gt;

&lt;p&gt;Let's assume that we have a simple class with an empty method. The IL code for it will be the following:&lt;br&gt;
&lt;a href="https://media.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%2F3y49s7h4tta4b49sj0ll.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F3y49s7h4tta4b49sj0ll.png" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;It isn't very readable, but we can see that in some way it is similar to original method, isn't it? But what will happen when we add async word to the method declaration?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F0dkv81xzktdw2wv4hjp6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F0dkv81xzktdw2wv4hjp6.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, IL code is completely different. Instead of having a method &lt;code&gt;DoSth&lt;/code&gt;, we currently have a class. Why? Because async/await words are using a state machine. If you take a close look at the IL code, you can see that &lt;code&gt;DoSth&lt;/code&gt; implements interface &lt;code&gt;IAsyncStateMachine&lt;/code&gt;, contains property &lt;code&gt;state&lt;/code&gt;, and has method &lt;code&gt;MoveNext&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  How does state machine work?
&lt;/h3&gt;

&lt;p&gt;It is important that state machine is not an entity that exists only in C# world, or that is related only to asynchronism. State machine is an implementation of the state design pattern, and you can create your own state machine. It can behave differently depending on its internal state. Sounds confusing? Let's imagine that we have following state machine. &lt;br&gt;
&lt;a href="https://media.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%2F8q1e01116djtwm6iiph3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F8q1e01116djtwm6iiph3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can be hungry or full - these are the states that our machine has. As you may have guessed, we aren't usually all the time hungry or full, during the day it is possible to change the state. By eating pizza, you can move from hungry to full state, and by going swimming your state can reach hungry.  &lt;/p&gt;

&lt;p&gt;Analogous situation we have in C# when it comes to asynchronous programming. The previously mentioned &lt;code&gt;MoveNext&lt;/code&gt; method is responsible for changing the state of the machine and do some actions. &lt;/p&gt;

&lt;p&gt;Let's assume that we have the simple async method:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;DoSth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetStringAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;The method &lt;code&gt;MoveNext&lt;/code&gt; that is generated for this code can look similar to this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;MoveNext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;TaskAwaiter&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;awaiter&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="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;awaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetStringAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;GetAwaiter&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="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsCompleted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskAwaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stateMachine&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asyncTaskMethodBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AwaitUnsafeOnCompleted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;stateMachine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;return&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;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;awaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskAwaiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskAwaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TaskAwaiter&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt;
            &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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;I changed some variable names to make it more readable. The flow of this state machine is the following:&lt;br&gt;
1) At the beginning we have &lt;code&gt;this.state&lt;/code&gt; set to -1.&lt;br&gt;
2) &lt;code&gt;MoveNext&lt;/code&gt; method is called. Because of the state value the &lt;code&gt;if&lt;/code&gt; statement is true, so we execute this piece of code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;awaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetStringAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;GetAwaiter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's assume that the &lt;code&gt;awaiter&lt;/code&gt; is not completed so we set the state to 0 and exit the method:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskAwaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stateMachine&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asyncTaskMethodBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AwaitUnsafeOnCompleted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;stateMachine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3) &lt;code&gt;MoveNext&lt;/code&gt; method is called again. The &lt;code&gt;else&lt;/code&gt; block will be executed because the current machine state is 0:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="n"&gt;awaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskAwaiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskAwaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TaskAwaiter&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt; 
&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As a result, the state will be set temporarily to -1. Later the following piece of code will be run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, the last piece of code will be executed:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;4) The machine is in state -2, and we received the result of our operation. &lt;/p&gt;

&lt;p&gt;The above example shows state machine that was generated by a source code containing only one await keyword. When we use await more than once in the given method, the state machine ends up with more complicated &lt;code&gt;MoveNext&lt;/code&gt; version. If you would like to experiment with it, I recommend &lt;a href="https://sharplab.io/" rel="noopener noreferrer"&gt;https://sharplab.io/&lt;/a&gt; for viewing Intermediate Language or a downloadable tool ILSpy which has similar functionality.&lt;/p&gt;

&lt;p&gt;So far we have learned what a state machine is and how it helps us with asynchronism, but we still have one mystery left to unravel: who calls &lt;code&gt;MoveNext&lt;/code&gt; method?&lt;/p&gt;

&lt;h3&gt;
  
  
  Thread pool
&lt;/h3&gt;

&lt;p&gt;Thread pool is an extremely important entity that takes care of managing the threads. Thanks to it, we don't need to worry about creating a new thread or reusing existing one - thread pool is responsible for that.&lt;/p&gt;

&lt;p&gt;One part of thread pool is a queue that stores free threads.&lt;br&gt;
&lt;a href="https://media.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%2Flzrgcoukqwe6z772xhm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flzrgcoukqwe6z772xhm9.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Since creating a new thread is costly, thread pool monitors all threads and when one of them in no longer used, it is moved to the queue. As soon as our application needs additional thread, thread pool checks if there are free threads to schedule, and only if there are no threads in the queue, a new one is created. We already know the basics of thread pool, so let's get back to our state machine and &lt;code&gt;MoveNext&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;From Thread Pool's perspective, what will the execution of the following program approximately look like?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;DoSth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetStringAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.kamilbugno.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;Given some simplification, the way how thread pool helps us with the execution is not very complicated: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;State machine will be created, and thread pool will schedule the call to the Internet by running &lt;code&gt;MoveNext&lt;/code&gt; method (a new/different thread can be used to do it),&lt;/li&gt;
&lt;li&gt;The web request was sent, and we are waiting for response...&lt;/li&gt;
&lt;li&gt;Thread pool is notified by external entity such as a network driver that the web request completed, and it runs &lt;code&gt;MoveNext&lt;/code&gt; method again (a new/different thread can be used to do it). &lt;/li&gt;
&lt;li&gt;We receive the result of our operation and &lt;code&gt;DoSth&lt;/code&gt; method is completed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By now you should know all the necessary things to consciously use the power of asynchronism in C#.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;As you can see, asynchronous programming can help you to create more effective application and to use the available resources (such as CPU, memory, etc.) better. I hope this post enriched your knowledge about asynchronism and currently, you will be able to easily write asynchronous code.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;You can also read this post on my official blog: &lt;a href="https://blog.kamilbugno.com/" rel="noopener noreferrer"&gt;https://blog.kamilbugno.com/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Availability in Azure</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Wed, 09 Mar 2022 07:14:51 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/availability-in-azure-4m5o</link>
      <guid>https://forem.com/kamilbugnokrk/availability-in-azure-4m5o</guid>
      <description>&lt;p&gt;Sometimes we observe a downtime of popular websites because of a cloud provider's outage. The fact that we use a cloud doesn't automatically guarantee high availability of our application - there is a shared responsibility between cloud providers and its clients. Let's learn how we can use the power of cloud to make sure that our Azure resources are high available.&lt;/p&gt;

&lt;h2&gt;
  
  
  How much money can the company lose due to the outage?
&lt;/h2&gt;

&lt;p&gt;Are you wondering how much can the outage cost? For the most popular services, the loss of money is likely to be mind-boggling - for example, YouTube was down for 37 minutes in 2020 and it resulted in losses of $1.7 million [1]. In 2013 Amazon.com had a downtime and each minute of the outage costed them about $66,240 [2]. These enormous numbers show that high availability is an important topic and can have a critical impact on the company's finances, so let's delve into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to protect our application from downtime?
&lt;/h2&gt;

&lt;p&gt;Imagine that we have a business-critical application hosted on virtual machine. There are several ways how we can reduce the risk of outage.&lt;/p&gt;

&lt;h3&gt;
  
  
  No protection
&lt;/h3&gt;

&lt;p&gt;The starting point is to have a single virtual machine. Users can access the application by sending requests directly to HTTP/S port of the VM and in this way interact with our application. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5pRaluX---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auchlsqk1teq0tjiu4hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5pRaluX---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auchlsqk1teq0tjiu4hv.png" alt="Image description" width="749" height="217"&gt;&lt;/a&gt;&lt;br&gt;
As long as the virtual machine works correctly our application is available. This is not a recommend solution because each downtime of our single VM will result in situation where our users won't be able to access the application. Let's see how we can improve it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Isolated hardware redundancy
&lt;/h3&gt;

&lt;p&gt;To help us protect the virtual machines, Azure introduced special entities called fault and update domains. Let's take a look at them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fault domain - it is a set of virtual machines that use the same power and network infrastructure. When there will be a power, network or hardware issue, all virtual machines in that domain will be affected.&lt;/li&gt;
&lt;li&gt;update domain - it is a set of virtual machines that can be updated and rebooted at the same time. Azure ensures that two virtual machines that are in a separate update domain will not be affected at the same time, when maintenance activities will be required.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a result, we can use the power of the fault and update domains and deploy two virtual machines - each in a separate domain. It can be done by using an availability set:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zaz-aodp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7n5vjac2prdkjgj5q5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zaz-aodp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7n5vjac2prdkjgj5q5g.png" alt="Image description" width="708" height="450"&gt;&lt;/a&gt;&lt;br&gt;
All you need to do is to create two virtual machines and during the creation add them to the availability set. After it you can check that the virtual machines are placed in a different update and fault domain:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ezhAnlwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1mi2alw7ieiqz0rt1ay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ezhAnlwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1mi2alw7ieiqz0rt1ay.png" alt="Image description" width="759" height="141"&gt;&lt;/a&gt;&lt;br&gt;
There is also one important fact - we need a special service that will be responsible for balance the traffic to our virtual machines. We can use Azure Load Balancer for it and configure backend pool to contain our virtual machines. The architecture of that solution is the following:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cVvMGZPh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahnhj6vv6pf82dba5mpj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cVvMGZPh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahnhj6vv6pf82dba5mpj.png" alt="Image description" width="880" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This solution helps us to provide higher availability, but it has its limits. For example, when the whole data center will be down, our application still will be impacted. For that scenario, we will need more protection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Availability zone redundancy
&lt;/h3&gt;

&lt;p&gt;It is good to know that Azure has in almost every region three separate data centers. What is more, there is a rule that each of the data centers should be in a distance of at least 16 km. Thanks to it, when some natural disaster occurs, it is more likely that not all the data centers will be impacted. During the creation of the VM we can select the availability zone and add virtual machines to a separate zone. We still need a load balancer to handle the traffic and it can be configured for our virtual machines:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VEClCJuj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02iwvajdkm5rj234uwry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VEClCJuj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02iwvajdkm5rj234uwry.png" alt="Image description" width="719" height="205"&gt;&lt;/a&gt;&lt;br&gt;
Thanks to it, when one of the data center will be down, load balancer will send traffic to the second virtual machine in a different availability zone (AZ). &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kyVUFe_j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m39n9ybe1kpds4n5e6k9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kyVUFe_j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m39n9ybe1kpds4n5e6k9.png" alt="Image description" width="880" height="353"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This solution can fulfill the most common availability requirements, but what if we need a higher protection?&lt;/p&gt;

&lt;h3&gt;
  
  
  Region redundancy
&lt;/h3&gt;

&lt;p&gt;To achieve region redundancy, some more steps are required. It is important that currently there is no load balancer in general availability (GA) that supports sending traffic across several regions. For that scenario we need to use a different service. One of the options is Traffic Manager. We can configure Traffic Manager to send requests to our virtual machines placed in different regions:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--42Hh_hZi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9waxy6wl10gqd2x73v5r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--42Hh_hZi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9waxy6wl10gqd2x73v5r.png" alt="Image description" width="880" height="286"&gt;&lt;/a&gt;&lt;br&gt;
During the creation of the Traffic Manager, we are required to select a routing method:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RQgpM8xa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6fadxjb99j2t4xxtbhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RQgpM8xa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6fadxjb99j2t4xxtbhw.png" alt="Image description" width="424" height="498"&gt;&lt;/a&gt;&lt;br&gt;
Let's take a look at some of these values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;priority - it is used to configure primary, secondary, and so on resources to which the traffic will be redirected. For example, we want to send users' requests to the first VM and only when the VM is unavailable the requests should be transferred to the second VM. In that case, the first VM should receive priority 1 and the second the priority 2:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3XHhoDYK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ceo9sw73wg0b90jp67e1.png" alt="Image description" width="321" height="464"&gt;
&lt;/li&gt;
&lt;li&gt;performance - it gives the opportunity to use not only the power of availability but also improve the performance. Your users will be redirected to resources which are the closest for them from the network latency point of view. It is determined by using the source IP address that is related to the recursive DNS service.&lt;/li&gt;
&lt;li&gt;weighted - you can distribute the traffic based on the weight. For example, when there is a need to balance the traffic equally between two VMs, you should assign the same weight (in our case the value is 50):
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t1CXlTy2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7a4nq7r0atdqu5gsdz9z.png" alt="Image description" width="880" height="315"&gt;
This routing method can also be used for blue-green deployments. Let's assume that we have an updated version of our application, and we want to provide it gradually to the users. We can configure that only 20% of our users will be able to use the new version - when it turns out that there are no issues related with the newly added functionality, we can make the latest version available for more users.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XPCMfmH7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryq4ifd4vqmd72co7huh.png" alt="Image description" width="880" height="315"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So far, we discussed having only two virtual machines. Let's be clear, in a real-life scenario we will usually use much more virtual machines - that brings us to a solution when we use both Traffic Manager and Load balancer with multiple virtual machines:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CMHbm6mS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r5elgjo3w3sskhn8xn2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CMHbm6mS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r5elgjo3w3sskhn8xn2.png" alt="Image description" width="880" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Availability with scalability
&lt;/h2&gt;

&lt;p&gt;The above architecture is great when it comes to high availability, but from the scalability point of view it can be improved. Let's assume, that our application has a varied user traffic - at some point a vast number of users visit our application, but there are also periods of lower traffic. In that case, the best option is to use virtual machine scale sets (VMSS). VMSS is responsible for horizontal scaling - when the traffic is huge, it can automatically deploy new virtual machines, and when the traffic is decreased, number of virtual machines can be reduced. So the final solution can look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K8fYqtYv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq99eaq5qm76szdozprw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K8fYqtYv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq99eaq5qm76szdozprw.png" alt="Image description" width="880" height="664"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Availability is a huge topic
&lt;/h2&gt;

&lt;p&gt;In this post I tried to focus only on virtual machines and load balancer/traffic manager. It is good to remember that this topic is very wide and there are some other important challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;availability of data (CosmosDB, Azure SQL Database, storage accounts, etc.)&lt;/li&gt;
&lt;li&gt;balance the traffic (Application Gateway, Azure Front Door, etc.)&lt;/li&gt;
&lt;li&gt;data backup and recovery (Azure Site Recovery, Azure Backup, etc.)&lt;/li&gt;
&lt;li&gt;more and more...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see, there are a lot of areas to learn about availability - you can treat this post as a small introduction and I hope that it will encourage you to explore this topic deeper.&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
[1] &lt;a href="https://www.foxbusiness.com/technology/google-lost-ad-revenue-during-youtube-outage-expert"&gt;https://www.foxbusiness.com/technology/google-lost-ad-revenue-during-youtube-outage-expert&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://www.forbes.com/sites/kellyclay/2013/08/19/amazon-com-goes-down-loses-66240-per-minute/?sh=5ebfbeae495c"&gt;https://www.forbes.com/sites/kellyclay/2013/08/19/amazon-com-goes-down-loses-66240-per-minute/?sh=5ebfbeae495c&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You can also read this post on my official blog: &lt;a href="https://blog.kamilbugno.com/"&gt;https://blog.kamilbugno.com/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Driving a robot with a neural network [free meetup online]</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Tue, 25 Jan 2022 13:02:52 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/driving-a-robot-with-a-neural-network-free-meetup-online-3967</link>
      <guid>https://forem.com/kamilbugnokrk/driving-a-robot-with-a-neural-network-free-meetup-online-3967</guid>
      <description>&lt;p&gt;Have you ever wondered what it takes to drive a robot with a neural network? Take part in a free online meetup: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.meetup.com/TWF-Meetup/events/283478382/"&gt;https://www.meetup.com/TWF-Meetup/events/283478382/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.meetup.com/TWF-Meetup/events/283478382/"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rXHoaqkK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irefyav4nnms7drq8n8p.jpg" alt="Image description" width="880" height="587"&gt;&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>neuralnetwork</category>
      <category>robot</category>
    </item>
    <item>
      <title>RxJS in Angular: from beginner to advanced beginner</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Thu, 28 Oct 2021 12:58:16 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/rxjs-in-angular-from-beginner-to-advanced-beginner-78c</link>
      <guid>https://forem.com/kamilbugnokrk/rxjs-in-angular-from-beginner-to-advanced-beginner-78c</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=YesyFEyj3UE"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HA25sEQ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrn239wkhquk2z0hg2gq.png" alt="RxJS in Angular: from beginner to advanced beginner" width="880" height="442"&gt;&lt;/a&gt;&lt;br&gt;
View it on YouTube: &lt;a href="https://www.youtube.com/watch?v=YesyFEyj3UE"&gt;RxJS in Angular: from beginner to advanced beginner&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>programming</category>
      <category>youtube</category>
      <category>twf</category>
    </item>
    <item>
      <title>Dependency injection in .NET Core</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Mon, 20 Sep 2021 09:32:50 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/di-in-net-core-5c08</link>
      <guid>https://forem.com/kamilbugnokrk/di-in-net-core-5c08</guid>
      <description>&lt;p&gt;&lt;em&gt;You can also read this post on my blog: &lt;a href="//blog.kamilbugno.com/"&gt;blog.kamilbugno.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Have you ever wondered what DI has to do with sushi, why DI is important and how to achieve it in .NET Core? If yes, this article is for you!&lt;/p&gt;

&lt;h2&gt;
  
  
  DI and sushi
&lt;/h2&gt;

&lt;p&gt;I love sushi. Dipping into soy sauce with a bit of Wasabi paste make sushi delicious 🍽&lt;br&gt;
&lt;a href="https://media.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%2Fzyby0ncsfatuoa1kueom.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fzyby0ncsfatuoa1kueom.jpg" alt="Sushi"&gt;&lt;/a&gt;&lt;br&gt;
But what does this have to do with DI? &lt;/p&gt;

&lt;p&gt;Let's assume, that I want to prepare sushi on my own. Doing it in C# (haha) might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SushiService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Sushi&lt;/span&gt; &lt;span class="nf"&gt;DoSushi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;whiteRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetWhiteRice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;pot&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetPot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;boiledRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Boil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;whiteRice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;smokedSalmon&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetSmokedSalmon&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;seaweedSheets&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetSeaweedSheets&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;//some other steps&lt;/span&gt;
        &lt;span class="c1"&gt;//...&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sushi&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;SmokedSalmon&lt;/span&gt; &lt;span class="nf"&gt;GetSmokedSalmon&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;SeaweedSheets&lt;/span&gt; &lt;span class="nf"&gt;GetSeaweedSheets&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Pot&lt;/span&gt; &lt;span class="nf"&gt;GetPot&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;WhiteRice&lt;/span&gt; &lt;span class="nf"&gt;GetWhiteRice&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why is it not a good idea to prepare sushi without DI?
&lt;/h3&gt;

&lt;p&gt;It can seem correctly, but there are several drawbacks of this solution. For example, the above code assumes, that we can use only one, specific pot. Despite the fact, that in my cupboard there are a lot of pots and all of them can be used to boil something, I don't have sufficient freedom to choose them. What is more, if I have already boiled rice (because I prepared too much for a previous meal), I cannot use it for my sushi - sushi method requires to prepare new rice. Analogous situation is for ingredients, I can have multiple seaweed sheets or smoked salmons (one in the fridge, another in the freezer, etc.), but I am able to use only one of them no matter how many times and when I will be making sushi. As you can see, it is not the best way of preparing sushi. &lt;/p&gt;

&lt;h3&gt;
  
  
  Sushi with DI is delicious
&lt;/h3&gt;

&lt;p&gt;Don't worry, you don't have to be hungry - we can simply refactor our solution and with the power of dependency injection prepare delicious sushi. &lt;/p&gt;

&lt;p&gt;The first step is to create abstraction that is responsible for certain steps of preparing sushi.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SushiService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IRiceRepository&lt;/span&gt; &lt;span class="n"&gt;_riceRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ISalmonRepository&lt;/span&gt; &lt;span class="n"&gt;_salmonRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ISeaweedSheetsRepository&lt;/span&gt; &lt;span class="n"&gt;_seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IPotRepository&lt;/span&gt; &lt;span class="n"&gt;_potRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;SushiService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRiceRepository&lt;/span&gt; &lt;span class="n"&gt;riceRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                        &lt;span class="n"&gt;ISalmonRepository&lt;/span&gt; &lt;span class="n"&gt;salmonRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;ISeaweedSheetsRepository&lt;/span&gt; &lt;span class="n"&gt;seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                        &lt;span class="n"&gt;IPotRepository&lt;/span&gt; &lt;span class="n"&gt;potRepository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_riceRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;riceRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_salmonRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;salmonRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_seaweedSheetsRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_potRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;potRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Sushi&lt;/span&gt; &lt;span class="nf"&gt;DoSushi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;whiteRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_riceRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetRice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;pot&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_potRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetPot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;boiledRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Boil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;whiteRice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;smokedSalmon&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_salmonRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSmokedSalmon&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;seaweedSheets&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSeaweedSheets&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;//some other steps&lt;/span&gt;
        &lt;span class="c1"&gt;//...&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sushi&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advanteges of DI
&lt;/h3&gt;

&lt;p&gt;As a result, our sushi is not dependent on the exact source of salmon or pot instance. It uses repositories to retrieve the desired objects without knowing about the details. From the &lt;code&gt;SushiService&lt;/code&gt; perspective it doesn't matter what exact pot we use and how to obtain it - from now on these considerations belong to &lt;code&gt;IPotRepository&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We can also modify the way of getting boiled rice. Since we use only boiled rice, we can create another piece of abstraction, that is responsible for providing &lt;code&gt;SushiService&lt;/code&gt; with boiled rice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SushiService&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IRiceService&lt;/span&gt; &lt;span class="n"&gt;_riceService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ISalmonRepository&lt;/span&gt; &lt;span class="n"&gt;_salmonRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ISeaweedSheetsRepository&lt;/span&gt; &lt;span class="n"&gt;_seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;SushiService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRiceService&lt;/span&gt; &lt;span class="n"&gt;riceService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                         &lt;span class="n"&gt;ISalmonRepository&lt;/span&gt; &lt;span class="n"&gt;salmonRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                         &lt;span class="n"&gt;ISeaweedSheetsRepository&lt;/span&gt; &lt;span class="n"&gt;seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;_riceService&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;riceService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;_salmonRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;salmonRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;_seaweedSheetsRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Sushi&lt;/span&gt; &lt;span class="nf"&gt;DoSushi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;boiledRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_riceService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetBoiledRice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;smokedSalmon&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_salmonRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSmokedSalmon&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;seaweedSheets&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_seaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSeaweedSheets&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

         &lt;span class="c1"&gt;//some other steps&lt;/span&gt;
         &lt;span class="c1"&gt;//...&lt;/span&gt;

         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sushi&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we don't have to worry about the process of boiling our rice and what piece of equipment we need to use for boiling. All of it is done by &lt;code&gt;IRiceService&lt;/code&gt;. The advantage of this solution is the fact, that we can reuse our &lt;code&gt;IRiceService&lt;/code&gt; in different situations. You don't have to write the same piece of code every time when you want to use boiled rice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt; &lt;span class="c1"&gt;//...&lt;/span&gt;
 &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;whiteRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_riceRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetRice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
 &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;pot&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_potRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetPot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
 &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;boiledRice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Boil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;whiteRice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, you can simply call &lt;code&gt;GetBoiledRice&lt;/code&gt; from &lt;code&gt;IRiceService&lt;/code&gt;. Thanks to this, when the method of preparing boiled rice change, you will modify only one place - &lt;code&gt;RiceService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is also good to know that next benefit of using DI is the testability aspect. Let's imagine that &lt;code&gt;ISalmonRepository&lt;/code&gt; sends HTTP request to order salmon online and it will be shipped to your house. It will be a waste of money to buy salmon each time when you want to check if your method of preparing sushi works. For example, you can ASSUME that you have the salmon and use this assumption when you verify other steps. This is the way how we can do it in C# for all dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
 &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;VerifySushiMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//Arrange&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;riceServiceMock&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IRiceService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;salmonRepositoryMock&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ISalmonRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;seaweedSheetsRepositoryMock&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ISeaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
     &lt;span class="c1"&gt;//...&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sushiService&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SushiService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;riceServiceMock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;salmonRepositoryMock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;seaweedSheetsRepositoryMock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

     &lt;span class="c1"&gt;//Act&lt;/span&gt;
     &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sushi&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sushiService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;DoSushi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

     &lt;span class="c1"&gt;//Assert&lt;/span&gt;
     &lt;span class="c1"&gt;//...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  DI in .NET Core
&lt;/h3&gt;

&lt;p&gt;The last step that is required to complete the process is to inject the dependencies. In .NET Core there is a built-in container that help you to use DI. In &lt;code&gt;Startup&lt;/code&gt; class there is  &lt;code&gt;ConfigureServices&lt;/code&gt; method that you can use to specify the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ConfigureServices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IServiceCollection&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//...&lt;/span&gt;
       &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IRiceService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RiceService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
       &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ISeaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SeaweedSheetsRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt; 
       &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ISalmonRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SalmonRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
       &lt;span class="c1"&gt;//...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.NET Core provides you with the option to control the lifetime of injected services. There are three types to choose from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AddSingleton - it creates one object that will be reused for all requests.&lt;/li&gt;
&lt;li&gt;AddTransient - it creates an object each time when it is requested from the code, so the new instance is provided to every class that use the specified interface.&lt;/li&gt;
&lt;li&gt;AddScoped - it creates the object for each client request, so the object will be different across different client requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;To sum up, using DI is extremely useful because it makes your code more readable, testable, and flexible. As you can see, it is not hard to write a piece of code that use the power of dependency injection. I might even risk saying that it's easier to implement it in C# than making real sushi yourself.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Revision questions for AI-102</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Fri, 18 Jun 2021 09:33:15 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/revision-questions-for-ai-102-1pil</link>
      <guid>https://forem.com/kamilbugnokrk/revision-questions-for-ai-102-1pil</guid>
      <description>&lt;p&gt;&lt;em&gt;You can also read this post on my blog: &lt;a href="//blog.kamilbugno.com/"&gt;blog.kamilbugno.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Would you like to revise your knowledge before the exam? During preparation to AI-102 I made lots of notes, and I decided to share them with you. This article doesn't cover all topics required for AI-102, but I believe it can be a great resource to structure your knowledge. Let's start go deeper into the AI-102 world!&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Cognitive Services?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lamLyTyg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myuasvoapdzho4kv2o5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lamLyTyg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myuasvoapdzho4kv2o5t.png" alt="Cognitive Services" width="880" height="480"&gt;&lt;/a&gt;&lt;br&gt;
It is sometimes called an 'umbrella' service because it contains other AI services such as Face, LUIS, Speech to Text, etc. All the contained services can be used independently - you can search them in Azure Marketplace and add them to your solution. So why do we need Cognitive Services? It is more comfortable to use it when we know that we need several different services related to AI and thanks to Cognitive Services, we don't have to add each of the services separately. There is also a difference in a free tier: Cognitive Services don't have free tier options. On the contrary, other AI services such as Computer Vision and Face provide you with free tier options. &lt;/p&gt;

&lt;h2&gt;
  
  
  Can I use Cognitive Services as a container?
&lt;/h2&gt;

&lt;p&gt;Yes, you can. Thanks to it, you can use AI functionalities on-premises. There are two vital facts related to it: not all AI services support containerization (for example, Face), and you must send metering data to Azure because of the billing process (Azure needs to know how you use its services to be able to prepare a bill for you). &lt;/p&gt;

&lt;h2&gt;
  
  
  What are the benefits of having Cognitive Services as a container?
&lt;/h2&gt;

&lt;p&gt;You have the possibility of controlling your data. By using containers, you don't have to send your data to the cloud to use AI services - all the work is done on-premises. What is more, you can use the advantages of containers: portable application architecture and scalability achieved by using orchestration tools such as Kubernetes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Computer Vision API?
&lt;/h2&gt;

&lt;p&gt;It enables you to analyse content in both images and video. You can use either REST API or client library (for C#, Python, Java, etc.) There are several crucial functions, that this API provides - below there are methods that you can use in Python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.describe_image&lt;/code&gt; - generate a human-readable sentence that describes the image,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.tag_image&lt;/code&gt; - returns tags based on recognizable objects, living beings, scenery, and actions,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.analyze_image&lt;/code&gt; - returns object that contains several properties:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;brands.name&lt;/code&gt; - detects corporate brands and logos in the image,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;adult.is_adult_content&lt;/code&gt; - returns true if the image show nudity and sexual acts,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;adult.is_racy_content&lt;/code&gt; - returns true of the image is sexually suggestive.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.analyze_image_by_domain("celebrities", image_url)&lt;/code&gt; - recognizes celebrities,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.analyze_image_by_domain("landmarks", image_url)&lt;/code&gt; - detects landmarks in the image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.generate_thumbnail(width, height, image_url)&lt;/code&gt; - creates reduced-size representation of an image. It can change the aspect ratio and remove distracting elements from the image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;computervision_client.read(image_text_url, raw=True)&lt;/code&gt; uses Read API to return (in an async way) object that contains &lt;code&gt;result.analyze_result.read_results&lt;/code&gt; - it provides you with the extracted printed or handwritten text from an image. Read API supports files in JPEG, PNG, BMP, PDF, and TIFF formats.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Form Recognizer?
&lt;/h2&gt;

&lt;p&gt;Form Recognizer is a part of Computer Vision, and you can use it to extract key-value pairs and table data from form documents. It contains several methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;form_recognizer_client.begin_recognize_content_from_url(form_url)&lt;/code&gt; - it returns a collection of objects: one for each page in the submitted document. You can access cells and texts from the returned objects,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;form_recognizer_client.begin_recognize_receipts_from_url(receipt_url)&lt;/code&gt; - it extracts common fields from US receipts by using a pre-trained receipt model,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;form_recognizer_client.begin_recognize_business_cards_from_url(business_cards_url)&lt;/code&gt; - it extracts common fields from English business cards by using a pre-trained model.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How can I detect faces on images?
&lt;/h2&gt;

&lt;p&gt;You have two possibilities: you can run Computer Vision method: &lt;code&gt;computervision_client.analyze_image(image_url, ["faces"])&lt;/code&gt; or you can use Face API. It is important to know the difference between them. Computer Vision's &lt;code&gt;analyze_image&lt;/code&gt; method only provides a subset of the Face service functionality, so you are not able to use more advanced solutions with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Face API?
&lt;/h2&gt;

&lt;p&gt;It provides you with AI algorithms related to human faces. This service contains five main functionality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;face detection - it simply detects faces and returns the rectangle coordinates of their locations. You can use &lt;code&gt;face_client.face.detect_with_url&lt;/code&gt; method in Python to fulfil the detection.
&lt;/li&gt;
&lt;li&gt;face verification - it helps to answer the following question: "Are these two images the same person?". You can use &lt;code&gt;face_client.face.verify_face_to_face&lt;/code&gt; for it.&lt;/li&gt;
&lt;li&gt;face identification - it helps to answer the following question: "Can this detected face be matched to any enrolled face in a database?". You can use &lt;code&gt;face_client.face.identify&lt;/code&gt; method for identification,&lt;/li&gt;
&lt;li&gt;find similar faces - let's imagine that we have a target face and a set of candidate faces. The goal of this feature is to find a smaller set of faces (from candidate faces) that look like the target face. You can use &lt;code&gt;face_client.face.find_similar&lt;/code&gt; method for this,&lt;/li&gt;
&lt;li&gt;face grouping - it divides faces into several groups based on similarity. From the code perspective it is a bit more complicated than the methods above. Firstly, you have to create group (&lt;code&gt;face_client.person_group.create&lt;/code&gt;), secondly, you need to define one or more person in that group (&lt;code&gt;face_client.person_group_person.create&lt;/code&gt;), later you should add images of the newly created person (&lt;code&gt;face_client.person_group_person.add_face_from_stream&lt;/code&gt;), penultimate step is to train the model (&lt;code&gt;face_client.person_group.train&lt;/code&gt;) and finally you can identify the person from the group (&lt;code&gt;face_client.face.identify&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Custom Vision?
&lt;/h2&gt;

&lt;p&gt;The main goal of Custom Vision is to create custom image identifiers. You can train the model to serve your individual needs. Let's imagine that you want to have a service that distinguish cat from dog. You upload several images of cats with 'cat' labels and do the same for dog. After training, you are able to upload a new image, that the model hasn't seen yet, and you will receive information if the image shows cat or dog with the great accuracy. &lt;/p&gt;

&lt;p&gt;It is also good to know that Custom Vision has two main features:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VI_Jdy5x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4p4d3su3e9edgeb9zmp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VI_Jdy5x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4p4d3su3e9edgeb9zmp.png" alt="Project Types" width="711" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;image classification - it applies labels to an image,&lt;/li&gt;
&lt;li&gt;object detection - it not only add labels, but it also returns the coordinates of the labelled object location. It is important to remember that you have to add labels and coordinates for training data on your own.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How can I use Custom Vision?
&lt;/h2&gt;

&lt;p&gt;You have three options: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;online site (&lt;code&gt;https://www.customvision.ai/&lt;/code&gt;), &lt;/li&gt;
&lt;li&gt;SDK that support C#, Python, etc., &lt;/li&gt;
&lt;li&gt;REST API. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each option provides you with the same type of AI functionality. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Video Indexer?
&lt;/h2&gt;

&lt;p&gt;It is an AI service that provides you with the possibility of extracting information from video. It is focus on both visual and audio aspect of the video.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GjV2FLn0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h33rqa0r5gci2nymjav.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GjV2FLn0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h33rqa0r5gci2nymjav.png" alt="Video Indexer" width="451" height="242"&gt;&lt;/a&gt;&lt;br&gt;
The functionality is really impressive and includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;face detection - it detects faces that are visible on the video,&lt;/li&gt;
&lt;li&gt;celebrity identification,&lt;/li&gt;
&lt;li&gt;visual text recognition - it extracts text from the video frame,&lt;/li&gt;
&lt;li&gt;visual content moderation - it informs you if the adult content is displayed on the video,&lt;/li&gt;
&lt;li&gt;scene segmentation - it determines when a scene is changed on the video,&lt;/li&gt;
&lt;li&gt;rolling credits - it identifies when rolling credits are displayed,&lt;/li&gt;
&lt;li&gt;audio transcription,&lt;/li&gt;
&lt;li&gt;noise reduction,&lt;/li&gt;
&lt;li&gt;speaker enumeration - it can understand which speaker spoke which words and when. It is good to know that only sixteen different speakers can be detected in a single audio-file.&lt;/li&gt;
&lt;li&gt;emotion detection - identifies emotions based on speech (joy, sadness, anger, ect.),&lt;/li&gt;
&lt;li&gt;keywords extraction - it extracts keywords from speech and visual text.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How can I use Video Indexer?
&lt;/h2&gt;

&lt;p&gt;You have two options: online site (&lt;a href="https://www.videoindexer.ai"&gt;https://www.videoindexer.ai&lt;/a&gt;) and API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can I customise Video Indexer?
&lt;/h2&gt;

&lt;p&gt;Yes, to some extend you can. For example, if some faces are detected on your video and they are not celebrities they will be left unnamed. You can describe unrecognized faces, and as a result, your newly labelled faces will be saved in the model. So, from that point Video Indexer will be able to recognize these faces. Similar case is for brands. You can customize brands model to exclude certain brands from being detected or include brands that should be part of your model but weren't detected. What is more, you can also customize language model for example to be able to know that you don't want to translate 'Kubernetes' word, because it is from a technical domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Text Analytics API?
&lt;/h2&gt;

&lt;p&gt;Text Analytics API a part of Language APIs and it is focused on Natural Language Processing. The functionalities that Text Analytics provides are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sentiment analysis - it can determine if the document/sentence is positive, negative, or neutral,&lt;/li&gt;
&lt;li&gt;key phrase extraction - it returns a list of key phrases,&lt;/li&gt;
&lt;li&gt;language detection - you can send a text in some language and the response will contain the name of used language and the confidence score,&lt;/li&gt;
&lt;li&gt;named entity recognition - it can recognize some entities in the text (for example people, places, organizations, etc.).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How can I use Text Analytics API?
&lt;/h2&gt;

&lt;p&gt;You can use either SDKs (for C#, Python, etc.) or REST API. For example, in C# there is a method &lt;code&gt;textAnalyticsClient.AnalyzeSentiment(document)&lt;/code&gt; for sentiment analysis or you can use REST API - &lt;code&gt;/text/analytics/v3.0/sentiment&lt;/code&gt; endpoint will return the same data as the SDK's method.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many languages are supported for Text Analytics?
&lt;/h2&gt;

&lt;p&gt;Each functionality has a separate supported languages list. For example, key phrase extraction supports both English and Polish (and many more), but as for sentiment analytics Polish is not supported. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Speech service?
&lt;/h2&gt;

&lt;p&gt;As a name suggests, it is focused on the speech. These are the provided functionalities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speech-to-Text,&lt;/li&gt;
&lt;li&gt;Text-to-Speech,&lt;/li&gt;
&lt;li&gt;Speech-to-Speech translation,&lt;/li&gt;
&lt;li&gt;Speech-to-Text translation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is important that both Speech-to-Speech and Speech-to-Text translation are supported only for SDK - there is no REST API for them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Can I customize Speech service?
&lt;/h2&gt;

&lt;p&gt;Yes, you can create a custom model for Speech-to-Text functionality by using Custom Speech service, and for Text-to-Speech by using Custom Voice service.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Language Understanding (LUIS)?
&lt;/h2&gt;

&lt;p&gt;It is a conversational AI service. You can use it to understand users needs. Let's image a chatbot or voice assistant that should increase the productivity of the employees. When the user writes/says 'I want to send email to Tom', LUIS will use trained model to predict the meaning of user input - as a result, SendEmail method will be executed with EmailTo property set to Tom. &lt;/p&gt;

&lt;h2&gt;
  
  
  How can I use LUIS?
&lt;/h2&gt;

&lt;p&gt;You have three options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dedicated website (&lt;code&gt;https://www.luis.ai/&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;REST API;&lt;/li&gt;
&lt;li&gt;SDKs (C#, Python, etc. are supported).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are utterances?
&lt;/h2&gt;

&lt;p&gt;Utterances are the user input. In the above example, we use the following text 'I want to send email to Tom' - it is the utterance.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is intent?
&lt;/h2&gt;

&lt;p&gt;Intent is an action that the user wants to perform. In the above example 'SendEmail' is the intent. LUIS determines intent based on user input (utterances).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is entity in LUIS?
&lt;/h2&gt;

&lt;p&gt;Some actions can require attributes. LUIS is able to recognize the entities from utterances. For example, {"EmailTo": "Tom"} is an entity retrieved from the sentence 'I want to send email to Tom'.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should I do to start using LUIS?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add Microsoft.CognitiveServices to your Resource providers:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wHYmGWbx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sc16g5igpnkzpas63m1w.png" alt="Resource providers" width="880" height="374"&gt;
&lt;/li&gt;
&lt;li&gt;Create Cognitive Services account:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c1SRt9qG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw9lfiy135mau0b9jver.png" alt="Cognitive Services account" width="733" height="661"&gt;
&lt;/li&gt;
&lt;li&gt;Create app:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ldCBntcJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fw0e868vmzig7ttzhn92.png" alt="App creation" width="737" height="563"&gt;
&lt;/li&gt;
&lt;li&gt;Build your language models by specifying intents and entities - you can use prebuild data. For example, when you select Email.SendEmail intend then a lot of utterances is automatically added:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m5UW1DCQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcm1nhq631a7gxkrj7wa.png" alt="Prebuild model" width="880" height="320"&gt;
Entities can also be predefined:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cxrdfhkS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rho9b9y49tsv5z7y1agm.png" alt="Predefined" width="735" height="652"&gt;
&lt;/li&gt;
&lt;li&gt;Train the model:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7AwEYvnT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtzskslednocin2v6ths.png" alt="Train the model" width="816" height="134"&gt;
&lt;/li&gt;
&lt;li&gt;Publish to endpoint;&lt;/li&gt;
&lt;li&gt;Test from endpoint.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Azure Cognitive Search Service?
&lt;/h2&gt;

&lt;p&gt;As a name suggests, this service enable you to use search functionality on your data. For example, you can search all data that are stored Azure SQL Database or Azure Cosmos DB.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between index and indexer?
&lt;/h2&gt;

&lt;p&gt;Indexer can crawl your data source and create an index. The index is a collection of documents that can be searched. Index can contains additional data that specify how the search functionality is used on it, such as: sortable (true | false), filterable (true | false), searchable (true | false), etc. &lt;/p&gt;

&lt;h2&gt;
  
  
  How can I create an indexer?
&lt;/h2&gt;

&lt;p&gt;All indexers are required to contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name - the name of the indexer;&lt;/li&gt;
&lt;li&gt;dataSourceName - you can choose from:

&lt;ul&gt;
&lt;li&gt;SQL Managed Instance&lt;/li&gt;
&lt;li&gt;SQL Server on Azure Virtual Machines&lt;/li&gt;
&lt;li&gt;Azure SQL Database&lt;/li&gt;
&lt;li&gt;Azure Table Storage&lt;/li&gt;
&lt;li&gt;Azure Blob Storage&lt;/li&gt;
&lt;li&gt;Azure Cosmos DB&lt;/li&gt;
&lt;li&gt;Azure Data Lake Storage Gen2&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;targetIndexName - the name of your index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the technical point of view, you can create indexer using Azure portal, REST API, SDKs (.NET, Java, Python and JavaScript are supported).&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I run indexers?
&lt;/h2&gt;

&lt;p&gt;You can run indexers after the creation, so it will retrieve desired data from the source and put it to index. As a result, they will be available for the search functionality. But what about the situation when data is changing all the time, and you want to search up-to-date data? In that case, you can use a scheduler for it and specify when the indexer should be run. For example, you can define that the indexer will be run every hour.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I support autocomplete and autosuggest in my Search Service?
&lt;/h2&gt;

&lt;p&gt;You need to specify the &lt;code&gt;suggesters&lt;/code&gt; property in the index definition. This field requires three pieces of data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name - name of your suggester,&lt;/li&gt;
&lt;li&gt;sourceFields - it can contain one or more fields that will be used as a data source for the autocomplete or autosuggest options,&lt;/li&gt;
&lt;li&gt;searchMode - only one is available now - &lt;code&gt;analyzingInfixMatching&lt;/code&gt;. It checks the beginning of a given term.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is important that only one suggester can be defined per index.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should I support synonyms in my Search Service?
&lt;/h2&gt;

&lt;p&gt;It makes your search functionality richer. For example, when user tries to search for &lt;em&gt;USA&lt;/em&gt;, it will be great if the retrieved results contains also data for &lt;em&gt;United States&lt;/em&gt;, &lt;em&gt;United States of America&lt;/em&gt; and even &lt;em&gt;USofA&lt;/em&gt;. An index definition contains &lt;code&gt;synonymMaps&lt;/code&gt; property and in that place you can specify the synonyms.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is QnA Maker?
&lt;/h2&gt;

&lt;p&gt;QnA Maker enables you to create conversational app with Question and Answer functionality. The user provides the question (in text or speach) and the QnA Maker responds with the best suited answer. You can import data (that is called knowledge base) from documents (PDFs, DOCXs) and websites (URLs) where FAQ section is available. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a multi-turn conversation?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UaqpBPGt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6m0d01mx43dvqta1sic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UaqpBPGt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6m0d01mx43dvqta1sic.png" alt="Turn" width="880" height="461"&gt;&lt;/a&gt;&lt;br&gt;
Multi-turn is a follow-up prompt that help to specify what the user wants to achieve. For example, when you visit Adobe page, you can see that the chatbot uses multi-turn to determine whether the user is satisfied with the answer. Vodafone uses it to check if the user is their existing customer or a new client. Scandinavian Airlines provides multi-turn conversation to specify the main topic of the discussion. As you can see, additional follow-up prompts can help to create a satisfying bot conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it chit-chat functionality of QnA Maker?
&lt;/h2&gt;

&lt;p&gt;When you create your knowledge base, you have an option for adding chit-chat. Chit-chat makes your bot more human-friendly and engaging. '&lt;em&gt;How are you?&lt;/em&gt;', '&lt;em&gt;What is your name?&lt;/em&gt;', '&lt;em&gt;How old are you?&lt;/em&gt;' - these are the examples of chit-chat questions. When you want to add these functionality to your QnA Maker, you have several personalities to choose from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;professional,&lt;/li&gt;
&lt;li&gt;friendly,&lt;/li&gt;
&lt;li&gt;witty,&lt;/li&gt;
&lt;li&gt;caring,&lt;/li&gt;
&lt;li&gt;enthusiastic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What is the difference? For example, when you ask '&lt;em&gt;When is your birthday?&lt;/em&gt;' professional personality will respond '&lt;em&gt;Age doesn't really apply to me&lt;/em&gt;' and friendly with '&lt;em&gt;I don't really have an age&lt;/em&gt;'.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is active learning for QnA Maker?
&lt;/h2&gt;

&lt;p&gt;As a name suggests, you can enable QnA Maker to learn based on user inputs. Thanks to it, you can add new questions (that are suggested by the QnA Maker) to your knowledge base. You have to review and accept or reject QnA Maker suggestions in the portal - there is no other way of managing suggestions.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I build my bot?
&lt;/h2&gt;

&lt;p&gt;You have three main options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bot Framework SDK that supports C#, JS, Python and Java,&lt;/li&gt;
&lt;li&gt;BF (Bot Framework) Command Line Interface,&lt;/li&gt;
&lt;li&gt;Bot Framework Composer - it is an IDE for developing bots.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How can I debug my bot using Bot Framework SDK?
&lt;/h2&gt;

&lt;p&gt;There is Bot Framework Emulator that provide you with the functionality of testing and debugging bots. It is a desktop application for Windows, OS X and Linux.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Adaptive Cards?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dwWOevdt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2juvliaqe64h15w47rlp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dwWOevdt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2juvliaqe64h15w47rlp.png" alt="Adaptive Cards" width="880" height="283"&gt;&lt;/a&gt;&lt;br&gt;
Adaptive Cards can enrich communication by using text, graphics, and buttons to your bot conversation. You can create Adaptive Cards in the Designer (&lt;code&gt;https://adaptivecards.io/designer/&lt;/code&gt;) or by Bot Framework SDK.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a skill?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K1QqFQDl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqdftawtuw4d063xoclf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K1QqFQDl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqdftawtuw4d063xoclf.png" alt="Skill" width="794" height="245"&gt;&lt;/a&gt;&lt;br&gt;
In Bot Framework a skill is a bot that performs tasks for another bot. Thanks to it, you can extend your existing bot by consuming another skill (bot). It is good to know that a main skill consumer that interact with the user is called a root bot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which Cognitive Services can I integrate into my bot?
&lt;/h2&gt;

&lt;p&gt;It is common to integrate QnA Maker service, LUIS service, and Speech service into the bot. &lt;/p&gt;

</description>
      <category>ai</category>
      <category>azure</category>
      <category>certification</category>
    </item>
    <item>
      <title>I created modern flashcard system with spaced repetition - WordTester</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Thu, 06 May 2021 15:31:07 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/i-created-modern-flashcard-system-with-spaced-repetition-wordtester-1h0a</link>
      <guid>https://forem.com/kamilbugnokrk/i-created-modern-flashcard-system-with-spaced-repetition-wordtester-1h0a</guid>
      <description>&lt;p&gt;There are several tools for learning foreign words using flashcards and spaced repetition, but I found that most of them don't include the functionality of learning in context. As a result, I remember a lot of foreign words but I have difficulty to use them. One day, I decided to create a system that can resolve this issue - &lt;a href="http://wordtester.org"&gt;WordTester&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why?
&lt;/h1&gt;

&lt;p&gt;The main goal was to create an application that requires to specify not only translated word but also a definition and example of the use of the given word. Thanks to it, I believe that the quality of learning and memorization will be increased. Also, I wanted to implement modern flashcards - not flashcards that only have two sides (original word and translated word). WordTester provides flashcards with 11 sides! &lt;/p&gt;

&lt;h1&gt;
  
  
  How?
&lt;/h1&gt;

&lt;p&gt;I decided that it would be beneficial for me to learn new technology, that is why I chose Blazor for frontend (it turned out to be really great). Backend is implemented in .NET 5.&lt;/p&gt;

&lt;h1&gt;
  
  
  Some screens
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aGDQ4dAm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaedb46cu9jtxnzodhif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aGDQ4dAm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaedb46cu9jtxnzodhif.png" alt="WordTester"&gt;&lt;/a&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cxZLNfWr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v7wo0eqvjteinlrjr6u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cxZLNfWr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v7wo0eqvjteinlrjr6u.png" alt="WordTester"&gt;&lt;/a&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Iv0oaeqM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk3wxs6ptzf44yndtowp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Iv0oaeqM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk3wxs6ptzf44yndtowp.png" alt="WordTester"&gt;&lt;/a&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rtjUmdmU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7c84n9s0pus5e8pd7n6n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rtjUmdmU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7c84n9s0pus5e8pd7n6n.png" alt="WordTester"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Useful links:&lt;br&gt;
&lt;a href="http://wordtester.org"&gt;Website&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/KamilBugnoKrk/WordTester"&gt;Repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope that thanks to WordTester process of learning a foreign language will be more comfortable and easier for you. Let me know what do you think about my first side project!&lt;/p&gt;

&lt;p&gt;Edit: I received feedback from you that the UI is not very good, so a new version has been deployed:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GstDZsGW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywpfhn6uwjif0bdwmyhy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GstDZsGW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywpfhn6uwjif0bdwmyhy.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>opensource</category>
      <category>blazor</category>
      <category>csharp</category>
    </item>
    <item>
      <title>How to protect Azure resources from DDoS?</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Thu, 21 Jan 2021 09:28:36 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/how-to-protect-azure-resources-from-ddos-53p1</link>
      <guid>https://forem.com/kamilbugnokrk/how-to-protect-azure-resources-from-ddos-53p1</guid>
      <description>&lt;p&gt;&lt;em&gt;You can also read this post on my blog: &lt;a href="//blog.kamilbugno.com/"&gt;blog.kamilbugno.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It will not come as a surprise - every resource has its limits. Servers, virtual machines, networks, etc. are not designed in a way that supports unlimited concurrent users. Let’s imagine that someone really wants to make a given website unavailable. How can they do this? Attackers can use that weakness and send a lot of requests to a given website. At some point, the service cannot handle such traffic and become unavailable - it is called Denial-of-service attack. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between DoS and DDoS?
&lt;/h2&gt;

&lt;p&gt;When attackers send requests from one source it is DoS (Denial-of-service attack):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx2ss5zylta68ktatvy7q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx2ss5zylta68ktatvy7q.png" alt="DoS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sending traffic from a lot of various sources to make some website unavailable is called DDoS (Distributed denial-of-service):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyjgt6srhpsw88vq7u89j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyjgt6srhpsw88vq7u89j.png" alt="DDoS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What does the expression 'many requests' really mean? It turned out that in 2012 the DDoS attack that contained 60 Gbps traffic was enough to make several US banking systems unavailable&lt;sup&gt;1&lt;/sup&gt;. In 2016 OVH reported attack that reached 1.1 Tbps&lt;sup&gt;2&lt;/sup&gt; and two years later GitHub was attacked by 1.3 Tbps&lt;sup&gt;3&lt;/sup&gt;. If it is hard for you to imagine that huge amount of data, this level of traffic will be enough to transfer ~80 hours of HD video in one second! To make matters worse Cisco indicates that the frequency of DDoS attacks will increase - from 7.9 million per year in 2018 to 15.4 million in 2023&lt;sup&gt;4&lt;/sup&gt;. As you can see, the DDoS attack is a real threat so protecting the resources from such kind of attack is indispensable if we want to reduce the possibility that our system will be unavailable for customers. &lt;/p&gt;

&lt;h2&gt;
  
  
  How can I protect Azure resources from DDoS?
&lt;/h2&gt;

&lt;p&gt;You have two options: you can do nothing, or you can enable the DDoS Protection Standard.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do nothing
&lt;/h3&gt;

&lt;p&gt;Why can you do nothing and still have your resources protected in Azure? The answer is simple: all Azure resources have enabled Azure DDoS Protection Basic. The main goal of this tier is to protect Azure’s infrastructure. It provides the ability to monitor the traffic and automatically mitigates the attack when it occurs. However, if you want more advanced functionality, this solution may not meet your requirements. It has also one important limitation: Azure can perceive some traffic as safe and inoffensive, but it can be too heavy for your application to be able to handle it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Use DDoS Protection Standard
&lt;/h3&gt;

&lt;p&gt;Alerts, mitigation reports, DDoS Rapid Response (DRR) team that can help you during the attack and cost guarantee are included only in Standard option. You will be charged about $3,500/month for protecting up to one hundred resources&lt;sup&gt;5&lt;/sup&gt;. It is the best option if you want to preserve your application from DDoS since it "will be protected with a profile that is tuned to its expected traffic volume, providing a much tighter defense against DDoS attacks"&lt;sup&gt;6&lt;/sup&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  How does Azure DDoS Protection work?
&lt;/h2&gt;

&lt;p&gt;It analyses the traffic using machine learning solutions and if some requests seem to be suspicious, they will be dropped. Traffic that is originated by real users will be forwarded to the given website. Thanks to it during the attack the website is still available for customers and at the same time the attackers are blocked to reach the final service.&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fslsi6a4lyei5tqapxiqt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fslsi6a4lyei5tqapxiqt.png" alt="DDoS protection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I enable Azure DDoS Protection Standard?
&lt;/h2&gt;

&lt;p&gt;To have resources that are protected by Azure DDoS service in Standard plan you must take several steps. This part of the article guides you with all the necessary actions. The goal is to enable Azure DDoS Protection Standard for one resource and configure sending email alert when the attack occurs.  &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Create a resource to protect
&lt;/h3&gt;

&lt;p&gt;Let's assume that we would like to protect a virtual machine. Go to the top bar and search for a virtual machine. You can choose any name, region, image, etc. - it doesn't have an influence on later steps. Creating a virtual machine can automatically generate the virtual network that we will use in the next step. When it is done copy the public IP address of the virtual machine for future purposes. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foza9oexfna1fi4lwys4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foza9oexfna1fi4lwys4f.png" alt="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Create DDoS Protection plan and assign it to your resource
&lt;/h3&gt;

&lt;p&gt;Go to DDoS protection plans -&amp;gt; click New and fill the required fields. When it is created go to Protected resources section for your plan and add a new resource by choosing your virtual network. As you can see in this case, DDoS Protection plan is assigned to the virtual network, but you can also assign DDoS Protection plan to Application Gateway, Load Balancer or Virtual Machine Scale Set. It is good to know that DDoS plan can be even used across multiple subscriptions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg0ewkp8mqwb6df6ieokp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg0ewkp8mqwb6df6ieokp.png" alt="2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Register resource provider
&lt;/h3&gt;

&lt;p&gt;We would like to use Azure Monitor to collect data and use them to sent alerts. To be able to do this we need to add microsoft.insights resource provider to our subscription. Go to Subscription -&amp;gt; Resource Providers and make sure that microsoft.insights is registered. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnrllw8enjjfikf6odqpj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnrllw8enjjfikf6odqpj.png" alt="3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Create an alert rule
&lt;/h3&gt;

&lt;p&gt;Go to Azure Monitor -&amp;gt; Alerts -&amp;gt; New alert rule -&amp;gt; Select resource and choose your virtual machine public IP address.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F46b35a1pw2rhqppdepkz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F46b35a1pw2rhqppdepkz.png" alt="4-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In condition section choose as a signal name 'Under DDoS attack or not'. In Alert logic set threshold value as 0. Thanks to it the alert will be activated when the maximum 'under ddos attack or not' value is greater or equal to one. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu4yi8qt3iul5yzmtktuq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu4yi8qt3iul5yzmtktuq.png" alt="4-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In actions section create new action groups. For notification select 'Email/SMS message/Push/Voice' and write your email address.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7guy2i2ih5k5jyo65k08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7guy2i2ih5k5jyo65k08.png" alt="4-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Simulate attack on your resources.
&lt;/h3&gt;

&lt;p&gt;There is only one official tool that can be used to simulate DDoS attack on your Azure resources&lt;sup&gt;7&lt;/sup&gt;. It is called BreakingPoint Cloud&lt;sup&gt;8&lt;/sup&gt; and it offers a trial subscription that contains 5GB of free outbound traffic. After creating your free account, you need to authorize using your Azure subscription - they would like to be sure that you attack only resources that are created by you. Later, you can choose the details of the attack. At this point, the public IP address of your virtual machine will be useful. During configuration of the attack please pay attention to estimated outbound data - for a free trial you have only 5 GB to use. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1zd9p34cg0eg0gkti00e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1zd9p34cg0eg0gkti00e.png" alt="5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6. See the results
&lt;/h3&gt;

&lt;p&gt;After the simulation of the DDoS attack, you can see that DDoS Protection detected suspicious packets.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F84xql08k4l0zxo8qo5p6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F84xql08k4l0zxo8qo5p6.png" alt="6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you check your inbox, you should see an email about the DDoS attack:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxllago4esulgs3gm0710.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxllago4esulgs3gm0710.png" alt="6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Clean your resources
&lt;/h3&gt;

&lt;p&gt;Don't forget to remove unused resources. To delete the DDoS Protection Standard plan, you should go the virtual network -&amp;gt; DDoS Protection and disable it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5r33k1tfbufm4nctzcox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5r33k1tfbufm4nctzcox.png" alt="7-1"&gt;&lt;/a&gt;&lt;br&gt;
After that, you can simply delete your plan and other resources.&lt;/p&gt;

&lt;p&gt;During attack simulation, we used the app that needs some permissions in our account. To cancel those permissions, go to MyApplications&lt;sup&gt;9&lt;/sup&gt; and click 'revoke permissions'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4cgirfx5m0ltaa2bhz4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4cgirfx5m0ltaa2bhz4b.png" alt="7-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This near-real live scenario can be used to protect your cloud resources from DDoS attack. Thanks to Standard plan you can be alerted when the attack occurs and have access to metrics collected by Azure Monitor. Also, you can contact DDoS Rapid Response (DRR) team to get some help and you can be sure that you don't be charged for scaling of your services as a result of DDoS attack. As you can see, Azure offers an interesting solution to make your cloud resources safer. &lt;/p&gt;

&lt;p&gt;In this article, you can get to know the basics of DDoS attack and understand why it is a genuine issue. I showed you how to configure Azure DDoS Protection for your resource and how to simulate DDoS attack to your virtual machine. I hope that you enjoyed this post! &lt;/p&gt;



&lt;h5&gt;
  
  
  References:
&lt;/h5&gt;

&lt;p&gt;[1] &lt;a href="https://www.cio.com/article/2389721/ddos-attacks-against-us-banks-peaked-at-60-gbps.html" rel="noopener noreferrer"&gt;https://www.cio.com/article/2389721/ddos-attacks-against-us-banks-peaked-at-60-gbps.html&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://arstechnica.com/information-technology/2016/09/botnet-of-145k-cameras-reportedly-deliver-internets-biggest-ddos-ever/" rel="noopener noreferrer"&gt;https://arstechnica.com/information-technology/2016/09/botnet-of-145k-cameras-reportedly-deliver-internets-biggest-ddos-ever/&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://www.wired.com/story/github-ddos-memcached/" rel="noopener noreferrer"&gt;https://www.wired.com/story/github-ddos-memcached/&lt;/a&gt;&lt;br&gt;
[4] &lt;a href="https://www.cisco.com/c/en/us/solutions/collateral/executive-perspectives/annual-internet-report/white-paper-c11-741490.html" rel="noopener noreferrer"&gt;https://www.cisco.com/c/en/us/solutions/collateral/executive-perspectives/annual-internet-report/white-paper-c11-741490.html&lt;/a&gt;&lt;br&gt;
[5] &lt;a href="https://azure.microsoft.com/en-ca/pricing/details/ddos-protection/" rel="noopener noreferrer"&gt;https://azure.microsoft.com/en-ca/pricing/details/ddos-protection/&lt;/a&gt;&lt;br&gt;
[6] &lt;a href="https://docs.microsoft.com/en-us/azure/ddos-protection/ddos-faq#are-services-unsafe-in-azure-without-the-service" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/azure/ddos-protection/ddos-faq#are-services-unsafe-in-azure-without-the-service&lt;/a&gt;&lt;br&gt;
[7] &lt;a href="https://github.com/MicrosoftDocs/azure-docs/issues/59335" rel="noopener noreferrer"&gt;https://github.com/MicrosoftDocs/azure-docs/issues/59335&lt;/a&gt;&lt;br&gt;
[8] &lt;a href="https://www.keysight.com/zz/en/products/network-security/breakingpoint-cloud.html" rel="noopener noreferrer"&gt;https://www.keysight.com/zz/en/products/network-security/breakingpoint-cloud.html&lt;/a&gt;&lt;br&gt;
[9] &lt;a href="https://myapplications.microsoft.com" rel="noopener noreferrer"&gt;https://myapplications.microsoft.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>azure</category>
    </item>
    <item>
      <title>104 revision questions for AZ-104</title>
      <dc:creator>Kamil Bugno</dc:creator>
      <pubDate>Sat, 21 Nov 2020 16:57:45 +0000</pubDate>
      <link>https://forem.com/kamilbugnokrk/104-revision-questions-for-az-104-49oc</link>
      <guid>https://forem.com/kamilbugnokrk/104-revision-questions-for-az-104-49oc</guid>
      <description>&lt;p&gt;&lt;em&gt;You can also read this post on my blog: &lt;a href="//blog.kamilbugno.com/"&gt;blog.kamilbugno.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are preparing for AZ-104 exam, you are likely to find this article highly valuable. Together, we will go over the topics that will help you in preparation to become Microsoft Azure Administrator. When I was learning for the exam, I decided to note the most vital facts about Azure. This post doesn't cover all topics required to AZ-104, but I hope that this list can help you to revise your knowledge before the exam. Let's start!&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What is Azure AD?
&lt;/h3&gt;

&lt;p&gt;Azure Active Directory (AD) is known to be a platform to handle identities. It offers single sign-on (SSO), multi-factor authentication, conditional access policies, etc. &lt;/p&gt;

&lt;h3&gt;
  
  
  2. What are dynamic membership rules?
&lt;/h3&gt;

&lt;p&gt;You can define membership rules for users. If a given user meets the condition, they are added to a group. Thanks to it you can e.g. create groups for users who are from London and are developers. You don't have to add these users manually.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftk35le0n0jw3mpx1nfmj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftk35le0n0jw3mpx1nfmj.png" alt="Dynamic membership rules"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  3. What is user risk policy?
&lt;/h3&gt;

&lt;p&gt;Azure AD Identity Protection offers functionality to detect which users' behaviours are unusual and suspect. You can define the risk level that you accept and determine what should happen when the expected risk is higher than you agreed. For example, you can specify that if the risk of compromising a user account is medium or high then the user is blocked.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fitcegqblvs8dzxyuiw6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fitcegqblvs8dzxyuiw6p.png" alt="user risk policy"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  4. What is sign-in risk policy?
&lt;/h3&gt;

&lt;p&gt;It is similar to the user risk policy. You can define what should happen when the specified risk that the sign-in is coming from someone else than the user is reached. &lt;/p&gt;
&lt;h3&gt;
  
  
  5. What is conditional access?
&lt;/h3&gt;

&lt;p&gt;As a name suggest, you can specify what should happen for given users when they try to log in. You can, for example, specify that all guests and external users who use Android devices should use multi-factor authentication:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0xwxv6zi9bqqinmcfnz8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0xwxv6zi9bqqinmcfnz8.png" alt="Conditional access"&gt;&lt;/a&gt;&lt;br&gt;
It is important that you use conditional access only when you disable security defaults. Security defaults are default rules managed by Microsoft that help to protect from well-known identity-related attacks. &lt;/p&gt;
&lt;h3&gt;
  
  
  6. What are management groups?
&lt;/h3&gt;

&lt;p&gt;You can group multiple subscriptions together by using management groups. It is important that as opposed to resource groups, management groups can be nested, so you can create a logical hierarchy of your subscriptions. For example, the management group "C" contains the subscription "3" and another management group - "D" that holds subscription "4":&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbrsm688kb5vk9qx25bx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbrsm688kb5vk9qx25bx3.png" alt="Management groups"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  7. Why should I use management groups?
&lt;/h3&gt;

&lt;p&gt;Management groups simplify the administration of your subscriptions. They are usually used together with RBAC or Azure Policies.&lt;/p&gt;
&lt;h3&gt;
  
  
  8. What is RBAC?
&lt;/h3&gt;

&lt;p&gt;RBAC stands for role-based access control. Let's imagine that there are several departments related to your current project: developers, database admins, financial analysts and so on. It will be a good idea to specify which user has what kind of permission. We would like to prevent the situation when financial analysts delete the database or when database admins swap the application's deployment. RBAC offers you an opportunity to define the rules for users who use your Azure subscription. So, you can add necessary rights to developers, other set of permissions to database admins and different privileges to financial analysts.&lt;/p&gt;
&lt;h3&gt;
  
  
  9. What are the scopes for RBAC?
&lt;/h3&gt;

&lt;p&gt;RBAC can be applied to these levels: management group, subscription, resource group, and a given resource.&lt;/p&gt;
&lt;h3&gt;
  
  
  10. What are Azure policies?
&lt;/h3&gt;

&lt;p&gt;Azure policies are used to enforce specified rules and effects for your resources. Let's assume that you want to be sure that all the resources have a tag. Or you want to guarantee that your virtual machines are deployed only in a given region. You can create Azure policies and determine what requirements should be met and what will happen when these conditions are or are not fulfilled.&lt;/p&gt;
&lt;h3&gt;
  
  
  11. What are the scopes for Azure policies?
&lt;/h3&gt;

&lt;p&gt;You can assign a policy to a management group, a subscription, and a resource group. It is good to know that there is also an exclusion option. You can e.g., select that the policy is defined for all resources in your subscription except the resources from a given resource group:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fija7esc9jqr330r6wust.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fija7esc9jqr330r6wust.png" alt="Azure policies"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  12. What are resource locks?
&lt;/h3&gt;

&lt;p&gt;Let's imagine that you have a lot of virtual machines - for development, testing and production environment. Sometimes you need to delete virtual machines from the development environment only. To be sure, that you don't delete by mistake any virtual machines from testing or production environment, you can use locks. There are two types of locks: delete (it prevents from deleting a resource) and read-only (it prevents from changing the resources):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2hnotarltfluwwthxnka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2hnotarltfluwwthxnka.png" alt="Locks"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  13. What are the scopes for Azure locks?
&lt;/h3&gt;

&lt;p&gt;You can create a lock for a subscription, resource group, and resource.&lt;/p&gt;
&lt;h3&gt;
  
  
  14. Why read-only locks can be misleading?
&lt;/h3&gt;

&lt;p&gt;Sometimes it is hard to say what does 'modification' mean for Azure resources. There is a lot of side effects of using this type of lock. For example: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A read-only lock for a virtual machine prevents the users from starting or restarting the virtual machine.&lt;/li&gt;
&lt;li&gt;A read-only lock on a storage account prevents the users from listing the keys. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More examples you can find in &lt;a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/lock-resources#considerations-before-applying-locks" rel="noopener noreferrer"&gt;the official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  15. What will happen when I try to delete a resource with a delete lock?
&lt;/h3&gt;

&lt;p&gt;You will see the information that this operation cannot be completed:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyi14wxgfxp989cy3ooa8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyi14wxgfxp989cy3ooa8.png" alt="Azure Locks"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  16. What is a tag?
&lt;/h3&gt;

&lt;p&gt;A tag is a name and value pair that can be added to resources in Azure. Thanks to tags you can organize your resources. They are commonly used for cost management, resource management, and automation. Let's assume that you work in a company that maintains two applications: Chat Application and Gallery Application. Each app is hosted in a virtual machine. There are two environments: test and prod. When we add tags to these virtual machines, we will know which virtual machine represents what application and environment:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm9rup3opshfe3mfcpb05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm9rup3opshfe3mfcpb05.png" alt="Tags"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  17. Can a tag be inherited?
&lt;/h3&gt;

&lt;p&gt;No. For example, when you add a tag to a resource group, the resources within this group won't inherit this tag. &lt;/p&gt;
&lt;h3&gt;
  
  
  18. What is Azure Budget?
&lt;/h3&gt;

&lt;p&gt;You can create a budget limit for your subscription. Thanks to it, you are able to prevent from spending too much money on Azure. It is important that when you run out of budget, your resources are not stopped. The main goal of Azure Budget is to notify you when the specified limit is achieved.&lt;/p&gt;
&lt;h3&gt;
  
  
  19. What are Azure Action Groups?
&lt;/h3&gt;

&lt;p&gt;During the creation of the budget, you can specify Action Groups. They are a set of effects that should take place when the defined budget limit is met. For example, you can receive an email, phone call or push notification:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flxrar9xlvp35nax228lo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flxrar9xlvp35nax228lo.png" alt="Action Group"&gt;&lt;/a&gt;&lt;br&gt;
There is also an option to run Azure Function, Logic App or Webhook as an effect of the limit hit. &lt;/p&gt;
&lt;h3&gt;
  
  
  20. When can I use Azure Action Groups?
&lt;/h3&gt;

&lt;p&gt;It is good to know that Action Groups are not designed only to protect your budget, you can use them in other situations. For example, if your average virtual machine CPU is greater than 70%, then you can notify people in your team about this. That condition can be created by using Alerts in Azure Monitor.&lt;/p&gt;
&lt;h3&gt;
  
  
  21. What is Azure Monitor?
&lt;/h3&gt;

&lt;p&gt;It is a service that provides you with the rich ability to collect and analyze the telemetry of your resources. Azure Monitor contains several subservices and the most important are Application Insights, Log Analytics and Alerts.&lt;/p&gt;
&lt;h3&gt;
  
  
  22. What is Azure Application Insights?
&lt;/h3&gt;

&lt;p&gt;If you want to monitor your applications, Azure Application Insights is one of the best choices. You can collect information about request rates, response times, server, and browser exceptions and many more in a human-friendly way:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frt9s24uaxvonxigqy4b9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frt9s24uaxvonxigqy4b9.png" alt="Azure Application Insights"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  23. What is Log Analytics?
&lt;/h3&gt;

&lt;p&gt;Azure Log Analytics gives you an opportunity to write queries in Kusto query language to analyze your logs. Here are some examples of queries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To get all data from 'ExampleTable' table:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ExampleTable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;To count the number of rows contained in a table:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ExampleTable | count
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;To search for word 'error' in any property of the data:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ExampleTable | search "error"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It is good to know that Log Analytics can visualise the results of your query and, for example, you will receive a pie chart that you can pin to your dashboard. &lt;/p&gt;
&lt;h3&gt;
  
  
  24. What is Alerts?
&lt;/h3&gt;

&lt;p&gt;Alerts in Azure Monitor are commonly used to notify you when a certain condition is met. You can use logs and metrics to check if the notification should take place and, for example, send an email to the administrator when your server returns 500 error code.&lt;/p&gt;
&lt;h3&gt;
  
  
  25. What is Infrastructure as Code (IaC)?
&lt;/h3&gt;

&lt;p&gt;Instead of using the Azure portal to create your resources manually, you can define them in a text file - Azure Resource Manager create all your resources based on that file. The file that contains definitions of resources is called ARM template.&lt;/p&gt;
&lt;h3&gt;
  
  
  26. Why should I use ARM template?
&lt;/h3&gt;

&lt;p&gt;ARM template can be stored in a version control system and it is the first advantage of using templates. You can review the modifications and you know who and why have made any change. Additionally, you can save a lot of time, because when you want to deploy the same resources for several environments, you can use one template instead of doing these changes manually. The third reason is about avoiding human errors during the deployments. Since you don't have to make all changes by hand, there is a smaller room for mistakes. &lt;/p&gt;
&lt;h3&gt;
  
  
  27. What is a shared access signature?
&lt;/h3&gt;

&lt;p&gt;Shared access signature (SAS) is used with a storage account to give access to storage resources. You can specify what actions, services and types are allowed. There is even an option to determine a time slot when the SAS is valid: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F285jd878aahujo3c5f49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F285jd878aahujo3c5f49.png" alt="SAS"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  28. What is AZCopy?
&lt;/h3&gt;

&lt;p&gt;AZCopy is a tool that provides you with a possibility to copy data to/from your storage account. You can use it on Windows, Linux and macOS.&lt;/p&gt;
&lt;h3&gt;
  
  
  29. Which data I can copy using AZCopy?
&lt;/h3&gt;

&lt;p&gt;You can copy data to/from Blob and File Storage.&lt;/p&gt;
&lt;h3&gt;
  
  
  30. How can I authorize my actions in AZCopy?
&lt;/h3&gt;

&lt;p&gt;You can use SAS token to be able to copy the data. For Blob Storage you have also the second way of authorization: the credentials can be provided from Azure AD.&lt;/p&gt;
&lt;h3&gt;
  
  
  31. How can I use AZCopy from a command line?
&lt;/h3&gt;

&lt;p&gt;AZCopy provides several commands that you can use, but there are two that are the most important: &lt;code&gt;azcopy cp&lt;/code&gt; and &lt;code&gt;az login&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;To log in via Azure AD you should run this command and provide your account login/password:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;azcopy login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To upload a single file to your blob storage using SAS authorization you should run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;azcopy cp "/yourPath/yourFile.txt" "https://[your-storage-account].blob.core.windows.net/[container]/[path-to-blob]?[SAS-token]"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To download a file from Azure blob storage, you should change the order of arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;azcopy cp "https://[your-storage-account].blob.core.windows.net/[container]/[path-to-blob]?[SAS-token]" "/yourPath/yourFile.txt"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A similar command exists for File Storage. When you want to upload a file, you should use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;azcopy cp "/yourPath/yourFile.txt" "https://[your-storage-account].file.core.windows.net/[file-share-name]/[path-to-file]?[SAS-token]"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a command for downloading a file from File Storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;azcopy cp "https://[your-storage-account].file.core.windows.net/[file-share-name]/[path-to-file]?[SAS-token]" "/yourPath/yourFile.txt"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  32. What is Azure Import/Export job?
&lt;/h3&gt;

&lt;p&gt;If you have a lot of data that need to be transferred to/from Azure, you can find the idea of importing/exporting them by using physical disk very appealing. Instead of using an Internet connection, Azure can ship to you disk drives. After you upload/download the data to/from disk, you simply send the disk back:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb4jmdsm4v98kk8t4a8s1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb4jmdsm4v98kk8t4a8s1.png" alt="Import/Export job"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  33. What are storage types supported for Azure Import/Export job?
&lt;/h3&gt;

&lt;p&gt;For import, you can use both File and Blob storage. For export, only Blob storage is supported. &lt;/p&gt;
&lt;h3&gt;
  
  
  34. What is WAImportExport tool?
&lt;/h3&gt;

&lt;p&gt;It is used during performing Import/Export job. The tool can help you with preparing the disk drives that are sent for import. You can copy your data to the drive and encrypt the volume using WAImportExport.&lt;/p&gt;
&lt;h3&gt;
  
  
  35. What is scale sets?
&lt;/h3&gt;

&lt;p&gt;If you want to distribute your application across several virtual machines you should use a scale set. It provides the functionality of scaling the number of virtual machines: you can, for example, define that if the total CPU used by your VMs is greater then 85% for 10 minutes, a new virtual machine will be added to the set:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2kqwghydfbqviq61wqsh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2kqwghydfbqviq61wqsh.png" alt="Scale set"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  36. Why should I use scale sets?
&lt;/h3&gt;

&lt;p&gt;Scale sets provide you with great flexibility. You can adjust your environment depending on the needs. When few users use your service running on several virtual machines, you can save money by scaling in - the amount of active VMs is decreased. An analogous situation can be for scaling out - when many users request your service, your environment can automatically adjust to that burden. &lt;/p&gt;
&lt;h3&gt;
  
  
  37. What is a container?
&lt;/h3&gt;

&lt;p&gt;You can run your application with all its dependencies inside a container. It is commonly used to simplify the process of moving application form one environment to another. Instead of installing all the required software in every environment, you can use a container that has installed everything that is needed for the app. &lt;/p&gt;
&lt;h3&gt;
  
  
  38. What is the difference between a container and a virtual machine?
&lt;/h3&gt;

&lt;p&gt;You can use any operating system inside your virtual machine - it doesn't matter that the host operating system is the same or different. Containers use the operating system of the host, and as a result, they are usually lighter than a virtual machine.&lt;/p&gt;
&lt;h3&gt;
  
  
  39. What is Azure Kubernetes Service?
&lt;/h3&gt;

&lt;p&gt;When you use containers to run your application, Kubernetes can help you with managing them. If you have a lot of containers, Kubernetes orchestrate them, so you don't need to worry about scaling, access management or deployment of a new version of your app to every available container.&lt;/p&gt;
&lt;h3&gt;
  
  
  40. What is Azure Kubernetes node?
&lt;/h3&gt;

&lt;p&gt;A node in Kubernetes is a virtual machine.&lt;/p&gt;
&lt;h3&gt;
  
  
  41. What are Kubernetes pods?
&lt;/h3&gt;

&lt;p&gt;Pods are known to be the smallest deployable units of computing. It simply means that a pod can contain at least one container. &lt;/p&gt;
&lt;h3&gt;
  
  
  42. What is Kubernetes kubectl?
&lt;/h3&gt;

&lt;p&gt;Kubectl is a command-line client that provides you with the ability to manage your Kubernetes instance. There are a lot of commands, and here I presented only a small subset of them:&lt;br&gt;
To show all your nodes you can use this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get nodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A similar command is used to display information about your pods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get pods
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create resources defined in the YAML file you should use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f ./myFile.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you want to manually scale the resources to 5 you can do it by this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl scale --replicas=5 -f ./myFile.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  43. What is App Service?
&lt;/h3&gt;

&lt;p&gt;Using App Service you are able to host your web application. You can easily deploy your app, specify the deployment details (like deployment slots), and set the scaling rules. It supports several runtime environments for apps written in .NET, Java, Python, Ruby, NodeJS, and PHP:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7jufmimu2mtzfy2bdvp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7jufmimu2mtzfy2bdvp1.png" alt="App Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  44. What is App Service Plan?
&lt;/h3&gt;

&lt;p&gt;In the App Service Plan, you can specify the virtual machines for your web apps to run. It is important that the App Service Plan can contain several apps - but the operating system and location have to be the same for all your apps. &lt;/p&gt;

&lt;h3&gt;
  
  
  45. What are Deployment Slots?
&lt;/h3&gt;

&lt;p&gt;Deployment slot is your application with a specified hostname. Since there are several ways of how you can publish a new version of your application, Deployment Slots provide you with flexibility on how your app is deployed. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can, for example, have two Deployment Slots: first for the production version of your app, second that contains new features. You can specify that 80% of the workload will be sent to the old (production) version and 20% to the new version:
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ny9ht3jdyomlyjqijfk.png" alt="Deployment slots one"&gt;
&lt;/li&gt;
&lt;li&gt;You can also set that new version of the app won't be available to the users before it is verified by a QA team. After the testing, you can swap the versions:
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdv2jpqwwige93p2ytn8a.png" alt="Deployment slots two"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  46. What types of scaling are supported by App Service Plan?
&lt;/h3&gt;

&lt;p&gt;You can scale both up/down and out/in.&lt;/p&gt;

&lt;h3&gt;
  
  
  47. What are the differences between scaling up/down and scaling out/in?
&lt;/h3&gt;

&lt;p&gt;By scaling up you can add more CPU, disk space etc. You scale down when you change the resource type to lower piercing tier: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frklsocua243usy22yixu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frklsocua243usy22yixu.png" alt="Scale up/down"&gt;&lt;/a&gt;&lt;br&gt;
By scaling out you can add additional instances of the same resource. Scaling in is meant to decrease the number of running instances:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqmy0o2l8ijk7lr7au08w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqmy0o2l8ijk7lr7au08w.png" alt="Scale out/in"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  48. What is Azure DNS?
&lt;/h3&gt;

&lt;p&gt;Azure DNS provides you with the ability to host domains. When you open your favourite browser and write for example &lt;a href="http://www.dev.to"&gt;www.dev.to&lt;/a&gt;, the name '&lt;a href="http://www.dev.to"&gt;www.dev.to&lt;/a&gt;' is translated into an IP address of the server that hosts this website. The main goal of DNS is to convert the domain name to the IP address. &lt;/p&gt;

&lt;h3&gt;
  
  
  49. What are types of DNS supported in Azure?
&lt;/h3&gt;

&lt;p&gt;You can choose from Public DNS and Private DNS. When you buy a new domain for your app, you can use Public DNS to translate the domain name to the IP address of the web app. Private DNS is used inside your virtual network. For example, you can create database.test and server.test domain names and use it instead of IP addresses. It is important that database.test and server.test are not accessible from the Internet since it was creating using Private DNS. &lt;/p&gt;

&lt;h3&gt;
  
  
  50. What is a virtual network link?
&lt;/h3&gt;

&lt;p&gt;It is used with Private DNS to connect your domain (like database.test) with a virtual network. Thanks to it, the private domain name will be accessible from the virtual network. &lt;/p&gt;

&lt;h3&gt;
  
  
  51. What is autoregistration?
&lt;/h3&gt;

&lt;p&gt;When you create a virtual network link, you can enable the autoregistration functionality. When you create a new virtual machine in the virtual network, the DNS record for that VM is automatically created:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frkje51k7vpogxhc738oz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frkje51k7vpogxhc738oz.png" alt="autoregistration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  52. What is Azure Files?
&lt;/h3&gt;

&lt;p&gt;If you have a collection of files, you can use Azure Files to store them in the cloud - this is the main goal of Azure Files. &lt;/p&gt;

&lt;h3&gt;
  
  
  53. What protocols Azure Files uses?
&lt;/h3&gt;

&lt;p&gt;Both SMB (Server Message Block) and NFS (Network File System) are supported.&lt;/p&gt;

&lt;h3&gt;
  
  
  54. How can I use Azure Files?
&lt;/h3&gt;

&lt;p&gt;There are two ways of using Azure Files: you can direct mount of an Azure file share or cache Azure file share with Azure File Sync. &lt;/p&gt;

&lt;h3&gt;
  
  
  55. How can I create Azure File in my subscription?
&lt;/h3&gt;

&lt;p&gt;Azure Files can be treated as a subservice of Azure Storage Account, so you need to create Azure Storage Account to use Azure Files:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnz8xhraa66b856i4g46e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnz8xhraa66b856i4g46e.png" alt="Azure Files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  56. What is Azure File Sync?
&lt;/h3&gt;

&lt;p&gt;Azure File Sync as a name suggests it is meant to synchronise files. Let's assume that you store files in on-premises file server, and you would like to have them copied in the cloud - you can be sure that your files are synchronized between on-premises and cloud by using Azure File Sync:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk1wir2rv5gxurrazlwvb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk1wir2rv5gxurrazlwvb.png" alt="Azure File Sync"&gt;&lt;/a&gt;&lt;br&gt;
It is important that you should install an Azure File Sync agent on the server. &lt;/p&gt;

&lt;h3&gt;
  
  
  57. What are the differences between server endpoint and cloud endpoint?
&lt;/h3&gt;

&lt;p&gt;Server endpoint is a path on the Windows Server that is synchronized to an Azure file share. Cloud endpoint is simply your Azure file share. &lt;/p&gt;

&lt;h3&gt;
  
  
  58. What is Azure Sync Group?
&lt;/h3&gt;

&lt;p&gt;Sync group can be interpreted as a logic container. Every endpoint (server and cloud) that are within this sync group are kept in sync. You should create a sync group to use the power of Azure File Sync.&lt;/p&gt;

&lt;h3&gt;
  
  
  59. How many server endpoints can I have in one sync group?
&lt;/h3&gt;

&lt;p&gt;You can have several server endpoints. &lt;/p&gt;

&lt;h3&gt;
  
  
  60. What should I do to use Azure File Sync?
&lt;/h3&gt;

&lt;p&gt;There are several steps that you should follow to use Azure File Sync:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create Azure Files in your subscription.&lt;/li&gt;
&lt;li&gt;Register a server by installing an Azure File Sync agent.&lt;/li&gt;
&lt;li&gt;Create a sync group. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  61. What is a Virtual Network?
&lt;/h3&gt;

&lt;p&gt;Azure resources can communicate using the virtual network. When we create e.g., a VM, we can choose which virtual network would be in use. Each virtual network has an IP address range like 10.0.0.0/24. It is an IP address range using CIDR notation.&lt;/p&gt;

&lt;h3&gt;
  
  
  62. What is Network Interface (NIC)?
&lt;/h3&gt;

&lt;p&gt;Thanks to the network interface a virtual machine can have access to the rest of the world. The network interface can have one primary IP address and it is attached directly to a virtual machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  63. Can I have several subnets in a virtual network?
&lt;/h3&gt;

&lt;p&gt;Yes, but important fact is that each of the subnets has to be allocated inside the IP address range. Otherwise, the creation of subnet fails:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw807vi6fb7oiorcwrbc7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw807vi6fb7oiorcwrbc7.png" alt="Subnet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  64. Can I have no subnet in a virtual network?
&lt;/h3&gt;

&lt;p&gt;No, you have to have at least one subnet in your virtual network. Otherwise, you will not be able to create a virtual network:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh0hsj64qngtb20xk3zmp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh0hsj64qngtb20xk3zmp.png" alt="No subnet in VNET"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  65. How many IP addresses are reserved by Azure in my subnets?
&lt;/h3&gt;

&lt;p&gt;Azure reserves 5 IP addresses in each subnet: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fccstfx2i52ydoswkn69d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fccstfx2i52ydoswkn69d.png" alt="Reserved IP"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  66. What is the Network Security Group (NSG)?
&lt;/h3&gt;

&lt;p&gt;The main goal of the network security group is to filter the traffic. You can create several roles and specify which traffic is allowed or denied:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg82r52ou0727f2qg3m1w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg82r52ou0727f2qg3m1w.png" alt="NSG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  67. To which Azure resources can I assign NSG?
&lt;/h3&gt;

&lt;p&gt;You can assign NSG to a virtual machine (by using NIC - Network Interface) or to a subnet. &lt;/p&gt;

&lt;h3&gt;
  
  
  68. Can I assign NSG to more than one resources?
&lt;/h3&gt;

&lt;p&gt;Yes, you can e.g., assign one NSG to several NIC, but it is important that the location of these resources have to be the same. &lt;/p&gt;

&lt;h3&gt;
  
  
  69. Can I assign several NSG to a virtual machine?
&lt;/h3&gt;

&lt;p&gt;Yes. You can assign first NSG to NIC and second to subnet. Then the virtual machine that is inside the subnet will have two NSG. You can check it in Network Watcher -&amp;gt; Effective security rules: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fflg69ctxqp76ub6ewedb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fflg69ctxqp76ub6ewedb.png" alt="Two NSG to one VM"&gt;&lt;/a&gt;&lt;br&gt;
It is important that if one of the NSG denies some traffic then the traffic is not allowed - even when the second NSG permit it. &lt;/p&gt;

&lt;h3&gt;
  
  
  70. How can I check if NSG blocks the traffic?
&lt;/h3&gt;

&lt;p&gt;You can use Network Watcher to check which rule blocks traffic from your virtual machine to other resources. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl2r809kj2bv2hteqi7em.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl2r809kj2bv2hteqi7em.png" alt="Network Watcher"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  71. What will happen when my VM doesn't have any NSG?
&lt;/h3&gt;

&lt;p&gt;If your VM is not associated with any NGS then all traffic is allowed.&lt;/p&gt;

&lt;h3&gt;
  
  
  72. What is Azure Firewall?
&lt;/h3&gt;

&lt;p&gt;You can create traffic policies using Azure Firewall. If you want to allow or deny some traffic you can do it by defining NAT rules, network rules, and applications rules. It is good to know that you can even enable the functionality of detecting traffic that goes to or from identified malicious domains or IP addresses.&lt;/p&gt;

&lt;h3&gt;
  
  
  73. What is a route table?
&lt;/h3&gt;

&lt;p&gt;In a route table, you can define how the traffic will be sent by determining a rule: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4rwedku7dgr61r52hxqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4rwedku7dgr61r52hxqw.png" alt="Route table"&gt;&lt;/a&gt;&lt;br&gt;
For example, we defined that all traffic to 10.3.0.0/16 will go through a virtual appliance (10.4.0.0).&lt;/p&gt;

&lt;h3&gt;
  
  
  74. To which Azure resources can I assign a route table.
&lt;/h3&gt;

&lt;p&gt;You can only assign a route table to a subnet.&lt;/p&gt;

&lt;h3&gt;
  
  
  75. When is it a good idea to use route tables?
&lt;/h3&gt;

&lt;p&gt;A route table is commonly used when we have an Azure Firewall, and we would like to send all traffic from/to a given subnet through the Firewall. As a virtual appliance, you have to specify the public IP of the Firewall. &lt;/p&gt;

&lt;h3&gt;
  
  
  76. What is Azure Bastion?
&lt;/h3&gt;

&lt;p&gt;During the creation of a virtual machine, you can choose which port will be opened to connecting to the newly created VM. The disadvantage of this solution is the fact, that having a given port open for all the world is not secure. You can see the following warning about it: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp4jla4bml07wukhdk6r1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp4jla4bml07wukhdk6r1.png" alt="Warning"&gt;&lt;/a&gt;&lt;br&gt;
Instead of opening RDP or SSH port of your virtual machine, you can use Azure Bastion to securely connect to the VM. Azure Bastion uses TLS on port 443, so you can connect the VM via web browser:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flcty3hcmyf9sj3g6hrys.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flcty3hcmyf9sj3g6hrys.png" alt="Connect to VM using Bastion"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo1bn1sn5glh49bf1qqih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo1bn1sn5glh49bf1qqih.png" alt="Connect to VM using Bastion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  77. How does Azure Bastion work?
&lt;/h3&gt;

&lt;p&gt;During the creation of Azure Bastion, you have to create a subnet named "AzureBastionSubnet". When you connect to VM by Bastion, the traffic flow is the following:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy6voj8mb77j7n05xz3hz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy6voj8mb77j7n05xz3hz.png" alt="Azure Bastion"&gt;&lt;/a&gt;&lt;br&gt;
You connect to Azure Portal (using HTTPS), Azure Portal connects to Bastion (using HTTPS) and Bastion connect to a given virtual machine (using RDP or SSH).&lt;/p&gt;

&lt;h3&gt;
  
  
  78. What is VNET peering?
&lt;/h3&gt;

&lt;p&gt;When you have two virtual networks, "A" and "B", resources that are inside "A" network cannot communicate to resources from "B" network. The main goal of VNET peering is to enable that communication. To create a peering to a virtual network -&amp;gt; settings -&amp;gt; peerings:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzedwps1cvxtg6voh1hsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzedwps1cvxtg6voh1hsa.png" alt="VNET peering"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  79. Is a virtual network peering transitive?
&lt;/h3&gt;

&lt;p&gt;Let's assume that we have the following situation:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy2xln5zae783kbred5ib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy2xln5zae783kbred5ib.png" alt="Transitive peering"&gt;&lt;/a&gt;&lt;br&gt;
VnetA has a peering connection to VnetB and VnetB has a peering connection to VnetC. Can resources from VnetA communicate to resources in VnetC? No. There is no default transitive peering. If you want to enable that traffic, you should change the settings of these peerings and allow forwarded traffic from VnetB to VnetA and from VnetB to VnetC.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe84fdq06gubiicnp3kv6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe84fdq06gubiicnp3kv6.png" alt="Transitive peering"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  80. Can I create a peering connection to any virtual network I want?
&lt;/h3&gt;

&lt;p&gt;You can create a peering connection between virtual networks in the same region or in different regions, but you cannot connect two virtual networks that have overlapping IP addresses:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6j4ba0ohj2q2g182cjee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6j4ba0ohj2q2g182cjee.png" alt="Overlapping virtual networks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  81. What is Network Watcher?
&lt;/h3&gt;

&lt;p&gt;Network Watcher helps you to monitor your network. It has several functionalities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;connection troubleshoot - allows you to &lt;strong&gt;examine&lt;/strong&gt; a connection between your VM and another resource. You should use it when you want to diagnose outbound VM communication issues.&lt;/li&gt;
&lt;li&gt;connection monitor - thanks to it you can, for example, &lt;strong&gt;monitor&lt;/strong&gt; communication between your VMs.&lt;/li&gt;
&lt;li&gt;VPN diagnostics - allows you to &lt;strong&gt;diagnose&lt;/strong&gt; the health of your gateway. If there is a communication issue between your virtual networks, you should use it.&lt;/li&gt;
&lt;li&gt;NSG flow log - allows you to &lt;strong&gt;log&lt;/strong&gt; the information about the traffic that goes through your NSG.&lt;/li&gt;
&lt;li&gt;IP flow verify - enable you to &lt;strong&gt;check&lt;/strong&gt; if traffic is allowed or denied to/from your virtual machine. We used it to check if NSG blocks the traffic.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  82. What are Custom Script Extensions?
&lt;/h3&gt;

&lt;p&gt;Let's imagine that you want to create several virtual machines with SQL Server installed on them. You can connect to every machine and install SQL Server manually, but it can take you a lot of time and is error prone. The main goal of Custom Script Extensions is to take such responsibilities from you. You can define a script that installs SQL Server on your virtual machine and determine that the script is run after the deployment. &lt;/p&gt;

&lt;h3&gt;
  
  
  83. What is a load balancer?
&lt;/h3&gt;

&lt;p&gt;Load balancer as a name suggests is meant to balancing incoming traffic to resources. Let's assume that we have three virtual machines which serve web application, and we want to distribute the traffic between them. Users can initiate a connection to the load balancer and their flow is sent to one of the VM:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkumf13qdgzi2u3xlketk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkumf13qdgzi2u3xlketk.png" alt="Load balancer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  84. What is a health probe?
&lt;/h3&gt;

&lt;p&gt;The load balancer should send traffic only to resources (virtual machines) that are able to receive it. Imagine that one of the virtual machines is unavailable - we don't want to send the user's traffic to it. To prevent such situations the load balancer uses health probes. Every several seconds the load balancer sends TCP/HTTP/HTTPS request to the virtual machines. If a given virtual machine doesn't send any response (for TCP health probes) or returns HTTP response code different than 200 (for HTTP/HTTPS health probes), the probe fails. You can define the number of failed probes when the virtual machine is believed to be unavailable - the load balancer will stop sending user flow to that VM.&lt;/p&gt;

&lt;h3&gt;
  
  
  85. What are the differences between public and internal load balancers?
&lt;/h3&gt;

&lt;p&gt;A public load balancer is used to balance traffic from the Internet to your VMs. An internal load balancer is used for traffic that is inside your virtual network only.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwns6da8bfoo6pnkncmo5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwns6da8bfoo6pnkncmo5.png" alt="Load balancers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  86. What type of load balancers are available in Azure?
&lt;/h3&gt;

&lt;p&gt;There are two load balancers: Azure Load Balancer and Azure Application Gateway.&lt;/p&gt;

&lt;h3&gt;
  
  
  87. What is Azure Application Gateway?
&lt;/h3&gt;

&lt;p&gt;Azure Application Gateway is a load balancer, but it uses an application layer of the OSI model. It means that Application Gateway can balance traffic based on attributes of HTTP requests:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgnatxp6cvzqjrbqccjc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgnatxp6cvzqjrbqccjc2.png" alt="Application Gateway"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  88. What is Azure Load Balancer?
&lt;/h3&gt;

&lt;p&gt;Azure Load Balancer uses a transport layer of the OSI model, so it doesn't have access to the HTTP request details.&lt;/p&gt;

&lt;h3&gt;
  
  
  89. What is a VPN?
&lt;/h3&gt;

&lt;p&gt;VPN stands for a virtual private network. It is used to connect to some resources in a secure way. For example, you can use the VPN to join a virtual network in Azure and thanks to it, you are able to connect to private IPs of your Azure virtual machines. The traffic uses public Internet but is it end-to-end encrypted.&lt;/p&gt;

&lt;h3&gt;
  
  
  90. What is the difference between Point-to-Site (P2S) and Site-to-Site (S2S) VPN connections?
&lt;/h3&gt;

&lt;p&gt;Point-to-Site (P2S) VPN connection is used when you want to connect your virtual network from a client computer. The computer is treated as 'Point', and your virtual network as a 'Site'.&lt;br&gt;
Site-to-Site (S2S) VPN connection lets you connect Azure virtual network to your on-premises network. &lt;/p&gt;

&lt;h3&gt;
  
  
  91. What is Virtual Network Gateway?
&lt;/h3&gt;

&lt;p&gt;Virtual Network Gateway is commonly used to create VNet-to-VNet connection. When you want to connect to virtual networks, you can create a virtual network gateway in each of these virtual networks and specify the details of the connection.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhas7hgtkasny13uiv1np.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhas7hgtkasny13uiv1np.png" alt="VNET Gateway"&gt;&lt;/a&gt;&lt;br&gt;
It is important that you have to create a subnet named 'GatewaySubnet' that is used by the virtual network gateway. &lt;/p&gt;

&lt;h3&gt;
  
  
  92. Can I create a VNet-to-Vnet connection to any Azure virtual network I want?
&lt;/h3&gt;

&lt;p&gt;Yes, but the IP address range for these virtual networks should not overlap if you want to connect them.  &lt;/p&gt;

&lt;h3&gt;
  
  
  93. What is VPN Gateway?
&lt;/h3&gt;

&lt;p&gt;It is similar to the virtual network gateway, but it uses encryption before sending the traffic. It is commonly used when you want to connect your on-premises network and Azure virtual network.&lt;/p&gt;

&lt;h3&gt;
  
  
  94. How many VPN Gateway I can have in one virtual network.
&lt;/h3&gt;

&lt;p&gt;You can have only one VPN gateway, but that gateway can have several connections.&lt;/p&gt;

&lt;h3&gt;
  
  
  95. What is an active-active connection?
&lt;/h3&gt;

&lt;p&gt;Let's imagine that we want to create S2S VPN connection. We could do it by creating a VPN Gateway in the virtual network and establish one connection. But what will happen when the connection will be broken? You would not be able to send data between your network and on-premises for several seconds. To avoid such situations, we can use active-active connection. It simply means that a VPN gateway has two Gateway IP configurations with two public IP addresses and connects to one or two (dual-redundancy) IP addresses from on-premises:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fksj7u2franqjkoqo6vd9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fksj7u2franqjkoqo6vd9.png" alt="Active-active"&gt;&lt;/a&gt;&lt;br&gt;
To use active-active connection you should upgrade your SKU to VpnGw1, VpnGw2 or VpnGw3.&lt;/p&gt;

&lt;h3&gt;
  
  
  96. What is ExpressRoute?
&lt;/h3&gt;

&lt;p&gt;It is very similar to a VPN, but instead of using the public Internet to send traffic, the connection is established by an Azure connectivity provider.&lt;/p&gt;

&lt;h3&gt;
  
  
  97. What is WAN?
&lt;/h3&gt;

&lt;p&gt;If you have a highly developed network architecture that contains P2S VPN connections, S2S VPN connections, ExpressRoute and Azure virtual networks and you want to have the ability to connect it all together, you can use WAN:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faddmf356fwsr2a9huitt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faddmf356fwsr2a9huitt.png" alt="WAN"&gt;&lt;/a&gt;&lt;br&gt;
For example, you can connect from your personal computer using P2S VPN connection to WAN and later to your corporate network by ExpressRoute.&lt;/p&gt;

&lt;h3&gt;
  
  
  98. What is Azure Backup Service?
&lt;/h3&gt;

&lt;p&gt;As a name suggests, Azure Backup Service is responsible for backing up your data. You can back up several resources in Azure like virtual machine and file share:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2vgp8yy2fy7fe0tg14fl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2vgp8yy2fy7fe0tg14fl.png" alt="Azure Backup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  99. What is Recovery Services Vault?
&lt;/h3&gt;

&lt;p&gt;Recovery Service Vault is a container for your backups. When you want to create a backup, you should create this vault first.&lt;/p&gt;

&lt;h3&gt;
  
  
  100. What is the relationship between virtual machines and Recovery Services Vault?
&lt;/h3&gt;

&lt;p&gt;One virtual machine can be backed up by one Backup Service, and the Recovery Services Vault should be in the same region as the virtual machine. One Recovery Services Vault can contain several backups.&lt;/p&gt;

&lt;h3&gt;
  
  
  101. What is Backup Policy?
&lt;/h3&gt;

&lt;p&gt;In Backup Policy you can define when the backup will be taken and how long it will be available. For example, you can define that the daily backup will be stored for 10 days. Backups that were taken every Sunday will be available for 12 weeks and when the backup was created on the first Sunday of the month, it will be saved for 60 months. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8h596i09wx744bnjvw4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8h596i09wx744bnjvw4f.png" alt="Backup Policy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  102. What is Update Domain?
&lt;/h3&gt;

&lt;p&gt;Sometimes it is needed to conduct planned maintenance for virtual machines. In that situation, the virtual machine can be restarted and, as a result, it is not available for a short time. To avoid that all your virtual machines are restarted at the same time, you can use Update Domain. All virtual machines in a given Update Domain can be restarted together, but it never happens that several Update Domains are restarted at once. &lt;/p&gt;

&lt;h3&gt;
  
  
  103. What is Fault Domain?
&lt;/h3&gt;

&lt;p&gt;Fault Domain can be treated as a single hardware rack. If there is a malfunction for the rack, then all allocated resources are not available. To prevent situations when all your resources are unusable because of a hardware failure, you should place them in separate Fault Domains. &lt;/p&gt;

&lt;h3&gt;
  
  
  104. What is Availability Set?
&lt;/h3&gt;

&lt;p&gt;To ensure high availability of your virtual machines you can use Availability Set. You can define how many Fault Domains and Update Domains it contains:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffxn6v3qjp9o1bwmknpsb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffxn6v3qjp9o1bwmknpsb.png" alt="Availability Set"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;If you find this blog post helpful for your learning journey in Azure, please write a comment!&lt;/p&gt;

</description>
      <category>learninpublic</category>
      <category>azure</category>
    </item>
  </channel>
</rss>
