<?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: Arpit Dubey</title>
    <description>The latest articles on DEV Community by Arpit Dubey (@thebedroomprogrammer).</description>
    <link>https://dev.to/thebedroomprogrammer</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%2F78196%2Fd472c4c5-930f-4ecf-b7bb-15fd941259a6.jpeg</url>
      <title>DEV Community: Arpit Dubey</title>
      <link>https://dev.to/thebedroomprogrammer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thebedroomprogrammer"/>
    <language>en</language>
    <item>
      <title>My 2 cents on Deno 🦕</title>
      <dc:creator>Arpit Dubey</dc:creator>
      <pubDate>Fri, 15 May 2020 22:46:22 +0000</pubDate>
      <link>https://dev.to/thebedroomprogrammer/my-2-cents-on-deno-3meb</link>
      <guid>https://dev.to/thebedroomprogrammer/my-2-cents-on-deno-3meb</guid>
      <description>&lt;p&gt;With the emergence of new tech, framework, programming language, and patterns every day the web industry is more confusing than ever, making it very hard for a newbie get their feet wet and get the taste of how it feels to get going or start development in the web dev industry. Every time a new tech is introduced everyone goes on to throwing benchmark results on how the new stuff is better than the old one and how it can replace a huge chunk and make the development better.&lt;/p&gt;

&lt;p&gt;I as a developer often get overwhelmed and fatigued by this continuous punch of information and knowledge thrown at my face. We, instead of diving deep in the use cases of a particular tech start comparing it with all the other tech that is around it. Deno is one of the same "runtime" that falls into the category.&lt;/p&gt;

&lt;p&gt;"What is Deno?", you may ask. So "Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust". It has been under development for almost 2 years and I have been following for less than 6 months and this is what I have found out.&lt;/p&gt;

&lt;p&gt;Deno draws a lot of its resemblance from "Node". Just try to rearrange the letters in "N O D E" and you will get a gist of what I am talking about. Deno is the brainchild of Ryan Dahl the creator of NodeJS who left the NodeJS Foundation in January 2012 after creating and contributing to the open-source Javascript runtime for almost 4 years or more. He made JS run on a server possible and he is the sole reason why starting up as a web developer in today's times is a lot easier because a lot of people pick up NodeJS first to learn web development as it is a lot easier to get up and running with it. Node did good and is doing extremely well in the current times but there were a lot of problems with it. Since the development around Node was so fast that with the passing years a lot of code was added to it and some very bad decisions were made around it. As people were adopting the runtime faster than ever before, it became a lot harder for the devs to manage it or fix a bug as devs were relying on them, so they had to patch it instead of rewriting the whole feature which resulted in Node become a Big Ball of Mud.&lt;/p&gt;

&lt;p&gt;The devs were under great technical debt and it was impossible for them to undo the changes and the decisions that were made in haste, so everyone decided to keep going forward. Ryan Dahl, on the other hand after leaving the foundation and working in a Google Scholar program wanted to change that. So he laid down the idea of Deno that targets and tries to remove all the shortcomings of NodeJS that it faces as a Javascript runtime some of which are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No native support for promises.&lt;/li&gt;
&lt;li&gt;Lack of security.&lt;/li&gt;
&lt;li&gt;A poorly designed module system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A lot of learning has been taken from NodeJS since Node is out in the wild for almost 11 years. Some of the great features of Deno that tackles the problem that Node has are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has no default access to the file system, your environment variables, and your network so better security. Everything is turned off by default and is not granted access until you specifically provide the access.&lt;/li&gt;
&lt;li&gt;Inspired by GoLang(Another great systems language turned server-side tech) it uses a URL path to resolve the modules. So instead of a module being hosted on a registry on a single platform, the dev can host the dependency anywhere they like and can import them using the URL just like we do in browsers. There is no central repository for it and since Deno always caches the dependency it provides an extra layer of robustness in case the platform hosting the dependency is down.
Deno uses typescript as the default language. &lt;/li&gt;
&lt;li&gt;Typescript has been a boon for JS devs out there. It allows a dev to write bug-free type-safe code by restricting the developer to follow certain rules and guidelines so they can be sure that the code they ship is going to work all time.&lt;/li&gt;
&lt;li&gt;The underlying implementation is written in Rust(the equivalent of C++ for Node) and the event loop is Tokio(Rust). Since the development in the Rust community is far active than ever before and as the language claims that it helps developers write memory-safe code without letting them worry much about the memory manager management and thread-safety issues like that in C++, it holds a lot of promises for Deno.
Deno's first version was released on 13th May 2020 and the repo has already gained 55k+ stars on Github and a lot of love and support from the community. But Deno is not here to dethrone NodeJS. Node has already been adopted by a large number of organizations and has set its root deep in the web dev industry it would be very very hard for Deno to dethrone Node. If used with caution Node is a powerful runtime that enables a developer to do any task that its counterparts do and it is performant too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The open-source community has opened a great realm of competition that has made the coexistence of a great variety of tech solving a variety of problem possible in different ways. It is up to you to decide what path you want to take. Since every community is trying to get better than the other it keeps them on toes to make the product even better which always enhances the product. Hence it is a win-win situation for us developers.&lt;/p&gt;

&lt;p&gt;All you need to do is instead of going blindly behind a tech decide that does it solve the problem that you want to solve in an efficient way and will it be feasible to put out the cost of learning and implementing the tech as a use case in your scenario possible.&lt;/p&gt;

&lt;p&gt;In conclusion, I would like to say that do not get overwhelmed by the knowledge coming at you. Change is constant and all you need to do is adapt according to the situation.&lt;/p&gt;

&lt;p&gt;Yearn, Learn, and Earn.&lt;/p&gt;

&lt;p&gt;I would highly encourage you to take a look at the references that I have referred to make this article possible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://deno.land/"&gt;Denos official website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.infoworld.com/article/3529779/what-is-deno-a-better-nodejs.html"&gt;Interview of Ryan Dahl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/denoland/deno"&gt;Denos official repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.logrocket.com/what-is-deno/"&gt;A great article on Deno on LogRocket&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  If you’re excited by this and want to work on cutting edge technology that touches millions of users daily, drop us a hello at &lt;a href="//mailto:careers@gamezop.com"&gt;careers@gamezop.com&lt;/a&gt;. We are hiring for both backend (Golang / Elixir / Node) and frontend (React / Node) roles.
&lt;/h3&gt;

</description>
      <category>deno</category>
      <category>node</category>
      <category>backend</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Part 2. Managing multiple stores using GenServer processes — Elixir/OTP</title>
      <dc:creator>Arpit Dubey</dc:creator>
      <pubDate>Tue, 25 Feb 2020 12:15:33 +0000</pubDate>
      <link>https://dev.to/thebedroomprogrammer/part-2-managing-multiple-stores-using-genserver-processes-elixir-otp-5f63</link>
      <guid>https://dev.to/thebedroomprogrammer/part-2-managing-multiple-stores-using-genserver-processes-elixir-otp-5f63</guid>
      <description>&lt;h1&gt;
  
  
  Part 2. Managing multiple stores using GenServer processes — Elixir/OTP
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B96bz7vs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/4896/1%2ArN-RzouMEhwJWIUex2JZ6w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B96bz7vs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/4896/1%2ArN-RzouMEhwJWIUex2JZ6w.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey, have you stumbled upon this post directly without reading the previous article because you searched for something and Google decided to teleport you here? Well, then I would suggest you read the previous article which is the first part of the 7 part series that I am writing.&lt;/p&gt;

&lt;p&gt;Now, if you have read the previous article then let’s begin directly with this article.&lt;/p&gt;

&lt;p&gt;So previously we created a server process that stores some values in a key-value store. Now what we will aim for is to create a server process with a Manager process which will help us manage multiple key-value stores.&lt;/p&gt;

&lt;p&gt;With the introduction of a manager process, we will be able to create multiple stores and store key-values in them separately because guess what, we are not learning Elixir to just to deal with a single process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s get started 🏎️
&lt;/h2&gt;

&lt;p&gt;Before beginning make sure you have Elixir and Mix installed in your system.&lt;/p&gt;

&lt;p&gt;Now create a mix project by typing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mix new ex2_key_val_with_multiple_stores
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command should create your project structure like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xpd3luqS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/514/1%2Aoanz6RnWOOkLXB2aur-DKg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xpd3luqS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/514/1%2Aoanz6RnWOOkLXB2aur-DKg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Delete all the files in the lib folder and create a file with name &lt;strong&gt;manager.ex&lt;/strong&gt; and &lt;strong&gt;store.ex&lt;/strong&gt;. Also copy the previously created &lt;strong&gt;server.ex&lt;/strong&gt; in the same folder.&lt;/p&gt;

&lt;p&gt;The reason why we are creating these many modules is that it is always good to decouple your code in the form of modules and functions wherever possible. It makes testing a lot easier and makes parts of your app to plug and play anywhere you want.&lt;/p&gt;

&lt;p&gt;With the introduction of a manager process to manager multiple instances of stores we are looking to achieve an architecture like in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_hC8crWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/711/1%2AXAwz0s7AMcKRO8TrzQntXg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_hC8crWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/711/1%2AXAwz0s7AMcKRO8TrzQntXg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;init/1&lt;/strong&gt; accepts one argument. This is the second argument provided to &lt;strong&gt;GenServer.start/2&lt;/strong&gt;, and you can use it to pass data to the server process while starting it.&lt;/li&gt;
&lt;li&gt;The result of &lt;strong&gt;init/1&lt;/strong&gt; must be in the format &lt;strong&gt;{:ok, initial_state}&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;handle_cast/2&lt;/strong&gt; accepts the request and the state and should return the result in the format &lt;strong&gt;{:noreply, new_state}&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;handle_call/3&lt;/strong&gt; takes the request, the caller information, and the state. It should return the result in the format &lt;strong&gt;{:reply, response, new_state}&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show me some code 👨‍💻
&lt;/h2&gt;

&lt;p&gt;Here we now have 3 entities to care about.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Manager&lt;/li&gt;
&lt;li&gt;Server&lt;/li&gt;
&lt;li&gt;Store&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OU4uYULj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1290/1%2A_vwuexUHUTT8798j1BHxIA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OU4uYULj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1290/1%2A_vwuexUHUTT8798j1BHxIA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Manager process is a GenServer whose task would be to create a store as a server process and maintain a map of stores created with their name and PID which the client would use to communicate.&lt;/p&gt;

&lt;p&gt;In the Manager file we startup a GenServer and this time we pass an extra parameter of name: &lt;strong&gt;MODULE&lt;/strong&gt; to it. This registers the GenServer’s name as it’s module name so we won’t be needing a PID to communicate to it. Since we are going to create only one Manager there would only be one Manager with that name.&lt;/p&gt;

&lt;p&gt;The internal state of our Manager will store all the store related information in the form of Map with keys being the name of the store and values being their respective PIDs.&lt;/p&gt;

&lt;p&gt;Whenever a command to create a store is issued to the Manager it first checks if the store with the same name is already present in its state or not. If yes then it simply returns the PID else it starts up a new server process and stores the name and PID of that server process in its an internal state and then returns the PID of the newly created server process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7_nM0jdm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1158/1%2A8TAaOBcbCoE6sXDAlaW7tA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7_nM0jdm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1158/1%2A8TAaOBcbCoE6sXDAlaW7tA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Server is the simple GenServer that handle the task to manipulate the store values when the client issues a specific operation to it to perform.&lt;br&gt;
The server code is the** &lt;strong&gt;same as before. It handles all the operations like **put&lt;/strong&gt;, &lt;strong&gt;get&lt;/strong&gt; and del but in a more abstracted way with the help of a Store module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n01DoZqW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/822/1%2AefePbTB4Nnq5ijAA7gb6uw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n01DoZqW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/822/1%2AefePbTB4Nnq5ijAA7gb6uw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To promote code reuse we took out the Store operations in a different module called KeyVal.Store. This makes the app simple.&lt;/p&gt;
&lt;h2&gt;
  
  
  The final run 📟
&lt;/h2&gt;

&lt;p&gt;In order to start the whole system, we first initiate an elixir session&lt;br&gt;
by typing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iex -S mix
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IjSAEcto--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/2048/1%2A57xmO6qYbAXAykmk1aeQIA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IjSAEcto--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/2048/1%2A57xmO6qYbAXAykmk1aeQIA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we start the Manager process with the startup command. After starting the manager process we create two stores with different names and store their PIDs in local variables.&lt;/p&gt;

&lt;p&gt;Then we communicate to the servers with their respective PIDs to store, retrieve or delete values in the store. Since both the server processes are different the values stored in them are completely different. You won’t be able to query for a value stored in Server 1 using the PID od Server 2 since they do not share the stores.&lt;/p&gt;

&lt;p&gt;So this is how you can Manager multiple stores using GenServers.&lt;/p&gt;

&lt;p&gt;I hope this post has helped you get a little bit better understanding of the whole process.&lt;/p&gt;

&lt;p&gt;The complete source code of all the parts are &lt;a href="https://github.com/thebedroomprogrammer/elixir-otp"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References 📝
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.manning.com/books/elixir-in-action-second-edition"&gt;Elixir in Action. 2nd Edition.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hexdocs.pm/elixir/GenServer.html****"&gt;GenServer behaviour docs.&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>elixir</category>
      <category>erlang</category>
      <category>functional</category>
      <category>elixirconf</category>
    </item>
    <item>
      <title>Part 1. GenServer — Elixir/OTP</title>
      <dc:creator>Arpit Dubey</dc:creator>
      <pubDate>Mon, 24 Feb 2020 19:20:35 +0000</pubDate>
      <link>https://dev.to/thebedroomprogrammer/part-1-genserver-elixir-otp-53h</link>
      <guid>https://dev.to/thebedroomprogrammer/part-1-genserver-elixir-otp-53h</guid>
      <description>&lt;h1&gt;
  
  
  Part 1. GenServer — Elixir/OTP
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F9jnzx4d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/7912/1%2AlOqD_NHM7gQY9Ax9TF4wZg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F9jnzx4d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/7912/1%2AlOqD_NHM7gQY9Ax9TF4wZg.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the beginning of 2020 Elixir has been gaining a lot of popularity in the field of functional programming language. Being a software engineer who always looks out to try something new I instantly bought the idea of learning the language when it was introduced at my workplace by a fellow game developer.&lt;/p&gt;

&lt;p&gt;As I started learning the language I found it a little difficult as I never wrote a single line in a functional way using Elixir. It promotes a coding style that helps developers write code that is short, concise, and maintainable which was pretty new to me.&lt;/p&gt;

&lt;p&gt;As I dived into the language further I was introduced to the concepts of scalability, concurrency and fault tolerance. Everything about the language seems perfect the tooling to write, test and maintain code is smooth. Also when companies like Discord and Square-Enix are using it to handle large user base in real-time it gives a lot of credibility to the language.&lt;/p&gt;

&lt;p&gt;The area where elixir shines is handling scalability and fault tolerance. As it can be pretty daunting to wrap your head around the concepts I decided to document every task that I did to get hold of the concepts in the easiest possible manner. This post and the posts to come deal with the concepts of writing concurrent and fault-tolerant code using Erlang’s OTP(Open Telecom Platform) behaviours, so if you are new to Elixir you might want to skip this post as it might be a little challenging to get directly into the intermediate parts of the language.&lt;/p&gt;

&lt;p&gt;I am not going to dive deep into what Elixir is and how it runs on a system as this post is more about using OTP behaviours in Elixir to achieve concurrency and fault tolerance.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to expect from this article 🤔
&lt;/h2&gt;

&lt;p&gt;This will be a 7 part article where we will be learning Elixir concepts by building and continuously improvising a key-value store application. The application would let you simply create, update and delete a value in a store.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Part 1. Creating a key-value store using GenServer.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 2. Managing multiple stores using GenServer processes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 3. Persisting data in key-value store using GenServer.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 4. Making operations on the key-value store faster by using worker processes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 5. Adding Supervisors to the key-value store.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 6. Managing multiple Supervisors and a supervision tree for the key-value store.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 7. Writing your first Webserver to expose the key-value store to the web.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let’s get started 🏎️
&lt;/h2&gt;

&lt;p&gt;Before beginning make sure you have Elixir and Mix installed in your system.&lt;/p&gt;

&lt;p&gt;Now create a mix project by typing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mix new ex1_a_gen_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command should create your project structure like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bak6N4kX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1028/1%2Aj6yiPZcI304VLTt73J9PsA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bak6N4kX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1028/1%2Aj6yiPZcI304VLTt73J9PsA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;delete all the files in the lib folder and create a file with name server.ex&lt;/p&gt;

&lt;p&gt;This is the file where we are going to write all of our code.&lt;br&gt;
Before starting to write the code let’s start with what are we going to build and how are we going to implement it using a GenServer.&lt;/p&gt;

&lt;p&gt;The key-value store that we are going to build would be a simple process that should do the following three tasks.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Put a value against a key in the store.&lt;/li&gt;
&lt;li&gt;Retrieve a value against a key in the store.&lt;/li&gt;
&lt;li&gt;Delete a value against a key in the store.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let’s discuss what is a &lt;strong&gt;Genserver&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Genserver&lt;/strong&gt; is behaviour module for implementing the server of a client-server relation.&lt;/p&gt;

&lt;p&gt;It is a process like any other Elixir process and it can be used to keep state, execute code asynchronously and so on. The advantage of using a generic server process (GenServer) implemented using this module is that it will have a standard set of interface functions and include functionality for tracing and error reporting. It will also fit into a supervision tree(We will discuss this later in the coming posts).&lt;/p&gt;

&lt;p&gt;So we are going to maintain our state i.e. the key-value store in the form of a map in a GenServer process and we are going to talk to the process in an async way by message passing.&lt;/p&gt;

&lt;p&gt;In order to make we create out key-value store app we need to implement these three callbacks in our server. &lt;strong&gt;init/1, handle_cast/2, and handle_call/3&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qhcK_lTd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1522/1%2AiTkevSPaHHmxONkG7Y7EAA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qhcK_lTd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1522/1%2AiTkevSPaHHmxONkG7Y7EAA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;init/1&lt;/strong&gt; accepts one argument. This is the second argument provided to &lt;strong&gt;GenServer.start/2&lt;/strong&gt;, and you can use it to pass data to the server process while starting it.&lt;/li&gt;
&lt;li&gt;The result of &lt;strong&gt;init/1&lt;/strong&gt; must be in the format &lt;strong&gt;{:ok, initial_state}&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;handle_cast/2&lt;/strong&gt; accepts the request and the state and should return the result in the format &lt;strong&gt;{:noreply, new_state}&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;handle_call/3&lt;/strong&gt; takes the request, the caller information, and the state. It should return the result in the format &lt;strong&gt;{:reply, response, new_state}&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show me some code 👨‍💻
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bpYypwEk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/2180/1%2A6Zu6ahGRXn9zyV_xwPFjYQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bpYypwEk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/2180/1%2A6Zu6ahGRXn9zyV_xwPFjYQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so here we start our GenServer by calling &lt;strong&gt;GenServer.start/2&lt;/strong&gt;. The first argument is the module itself while the second argument can be any arbitrary argument that you want to pass to the server while it’s initialization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;GenServer.start/2&lt;/strong&gt; will call the &lt;strong&gt;init/1&lt;/strong&gt; function which would accept the argument passed to it while starting the server. It eventually returns &lt;strong&gt;{:ok, %{}}&lt;/strong&gt; which is our empty initial state.&lt;/li&gt;
&lt;li&gt;The put, get and del functions are implemented to accept a &lt;strong&gt;PID&lt;/strong&gt; and some relevant information and pass the information to the GenServer process running on the PID.&lt;/li&gt;
&lt;li&gt;After receiving the info depending on whether is a cast or a call request the server call the appropriate handler.&lt;/li&gt;
&lt;li&gt;Since the state is not mutable every time when the server state is changed a new state is returned from the handle call or cast functions to the update the server state.&lt;/li&gt;
&lt;li&gt;As the function names suggest &lt;strong&gt;Map.fetch, Map.put&lt;/strong&gt; and &lt;strong&gt;Map.delete&lt;/strong&gt; do what their function name describes.&lt;/li&gt;
&lt;li&gt;The reason why we are using &lt;strong&gt;handle_call&lt;/strong&gt; for the get request is because it is the request where we care about the result returned to us. So we wait for the process to give us back the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The final run 📟
&lt;/h2&gt;

&lt;p&gt;On starting the server we store the PID in a variable and then all the communication is happening using the PID of the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pr8E0euH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/4096/1%2Ab3OB-Gu57kfrsAgjO8T97Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pr8E0euH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/4096/1%2Ab3OB-Gu57kfrsAgjO8T97Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use the exposed put, get and del functions on the Server module to perform operations on the key-value store all you need to remember is the PID of the store process that you started.&lt;/p&gt;

&lt;p&gt;This is the basic implementation of a GenServer in Elixir. I hope this post has helped you get a little bit better understanding of the whole process.&lt;/p&gt;

&lt;p&gt;The complete source code of all the parts are &lt;a href="https://github.com/thebedroomprogrammer/elixir-otp"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References 📝
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.manning.com/books/elixir-in-action-second-edition"&gt;Elixir in Action. 2nd Edition.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hexdocs.pm/elixir/GenServer.html****"&gt;GenServer behaviour docs.&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>elixir</category>
      <category>erlang</category>
      <category>genserver</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
