<?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: Sameh Muhammed</title>
    <description>The latest articles on DEV Community by Sameh Muhammed (@smuhammed).</description>
    <link>https://dev.to/smuhammed</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%2F939341%2F9e1f9e57-f5e4-401d-a823-b92dd64aaa10.jpeg</url>
      <title>DEV Community: Sameh Muhammed</title>
      <link>https://dev.to/smuhammed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smuhammed"/>
    <language>en</language>
    <item>
      <title>TDD V.S BDD</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Sun, 04 Dec 2022 18:39:05 +0000</pubDate>
      <link>https://dev.to/smuhammed/tdd-vs-bdd-3434</link>
      <guid>https://dev.to/smuhammed/tdd-vs-bdd-3434</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We talked &lt;a href="https://dev.to/smuhammed/unit-testing-is-not-just-a-best-practice-46ab"&gt;&lt;strong&gt;Here&lt;/strong&gt;&lt;/a&gt; about how important unit testing is in the software development process, and benefits you got from writing unit tests, but here we are comparing between two methodologies which are &lt;strong&gt;TDD&lt;/strong&gt; and &lt;strong&gt;BDD&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Driven Development (TDD)
&lt;/h2&gt;

&lt;p&gt;Test driven development is a methodology that aims on writing the test cases of any piece of code before you go ahead and implement it, after that you write the logic and cover the test cases you wrote before (a.k.a red/green methodology), and so on.. By this you are covering all your logic with safety net that will save your back in case any upcoming changes that may break your code, and the quality of your code here is measured by how much percentage you cover from code with unit tests and based on that your code can be pass or fail on automated pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Gap with TDD
&lt;/h2&gt;

&lt;p&gt;We all agree on that the TDD has a lot of advantages that make you follow this methodology while you are developing your product, but on the other hand no matter how much your test cases cover from your logic, there is no guarantee that combining these covered pieces of logic will deliver the desired functionality. Yes each piece of logic does its' job perfectly, but still have no guarantee, and in real software development you don't deliver code, you deliver functionalities/features that what real matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Behavioural Driven Development (BDD)
&lt;/h2&gt;

&lt;p&gt;Behavioural driven development is a new methodology that aims on test too, but not at the the level of methods or piece of code but at the level of functionality/feature, which you are writing what you expect from this feature to deliver to user and cover all scenarios that have this feature to do, then implementing the logic that perform this feature. By this you are protecting the most important part of the software. &lt;strong&gt;Its' features.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  BDD with Cucumber Framework
&lt;/h2&gt;

&lt;p&gt;Cucumber is a framework that enables the BDD methodology by giving the ability to write a software features in some script files called &lt;strong&gt;feature&lt;/strong&gt; file as below and locate these files with source code.&lt;/p&gt;

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

&lt;p&gt;What empowers these files that its' format follow almost the Agile user story format which makes reading and writing them easy even for non-technical persons like scrum masters and product managers and making them more engaged with  development process and code.&lt;/p&gt;

&lt;p&gt;Then the developer can take these feature files and start implementing the logic based on them as below example with Java.&lt;/p&gt;

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

&lt;p&gt;By making the predicates more general you can get benefit from reusing them and reduce the amount of code to cover the scenarios.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are multiple frameworks beside Cucumber to enable BDD methodology. more details in Resource section  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  At the End!
&lt;/h2&gt;

&lt;p&gt;Both two methodologies has its' own benefits, and to get the most safety for our product we should combine both of them, there is no benefit from having 100% unit test coverage, not all pieces of logic have to be covered by unit test, but we can use unit test to cover the most important functions that has a critical job to do and we can cover the rest of code by BDD.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://cucumber.io/docs/cucumber/" rel="noopener noreferrer"&gt;Cucumber framework&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=AHHLDaTVXqg" rel="noopener noreferrer"&gt;Cucumber with spring boot example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.perfecto.io/blog/bdd-testing-frameworks" rel="noopener noreferrer"&gt;BDD frameworks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>opensource</category>
    </item>
    <item>
      <title>A closer look to docker</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Thu, 17 Nov 2022 10:29:53 +0000</pubDate>
      <link>https://dev.to/smuhammed/a-closer-look-to-docker-e8m</link>
      <guid>https://dev.to/smuhammed/a-closer-look-to-docker-e8m</guid>
      <description>&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;In early years of software development process regarding to deploy software on multiple machines you have to repeat deployment steps for every machine you have, Then the need of isolating applications with it’s own dependencies and libraries to be compatible with versions and reduce the overhead of repeating this work on multiple machines/servers came up.&lt;/p&gt;

&lt;h2&gt;
  
  
  One step ahead, Virtual Machine
&lt;/h2&gt;

&lt;p&gt;VM is a guest Operating System can be run on top of another OS on same hardware and sharing physical components and resources of that machine by using a middle ware the helping in sharing resources operation.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Virtual Machine Pros &amp;amp; Cons
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;br&gt;
● Portable&lt;br&gt;
● Less hardware cost&lt;br&gt;
● Supports legacy operating systems&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;br&gt;
● Consuming resource&lt;br&gt;
● Less performance&lt;/p&gt;

&lt;h2&gt;
  
  
  Another step ahead, Containers (Docker)
&lt;/h2&gt;

&lt;p&gt;Docker is a software creates isolated containers that hold a software with it’s own libraries that can be run on any OS and can be transferred from machine to another through docker images.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Docker Components
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Docker client&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker client is responsible for taking the commands from user like (docker ps, docker images) and talk to docker server to execute the command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker server also known as Docker daemon responsible for running the commands that come from docker client like listing the images, run container and so on..&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Host&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker host is the host operating system for docker which docker uses to share it’s resources with containers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Image&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A read-only and sharable set of files that are compiles to create one or more container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Container&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Is a writable instance of image and each container has it’s own file system layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s a repository of docker images you can deal with it as git repository review your images add tags to your updates and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Registry&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A place responsible for providing docker images, that users can upload and download images like docker hub, thus you don't need to install any software from scratch and handle its' configuration, just pull the image and run the container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker components interaction
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Docker container holds a process
&lt;/h2&gt;

&lt;p&gt;Each docker container has a process that starts when you execute docker run command, When this process is finished the container stopped and as much as process is running container will be working as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Docker containers run among any OS?
&lt;/h2&gt;

&lt;p&gt;First of all, any docker image is already built on top of &lt;strong&gt;Linux&lt;/strong&gt; OS and that the common in docker world, so when the container runs on Linux system this OS will be docker host directly, Then the container can use up to 100% of machine resources.&lt;/p&gt;

&lt;p&gt;On the other hand when we use Docker on top of &lt;strong&gt;Mac or Windows&lt;/strong&gt;, there is software called &lt;strong&gt;Docker Desktop&lt;/strong&gt; you have to install, this application installs internally a small version of &lt;strong&gt;Linux&lt;/strong&gt; OS and this will be the Docker Host and also docker desktop allow you to &lt;strong&gt;manage&lt;/strong&gt; how much resources can docker take for containers.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Long short story: You always run containers based on Linux OS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: there is a windows based images also but it’s not common to use.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
    </item>
    <item>
      <title>About DB Transaction and Spring @Transactional</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Sat, 12 Nov 2022 14:05:59 +0000</pubDate>
      <link>https://dev.to/smuhammed/about-db-transaction-and-spring-transactional-2da0</link>
      <guid>https://dev.to/smuhammed/about-db-transaction-and-spring-transactional-2da0</guid>
      <description>&lt;h2&gt;
  
  
  What is database Transaction?
&lt;/h2&gt;

&lt;p&gt;Database transaction is a group of SQL statements that have to either success all or fail all, So that we can guarantee data consistency, As below image any break in the middle of these SQL statements will cause data inconsistency.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4xexpj3l12518ig98fr.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%2Fj4xexpj3l12518ig98fr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So you have to run these as a transaction, If any failure happen it will rollback all the changes and this provide Atomicity concept therefore transaction can not be divided (Any persistence tool have to provide Atomicity, Consistency, Isolation, Durability properties to it's system)&lt;/p&gt;

&lt;h2&gt;
  
  
  So, What about Spring @Transactional annotation?
&lt;/h2&gt;

&lt;p&gt;When you are using Spring Data JPA, you are using &lt;strong&gt;CrudRepository&lt;/strong&gt; implementation that Spring gives you out of the box which build on top Hibernate framework which uses &lt;strong&gt;EntityManager&lt;/strong&gt; class to deal with your database.&lt;br&gt;
As EntityManager class, Any communication with database have to be done throw transaction as below example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmfxq607q0rl8rmjn8dz.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%2Fjmfxq607q0rl8rmjn8dz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And for reducing any boilerplate code regarding to begin transaction and commit it or rollback, Spring added &lt;strong&gt;@Transactionl&lt;/strong&gt; annotation so that any method annotated with this will guarantee a lot of things mainly now beginning DB transaction and providing Atomicity to your logic.&lt;/p&gt;

&lt;p&gt;But you may wondering about using JPA repositories in methods without adding @Transactional and it works, Yes you are right because JPA methods (save() , findById(id) .. ) their implementation already annotated with @Transactional which taking care of creating DB transaction if not exists.&lt;/p&gt;

&lt;p&gt;Additionally, JPA methods like (save() , saveAll()) not perform any commit statements instead they just change the entity state in Persistence Context (EntityManager class), Then when transaction ends it performs flush operation that writes the records into DB then when closing the session commit statement is fired.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiue7piup1thm9zc80vj8.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%2Fiue7piup1thm9zc80vj8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a result, when you are using many JPA methods in one business logic method without @Transactional that means each JPA method will open it's own DB transaction and close it after finish, Therefore using JPA save() in loop may cause performance issue regarding to multiple open/close for transactions, In this case using saveAll() will be better.&lt;/p&gt;

&lt;h2&gt;
  
  
  @Transactionl 's propagation property
&lt;/h2&gt;

&lt;p&gt;@Trasnactional have propagation property with &lt;strong&gt;REQUIRED&lt;/strong&gt; value as default which means in case of no transaction found start one, if there is one already found use it, So when using @Transactionl on your methods or services any inner JPA methods will use same transaction to deal with DB instead of creating one for each method.&lt;/p&gt;

&lt;p&gt;There is more about propagation property and isolation as well, you can get more information resource section&lt;/p&gt;

&lt;h2&gt;
  
  
  @Transactional read-only property
&lt;/h2&gt;

&lt;p&gt;At end of each transaction hibernate framework needs to know if it should fire any insert/update statements or not, So it fires dirty checking operation so it can know if there any entity changed and fire statement for it, so in case you have a methods with just retrieve operations it's better to set read-only property to true so you can avoid dirty checking operation (by default it has false value)&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.baeldung.com/spring-transactional-propagation-isolation" rel="noopener noreferrer"&gt;Spring Transaction propagation isolation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Database Isolation levels</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Wed, 09 Nov 2022 14:15:40 +0000</pubDate>
      <link>https://dev.to/smuhammed/database-isolation-levels-5ha2</link>
      <guid>https://dev.to/smuhammed/database-isolation-levels-5ha2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When you have one single user that will use your application that the optimal case and Youtopia of our software industry, but when it comes to multiple users, that what will bring all evil to the application as you now have to manage concurrency and pay attention and resources to manage that, databases engineers suffered from this issue and came with multiple solutions. Let's discuss them mainly with &lt;strong&gt;Read&lt;/strong&gt; operation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/smuhammed/optimistic-vs-pessimistic-databases-n73"&gt;Here&lt;/a&gt; we discussed DB concurrency from &lt;strong&gt;Write&lt;/strong&gt; operation point of view&lt;/p&gt;

&lt;h2&gt;
  
  
  Our use case
&lt;/h2&gt;

&lt;p&gt;Let's say we have below use case, we have an E-commerce application with 2 actors user and admin.&lt;/p&gt;

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

&lt;p&gt;At the same moment each on of them fire a request to the application that will start a transaction on DB&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User want to submit new Order.&lt;/li&gt;
&lt;li&gt;Admin want to generate weekly report of financial and order counts state of application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The point is, the currently processing order should be displayed on the admin report or not (it's not finished yet we are still processing)?&lt;/p&gt;

&lt;p&gt;Some people say, yes we should include it as it coming order, and not included it me display a wrong data on report, but what if we include the order and while processing some failure happens on this transaction and DB decided to rollback and delete all the data regarding to this order, now you also have wrong report data.&lt;/p&gt;

&lt;p&gt;Because of this database engineers decided that, this decision depends on multiple factors, like business domain criticality, admin and business requirements and etc.., So they decided to defer this decision up to other engineers to decide which case is applicable for their situation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isolation levels
&lt;/h2&gt;

&lt;p&gt;As ACID principals of database engine the letter 'I' stands for Isolation, that each transaction should run in isolated environment, and database engine provides multiple isolation levels/types to describe how transactions should behave regarding to another transaction work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read Committed isolation level&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is default transaction isolation level for many databases, by using this the transaction is allowed only to read data that committed as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7VS83-H3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sexqoycemelonotx29h4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7VS83-H3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sexqoycemelonotx29h4.png" alt="Image description" width="751" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read Uncommitted / Dirty Read isolation level&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this isolation level the transaction is allowed to read any data written by any transaction at any time, but this may lead to wrong results as this can be rolled back at any time as below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cddlIIuI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kfl41qie4eyz7840o62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cddlIIuI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kfl41qie4eyz7840o62.png" alt="Image description" width="450" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snapshot isolation level&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This isolation level is capturing a database state when a new transaction begin even the data have been committed or not and when asking for values database provides data based on this snapshot even if it changed during time as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GX50Udu9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/joq4tysr46dkml79gkbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GX50Udu9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/joq4tysr46dkml79gkbj.png" alt="Image description" width="746" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also known by "&lt;strong&gt;Repeatable Read&lt;/strong&gt;" but the most descriptive one is &lt;strong&gt;Snapshot&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serializable isolation level&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This isolation level is the strongest one, as it prevents transaction race at all each transaction is processed one by one and not concurrent, this will prevent the issue from its source but this will make a performance impact on your application as below image.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pxWzaQOh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vffjvlzuxap5nd9cejv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pxWzaQOh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vffjvlzuxap5nd9cejv.png" alt="Image description" width="880" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Isolation levels comparison
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XskObXle--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3izrjz7wfrcjc4a0iob3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XskObXle--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3izrjz7wfrcjc4a0iob3.png" alt="Image description" width="880" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLb4xiDD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ev5c9eaa6m6fq4eqkqj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLb4xiDD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ev5c9eaa6m6fq4eqkqj.png" alt="Image description" width="880" height="673"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;At the end, there is no best isolation level you can choose it depends what is your use case and what is the requirements and business needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources and References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://fauna.com/blog/introduction-to-transaction-isolation-levels"&gt;Transaction Isolation Levels&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://mkdev.me/en/posts/transaction-isolation-levels-with-postgresql-as-an-example"&gt;Transaction Isolation Levels in PostgreSQL&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>Layer 4 V.S Layer 7 load balancing</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Sun, 06 Nov 2022 21:27:55 +0000</pubDate>
      <link>https://dev.to/smuhammed/layer-4-vs-layer-7-load-balancing-m2h</link>
      <guid>https://dev.to/smuhammed/layer-4-vs-layer-7-load-balancing-m2h</guid>
      <description>&lt;h2&gt;
  
  
  What is Load Balancing?
&lt;/h2&gt;

&lt;p&gt;load balancing is a mechanism or strategy applied when you have an API or back end service that heavily used in the system so you can run multiple instances form that component and on top of them there is another service that distribute requests among of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Load Balancing Algorithms
&lt;/h2&gt;

&lt;p&gt;Most popular two load balancing algorithms are &lt;strong&gt;IP Hashing&lt;/strong&gt; and &lt;strong&gt;Round Robin&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  IP Hashing
&lt;/h2&gt;

&lt;p&gt;Here the load balancer hashes the request and match the hash with one of services, whenever this request came again it will stick with same service&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OUk5NxEB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85qdkpfz9c2xs53r20wq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OUk5NxEB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85qdkpfz9c2xs53r20wq.png" alt="Image description" width="880" height="543"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Round Robin
&lt;/h2&gt;

&lt;p&gt;Here the requests distributed among service equally as image below&lt;/p&gt;

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

&lt;h2&gt;
  
  
  So, What is Layer 4 and Layer 7?
&lt;/h2&gt;

&lt;p&gt;regarding to OSI (open system interconnection) Model the network model has 7 layers described as below image&lt;/p&gt;

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

&lt;p&gt;So Layer 4 and Layer 7 we are talking about is &lt;strong&gt;transport&lt;/strong&gt; layer and &lt;strong&gt;application&lt;/strong&gt; layer in OSI model, let's talk about each one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 7 : Application Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here your load balancer service will receive the whole request from the client and decide which service should receive this request and forward the request to it, This means that the load balancer service has full awareness of what is received and what is sent, Therefore as benefit from this you can enable &lt;strong&gt;caching&lt;/strong&gt; for responses, Whenever got this request before it's not needed to forwarded the request to server just pull up the response from the cache and return it to client, also you can define here some URLs you don't want some users to access them so load balancer can &lt;strong&gt;act&lt;/strong&gt; as &lt;strong&gt;security&lt;/strong&gt; layer to prevent this access, But it also has a security wise if load balancer service hacked, Hacker can now know a lot of data about your system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 4 : Transport Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here as OSI model describers the data packet is surrounded by source and destination addresses (IP address and port) as below image&lt;/p&gt;

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

&lt;p&gt;So when you enable layer 4 load balancing, Your load balancer service will not wait until gather all packets and recognize the request, It will directly open a connection with one of your back end servers and send the packets to it, Therefore the load balancer has no awareness of what happen here it just act as &lt;strong&gt;transporter&lt;/strong&gt; only, So you &lt;strong&gt;can't&lt;/strong&gt; get benefit of &lt;strong&gt;caching&lt;/strong&gt; or prevent some URLs from being processed in load balancing layer, but it also &lt;strong&gt;enhance&lt;/strong&gt; the &lt;strong&gt;security&lt;/strong&gt; of load balancer service as it's now don't have any information about your system and also it faster in packet transporting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3tDLAWE7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgjfw10ij5lt6497qg7q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3tDLAWE7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgjfw10ij5lt6497qg7q.png" alt="Image description" width="880" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more better explanation watch this &lt;a href="https://youtu.be/aKMLgFVxZYk"&gt;video&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>webdev</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Optimistic V.S Pessimistic databases</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Tue, 01 Nov 2022 16:09:15 +0000</pubDate>
      <link>https://dev.to/smuhammed/optimistic-vs-pessimistic-databases-n73</link>
      <guid>https://dev.to/smuhammed/optimistic-vs-pessimistic-databases-n73</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When you have one single user that will use your application that the optimal case and Youtopia of our software industry, but when it comes to multiple users, that what will bring all evil to the application as you now have to manage concurrency and pay attention and resources to manage that, databases engineers suffered from this issue and came with multiple solutions. Let's discuss them mainly with &lt;strong&gt;write&lt;/strong&gt; operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling multiple writes
&lt;/h2&gt;

&lt;p&gt;Let's define the problem first and then discuss the solitons.&lt;br&gt;
As below image 2 users are trying to increment same counter value in DB each user read same value from DB because they start they transaction almost at the same time, correct result of this operations must to set counter value to 44 but because race condition the result is 43 and this gives us wrong information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VE-02sFY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enaef2zbxlyk85h8fdjp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VE-02sFY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enaef2zbxlyk85h8fdjp.png" alt="Image description" width="880" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or as below image 2 users read same record A from DB and each one runs its' own logic upon it, but regarding that user Y finished its' write late so database kept its' value, and when user X try to refresh the record he will not find his updates and this known by &lt;strong&gt;Lost Updates or Last Write Win (LWW)&lt;/strong&gt; problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OfasIMkj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gh30ux6v0fijruzqeje.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OfasIMkj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gh30ux6v0fijruzqeje.png" alt="Image description" width="540" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  First solution: Locking
&lt;/h2&gt;

&lt;p&gt;first solution for these problems is locking, whenever any transaction/user want to write to database he must get a lock upon records and prevent any transaction/user from accessing these records.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Explicit lock&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Database allows user to acquire locks on demand as below&lt;br&gt;
&lt;code&gt;SELECT * FROM customers WHRER id = 5 FOR UPDATE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FOR UDATE&lt;/strong&gt; clause indicates that user needs a lock here so he can do his updates safely, and this is helpful when you are using &lt;strong&gt;read-modify-write&lt;/strong&gt; approach as below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BEGIN&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT * FROM customers WHERE id = 5 FOR UPDATE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//do your logic&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UPDATE customers SET is_done = true WHERE id = 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COMMIT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But there is also another approach which is called &lt;strong&gt;atomic updates&lt;/strong&gt; or &lt;strong&gt;compare-and-set&lt;/strong&gt; which when you fire update statement database will acquire a lock and not let any transaction/user from accessing it what records your updating now until you commit.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UPDATE customers SET available = true WHERE id = 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Table V.S Row locking level&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table level lock&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;in this locking mechanism database lock the entire table you are working on and don't let any transaction accessing it even for another set of records, and keep this lock until you finish your transaction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Row level lock&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this mechanism database locks only the records you are working on not entire table.&lt;/p&gt;

&lt;p&gt;You may think Row level is better than table, but it depends because managing these locks by acquiring and releasing operations can be expensive in case of huge set of rows modification at same time, but also Table level lock can affect your user experience in case of long running transaction started on main table so it's now have a lock and no one can access the table until it finish.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Phase Lock (2PL)
&lt;/h2&gt;

&lt;p&gt;what we discussed till now has a performance impact as any transaction starts, it locks the others until it finishes, even if it just read-only transaction, so database engineers decided to do something smart, it's Two Phase Lock (2PL).&lt;/p&gt;

&lt;p&gt;In this mechanism you have 2 types of lock, &lt;strong&gt;Shared&lt;/strong&gt; and &lt;strong&gt;Exclusive&lt;/strong&gt; and here how it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When any transaction want to read a record, it first have to acquire &lt;strong&gt;Shared&lt;/strong&gt; lock.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can have any number of &lt;strong&gt;Shared&lt;/strong&gt; locks on same record, by this you allow multiple reads on same records.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you want to update the record you have to acquire &lt;strong&gt;Exclusive&lt;/strong&gt; lock, to acquire this type of lock all &lt;strong&gt;Shared&lt;/strong&gt; locks have to be released first so now you are the only person who have the lock.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you acquired the &lt;strong&gt;Exclusive&lt;/strong&gt; lock, no one can access this record until you finishes your updates and release the Exclusive lock.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;This approach enhance performance a little bit as now not any transaction/operation locks the record, just write operation, but still has performance impact regarding to managing locks.&lt;/p&gt;

&lt;h2&gt;
  
  
  To be Optimistic or not to be, that the question
&lt;/h2&gt;

&lt;p&gt;What we discussed until now is known by &lt;strong&gt;Pessimistic&lt;/strong&gt; approach, you are pessimistic that if you didn't do locking there a problem will happen, so in any case you do restrictions.&lt;/p&gt;

&lt;p&gt;But there is another approach called &lt;strong&gt;Optimistic&lt;/strong&gt;, by this approach you will not do any locks here you will keep all transactions free to read and write any time they want, &lt;strong&gt;BUT&lt;/strong&gt; in order to solve the conflicts and prevent lost updates we will do these steps&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In each table there will be what called version column with default value 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever any update operation happen to this record it increments version by 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In case of 2 transactions are updating same record at same time first one will increment the version by 1, so the second one has a outdated version number now, so the database will decide to terminate the second one and they have to fire the transaction again after getting the most recent version of this record.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Regarding all these techniques we discussed here, there is no best technique or worst one, it depends what your application accept and what not, regarding to this you choose which database engine that provide locking mechanism will match your application requirement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Designing Data-Intensive application Chapter 7 : Transactions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://youtu.be/I8IlO0hCSgY"&gt;Optimistic V.S Pessimistic Video&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>GraphQL : Deal REST API like SQL</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Sat, 29 Oct 2022 20:36:01 +0000</pubDate>
      <link>https://dev.to/smuhammed/graphql-deal-rest-api-like-sql-4731</link>
      <guid>https://dev.to/smuhammed/graphql-deal-rest-api-like-sql-4731</guid>
      <description>&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;Have you ever faced these problems, as a back-end developer which front-end requires a lot of changes regarding field names, or field is missing in API response, or you are sending more fields than UI requires and front-end got mad about this, or you have multiple consumers and each consumer want the response as he wishes.&lt;/p&gt;

&lt;p&gt;On other hand as front-end developer have you faced that API response field names not match your object, you need same resource in different UI components and blocked until back-end handle them, back-end not formatting dates and strings before sending them to you or formatting don't match with your UI.&lt;/p&gt;

&lt;p&gt;What about if we can deal with REST APIs as some sort of SQL : &lt;strong&gt;selecting&lt;/strong&gt; what needed only, &lt;strong&gt;formatting&lt;/strong&gt; the response as we want and manipulate response with our needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  GraphQL in your service
&lt;/h2&gt;

&lt;p&gt;Graph Query Language is a manipulation language for APIs made by Facebook(meta) to solve problems of continuous UI changing with multiple devices and requirements and propagation of this changing impact to back-end APIs to accommodate with, so let back-end build one API and each consumer can &lt;strong&gt;pull&lt;/strong&gt; what he needs from it, &lt;strong&gt;manipulate&lt;/strong&gt; field name as he needs and &lt;strong&gt;formatting&lt;/strong&gt; the values as per his need.&lt;/p&gt;

&lt;h2&gt;
  
  
  How this magic happens?
&lt;/h2&gt;

&lt;p&gt;You have to do setup steps so that GraphQL engine can work upon it , At first you have to build &lt;strong&gt;graphql schema&lt;/strong&gt; that define what &lt;strong&gt;operations&lt;/strong&gt; you provide like getAllTransactions() , createTransaction() with what parameters these operations takes and what are they return , and this setup have to be resident in &lt;em&gt;**.graphql&lt;/em&gt;* extension with specific syntax as below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hc6UvLpl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07kkxglcbmejw7tfifzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hc6UvLpl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07kkxglcbmejw7tfifzt.png" alt="Image description" width="880" height="760"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After defining your schema you have to link between operations in schema and actual logic that have to be done during these operations call, Linking between operations and logic is done through &lt;strong&gt;DataFetcher&lt;/strong&gt; component, as Graphql engine not performing logic instead it just look at your schema and what requester requests, Then match between them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How GraphQL link between operation and logic have to be done?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you start your application GraphQL engine will pull your schema files and parse them, if there any syntax error it will crash. also &lt;strong&gt;DataFetcher&lt;/strong&gt; used to link between operation name and service method that should be executed with , Then this setup goes to SchemaGenerator to generate &lt;strong&gt;GraphQLSchema&lt;/strong&gt; and expose GraphQL API as below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--czN4tZfd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n924h00r0wkpznc6sqqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--czN4tZfd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n924h00r0wkpznc6sqqi.png" alt="Image description" width="594" height="701"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now GraphQL API responsible for&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Receiving the request from requester.&lt;/li&gt;
&lt;li&gt;Extract operation name and match with one of defined in schema files.&lt;/li&gt;
&lt;li&gt;Fire service method that wired with operation in DataFetcher.&lt;/li&gt;
&lt;li&gt;Get result from service method, filter what fields needed in Request and applying any format needed upon data.&lt;/li&gt;
&lt;li&gt;Return response to requester.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  request - response sample
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zfmrQSj7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4r0c1vejr9csg1ovmds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zfmrQSj7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4r0c1vejr9csg1ovmds.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fortunately, a lot of helper libraries now exists, and your setup will not go beyond define your schema files and implementing an interface that provide your business logic and let the magic begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nothing is perfect!
&lt;/h2&gt;

&lt;p&gt;let's taking about GraphQL pros and cons&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limiting the hassle of change back-end APIs when continues changes of UI.&lt;/li&gt;
&lt;li&gt;Supporting multiple API consumers with different requirements with same back-end service.&lt;/li&gt;
&lt;li&gt;One GraphQL API can handle multiple operations as per your schema files and don't worry about contract just give consumers one URL and your are done.&lt;/li&gt;
&lt;li&gt;Helper libraries for providing GraphQL UI that consumer can browse your schema and test the operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Having performance impact for GraphQL engine works so you have to use it wisely.&lt;/li&gt;
&lt;li&gt;Can't depend on HTTP error codes as GraphQL always send 200/Ok even with errors but it provide you with error filed in that case.&lt;/li&gt;
&lt;li&gt;You have to configure special exceptional handler layer for GraphQL as the request now takes different path from rest of your back-end service.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://graphql.org/"&gt;GraphQL Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://youtu.be/fVmQCnQ_EPs"&gt;GraphQL Video&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>webdev</category>
      <category>api</category>
    </item>
    <item>
      <title>Keep your code healthy with Refactoring</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Fri, 28 Oct 2022 08:42:32 +0000</pubDate>
      <link>https://dev.to/smuhammed/keep-your-code-healthy-with-refactoring-247c</link>
      <guid>https://dev.to/smuhammed/keep-your-code-healthy-with-refactoring-247c</guid>
      <description>&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;In software development day-by-day activities, you go through deadlines, workarounds and misunderstanding and under estimate of work, this leads to bad code and design quality regarding to bad or temporary decisions you took to pass the situation, and this affect whole project quality and upcoming development activities and this generates what called by &lt;strong&gt;Technical Debt&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Technical debt (also known as tech debt or code debt) describes what results when development teams take actions to expedite the delivery of a piece of functionality or a project which later needs to be refactored. In other words, it’s the result of prioritizing speedy delivery over perfect code.&lt;/p&gt;

&lt;p&gt;So, in order to eliminate technical debt you must revisit your code/decisions and try to refine them frequently and plan time in your weekly work to do that and this what called Refactoring.&lt;/p&gt;

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

&lt;p&gt;Refactoring is the process of &lt;strong&gt;restructuring&lt;/strong&gt; code, while &lt;strong&gt;NOT&lt;/strong&gt; changing its original functionality. The goal of refactoring is to &lt;strong&gt;improve internal code&lt;/strong&gt; by making many small changes without altering the codes' external behavior and it's not necessary to improve performance.&lt;/p&gt;

&lt;p&gt;So, the main goal of refactoring is enhancing code &lt;strong&gt;readability&lt;/strong&gt; and &lt;strong&gt;maintainability&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Refactor?
&lt;/h2&gt;

&lt;p&gt;There are multiple times that a refactor is good at.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of three&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Three strikes and you refactor, decide when similar pieces of code should be refactored to avoid duplication. It states that two instances of similar code do not require refactoring, but when similar code is used three times, it should be extracted into a new procedure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before adding new feature&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before adding new feature you have to take a look and see how this feature can affect your existing code, is it need a change in it? or what is the best way to add this feature? and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you found some ugly code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is a rule that says, leave the place cleaner than you found, when you find some code that can be better regarding to readability and maintainability don’t be hesitated and refactor it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactor principles
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;- Small changes at a time&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Always consider a small change at time, build and compile and test what you did, then complete the refactoring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Don't break your code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All your refactor changes have not to break the code, and always code must be compilable and runnable, cause of any urgent matter happens and you need to leave the refactoring right now, otherwise you don’t refactor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Do unit test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Writing unit test before refactoring can save your back and give you more confidence when refactoring cause you now have an alert when there is something goes wrong you will be notified once it happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactor Catalog
&lt;/h2&gt;

&lt;p&gt;refactor catalog describes solution for each code smell you may face in your daily work and code smells are categorized by&lt;/p&gt;

&lt;h2&gt;
  
  
  Bloaters
&lt;/h2&gt;

&lt;p&gt;Bloaters are code, methods and classes that have increased to such gargantuan proportions that they are hard to work with. Usually, these smells do not crop up right away, rather they accumulate over time as the program evolves (and especially when nobody makes an effort to eradicate them).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptoms&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A method contains too many lines of code. Generally, any method longer than 10 lines.&lt;/p&gt;

&lt;p&gt;some example &lt;a href="https://refactoring.guru/smells/long-method"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Primitive obsession&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use of primitives instead of small objects for simple tasks (such as currency, ranges, special strings for phone numbers, etc.)&lt;/p&gt;

&lt;p&gt;Use of constants for coding information (such as a constant USER_ADMIN_ROLE = 1 for referring to users with administrator rights.)&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/primitive-obsession"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A class contains many fields/methods/lines of code.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/large-class"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Long parameter list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you have more than three or four parameters for a method.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/long-parameter-list"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data clumps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sometimes different parts of the code contain identical groups of variables (such as parameters for connecting to a database). These clumps should be turned into their own classes.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/data-clumps"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Object oriented abusers
&lt;/h2&gt;

&lt;p&gt;All these smells are incomplete or incorrect application of object oriented programming principles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptoms&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alternative classes with different interfaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two classes perform identical functions but have different method names.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/alternative-classes-with-different-interfaces"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refused Bequest&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a subclass uses only some of the methods and properties inherited from its parents, the hierarchy is off kilter. The unneeded methods may simply go unused or be redefined and give off exceptions.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/refused-bequest"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Temporary field&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Temporary fields get their values (and thus are needed by objects) only under certain circumstances. Outside of these circumstances, they are empty.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/temporary-field"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dispensable
&lt;/h2&gt;

&lt;p&gt;A dispensable is something pointless and unneeded whose absence would make the code cleaner, more efficient and easier to understand.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Comments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Comments are usually created with the best of intentions, when the author realizes that his or her code isn't intuitive or obvious. In such cases, comments are like a deodorant masking the smell of fishy code that could be improved.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/comments"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duplicate code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/duplicate-code"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A data class refers to a class that contains only fields and crude methods for accessing them (getters and setters). These are simply containers for data used by other classes. These classes don't contain any additional functionality and can t independently operate on the data that they own.&lt;/p&gt;

&lt;p&gt;some examples &lt;a href="https://refactoring.guru/smells/data-class"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>refactoring</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to master your career</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Wed, 26 Oct 2022 14:02:59 +0000</pubDate>
      <link>https://dev.to/smuhammed/how-to-be-so-good-they-cant-ignore-you-304c</link>
      <guid>https://dev.to/smuhammed/how-to-be-so-good-they-cant-ignore-you-304c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you heard about below advices on social media or some of your friends told you them&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You have to follow your passion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you wanted to love your job, you have to make it follow your passion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Free yourself from your bored job and start looking something you are passionate about.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your wage is the price for getting rid of your passion and your dreams.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While we are going through this article we will try to spotlight on the truth of &lt;strong&gt;passion&lt;/strong&gt; from "&lt;strong&gt;So Good They Can't Ignore You&lt;/strong&gt;" book point of view, we will discuss about where the passion comes from, and how people love their jobs and show creativity, and what matter most is how you also can reach out to this stage to obtain a successful career path.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Step : Don't follow your passion
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Following your passion not only a bad advice, but it also can be dangerous&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Many people think that what makes other people successful in their jobs is the passion, and they support their thoughts about passion with that each one was born with a passion toward something specific, and each one should work hard to discover what is that thing, but the truth is that's not the case for the following reasons &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Researches and studies proved that human not born with passion regarding to specific job, but maybe with some sort of hoppy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Discovering your passion requires a lot of time and expertise for a lot of fields, and you may not reach out to something.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The truth is that passion comes from mastery of your job, and to get to this stage, you need a lot of time and hard work, so you can't easily say that you are passionate about something.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, for the above listed reasons the advice of following your passion is wrong.&lt;/p&gt;

&lt;p&gt;But, if following the passion isn't the right thing what we should do to have a great career path.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Two : Adapt the craftsman mindset
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Your nowadays hard work, opens multiple paths in the future&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The financial rule said that "if you want to obtain something rare and precious, you have to give something rare and precious", So, the craftsman mindset encourage you to acquire different skills at your job to be able to exchange them with a great job position.&lt;/p&gt;

&lt;p&gt;And here comes a question, what are the treats of great work environment that deserve your skills?&lt;/p&gt;

&lt;p&gt;The writer mention that the great work environment which provides below to their employees&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Innovation&lt;/strong&gt;: where the employee has the chance to show up his creativity without being tied with specific rules and regularities and he can always challenge his skills and measuring the his improvements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Impact&lt;/strong&gt;: where the employee can feel that his job affects people lives and makes it better&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Control&lt;/strong&gt;: where the employee has the full control over his duties and requirements, not be bothered with micromanagement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Relation&lt;/strong&gt;: where employee can feel that he is a part of something not just a task doer&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The craftsman mindset also says that, it doesn't matter what is your current job and how do you feel about it, if you are able to acquire more skills you will be able the exchange them with your dream job.&lt;/p&gt;

&lt;p&gt;On the other hand, to be realistic there is nothing can be done perfectly, the writer mention that the circumstances the craftsman mindset can backfire you&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;There is no enough space at your current job to show up your skills and be different from the others.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you feel that your work has no impact on other peoples, or it may harms the society.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When your current job force you to be with accompany you don't feel comfortable with.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Third Step: Seek for promotion
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;No one owes you a great career path, you have to deserve it&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Obtaining a control is something mandatory to boost your success toward a great career path, and we mean by control here is the space  of control over your work and activities,  which lets you do your work with the most suitable way for you. and the promotion is the shortest path to get this control, with promotion the micromanagement will be reduced and you have the ability to apply your thoughts and your way of work, therefore you can obtain more trust from your bosses so that you can perform your tasks in more comfortable way, so that the writer encourage you to seek for promotion at your job.&lt;/p&gt;

&lt;p&gt;the writer also mention that not all places provide you with control, so you have to choose wisely for your next move the company will provide you with much control.&lt;/p&gt;

&lt;p&gt;But while you looking for control you have to be careful and avoid these control traps you may encounter&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Avoid the fake control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fake control is when you are seeking the control in a field or domain you don't have enough skills or information about it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Control generate resistance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You will encounter resistance either from your colleges or from your managers, because you are asking for something they are not familiar with, so you have to arm yourself with skills and trust to tackle this resistance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fourth Step : Set a goal for your career path
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Any work you do can be a spark for a great career path&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Off course getting paid is reason number one for getting a job, but setting a goal for your job like for a software engineer "make life easier for people by providing helpful apps", or for a doctor "helping poor people to recover from diseases", setting such goals for your career path continuously recharge your power to tackle the obstacles and acquire skills needed to have a great and memorable career path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, to summarize the main idea about the truth of passion and how people can find it from the book and writer view&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mostly the passion is the result of hard work and continuous learning to acquire the needed skills to tackle your job tasks in more efficient and best way, then you can exchange these skills with a job in place that satisfies your needs and match your personal goals, so that your job doesn't became something you hate anymore, further more you can innovate and create new things.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;And here list of advices from the writer to help you acquire a great career path *&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;List the most required skills for your job and seek for acquiring them as soon as possible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always look for someone you can consult to help you take better decisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Seek for feedback from your colleges and managers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try to but your ideas in execution to test their effectiveness.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you want to evaluate your ideas, always ask yourself are people ready to pay for it or not.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Monitor how you are spending your day time to eliminate the bad and increase the good things.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use different methodologies when you are learning something new to tackle it quickly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sharping your skills by challenging yourself and not staying in comfort zone for a long time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mix between theory and practice to improve your skills.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set a goal so that it can be your compass to check if you are getting closer or far from it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be patient, it takes time to mastery.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.amazon.com/Good-They-Cant-Ignore-You/dp/1455509124"&gt;So good they can't ignore you book&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>WebSocket V.S HTTP</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Tue, 25 Oct 2022 11:02:44 +0000</pubDate>
      <link>https://dev.to/smuhammed/websocket-vs-http-1a78</link>
      <guid>https://dev.to/smuhammed/websocket-vs-http-1a78</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;WebScoket and HTTP both are protocols used to transmit data over network and both of them built upon TCP so what the purpose and when to use each one, and here is the story.&lt;/p&gt;

&lt;p&gt;Hence the more popular one is HTTP we will take it first, HTTP mechanism is based on Request/Response strategy so there is must be client (browser/mobile/another back end service) that send the request first and the server process the request and send the response then connection closed (can use keep-alive header to make TCP connection open for a while but mainly HTTP request is finished).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hxdhm4LK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0ebu0tcxcdpk8kxda91.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hxdhm4LK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0ebu0tcxcdpk8kxda91.png" alt="Image description" width="698" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So regarding to HTTP Request/Response mechanism which needs the client to ask for data then server response, we needed a way to make the server pushes data to the client (without client asking for it) this is the need for such WebSocket protocol, WebSocket works as this &lt;/p&gt;

&lt;p&gt;First the client needs to start to communication with the server so it sends an HTTP request to server with upgrade request to indicate to server this request need to be converted to another protocol, Then server respond with channel created, then communication established, therefore any of both side now are aware of each other and can accept data from each other so server can now send a data based on current event without request from client itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hKkQYQNg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67wrcm8rbgolu4t5bjtq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hKkQYQNg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67wrcm8rbgolu4t5bjtq.png" alt="Image description" width="880" height="793"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also it's very helpful for making &lt;strong&gt;Asynchronous&lt;/strong&gt; calls that the client should not wait for a long request to be done, just release it and when server is finished will send the result to client.&lt;/p&gt;

&lt;p&gt;HTTP request starts with http:// but WebSocket start with ws:// or wss:// for secure connection and also HTTP is stateless but off course WebSocket is stateful.&lt;/p&gt;

&lt;p&gt;whenever any of both side close the connection the communication channel is closed and need to be open again from client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=2Nt-ZrNP22A"&gt;WebSocket crash course&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://youtu.be/WmPKzFYKijM"&gt;Why is so hard to cancel HTTP request&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>architecture</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Consider Anti-corruption Layer when integrate with another system</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Sun, 23 Oct 2022 20:16:04 +0000</pubDate>
      <link>https://dev.to/smuhammed/consider-anti-corruption-layer-when-integrate-with-another-system-2c55</link>
      <guid>https://dev.to/smuhammed/consider-anti-corruption-layer-when-integrate-with-another-system-2c55</guid>
      <description>&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;You may be worked on projects that deal with third party APIs (APIs related to another service provider) or some libraries generated by another companies or you building new system but it deals with a legacy system, when you use or deal with these APIs you have to deal them with special handling in order to keep your code clean and maintainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The main problem is different languages
&lt;/h2&gt;

&lt;p&gt;Mostly these APIs you deal with outside your system are built with different language from that in your system, By different language I don't mean programming language I mean Domain language, Different objects and properties from that used in your system.&lt;/p&gt;

&lt;p&gt;If you start directly use this language (Objects and Properties) in the middle of your Domain business logic, you will find that you are make explicit conversion between your system objects and third party objects, also may handle exceptions that may occur from library or handle failure of HTTP requests in the middle of your business logic, and maybe your functionality is very small or trivial but because it uses a third party API it became more complex to read and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meet Anti-Corruption Layer
&lt;/h2&gt;

&lt;p&gt;Anti-corruption layer is one of DDD (Domain Driven Design) principles, name came from that preventing corruption of your business logic that may happen because of integration code, which tells you that if you will deal with any third party API you have to isolate integration code in different layer and any service in your system needs to use this API it should use it from Contract/Interface.&lt;/p&gt;

&lt;p&gt;Let's take an example, If you have a Social Insurance System and want to integrate with a third party that provides &lt;strong&gt;Citizen information&lt;/strong&gt; based on &lt;strong&gt;national id&lt;/strong&gt;, so your system have Insurer class that belongs to your system domain like this&lt;/p&gt;

&lt;p&gt;`Class Insurer {&lt;/p&gt;

&lt;p&gt;private String firstName;&lt;/p&gt;

&lt;p&gt;private String lastName;&lt;/p&gt;

&lt;p&gt;private String nationalId;&lt;/p&gt;

&lt;p&gt;private Integer age;&lt;/p&gt;

&lt;p&gt;// getters and setters &lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;And third party response will be like this &lt;/p&gt;

&lt;p&gt;`Class Citizen {&lt;/p&gt;

&lt;p&gt;private String fullName;&lt;/p&gt;

&lt;p&gt;private String id;&lt;/p&gt;

&lt;p&gt;private Date dateOfBirth;&lt;/p&gt;

&lt;p&gt;// getters and setters &lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;it's Obvious that 2 different classes and need conversion, &lt;strong&gt;so how our anti-corruption layer will be look like?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First we will build the contract/interface as this&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;`public interface CitizenSystemIntegration {&lt;/p&gt;

&lt;p&gt;public Insurer getInsurer(String nationalId);&lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;and our contract implementation service will be like&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;`class CitizenSystemIntegrationImpl implements CitizenSystemIntegration  {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @Override
    public Insurer getInsurer(String nationalId) {

        // build request object

        // call third party api

        // convert response to Insurer object

        // return

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

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Now whenever you need this facility you can create object from this implementation and use it, listing below benefits of this strategy &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Isolating the integration code.&lt;/li&gt;
&lt;li&gt;Reducing code duplication , as we have one place to edit and maintain.&lt;/li&gt;
&lt;li&gt;You can build your exception handling logic in one place.&lt;/li&gt;
&lt;li&gt;Your main system functionality is clear, there is no different object you need to handle, no specific exceptions you have to deal with and your domain service is clear.&lt;/li&gt;
&lt;li&gt;You can add logging functionality&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end , Anti-corruption layer is built based on same concept of Adapter Design Pattern&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Q2iFLu9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbgblqcci19a141i4mtj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Q2iFLu9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbgblqcci19a141i4mtj.png" alt="Image description" width="428" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://refactoring.guru/design-patterns/adapter"&gt;Adapter Pattern&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.thereformedprogrammer.net/wrapping-your-business-logic-with-anti-corruption-layers-net-core/"&gt;Anti-Corruption Layer&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>bestpractice</category>
      <category>adapter</category>
      <category>integration</category>
    </item>
    <item>
      <title>RestTemplate and FeignClient, The Dependency Injection Principle</title>
      <dc:creator>Sameh Muhammed</dc:creator>
      <pubDate>Thu, 20 Oct 2022 14:15:50 +0000</pubDate>
      <link>https://dev.to/smuhammed/dependency-injection-an-example-from-spring-boot-framework-2pj7</link>
      <guid>https://dev.to/smuhammed/dependency-injection-an-example-from-spring-boot-framework-2pj7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In any application you work with regardless it's a monolith or microservices it's common to deal with third parities using HTTP calls, Spring Boot provides multiple ways to do HTTP calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  RestTemplate Class
&lt;/h2&gt;

&lt;p&gt;RestTemplate is a class responsible for handling HTTP requests and map the response for you, for an example you can do this.&lt;/p&gt;

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

&lt;p&gt;Yeah, this it works fine , but we missed something, regardless static URL in the code, we are communicating with another system over network and this may faces a lot of failures, network may be unstable so the calling fails or another system we deal with has bugs, and all of this might affect badly on our system.&lt;/p&gt;

&lt;p&gt;So what, let's add try catch block.&lt;/p&gt;

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

&lt;p&gt;Yes it's more safer now, but what about if we want to handle the exception in special way like log the exception into database or send some notifications to user.&lt;/p&gt;

&lt;p&gt;Let's do it,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dx0leYqZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0t23464lavbkhnmwe6hq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dx0leYqZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0t23464lavbkhnmwe6hq.png" alt="Image description" width="880" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We did it and works fine, but here is the problem, now we broke &lt;strong&gt;Single Responsibility&lt;/strong&gt; principle and now this method is responsible for many things, so in order to make our code cleaner we may extract all business logic related to calling HTTP request to another service with it's error handling and just use this service to interact with third party.&lt;/p&gt;

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

&lt;p&gt;This better now what about using another technique,&lt;/p&gt;

&lt;h2&gt;
  
  
  Feign Client
&lt;/h2&gt;

&lt;p&gt;Feign Client also is a component used to handle HTTP requests but in another way, you create an interface then annotate it with @FeignClient and fill annotation properties and type your API signature as method as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9veNLLDP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy0qr728vbuai1najqez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9veNLLDP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy0qr728vbuai1najqez.png" alt="Image description" width="880" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then you use @Autowired upon interface and now you are having HTTP handler out of the box provided to you from Spring Boot, &lt;strong&gt;much easier right, isn't it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;what about error handling?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are multiple ways to do that you can use fallback property in @FeignClient you can use it to provide fallback class that contains error handling logic, you will find links below about Feign Client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Feign Client It's not only about ease code writing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Providing Feign Client in Spring Boot toolbox not only for ease code writing purpose for developer instead of using RestTemplate, but also it has an architectural decision and design.&lt;/p&gt;

&lt;p&gt;Let's say you are using RestTemplate for communication with third parties, and Spring team decides that they need to change some RestTemplate method signatures to add some value to the class, off course they have to support backward compatibility and deprecate methods gradually, but in case you need to catch up the updates you have to make changes to your code and test it again and deploy it.&lt;br&gt;
&lt;strong&gt;That's the key and main reason behind Feign Client.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While you are using Feign Client you don't depend on concreate class/implementation. Instead, you are using interface and out of the box Spring Boot knows that you are need here HTTP call, so it did that for you without interfering you about how it dose do that, they may still depends on RestTemplate class but they hide that from you, and now they can do whatever change they want and not affecting your code.&lt;/p&gt;

&lt;p&gt;Depending on contracts/interfaces while dealing with other system modules or third parties instead of concrete classes, makes your code loosely couple and each module can change its' behavior without affecting other modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So you have to follow this rule&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Depend on abstractions, not on concretions It means, coding to interfaces, not to concrete classes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Especially with parts that you expect changes during development process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At the end, what we were talked about is &lt;strong&gt;Dependency Inversion Principle&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.baeldung.com/rest-template"&gt;Guide to RestTemplate&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.baeldung.com/spring-cloud-openfeign"&gt;Introduction to OpenFeign&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/mguinea/understanding-dependency-inversion-principle-4a6a"&gt;Understanding Dependency Injection&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/samehmuh95"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>springboot</category>
      <category>solid</category>
    </item>
  </channel>
</rss>
