<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rahul Yarragodula</title>
    <description>The latest articles on DEV Community by Rahul Yarragodula (@rahulyr).</description>
    <link>https://dev.to/rahulyr</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%2F559308%2F2f82358b-218d-4b59-bc75-794d294ff52c.jpg</url>
      <title>DEV Community: Rahul Yarragodula</title>
      <link>https://dev.to/rahulyr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahulyr"/>
    <language>en</language>
    <item>
      <title>Develop a Todo GraphQL Server in Golang 🔥</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Tue, 05 Jul 2022 07:45:44 +0000</pubDate>
      <link>https://dev.to/rahulyr/develop-a-todo-graphql-server-in-golang-4b8c</link>
      <guid>https://dev.to/rahulyr/develop-a-todo-graphql-server-in-golang-4b8c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is a simple Todo application developed in Golang using GraphQL. This tutorial helps you to find the right way for building your own GraphQL Server implementation in Go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rahul-yr/learn-go-graphql"&gt;Click here&lt;/a&gt; to access full source code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.postman.com/rahul-public/workspace/learn-go-graphql-todo-app"&gt;Click here&lt;/a&gt; to access the Postman collections. &lt;/p&gt;

&lt;p&gt;In this tutorial we will focus mainly on 3 things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's beginner friendly.&lt;/li&gt;
&lt;li&gt;Focused on industry best practices.&lt;/li&gt;
&lt;li&gt;Deploy to the cloud.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The scope of this tutorial is to focus mostly on building graphQL servers rather than Go basics. If you're completely new to Golang, I would highly encourage you to have a strong knowledge on Go basics before going into this article.&lt;/p&gt;

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

&lt;p&gt;GraphQL is created by Facebook, implemented in their mobile app in 2012 and open-sourced in 2015.&lt;/p&gt;

&lt;p&gt;GraphQL is a query language and server-side runtime for APIs. GraphQL provides a flexible and intuitive syntax that enables clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time. &lt;/p&gt;

&lt;p&gt;As an alternative to REST, GraphQL lets developers construct requests that pull data from multiple data sources in a single API call. Therefore reducing the network calls and bandwidth that saves the battery life and CPU cycles consumed by the backend applications (&lt;a href="https://graphql.org/"&gt;Official source&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;Additionally, GraphQL gives API maintainers the flexibility to add or deprecate fields without impacting existing queries. &lt;/p&gt;

&lt;p&gt;GraphQL is rapidly becoming the standard for API-based data access.&lt;/p&gt;

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

&lt;p&gt;API developers create GraphQL schema that describes all the possible data that clients can query through that service. GraphQL schema is made up of object types, which defines the kind of object you can request and the fields it has. &lt;/p&gt;

&lt;p&gt;The most common operations on any GraphQL APIs are &lt;code&gt;Queries&lt;/code&gt; and &lt;code&gt;Mutations&lt;/code&gt;. &lt;code&gt;Queries&lt;/code&gt; is used for reading data from APIs. &lt;code&gt;Mutations&lt;/code&gt; are used for Create, Update and Delete operations.&lt;/p&gt;

&lt;p&gt;Each operation in GraphQL schema is attached to a function called &lt;code&gt;resolvers&lt;/code&gt;. A &lt;code&gt;resolver&lt;/code&gt; is used to perform the actual execution of &lt;code&gt;Query&lt;/code&gt; or &lt;code&gt;Mutation&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://learn-go-graphql.herokuapp.com/todo"&gt;Use this endpoint to perform live test&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/5cvnsjQHwYQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Develop a GraphQL Server in Go
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open your favorite code editor and create a go module using this &lt;code&gt;go mod init github.com/rahul-yr/learn-go-graphql&lt;/code&gt; command.&lt;/li&gt;
&lt;li&gt;Install the below modules.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Create a folder named &lt;code&gt;todo&lt;/code&gt; in the root directory and 3 files named &lt;code&gt;todo/models.go&lt;/code&gt;, &lt;code&gt;todo/schema.go&lt;/code&gt; and &lt;code&gt;todo/udf.go&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a Todo model in the &lt;code&gt;todo/models.go&lt;/code&gt; file. This model represents the todo item.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Implement CRUD operations.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's create the CRUD operations for the TODO model(&lt;code&gt;todo/udf.go&lt;/code&gt;). &lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The above snippet has 5 functions defined 2 for &lt;code&gt;Queries&lt;/code&gt; and 3 for &lt;code&gt;Mutations&lt;/code&gt;. As well as a variable to store all the todo items(you could use a database instance here).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GetTodos()&lt;/code&gt; method is used for fetching all the todo items.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GetTodo(id int)&lt;/code&gt; method is used for fetching a todo item based on item id.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;AddTodo(title string)&lt;/code&gt; method is used for creating a new todo item.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UpdateTodo(id int, title string, completed bool)&lt;/code&gt; method is used for updating the existing todo item based on item id.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DeleteTodo(id int)&lt;/code&gt; method is used for deleting the todo item based on item id.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implement GraphQL schema
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Now it's finally time to create the GraphQL schema (&lt;code&gt;todo/schema.go&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;GraphQL schema is implemented using this &lt;code&gt;graphql.NewSchema(graphql.SchemaConfig{...})&lt;/code&gt; method. This method actually resolves the GraphQL requests.&lt;/li&gt;
&lt;li&gt;You could define &lt;code&gt;Queries&lt;/code&gt;,&lt;code&gt;Mutations&lt;/code&gt; and other &lt;code&gt;object types&lt;/code&gt; using this &lt;code&gt;graphql.NewObject(graphql.Objectconfig{...})&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;This &lt;code&gt;graphql.Fields{...}&lt;/code&gt; method is useful for defining fields.&lt;/li&gt;
&lt;li&gt;Below method is used for declaring input arguments.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;graphql.FieldConfigArgument{
                    "some_id": &amp;amp;graphql.ArgumentConfig{
                        Type: graphql.SomeDataType,
                    },
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Resolve&lt;/code&gt; block is where actual execution happens.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Implement an endpoint for GraphQL
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a route handler(&lt;code&gt;graph/router.go&lt;/code&gt;) file.&lt;/li&gt;
&lt;li&gt;Here I have used the &lt;code&gt;gin&lt;/code&gt; package. You could use any http package of your choice for creating endpoints.&lt;/li&gt;
&lt;li&gt;The most important part here is to add this &lt;code&gt;graphql.Do(...)&lt;/code&gt; method. This actually resolves the graphql  request.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the server
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;main.go&lt;/code&gt; file for running the graphql server.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Run the server using &lt;code&gt;go run .&lt;/code&gt; command&lt;/li&gt;
&lt;li&gt;You can find the live &lt;a href="https://www.postman.com/rahul-public/workspace/learn-go-graphql-todo-app"&gt;postman api collection here&lt;/a&gt;. Refer this for api specifications.&lt;/li&gt;
&lt;li&gt;Below are the few snapshots of API requests and responses in action using Postman.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create a Todo item
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kGADPUZh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgh3aqki80ctqc9k5be4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kGADPUZh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgh3aqki80ctqc9k5be4.PNG" alt="create-todo.PNG" width="880" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Update a Todo item
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9_e6kE-6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhreyaivpxzo20seqks8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9_e6kE-6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhreyaivpxzo20seqks8.PNG" alt="update-todo.PNG" width="880" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Read a Todo item based on item id
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rrnz7K1w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsg5x0050a6l2cesy10g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rrnz7K1w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsg5x0050a6l2cesy10g.PNG" alt="read-todo-by-id.PNG" width="880" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Read all Todo items
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2w2VxuAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mfq034pdcwf6cpvrild.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2w2VxuAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mfq034pdcwf6cpvrild.PNG" alt="read-all-todos.PNG" width="880" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Delete a Todo item
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fHW5_MGj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idiy1k7zd75my7wjae6y.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fHW5_MGj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idiy1k7zd75my7wjae6y.PNG" alt="delete-todo.PNG" width="880" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy to Cloud
&lt;/h2&gt;

&lt;p&gt;Know how to deploy this repo to Heroku ? &lt;a href="https://dev.to/rahulyr/how-to-deploy-any-backend-to-heroku-easily--22mf"&gt;Click here to find out right way&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the great things you're gonna build with this. If you are struck somewhere feel free to comment. I am always available.&lt;br&gt;
Please find the complete code at &lt;a href="https://github.com/rahul-yr/learn-go-graphql"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>graphql</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is a Goroutine ? Find the right way to implement goroutine 🔥</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Tue, 21 Jun 2022 18:22:49 +0000</pubDate>
      <link>https://dev.to/rahulyr/what-is-a-goroutine-find-the-right-way-to-implement-goroutine-ocb</link>
      <guid>https://dev.to/rahulyr/what-is-a-goroutine-find-the-right-way-to-implement-goroutine-ocb</guid>
      <description>&lt;h2&gt;
  
  
  Why Go ?
&lt;/h2&gt;

&lt;p&gt;Go has been widely used in most of the popular production systems such as Kubernetes, Docker, Influx DB, etc. There are mainly 4 reasons behind adoption of Go at such a massive scale. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's simple and elegant&lt;/li&gt;
&lt;li&gt;Provides built-in concurrency at scale&lt;/li&gt;
&lt;li&gt;Designed to run on multiple cores &lt;/li&gt;
&lt;li&gt;It's Super fast performance 🚀&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goroutine is a way to achieve concurrency in Go 🔥. &lt;/p&gt;

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

&lt;p&gt;A goroutine is a lightweight thread managed by the Go runtime. Unlike Operating System thread which consumes memory usually in the range of &lt;strong&gt;MBs to function properly&lt;/strong&gt; these goroutines consume memory only in the &lt;strong&gt;range of KBs&lt;/strong&gt;. So it's &lt;strong&gt;easy and fast for go runtime to create goroutines than threads for concurrency&lt;/strong&gt;. Goroutines are not actual threads, these are multiplexed to actual OS threads or even you could assume that these goroutines are a kind of abstract layer over OS threads.&lt;/p&gt;

&lt;p&gt;Due to it's very &lt;strong&gt;small memory footprint&lt;/strong&gt;, it's easy for go runtime to create &lt;strong&gt;thousands of goroutines in a fraction of milliseconds&lt;/strong&gt;. But creating thousands of OS threads is heavily dependent on &lt;strong&gt;memory and the CPU&lt;/strong&gt;, so it's obviously slow compared with goroutines. &lt;/p&gt;

&lt;p&gt;As well as Goroutine does not have a Local Storage as a result goroutines are cheaper to implement than OS threads, so &lt;strong&gt;goroutines are much faster in terms of boot time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Context switching is also faster in case of &lt;code&gt;go&lt;/code&gt; than OS thread context switching, because OS threads are heavy resources.&lt;/p&gt;

&lt;p&gt;On top of this &lt;strong&gt;Go even has another coolest feature i.e. Channels&lt;/strong&gt;🔥. Channels are the built-in way to communicate with goroutines. In the case of OS threads, this communication is the most difficult thing you can think of.&lt;/p&gt;

&lt;p&gt;So this how go achieved better concurrency than traditional programming languages, which made &lt;strong&gt;go a default language of choice for most of the use cases&lt;/strong&gt;.   &lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax for goroutine
&lt;/h2&gt;

&lt;p&gt;The most difficult part of goroutine is to understand it more than its real implementation. So take your time to understand it before diving deep into the actual implementation.&lt;/p&gt;

&lt;p&gt;In other programming languages you might have used the &lt;code&gt;async await&lt;/code&gt; pattern for concurrency(Node.js and python - async await). But here it's even simpler than that. Just add the &lt;code&gt;go&lt;/code&gt; keyword to your function prefix( e.g : &lt;code&gt;go someFunctionName&lt;/code&gt;), now go runtime executes the same function in concurrency mode. See how cool is this 😆.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    go addTwoNumbers(1, 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Implement a program without goroutine
&lt;/h2&gt;

&lt;p&gt;Below is the snippet for adding 2 numbers. The &lt;code&gt;addition&lt;/code&gt; function has a &lt;code&gt;sleep&lt;/code&gt; command to mimic the processing time.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;If you execute the program using the &lt;code&gt;go run sequential.go&lt;/code&gt; command, you will get the output as below(result as summation of 2 numbers).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9PhvmGvX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkf6dqzfq8wg9s70t76d.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9PhvmGvX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkf6dqzfq8wg9s70t76d.PNG" alt="sequential-out.PNG" width="862" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement a program with goroutine
&lt;/h2&gt;

&lt;p&gt;Now let's convert this sequential execution to concurrency by adding &lt;code&gt;go&lt;/code&gt; to the prefix of the caller. Below is the snippet.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you execute now using this &lt;code&gt;go run concurrency_without_wg.go&lt;/code&gt; command, you will immediately notice that the output of &lt;strong&gt;addition function is not displayed&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SxhBzr2V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og58tlmml4oufz2zz29p.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SxhBzr2V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og58tlmml4oufz2zz29p.PNG" alt="concurrency_without_waitgroups_out.PNG" width="830" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you reached this far, then you have &lt;strong&gt;successfully executed your first program in concurrency mode&lt;/strong&gt; 🔥. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But why is there a discrepancy in the output ?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is happening because go runtime is not waiting for goroutines to finish their execution. So you need to explicitly update the details about goroutines to go runtime. This is where &lt;code&gt;wait groups&lt;/code&gt; comes into picture. That's the exact problem wait groups solve.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Wait groups ?
&lt;/h3&gt;

&lt;p&gt;WaitGroup(&lt;code&gt;sync.WaitGroup&lt;/code&gt;) is a mechanism to wait till the collection of goroutines finishes their tasks.&lt;br&gt;
There are mainly 3 helper functions available in golang to implement this feature. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add()&lt;/li&gt;
&lt;li&gt;Wait()&lt;/li&gt;
&lt;li&gt;Done()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Add(5)&lt;/code&gt; method takes an integer as an argument. This integer represents the number of goroutines available in this collection to the go runtime. &lt;code&gt;Wait()&lt;/code&gt; tells the go runtime to wait exactly at this position till the collection of goroutines finishes their execution. &lt;code&gt;Done()&lt;/code&gt; method is used to inform go runtime that this particular goroutine finished its execution.&lt;/p&gt;

&lt;p&gt;Below is the code snippet with 1 goroutine. Refer to the comments for deeper understanding.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you execute now, you will get the desired output as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KH9MlR-j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq148phvx3tpvzla99tx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KH9MlR-j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq148phvx3tpvzla99tx.PNG" alt="concurrency-out.PNG" width="839" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;You have successfully implemented goroutines 🔥, but without channels (A way to communicate with goroutines) this topic is never gonna be completed. &lt;/p&gt;

&lt;p&gt;To stay updated on &lt;code&gt;Channels&lt;/code&gt; you can follow this blog.&lt;/p&gt;

&lt;p&gt;I would 💝 to hear your feedback and comments on the great things you're gonna build with this. If you are struck somewhere feel free to comment. I am always available.&lt;/p&gt;

&lt;p&gt;If you feel this article is helpful for others then please consider a like.&lt;/p&gt;

&lt;p&gt;You can find the complete code at &lt;a href="https://github.com/rahul-yr/learn-go-concepts.git"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>goroutine</category>
      <category>webdev</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>How to implement Concurrency and Parallelism in Go? 🔥</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Mon, 20 Jun 2022 17:26:20 +0000</pubDate>
      <link>https://dev.to/rahulyr/how-to-implement-concurrency-and-parallelism-in-go-44jk</link>
      <guid>https://dev.to/rahulyr/how-to-implement-concurrency-and-parallelism-in-go-44jk</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Before diving deep into the actual topic, it's really important to make yourself familiar with the below terminologies.&lt;/p&gt;

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

&lt;p&gt;A core or processor is a complex logic in the CPU which can execute a set of instructions known as a program.  A Processor is a hardware entity that runs the actual &lt;code&gt;Process&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;In reality we won't be running our programs directly on the CPU core. This responsibility is handed over to the Operating system. Operating system writes your program instructions to the core or processor through something called &lt;code&gt;Process&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Process&lt;/code&gt; is a logical instance of your program which is managed by the operating system. There will be thousands of processes scheduled by the Operating system based on the priority of the program. Irrespective of the core or processor count an Operating system can have an arbitrary number of &lt;code&gt;logical processes&lt;/code&gt; but only one &lt;code&gt;process&lt;/code&gt; instruction gets executed per processor at any point of time.  &lt;/p&gt;

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

&lt;p&gt;A &lt;strong&gt;thread is a path of execution within a process&lt;/strong&gt;. A process can contain &lt;strong&gt;multiple threads&lt;/strong&gt;. All programs have at least one thread that gets created when the program is started, but a program can start more threads to perform work in parallel.&lt;/p&gt;

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

&lt;p&gt;Concurrency is a way to &lt;strong&gt;handle multiple tasks at once&lt;/strong&gt; by efficiently utilizing the available resources. Even though it deals with multiple tasks simultaneously, it could only execute one non-blocking task(execution state) at any point of time. The remaining tasks either be in a blocked state, yet to start, completed, hold or any other states.&lt;/p&gt;

&lt;p&gt;As you can see in the below snapshot it looks like we are executing multiple tasks simultaneously, but the truth is we are deprioritizing the tasks by wait time or blocked state. So that program could perform other important tasks during the same time and come back to the place where it left once it got the response. This is known as Concurrency.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G7XNuYLZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7c4a2kjy5bcbgrn629z2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G7XNuYLZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7c4a2kjy5bcbgrn629z2.png" alt="concurrency.drawio.png" width="802" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assume that you have created a program in any of your favorite programming languages that supports concurrency. As well as this program has 3 arbitrary tasks. Initially all the tasks are in default state(yet to start). &lt;/p&gt;

&lt;p&gt;Assume that all the 3 tasks have some preprocessing and network calls involved in it, So it takes arbitrary  time to get the response from the external systems.&lt;/p&gt;

&lt;p&gt;Once you run the program, you will see that task 1 is in execution state since it has some preprocessing involved. We also know that it has network calls involved so it's waiting(blocked state) for the response from external systems. Mean time task 2 starts executing its preprocessing layer and enters into the waiting state again due to network calls. Now there are 2 tasks in the blocked state so your program starts executing task 3 and it's preprocessing layers. Now even task 3 enters into a blocked state. Once any of the previous blocked states got the response then its respective task got completed. &lt;/p&gt;

&lt;p&gt;For some reason if 2 or more tasks in a blocked state get the response at the same time, then your &lt;strong&gt;program picks the task randomly and fulfills it one after another&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Parallelism is all about &lt;strong&gt;executing multiple jobs independently in parallel&lt;/strong&gt; at the same given time. Unlike concurrency it doesn't care about the task status, So it executes all tasks at once in parallel. To perform these independent actions it consumes additional resources.&lt;/p&gt;

&lt;p&gt;As you can see in the below snapshot all the 3 tasks started it's execution, waited and finished respective tasks at the same time. But overall speed wise it took less time compared to concurrency. In Terms of resource usage it still has a bandwidth to accommodate other tasks while all the tasks have been in idle state. With parallelism &lt;strong&gt;we were able to get things done faster, but weren't able to utilize the resources better.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---vJKc8kL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xy48u0qj946zi1bjuukj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---vJKc8kL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xy48u0qj946zi1bjuukj.png" alt="parallelism.drawio.png" width="791" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have a system with multiple cores, then you could definitely take advantage of Parallelism.&lt;/p&gt;

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

&lt;p&gt;A goroutine is a lightweight thread managed by the Go runtime. A thread could have thousands of goroutines due to its less compute intensiveness. The main difference between goroutine and thread is Goroutine does not have a Local Storage as a result goroutines are cheaper to implement than threads, so goroutines are much faster in terms of boot time.&lt;/p&gt;

&lt;p&gt;If you're new to goroutines then &lt;a href="https://dev.to/rahulyr/what-is-a-goroutine-find-the-right-way-to-implement-goroutine-ocb"&gt;click here for deeper understanding&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement Parallelism in Go
&lt;/h2&gt;

&lt;p&gt;Refer to the code snippet below.&lt;/p&gt;

&lt;p&gt;It has a variable named &lt;code&gt;threadProfile&lt;/code&gt; which will be used for tracking the number of threads being created at runtime. Here in Go, threads are the way to implement parallelism.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;runtime.GOMAXPROCS(runtime.NumCPU())&lt;/code&gt; is used to set the number of cores your application can use. If you don't set this variable by default it uses all the available cores. Usually it's defined in &lt;code&gt;init&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;runtime.LockOSThread()&lt;/code&gt; will call this function to prevent goroutines from reusing the existing threads by locking and unlocking the thread. It's a trick to see parallelism in action, because Go scheduler by default &lt;strong&gt;reuses the threads in  non-blocked state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We have added &lt;code&gt;log statements&lt;/code&gt; to see the count of threads before and after executions.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can execute the above snippet using the &lt;code&gt;go run parallelism.go&lt;/code&gt; command. You will get the similar output as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CRqNbvL0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7u0b2acwwsnzanea4ck.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CRqNbvL0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7u0b2acwwsnzanea4ck.PNG" alt="parallelism-output.PNG" width="617" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the &lt;code&gt;count := 10&lt;/code&gt; we can see that approximately 10 new threads are being created. It clearly shows that this program is executed in parallel by locking the threads. We locked the threads to force this to happen for demo purposes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement Concurrency in Go
&lt;/h2&gt;

&lt;p&gt;Refer to the code snippet below.&lt;/p&gt;

&lt;p&gt;It has almost similar variables and functions as defined above. Only major change is that we removed the &lt;code&gt;lock&lt;/code&gt; methods, because we want to see the default behavior of goroutines.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can execute the above snippet using the &lt;code&gt;go run concurrency.go&lt;/code&gt; command. If you get the similar output as below then you have successfully executed the program in concurrency mode. From the output you can understand that even for &lt;code&gt;count:=10&lt;/code&gt; all the 10 goroutines are accommodated in the same number of available threads without even creating a new thread.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4bgmD-Yd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yvs8l40ol9e5c6v0ze2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4bgmD-Yd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yvs8l40ol9e5c6v0ze2.PNG" alt="concurrency-out1.PNG" width="717" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you have seen concurrency in action, but do you know &lt;strong&gt;Go automatically creates the required number of threads dynamically ?&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's see both &lt;strong&gt;concurrency and parallelism in action&lt;/strong&gt; by updating &lt;code&gt;count:=1000&lt;/code&gt;(1000 goroutines). You will get the similar output as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--me0HS6qG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ww99n910j4w6emfj0c5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--me0HS6qG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ww99n910j4w6emfj0c5.PNG" alt="concurrency-parallelisim-in-action.PNG" width="870" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you watch the output closely you will find a new thread being created based on the demand.&lt;/p&gt;

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

&lt;p&gt;To get the best performance out of the box it's suggested to combine Concurrency and Parallelism in any language of your choice. In Go it's handled internally, but in other languages you need to explicitly define the details (node.js - clusters and async await , python - gunicorn and async await(FastAPI)).&lt;/p&gt;

&lt;p&gt;If you love this article, then definitely you will also enjoy learning &lt;a href="https://dev.to/rahulyr/how-to-implement-singleton-design-pattern-in-go--5e1d"&gt;Advanced Singleton design pattern implementation in Go&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the great things you're gonna build with this. If you are struck somewhere feel free to comment. I am always available.&lt;/p&gt;

&lt;p&gt;If you feel this article is helpful for others then please consider a like.&lt;/p&gt;

&lt;p&gt;Please find the complete code at &lt;a href="https://github.com/rahul-yr/learn-go-concepts.git"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>goroutine</category>
      <category>webdev</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Simple URL shortener app in Python for Beginners (🔥 FastAPI Demo)</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Wed, 15 Jun 2022 18:09:42 +0000</pubDate>
      <link>https://dev.to/rahulyr/simple-url-shortener-app-in-python-for-beginners-fastapi-demo-52g1</link>
      <guid>https://dev.to/rahulyr/simple-url-shortener-app-in-python-for-beginners-fastapi-demo-52g1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is a simple url shortener application developed in Python FastAPI.&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/rahul-yr/learn-fastapi-simple-url-shortner" rel="noopener noreferrer"&gt;Click here&lt;/a&gt; to access the Github repo.&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.postman.com/rahul-public/workspace/fastapi-simple-url-shortner-crud/collection/8974578-e4dd76aa-0334-47c3-ba64-c84e27ff58f6" rel="noopener noreferrer"&gt;Click here&lt;/a&gt; to access Postman API collection.&lt;/p&gt;

&lt;p&gt;In this tutorial we will focus mainly on 3 things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's &lt;strong&gt;beginner friendly&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Focused on industry &lt;strong&gt;best practices&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Deploy to cloud.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The scope of this tutorial is to focus mostly on building APIs rather than Python basics. If you're completely new to Python I would highly encourage you to have a strong knowledge on Python basics before going into this article.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Download and install VS code from &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Download and install Python from &lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What is a Virtual environment ?
&lt;/h2&gt;

&lt;p&gt;A virtual environment is an &lt;strong&gt;isolated environment&lt;/strong&gt; used to keep your project dependencies independently from any other projects or system configurations. This helps to avoid dependency conflicts with different versions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Live demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://learn-fastapi-url-shortener.herokuapp.com/url-shortener/list" rel="noopener noreferrer"&gt;click here to test live endpoints&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/_pl0OpmZ2Og"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Develop url shortener APIs
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open the VS code and create a new file named &lt;code&gt;main.py&lt;/code&gt; in the root folder.&lt;/li&gt;
&lt;li&gt;This &lt;code&gt;main.py&lt;/code&gt; file is responsible for running your application and acts as an entry point for your app.&lt;/li&gt;
&lt;li&gt;Create a Python virtual environment using this command &lt;code&gt;python -m venv virtual_env_name&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Activate your virtual environment with respective operating system commands.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Linux
source virtual_env_name/bin/activate

# Windows
virtual_env_name\Scripts\activate.bat

#Mac
source virtual_env_name/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Install all the required dependencies in the Python virtual environment.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# add fast api package
pip install fastapi

# add web server for running fast api
pip install "uvicorn[standard]"

# add package for cleaning cache files
pip install pyclean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Add below snippet to the &lt;code&gt;main.py&lt;/code&gt; file. This snippet has two endpoints of request method type GET. One endpoint returns &lt;code&gt;{"hello" : "world"}&lt;/code&gt; json response and other returns the dynamic parameter you passed as &lt;code&gt;item_id&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Run the application using the below command. The default host url is &lt;code&gt;&lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt;&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn main:app --reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Open the default endpoint and if you see something like this, then you have successfully 🔥 deployed your application locally.&lt;/li&gt;
&lt;/ul&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%2F1qvjmt0rt7xm9tduz8tu.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%2F1qvjmt0rt7xm9tduz8tu.PNG" alt="sample-fastapi-url.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to GET methods you can define any supported request methods you need.&lt;/li&gt;
&lt;li&gt;Remove the two methods you have added i.e &lt;code&gt;read_root&lt;/code&gt;, &lt;code&gt;read_item&lt;/code&gt; and it's finally time to implement the actual url shortener endpoints.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Create url shortener endpoints.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create the below files and directories in the root folder.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# folder : 
url_shortener

# files
url_shortener/__init__.py
url_shortener/database.py
url_shortener/router.py
url_shortener/models.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__init__.py&lt;/code&gt; file is used for representing the current folder as a package. So that it can be referenced in other packages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;router.py&lt;/code&gt; file is responsible for handling url shortener routes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;models.py&lt;/code&gt; is a common file for defining all the necessary models for url shorteners.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;database.py&lt;/code&gt; file is responsible for handling database operations. Since there are hundreds of  database options available on the internet and for the purpose of simplicity, here we mimic database operations with sleep commands and store the data in memory variables.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Define models
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Below is the snippet for &lt;code&gt;models.py&lt;/code&gt;. As you can see in the code, It has 2 models.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CreateUrlShortener&lt;/code&gt; model is responsible for validating creation of short URLs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CreateUrlShortenerResponse&lt;/code&gt; model is used as a json response model.
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Define database operations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Below is the snippet for the &lt;code&gt;database.py&lt;/code&gt; file. It has a member variable &lt;code&gt;all_data&lt;/code&gt; which stores all the data in memory. Here you can define your respective database instance instead of the &lt;code&gt;in memory&lt;/code&gt; variable.
As well as it has 3 functions for creating a short url, deleting a short url and fetching all the short urls. &lt;/li&gt;
&lt;li&gt;This snippet has well defined comments to help you understand the logic in detail.
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create endpoints
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Here is the snippet for the &lt;code&gt;router.py&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;In this we have defined an api router named &lt;code&gt;url_shortener&lt;/code&gt; and database instance named &lt;code&gt;mock_db_operations&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;url_shortener&lt;/code&gt; is used for mounting these endpoints to the main router.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mock_db_operations&lt;/code&gt; is used for performing db operations.&lt;/li&gt;
&lt;li&gt;It has 3 methods defined for creating, deleting, listing endpoints and 1 method for testing url redirects in action. Use the inline comments to understand the logic in detail.
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Update main file
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Below is the snippet for the &lt;code&gt;main.py&lt;/code&gt; file which has the url router mounted i.e &lt;code&gt;url_shortener&lt;/code&gt;.
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Run the application locally using below command
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn main:app --reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use &lt;a href="https://www.postman.com/rahul-public/workspace/fastapi-simple-url-shortner-crud/collection/8974578-e4dd76aa-0334-47c3-ba64-c84e27ff58f6" rel="noopener noreferrer"&gt;this postman collection&lt;/a&gt; as API specs and test the endpoints. &lt;/li&gt;
&lt;li&gt;Below are the few snapshots of API requests and responses in action using Postman.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create url shortener snapshot
&lt;/h3&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%2Fx21k6jla3r2jf7xf12m6.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%2Fx21k6jla3r2jf7xf12m6.PNG" alt="create-short-url.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  List all short urls
&lt;/h3&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%2Fkvnqo96mgubruqf31h3d.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%2Fkvnqo96mgubruqf31h3d.PNG" alt="list-short-urls.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Test redirects in action
&lt;/h3&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%2Fwjgwojpxvj17yu9eokcw.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%2Fwjgwojpxvj17yu9eokcw.PNG" alt="test-redirect.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Delete short URLs
&lt;/h3&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%2Fau5j7wi81k0xhldl54xt.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%2Fau5j7wi81k0xhldl54xt.PNG" alt="delete-short-url.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy to cloud
&lt;/h2&gt;

&lt;p&gt;Know how to deploy this repo to Heroku ? &lt;a href="https://dev.to/rahulyr/how-to-deploy-any-backend-to-heroku-easily--22mf"&gt;Click here to find out right way&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the great things you're gonna build with this. If you are struck somewhere feel free to comment. I am always available.&lt;/p&gt;

&lt;p&gt;Please find the complete code at &lt;a href="https://github.com/rahul-yr/learn-fastapi-simple-url-shortner" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>crud</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to deploy any backend to Heroku (easily🔥) ?</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Sun, 12 Jun 2022 15:19:11 +0000</pubDate>
      <link>https://dev.to/rahulyr/how-to-deploy-any-backend-to-heroku-easily--22mf</link>
      <guid>https://dev.to/rahulyr/how-to-deploy-any-backend-to-heroku-easily--22mf</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;For the purpose of demonstration and to keep things simple we will use this &lt;a href="https://github.com/rahul-yr/learn-go-simple-todo-crud"&gt;Repository&lt;/a&gt; &lt;strong&gt;developed in Go&lt;/strong&gt;. But you are free to choose &lt;strong&gt;any language&lt;/strong&gt; of your choice.&lt;/p&gt;

&lt;p&gt;Sample Repo for Python(&lt;a href="https://github.com/rahul-yr/learn-fastapi-simple-url-shortner"&gt;Url Shortener App&lt;/a&gt;) and you can find the respective implementation &lt;a href="https://dev.to/rahulyr/simple-url-shortener-app-in-python-for-beginners-fastapi-demo-52g1"&gt;tutorial here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a very simple &lt;a href="https://github.com/rahul-yr/learn-go-simple-todo-crud"&gt;Todo crud&lt;/a&gt; application which you can find on the internet. Feel free to use this repo. You can even take a step further and contribute to this open source repository with additional features. &lt;/p&gt;

&lt;p&gt;You can find the API specs and postman collection of this repo &lt;a href="https://blog.rahuldev.in/simple-todo-app-in-go-for-beginners-demo"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To follow along with this tutorial you need to have a Heroku Account. I am gonna use GitHub as a Cloud repository. You can use any of your favorites(Bit bucket, Azure DevOps, Google cloud Repo, etc.).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just a quick recap for those who don't know about Heroku &lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. Our platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market.(&lt;a href="https://www.heroku.com/about"&gt;Official read more&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Download and Install VS code from &lt;a href="https://code.visualstudio.com/"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Download and install git from &lt;a href="https://git-scm.com/"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Open your terminal and type these commands with respective details to configure git.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.name "example name"
git config --global user.email "example@github.com"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Download and install Heroku CLI from &lt;a href="https://devcenter.heroku.com/articles/heroku-cli#install-the-heroku-cli"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deploy
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open your terminal and clone this &lt;a href="https://github.com/rahul-yr/learn-go-simple-todo-crud"&gt;repo&lt;/a&gt; using the below command.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/rahul-yr/learn-go-simple-todo-crud.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--92a-Lig---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/joieoz62v2cb9khy8bna.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--92a-Lig---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/joieoz62v2cb9khy8bna.PNG" alt="clone-repo.PNG" width="880" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open the cloned repo in VS code&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's really important to tell Heroku what steps to follow during application startup. This is where &lt;code&gt;Procfile&lt;/code&gt; comes handy. You can create and mention all the steps in &lt;code&gt;Procfile&lt;/code&gt; specific to your requirement (&lt;a href="https://devcenter.heroku.com/articles/procfile"&gt;Click here to read more on Procfile&lt;/a&gt;). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For this scenario use this snippet in Procfile&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: some-app-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ApMuqXWX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c39wtei8hqjcdxb1uj33.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ApMuqXWX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c39wtei8hqjcdxb1uj33.PNG" alt="procfile-raw.PNG" width="880" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;heroku login&lt;/code&gt; command to add the heroku session to your user account. Once it's done you will get something like this.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WyEz9ARS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1wou88rz7opo1ajhx5r.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WyEz9ARS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1wou88rz7opo1ajhx5r.PNG" alt="heroku-login-raw.PNG" width="880" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's finally time to create an app in Heroku using the below command.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku create some-app-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t105Wz3y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5hhbf7vhae3i3si466g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t105Wz3y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5hhbf7vhae3i3si466g.PNG" alt="heroku-create.PNG" width="880" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now deploy your app to Heroku using below command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku your-branch-name:master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gzhKjBnr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2v37il3dyjszb36xkl1j.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gzhKjBnr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2v37il3dyjszb36xkl1j.PNG" alt="heroku-deploy-1.PNG" width="880" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you get the same status as below then you have successfully 🔥 deployed to Heroku.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yF6ajrLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b41k2y0zk5u8o7fswuau.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yF6ajrLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b41k2y0zk5u8o7fswuau.PNG" alt="heroku-deploy-2.PNG" width="880" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;heroku open&lt;/code&gt; command to view the deployed version of your app.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the great things you're gonna build with this. If you are struck somewhere feel free to comment. I am always available.&lt;/p&gt;

&lt;p&gt;Please find the complete code at &lt;a href="https://github.com/rahul-yr/learn-go-simple-todo-crud"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>deploy</category>
      <category>git</category>
      <category>heroku</category>
    </item>
    <item>
      <title>Simple Todo app in Go for Beginners (Demo)</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Sun, 12 Jun 2022 10:05:59 +0000</pubDate>
      <link>https://dev.to/rahulyr/simple-todo-app-in-go-for-beginners-demo-179g</link>
      <guid>https://dev.to/rahulyr/simple-todo-app-in-go-for-beginners-demo-179g</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is a simple Todo application developed in Go.&lt;/p&gt;

&lt;p&gt;Click here to access &lt;a href="https://github.com/rahul-yr/learn-go-simple-todo-crud"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click here to access &lt;a href="https://postman.com/rahul-public/workspace/go-simple-todo-crud"&gt;Postman collection&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The scope of this article is just to illustrate the features/specs of this Repo. If you are looking for a tutorial on how to build, please skip this article. As well as this repo is developed for &lt;strong&gt;absolute beginners&lt;/strong&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This repo is neither a Production nor a stable version. As well as don't expect any database connections or any third party integrations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This repo has 5 endpoints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;POST&lt;/strong&gt; &lt;code&gt;https://learn-go-simple-todo-crud.herokuapp.com/todo/create&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for creating todo's (E.g : {"title" : "Some task 1" , "completed" : false }&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GET&lt;/strong&gt; &lt;code&gt;https://learn-go-simple-todo-crud.herokuapp.com/todo/list&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for listing all the todo's&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GET&lt;/strong&gt; &lt;code&gt;https://learn-go-simple-todo-crud.herokuapp.com/todo/read/{id}&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for reading a Todo by id&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;DELETE&lt;/strong&gt; &lt;code&gt;https://learn-go-simple-todo-crud.herokuapp.com/todo/delete/{id}&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for deleting a Todo by id &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;POST&lt;/strong&gt; &lt;code&gt;https://learn-go-simple-todo-crud.herokuapp.com/todo/update/{id}&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for updating a Todo by id(E.g : {"title" : "Updated Task details 1", "completed": true}&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deploy to Cloud
&lt;/h2&gt;

&lt;p&gt;Know how to deploy this repo to Heroku ? &lt;a href="https://dev.to/rahulyr/how-to-deploy-any-backend-to-heroku-easily--22mf"&gt;Click here to find out right way&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;If you have any questions or looking for a tutorial then please use the comment section. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to implement Singleton design pattern in Go ? 🔥</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Sat, 11 Jun 2022 13:31:24 +0000</pubDate>
      <link>https://dev.to/rahulyr/how-to-implement-singleton-design-pattern-in-go--5e1d</link>
      <guid>https://dev.to/rahulyr/how-to-implement-singleton-design-pattern-in-go--5e1d</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Before diving deep into the actual topic, it’s really important to make yourself familiar with the terminologies below. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are Design patterns ?
&lt;/h2&gt;

&lt;p&gt;In software engineering design patterns are solutions to general problems that occurred during the development of a software application faced by the majority of developers. The solutions were standardized over a period of trial and errors.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Race condition ?
&lt;/h2&gt;

&lt;p&gt;A race condition is a scenario of a program where its behavior depends on relative timing or interleaving of multiple threads or processes. One or more possible outcomes may be undesirable, resulting in a bug. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Singleton Design Pattern?
&lt;/h2&gt;

&lt;p&gt;Singleton pattern restricts instantiation of a class or struct to only one single instance at any point of time. Usually its scope is defined as global to allow access of this instance across your application. This design pattern comes really handy in use cases such as &lt;strong&gt;logging&lt;/strong&gt;, &lt;strong&gt;caching&lt;/strong&gt;, &lt;strong&gt;Thread pool&lt;/strong&gt;, &lt;strong&gt;DB connections&lt;/strong&gt;,etc.&lt;/p&gt;

&lt;p&gt;Singleton pattern could be implemented in one of 2 ways  i.e &lt;strong&gt;Thread Safe&lt;/strong&gt; and &lt;strong&gt;Non Thread Safe&lt;/strong&gt;.  By the way it's suggested to use thread safe.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Thread safe?
&lt;/h3&gt;

&lt;p&gt;Thread safety is a mechanism to protect your application from entering into Race condition if it is having a shared state being manipulated by multiple threads at the same time.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Non Thread safe?
&lt;/h3&gt;

&lt;p&gt;It does not check the safety of the threads which makes it faster to run but at the same time, it becomes more unstable for multithreaded applications. It works best for either single thread or sequential applications.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First let’s see the Non Thread Safety in action&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Non Thread safe implementation in Go
&lt;/h1&gt;

&lt;p&gt;As you can see in the code snippet below, a struct named &lt;code&gt;SQLConnection&lt;/code&gt; and 3 functions named &lt;code&gt;mockConnectionNonThreadSafe&lt;/code&gt;, &lt;code&gt;performConcurrentAction&lt;/code&gt;, &lt;code&gt;performSequentialAction&lt;/code&gt; are being implemented.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the &lt;code&gt;mockConnectionNonThreadSafe&lt;/code&gt; function, a &lt;code&gt;sleep&lt;/code&gt; command is added to just mimic the real world scenario( creation of connection object always takes some arbitrary time). &lt;/p&gt;

&lt;p&gt;To see &lt;strong&gt;non thread safe sequential execution&lt;/strong&gt; in action, uncomment the &lt;code&gt;performSequentialAction&lt;/code&gt; block in main function and run the snippet using 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;go run singleton_pattern/non_thread_safe.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zQxIgB_P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwk9vwiue7ip1c5669ao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQxIgB_P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwk9vwiue7ip1c5669ao.png" alt="non-thread-safe-sequential.png" width="880" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you get the same output as above, then you have successfully implemented the &lt;code&gt;non thread safe sequential&lt;/code&gt; program. The output clearly shows that sequential execution works absolutely fine, in fact it’s just created a single instance which is kind of a singleton pattern, but performance is a trade off here because this is a step by step execution which means this program can’t take advantage of multiple cores and threads of a modern day cpu.&lt;/p&gt;

&lt;p&gt;To test &lt;code&gt;non thread safe concurrent&lt;/code&gt; execution, uncomment the &lt;code&gt;performConcurrentAction&lt;/code&gt; block in main function and run the snippet.&lt;br&gt;
The output will look something similar to this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gNm_QEZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32u0s939v5bgq2y8jbj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gNm_QEZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32u0s939v5bgq2y8jbj9.png" alt="non-thread-safe-concurrent.png" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you watch the output closely, you will find that the database instance is continuously modified by every concurrent execution due to some delay in creation of a connection instance ( which is expected in the real-world may be due to network or overload or something else). This is happening because the provided solution is not thread safe, that’s why the connection object is modified by other threads kind of a race condition. In this case we have achieved to successfully utilize the full cpu performance i.e cores and threads, but it’s still far away from the desired output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now it’s finally time to resolve this once for all.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  Thread Safe implementation in Go
&lt;/h1&gt;

&lt;p&gt;Again mostly similar code 1 struct and 3 functions(1 being &lt;code&gt;mockConnectionThreadSafe&lt;/code&gt;) has been implemented.&lt;br&gt;
As well as a new variable &lt;code&gt;once&lt;/code&gt; of type &lt;code&gt;sync.Once&lt;/code&gt;(&lt;code&gt;Once&lt;/code&gt; is a Golang thread safe built-in struct that is used to implement a singleton pattern) has been added. This variable has a method &lt;code&gt;Once&lt;/code&gt; which will handle thread safe execution.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Execute this snippet by uncommenting &lt;code&gt;performConcurrentAction&lt;/code&gt; block in main function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run singleton_pattern/thread_safe.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Py3eiXDT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqni0qevn9qy339h8cf2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Py3eiXDT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqni0qevn9qy339h8cf2.png" alt="thread-safe-concurrent.png" width="880" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have done everything correctly till now, then you will see the database instance being created only once( unlike connection objects being modified by other concurrent executions in case of non thread safe) even though we are using concurrent executions.&lt;br&gt;
The race condition has been handled and it’s working as expected by utilizing the full power of modern day CPU's. Congrats⭐ on top of that it’s absolutely thread safe.&lt;/p&gt;

&lt;p&gt;Now you can access the same instance variable wherever you want without any conflicts.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the great things you're gonna build with this. &lt;br&gt;
If you are struck somewhere feel free to comment. I am always available.&lt;/p&gt;

&lt;p&gt;Please find the complete code at &lt;a href="https://github.com/rahul-yr/learn-go-concepts/tree/main/singleton_pattern"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>singleton</category>
      <category>database</category>
    </item>
    <item>
      <title>How to implement state in React using Redux Toolkit (easy way🔥)</title>
      <dc:creator>Rahul Yarragodula</dc:creator>
      <pubDate>Thu, 09 Jun 2022 03:34:37 +0000</pubDate>
      <link>https://dev.to/rahulyr/implement-state-in-react-using-redux-toolkit-easy-way-2eff</link>
      <guid>https://dev.to/rahulyr/implement-state-in-react-using-redux-toolkit-easy-way-2eff</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This tutorial will introduce you to Redux toolkit and teach you how to use it right way following best practices. By the time you finish this tutorial, you should be able to build your own Redux Applications. &lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt; is a library for managing and updating application state using events called &lt;code&gt;actions&lt;/code&gt;. It serves as a centralized store for state, that needs to be used across your entire application(instead of props drilling), with rules ensuring that the state can only be updated in a predictable fashion.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Redux Toolkit ?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://redux-toolkit.js.org/"&gt;Redux Toolkit&lt;/a&gt; is an all in one state management library for React.js. It helps you to create and manage global state easily with a lot less boiler plate code. Redux toolkit is basically an extension on Redux and other dependent packages, functions for state management. It simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  How State management works in Redux ?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D_Y8B9SU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bahw6i1cct8lvv3t2pp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D_Y8B9SU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bahw6i1cct8lvv3t2pp.png" alt="Redux flow" width="561" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assume that you have created an action(&lt;em&gt;action is a function that triggers the respective reducer&lt;/em&gt;) named &lt;code&gt;incrementValue&lt;/code&gt; and reducer(&lt;em&gt;reducer is a function or piece of code, that actually updates the global state known as store based on action event&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Let's say you have a component with button for incrementing the counter value based on &lt;code&gt;onclick&lt;/code&gt; event. When you trigger the &lt;code&gt;onclick&lt;/code&gt; event it will &lt;code&gt;dispath&lt;/code&gt; an action named &lt;code&gt;incrementValue&lt;/code&gt;. That action resolves against the respective reducer and updates the global state in store. The registered subscribers of the store will get notified with latest state details. Please find the reference image attached above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to implement &lt;code&gt;Redux Toolkit to your app&lt;/code&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a new react app and install all the required dependencies&lt;/li&gt;
&lt;li&gt;Clean the boilerplate code&lt;/li&gt;
&lt;li&gt;Create a counter app using &lt;code&gt;useState&lt;/code&gt; (Optional)&lt;/li&gt;
&lt;li&gt;Implement a store, slice and actions&lt;/li&gt;
&lt;li&gt;Subscribe and Dispatch events&lt;/li&gt;
&lt;li&gt;Final thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's see this in action by building a simple &lt;code&gt;counter application&lt;/code&gt; using redux toolkit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. Create a new react app and install all the required dependencies
&lt;/h3&gt;

&lt;p&gt;Before creating a react app make sure you installed &lt;a href="https://nodejs.org/en/"&gt;Node.js&lt;/a&gt;. Once you installed the dependencies use below snippet to create a new react app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app counter-app
cd counter-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the below command to install dependent packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @reduxjs/toolkit react-redux react-bootstrap bootstrap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add below snippet to public/index.html file. This is for bootstrap cdn.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"&amp;gt;
&amp;lt;script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run the app using &lt;code&gt;npm start&lt;/code&gt; command. Open this &lt;a href="http://localhost:3000/"&gt;url&lt;/a&gt; If you see something like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ODjku3rS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8v1pcggwwievyryec2t7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ODjku3rS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8v1pcggwwievyryec2t7.PNG" alt="initial" width="880" height="468"&gt;&lt;/a&gt;&lt;br&gt;
Then you have successfully created your react app. &lt;/p&gt;
&lt;h3&gt;
  
  
  2. Clean the boilerplate code
&lt;/h3&gt;

&lt;p&gt;Remove everything from App.js and add below snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css';

function App() {
  return (
    &amp;lt;div className="App container-md pt-5"&amp;gt;
      &amp;lt;span&amp;gt; Will add details here... 🔥  &amp;lt;/span&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how it looks &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oxqrzFwa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/151z618510kd5gi7m3bb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oxqrzFwa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/151z618510kd5gi7m3bb.PNG" alt="cleanup template" width="303" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Create a counter app using &lt;code&gt;useState&lt;/code&gt; (Optional)
&lt;/h3&gt;

&lt;p&gt;Create a state variable &lt;code&gt;counter&lt;/code&gt; for storing the current value. As well as create functions to increment and decrement counter value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css';
import React, {useState} from 'react';
import { Button } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';

function App() {
  // create a state variable called counter and set it to 0
  const [counter , setCounter] = useState(0);

  // create a function called increment that adds 1 to the counter
  const incrementCounter = () =&amp;gt; {
    setCounter(counter + 1);
  }

  // create a function called decrement that subtracts 1 from the counter
  const decrementCounter = () =&amp;gt; {
    setCounter(counter - 1);
  }

  return (
    &amp;lt;div className="App container-md pt-5"&amp;gt;
      &amp;lt;div className = "pb-3"&amp;gt;
      &amp;lt;h2&amp;gt; Simple Counter Application... 🔥 &amp;lt;/h2&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className = "row"&amp;gt;
        &amp;lt;div className = "col-md-5"&amp;gt;
          &amp;lt;Button variant="primary" onClick = {incrementCounter}&amp;gt;
            Increment
          &amp;lt;/Button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className = "col-md-2"&amp;gt;
          &amp;lt;h4&amp;gt;
            {counter}
          &amp;lt;/h4&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className = "col-md-5"&amp;gt;
          &amp;lt;Button variant="primary" onClick = {decrementCounter}&amp;gt;
            Decrement
          &amp;lt;/Button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the command &lt;code&gt;npm start&lt;/code&gt; and you will see this below UI.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yUBzI5fN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2t0db1th99w604nnqrx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yUBzI5fN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2t0db1th99w604nnqrx.PNG" alt="use state" width="880" height="176"&gt;&lt;/a&gt;&lt;br&gt;
Great 🔥, you are ready to dive deep into the actual topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Implement a store, slice and actions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a store(&lt;code&gt;src/redux/store.jsx&lt;/code&gt;) which is responsible for subscribing and maintaining the global state.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit";
export const store = configureStore({
    reducer : {}
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Wrap the main component with Provider in index.js as shown below. This will provide access to global state &lt;code&gt;store&lt;/code&gt; across your application.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Provider } from "react-redux";
import { store } from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  &amp;lt;Provider store={store}&amp;gt;
    &amp;lt;React.StrictMode&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/React.StrictMode&amp;gt;
  &amp;lt;/Provider&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a slicer(&lt;code&gt;src/redux/counterSlicer.jsx&lt;/code&gt;) where you define actions and reducers.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from "@reduxjs/toolkit";

// Create the initial state of the counter
const initialState = {
    counter : 0
}

// Create the slice of the state
const counterSlice = createSlice({
    // The name of the slice
    name : 'counter',
    // The initial state of the slice
    initialState,
    // The reducers of the slice
    reducers : {
        // Increment the counter by 1 when the increment action is dispatched
        incrementAction : (state) =&amp;gt; {
            // Increment the counter by 1
            state.counter = state.counter + 1;
        },
        // Decrement the counter by 1 when the decrement action is dispatched
        decrementAction : (state) =&amp;gt; {
            // Decrement the counter by 1
            state.counter = state.counter - 1;
        }
    }
});

// Export the actions of the slice
export const {incrementAction , decrementAction } = counterSlice.actions;
// Export the reducer of the slicel
export default counterSlice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Update the store.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";

export const store = configureStore({
    reducer : {
        counter : counterReducer
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Subscribe and Dispatch events
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Subscribe to state &lt;code&gt;counter(same name as defined in store.jsx)&lt;/code&gt; in App.js.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;add import statement in App.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useSelector } from "react-redux";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;add subscriber to global state in App.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const counterStore = useSelector(state =&amp;gt; state.counter);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;listen to state changes in App.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;div className = "col-md-2"&amp;gt;
       &amp;lt;h4&amp;gt;
          {counterStore.counter}
        &amp;lt;/h4&amp;gt;
 &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run the application now, you will see exactly like below. But the increment and decrement won't work. Don't worry it will start working soon 😃&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1N3xa_WA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00u9tif374o7vuns45dl.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1N3xa_WA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00u9tif374o7vuns45dl.PNG" alt="selector" width="880" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now it's time to call actions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;add import statements
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useSelector, useDispatch } from "react-redux";
import { decrementAction, incrementAction } from "./redux/CounterSlice";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;create a dispath variable for calling actions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const dispatch = useDispatch();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;call the dispath event in increment and decrement onClick
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;Button variant="primary" onClick = {() =&amp;gt; dispatch(incrementAction()) }&amp;gt;
        Increment
  &amp;lt;/Button&amp;gt;
.....
&amp;lt;Button variant="primary" onClick = {() =&amp;gt; dispatch(decrementAction())}&amp;gt;
        Decrement
 &amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Final App.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css';
import React, {useState} from 'react';
import { Button } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import { useSelector, useDispatch } from "react-redux";
import { decrementAction, incrementAction } from "./redux/CounterSlice";

function App() {
  // create a state variable called counter and set it to 0
  const [counter , setCounter] = useState(0);
  const counterStore = useSelector(state =&amp;gt; state.counter);
  // create a dispatch variable
  const dispatch = useDispatch();

  // create a function called increment that adds 1 to the counter
  const incrementCounter = () =&amp;gt; {
    setCounter(counter + 1);
  }

  // create a function called decrement that subtracts 1 from the counter
  const decrementCounter = () =&amp;gt; {
    setCounter(counter - 1);
  }

  return (
    &amp;lt;div className="App container-md pt-5"&amp;gt;
      &amp;lt;div className = "pb-3"&amp;gt;
      &amp;lt;h2&amp;gt; Simple Counter Application... 🔥 &amp;lt;/h2&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className = "row"&amp;gt;
        &amp;lt;div className = "col-md-5"&amp;gt;
          &amp;lt;Button variant="primary" onClick = {() =&amp;gt; dispatch(incrementAction()) }&amp;gt;
            Increment
          &amp;lt;/Button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className = "col-md-2"&amp;gt;
          &amp;lt;h4&amp;gt;
            {counterStore.counter}
          &amp;lt;/h4&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className = "col-md-5"&amp;gt;
          &amp;lt;Button variant="primary" onClick = {() =&amp;gt; dispatch(decrementAction())}&amp;gt;
            Decrement
          &amp;lt;/Button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you should see increment and decrement in action 🔥.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W_h2ynIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwddk9mdn9152r7vnfaw.PNG" alt="dispatch" width="880" height="279"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Final thoughts
&lt;/h3&gt;

&lt;p&gt;Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the Great things your are building with React.&lt;/p&gt;

&lt;p&gt;If you are struck somewhere feel free to comment. I am always available.&lt;/p&gt;

&lt;p&gt;Please find the complete code at &lt;a href="https://github.com/rahul-yr/sh-redux-toolkit-counter"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>reduxtoolkit</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
