<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vasu Pal on Medium]]></title>
        <description><![CDATA[Stories by Vasu Pal on Medium]]></description>
        <link>https://medium.com/@vasu.pal?source=rss-c650134b1fa3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1g5rXqLbiEOuF_tZIk0Q2g.jpeg</url>
            <title>Stories by Vasu Pal on Medium</title>
            <link>https://medium.com/@vasu.pal?source=rss-c650134b1fa3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:52:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vasu.pal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to build a highly customizable and scalable Kafka Consumer using Goroutines?]]></title>
            <link>https://towardsdev.com/how-to-build-a-highly-customizable-and-scalable-kafka-consumer-using-goroutines-4c128b6b9058?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c128b6b9058</guid>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Tue, 05 Apr 2022 08:27:42 GMT</pubDate>
            <atom:updated>2024-01-17T17:41:54.076Z</atom:updated>
            <content:encoded><![CDATA[<p>A “Kafka Consumer” is a listener that watches for any new events inside a Kafka topic. I am here to guide you in coding a scalable Kafka consumer to quickly increase and decrease your consumer’s processing capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4c7YUQ59BdMamm67dYQ-Qg.jpeg" /></figure><p>Kafka is generally associated with scalability when building highly scalable and distributed systems. However, Kafka is merely a tool to help your application scale very well, often mistaken by many developers.</p><p>Before, we jump right on to some code. Here’s your quick introduction to Kafka in case you’re not familiar.</p><p><em>A Kafka is a distributed event processing and streaming platform that handles event ingestion and parallel processing at an unprecedented scale. Kafka does this by allowing streaming these events in real-time to multiple consumers at once.</em></p><p>Now let’s see how we can create a Kafka consumer in Go.</p><p><em>Note: I’m using a library called </em><a href="https://github.com/segmentio/kafka-go"><em>segmentio</em></a><em> to implement Kafka consumer.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5d94aa88021a83fa1d5caa1a233de62/href">https://medium.com/media/f5d94aa88021a83fa1d5caa1a233de62/href</a></iframe><p>The above code helps us quickly create new consumers in a structured way. Now let’s see how you can use this blueprint to start consuming messages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6af5c5a7182700b0e7471b5949a4dbf/href">https://medium.com/media/d6af5c5a7182700b0e7471b5949a4dbf/href</a></iframe><p>So, the above code creates ten consumers listening to the topic topic-1 parallelly using goroutines. Now, You may be wondering, since all the consumers are in parallel, is there any chance two consumers might pick the same event? The answer is No, cause we have declared all the consumers inside a single consumer group using GroupIDconfig.</p><p>Keeping multiple consumers inside a single consumer group helps Kafka check that no single event is sent to 2 different consumers. Thus, making the application process the events in parallel and distributed.</p><p>Therefore, you just need to increase the consumer count via ConsumerCount config to scale your application. And you will have that many consumers doing parallel processing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c128b6b9058" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/how-to-build-a-highly-customizable-and-scalable-kafka-consumer-using-goroutines-4c128b6b9058">How to build a highly customizable and scalable Kafka Consumer using Goroutines?</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gitty Workflow — Part 2]]></title>
            <link>https://medium.com/@vasu.pal/gitty-workflow-part-2-8bbe17c82b20?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bbe17c82b20</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-workflow]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Fri, 14 Jan 2022 20:32:33 GMT</pubDate>
            <atom:updated>2022-01-14T20:32:33.619Z</atom:updated>
            <content:encoded><![CDATA[<h3>Gitty Workflow — Part 2</h3><h4>This article is a continuation of the <a href="https://medium.com/@vasu.pal/gitty-workflow-6af3ec2482a8">Gitty Workflow</a> article, where we discussed the basics of Git and a simple Push/Pull workflow.</h4><p>This article will focus on a better git workflow involving an intermediary staging/develop branch; also explore some new concepts such as Conflicts, Rebasing, &amp; Merging along the way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*aiso-8pcdAsBpm3jwUFzzg.jpeg" /></figure><blockquote>You may be wondering, Why anyone would go into the hassle of maintaining an intermediary branch when one can directly push new features or updates to the main branch?</blockquote><p>Well, almost all production-ready software development projects require multi-level development, testing, and QA cycles. Thus, the possibility of errors or bugs in your main codebase also soars. Therefore, it may not be a good idea to directly push your features to the &quot;main&quot; branch until thoroughly tested. To prevent this, we often use a copy of the &quot;main&quot; branch, usually referred to as the &quot;staging&quot; or &quot;develop&quot; branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AoBXOPEyi1-Ebs29rKbnrw.jpeg" /></figure><blockquote><em>The purpose of a “staging” branch is to serve as a root branch for all the feature branches. And once a feature is complete, it is merged into the “staging” branch, thoroughly tested using the staging environments before being pushed to the “main” branch.</em></blockquote><h4>Conflicts</h4><p>While practicing this workflow, you may often encounter yourself with something called &quot;merge conflicts&quot; when you try to merge two branches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ytXdzr4GIS3EFeXJ.png" /></figure><p>And your code may turn into this mess.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jE4l4Xi0-g5hr-SX.png" /></figure><p>Let&#39;s look at what merge conflicts are, when they usually happen and how to resolve them.</p><blockquote>A conflict arises when two separate branches have made edits to the same line in a file.</blockquote><p>Let us understand this using an example. Let&#39;s say two developers are working on the same file sample.html .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ELN1C91TL-h7ha3FiBf4ow.png" /></figure><p>Dev-A &amp; Dev-B creates a branch f1 and f2 from develop respectively.</p><p>Dev-A adds a code at line 10, commits it, and merge his code back into the develop branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5K5Y7uhXPEL85BkNNr_edw.png" /><figcaption>Dev-A code @ line 10</figcaption></figure><p>Meanwhile, Dev-B adds a bit different code at the same line 10 position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UOobCoSDE5W69oxJC5LXfw.png" /><figcaption>Dev-B code @ line 10</figcaption></figure><p>Now, when Dev-B syncs his respective develop using git pull and tries to merge his version of code residing in the branchf2 into develop; it raises a merge conflict because f2 branch still holds an outdated version of sample.html i.e., version before the branch f1 was merged.</p><p>So, when f2 was trying to make a change exactly where f1 had committed its change, Git raises a merge conflict, allowing Dev-B to choose which version of code is final.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p8c6x8AHCdh6-sN9wXF6Zw.png" /></figure><p>However, we can avoid such merge conflicts at the time of pull request using <strong><em>Rebasing or Merge</em></strong> command before pushing your commits to remote.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pIXZgO7ypkuFDij99PpD0A.jpeg" /></figure><p>Both git rebase &amp; git merge solves the same underlying problem: sync changes from one branch to another. Let&#39;s take a look atgit rebase &amp; git merge in a little more depth.</p><h4>git merge</h4><p>git merge is the easiest and safest option to incorporate someone else&#39;s changes into yours.</p><p>Let&#39;s say you are working on a building APIs on a branch called api-dev which you forked from main. Meanwhile, one of your team members merge the UI-related changes in main.</p><p>To fully integrate your APIs, you want to bring in UI change that just merged in main inside your feature branch api-dev.</p><p>You can use</p><pre>git merge &lt;source_branch&gt; &lt;target_branch&gt; </pre><p>OR</p><pre>git checkout &lt;target_branch&gt;<br>git merge &lt;source_branch&gt;</pre><p>in this case, target_branch is api-devand source_branch is main</p><p>What git mergeis going to do is condense all the changes into a single commit and write it on top of the commit log.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n1VV9dubln0Z-Jt8CIUKnQ.jpeg" /></figure><p><strong>git rebase</strong></p><p>On the other hand, rebase will rewrite the entire commit history by inserting missing commits and sorting the commit log by order of commit time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7SoRsABA9FMSiDH0OVlz5g.jpeg" /></figure><h3><strong>Bonus</strong></h3><h4>git fetch</h4><p>git fetch is used to sync changes from your remote repo into your local repo. For example, let&#39;s say your teammate pushed a new feature branch, and you want to test out that branch. To check out the branch, you must sync it. And that&#39;s where git fetch is helpful.</p><p><strong><em>Note:</em></strong><em> </em><em>git fetch only syncs the remote repo commits, not actually bringing those changes from remote to local. For that, you must have to merge the remote repo with the local repo using </em><em>git merge origin/&lt;branch_name&gt; &lt;branch_name&gt; or use </em><em>git pull</em></p><p><strong>git pull</strong></p><p>git pull is a shorthand command for git fetch and git merge . Thus, whenever your remote repo is ahead of your local repo, you want to bring those changes. You can use git pull .</p><p>Therefore instead of</p><pre>git checkout main<br>git fetch <br>git merge origin/main</pre><p>you simply can do</p><pre>git checkout main<br>git pull</pre><p>And your local repo branch will be consistent with the remote repo branch.</p><p><strong><em>Note: </em></strong><em>you may encounter conflicts while doing </em><em>git pull as well.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bbe17c82b20" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gitty Workflow]]></title>
            <link>https://medium.com/@vasu.pal/gitty-workflow-6af3ec2482a8?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/6af3ec2482a8</guid>
            <category><![CDATA[svm]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Mon, 03 Jan 2022 10:46:17 GMT</pubDate>
            <atom:updated>2022-01-14T07:38:34.316Z</atom:updated>
            <content:encoded><![CDATA[<h4>There are a lot of articles on the internet about various git workflows. And sometimes people often confuse themselves as to which one is better for their project.</h4><p>I’ll be discussing some of the most widely used and production-tested workflow environments that you can integrate into your project management. This specific article will explore the most basic and presumably the simplest one out there, i.e., the Pull/Push Method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*YzvVMsnXMNCEY-Mkl32e_Q.jpeg" /></figure><blockquote>I’m assuming you already have git installed, up and running. If you don’t, you can download it from <a href="https://git-scm.com/downloads">here</a> and follow the instructions.</blockquote><p>Before we explore what push or pull precisely is. Let’s discuss how to set up a git project first.</p><p>It’s pretty simple actually, navigate to the directory that you want to add to git and type <em>git init.</em></p><pre>&gt; cd awsm<br>&gt; git init</pre><p>Once that’s done, you are good to “Git.”</p><p>First, let’s understand a fundamental mechanism that Git uses to make your project management efficient and highly available. It’s called Remote repository and Local repository.</p><p>A remote repository contains a copy of your project on a cloud, while a local repository is something that sits in your machine where you make changes. In an instance, there can only be one remote repo but multiple local repo’s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AnXIneATatEY1TsNt6DQtQ.jpeg" /></figure><p><em>You </em><strong><em>“push” your changes</em></strong><em> from your local repo to be available for other collaborators onto a remote repo. While you </em><strong><em>“pull” changes from remote repo</em></strong><em> into your local repo done by other collaborators.</em></p><p>Now let’s look at how you can initialize a remote repo and make it sync with your local one.</p><p>Pedagogically, I’ll be referencing Github in this article. However, there are a plethora of version control services using Git such as GitLab, BitBucket, etc. All these services follow almost the same principles as GitHub.</p><p>Refer to this <a href="https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository">article</a> for creating a git repo. Once that’s done, simply link remote and local repo using this command.</p><pre>git remote add origin &lt;remote_repo_url&gt;<br>git push -u origin --all</pre><p>OR</p><p><em>You can clone the git repo you’ve just created on Github; this way, you don’t have to go through the above-mentioned two steps.</em></p><pre>git clone &lt;remote_repo_url&gt;</pre><p>Suppose you’re a size of a relatively small team working on multiple features at once. For instance, one team member is working on authentication, the other is building APIs, and the third is working on UI. These are isolated features that can be developed parallelly, not wholly dependent on another.</p><p>To be efficient and have full tracking of changes also being isolated from other feature developments, <em>Git has a concept called “Branching.”</em></p><p><strong>A Branch allows a developer to separate the development scope of a particular feature from another feature.</strong> Thus, making Feature-A unbeknownst to Feature-B unless merged (will be discussed below).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L98foKUJUmhXWb0HDDRR0g.jpeg" /></figure><p>Whenever you create a new project, Git, by default, creates a branch for you called master/main. The Master branch represents the final changes/source of truth/production-ready changes of your code.</p><p>To create a new branch, go to the branch using git checkout &lt;branch_name&gt; you want to branch off.</p><p>Let’s say I want to create a feature-a branch from the master branch. Here’s how it’ll look.</p><pre>git branch feature-a     #creates new branch from master<br>git checkout feature-a   #switch from master to feature-a branch<br>____________________________________________________________________</pre><pre>// OR -&gt; shorthand for creating and checkout out new branch<br>git checkout -b feature-a</pre><p>So far, you’ve initialized the git repo (remote and locally), created a new branch called ‘feature-a.’ And you’ve built a couple of APIs for your feature, and now you want to push these changes to the remote repo and call it a day. <strong>But how do you actually push changes ??</strong></p><p>Pushing a change onto a repo requires a 2-step process of:</p><ul><li>Staging &amp;</li><li>Committing</li></ul><p><strong><em>Staging is a process that allows a developer to confirm (index) the changes made to files and later group those changes through committing.</em></strong></p><p>Let’s try to understand using an example.</p><p>Suppose, We’ve createdapi.go &amp; app.go containing code for our feature-a APIs, and auth.go &amp; permission.go containing code to authenticate users if they can use Feature-A or not. Now, we may want to group app.go and api.go changes since they are related to APIs, and auth.go and permission.go be grouped since they are related to authentication.</p><p>So, we stage app.go and api.go and then commit those changes using a meaningful message.</p><pre>git add app.go<br>git add api.go<br>git commit -m &quot;GET &amp; POST api for feature-a&quot;</pre><p>Then, we stage auth.go and persmission.go and register a commit.</p><pre>git add auth.go<br>git add permission.go<br>git commit -m &quot;authenticating user when calling for feature-a apis&quot; </pre><p>We have created two commits inside our commit log (a ledger containing all the commits). Each has a unique commit-id, which we can revisit at any given point to review the changes made inside that specific commit.</p><p>Now that you’ve committed all your changes. Your local repo is now two commits ahead of your remote repo since you’ve only made those commits inside your local repo. To push those two new commits, type git push and it should push these two commits onto the remote repo branch as well.</p><p>Alright, now that you’ve pushed all your changes in the ‘feature-a’ branch, it’s time to bring those changes into our ‘master’ branch.</p><p><em>If you’re using a git service such as GitLab or GitHub, you can generate a merge request or pull request to merge ‘feature-a’ into ‘master.’</em></p><p>However, you can also do this through a simple command called git merge</p><p>Let’s see how to merge the ‘feature-a’ branch into ‘master.’</p><pre>git checkout master<br>git merge feature-a</pre><p>What git merge is going to do is push all the ‘feature-a’ branch commits onto the ‘master’ branch commit log.</p><p>Suppose the ‘master’ branch has commit id 1|2|3 in the commit log and ‘feature-a’ has 7|8|9 commit ids. After merge, ‘master’ will have commit log something like 1|2|3|7|8|9 along with all the ‘feature-a’ branch changes.</p><p>The only thing that’s left to do is to push the new commits made into ‘master’ using git push.</p><p><em>In the next article, we’ll discuss how to set up and work with production and staging development environments using Branching.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6af3ec2482a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RPC vs REST: Demystifying Web API]]></title>
            <link>https://medium.com/@vasu.pal/rpc-vs-rest-demystifying-web-api-aa5527a87483?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa5527a87483</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[rest]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Sat, 06 Jun 2020 15:32:30 GMT</pubDate>
            <atom:updated>2021-12-05T17:40:55.772Z</atom:updated>
            <content:encoded><![CDATA[<h3>RPC vs. REST: Demystifying Web API</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*hr-LXdQ53JnDf2NGFst9wA.jpeg" /></figure><p>I want to start with the fundamental question that you often might have encountered in your job interview.</p><blockquote>What is an API?</blockquote><p>I can give you a well-sorted, nicely defined answer to that, but would it be enough to grip this concept? Because many of you look at API as <em>JSON over HTTP</em>, which is incorrect.</p><p>According to Wikipedia,</p><blockquote>An API is a computing interface which defines interactions between multiple software intermediaries</blockquote><p>Creating an API seems like a straightforward job; you have a logic that you need to expose externally somehow. However, there&#39;s a lot more to this than you expect. Especially when you consider internet giants like Facebook, Google, and others invest heavily in building APIs.</p><p>Building structured and well-thought API is the key to making your system accessible, scalable, and resilient. This is the reason terms like REST or RPC exist.</p><p>The Internet has been a constant medium of debate between RPC and REST. However, each has its own set of features and drawbacks, which we&#39;ll look at in this article.</p><h3>Remote Procedure Call (RPC)</h3><blockquote>RPC is calling a function over a network.</blockquote><p>RPC has been in the business for decades, with its implementations trailing back to the 80s. And it&#39;s still kicking in and supporting many large-scale distributed systems.</p><p><strong>So</strong>, <strong>how does it work?</strong></p><p>Let&#39;s say you have a function <strong>add </strong>which you want to expose using RPC.</p><pre>func add (x, y):<br>    return x + y</pre><p>RPC maintains all function definitions in a file which looks like this</p><pre>service add (int x, int y) returns int<br>service sub (int x, int y) returns int<br>...</pre><p>Typically, this file lives on server and client (server being the provider and client being a user).</p><p>The client knows what URL/endpoint to hit, what parameters this function accepts, parameters order, and what response a client should expect.</p><pre>GET <a href="https://myserver.com/api/add?a=2&amp;b=3">https://myserver.com/api/add?a=2&amp;b=3</a></pre><p><strong><em>RPC is more of a communication protocol rather than the implementation itself.</em></strong> However, if you want to implement RPC in your systems,<em> there are many open-source frameworks such as Apache Thrift or gRPC being the popular ones.</em></p><h4>Why use RPC?</h4><ul><li>It&#39;s relatively easy to understand and implement. You need to know a function name, parameters it accepts, and the rest is taken care of by RPC itself.</li><li>It&#39;s very efficient and high performance because of its low payload footprint and a well-defined communication protocol.</li><li>It is easier to design an API endpoint that only requires you to create a function.</li></ul><h4>Why does RPC suck?</h4><ul><li>Its implementation may differ from one framework to another. Thus, knowledge of that specific framework is required.</li><li>There is no clear understanding of an API&#39;s meaning and functionality with any reliable documentation.</li><li>Ambiguity in function naming may lead to confusion. F. Eg. There could be functions delete item() and removeItem() with no shared understanding of their implementation.</li></ul><h4>Ideal application?</h4><ul><li>If you have microservice architecture and if you do a lot to server-to-server calls. RPC is the best option.</li></ul><h3>Representational State Transfer (REST)</h3><blockquote>Unlike RPC, where function is the basic entity, REST paradigm has resource as an entity.</blockquote><p>REST has been a standard widely accepted and used in most of the modern distributed systems for communication. REST encapsulates information as a resource and allows it to perform operations on these resources via various endpoints.</p><p>Let&#39;s see how a REST API looks like in a blog service?</p><pre>GET <a href="https://myserver.com/api">https://myblog.com/api</a></pre><pre>{<br>  &quot;user&quot;: {<br>    &quot;endpoint&quot;: &quot;/user&quot;,<br>    &quot;description&quot;: &quot;Get User APIs&quot;<br>  },<br>  &quot;post&quot;: {<br>    &quot;endpoint&quot;: &quot;/post&quot;,<br>    &quot;description&quot;: &quot;Get the latest posts&quot;<br>  }<br>}</pre><p>As you can see, our entry point helps us navigate through various resources available, their description, and how to access those resources.</p><p>Not just resources, but APIs can contain various operations to be performed on a resource i.e., create and update a user shown in the example below.</p><pre>GET <a href="https://myblog.com/api/user">https://myblog.com/api/user</a></pre><pre>{<br>  &quot;user&quot;: {<br>    &quot;endpoint&quot;: &quot;/&lt;user_id&gt;&quot;,<br>    &quot;description&quot;: &quot;Get user detail&quot;,<br>    &quot;method&quot;: &quot;GET&quot;,<br>    &quot;headers&quot;: {<br>      &quot;Authorization&quot;: &quot;Access Token&quot;<br>    }<br>  },<br>  &quot;create&quot;: {<br>    &quot;endpoint&quot;: &quot;/create&quot;,<br>    &quot;description&quot;: &quot;create user&quot;,<br>    &quot;method&quot;: &quot;POST&quot;,<br>    &quot;value&quot;: {<br>      &quot;name&quot;: &quot;user name&quot;,<br>      &quot;email&quot;: &quot;<a href="/service/mailto:user@email.com">user@email.com</a>&quot;<br>    },<br>    &quot;headers&quot;: {<br>      &quot;Authorization&quot;: &quot;Access Token&quot;<br>    }<br>  },<br>  &quot;update&quot;: {<br>    &quot;endpoint&quot;: &quot;/&lt;user_id&gt;&quot;,<br>    &quot;description&quot;: &quot;update user&quot;,<br>    &quot;method&quot;: &quot;PUT&quot;,<br>    &quot;value&quot;: {<br>      &quot;name&quot;: &quot;updated name&quot;,<br>    },<br>    &quot;headers&quot;: {<br>      &quot;Authorization&quot;: &quot;Access Token&quot;<br>    }<br>  }<br>}</pre><p>REST APIs are stateless, meaning every subsequent request has nothing to do with previous ones.</p><p>The below example shows how pagination could look like in a REST API. Down below, we have post data, and count<strong> </strong>property<strong> </strong>has no. of posts returned. There is a <strong>page </strong>and <strong>next </strong>property in the response just so a user can navigate across various pages easily.</p><pre>GET <a href="https://myblog.com/api/post">https://myblog.com/api/post</a></pre><pre>{<br>  &quot;post&quot;: {<br>    &quot;value&quot;: [<br>        {<br>          &quot;title&quot;: &quot;Sample Post 1&quot;<br>        },<br>        {<br>          &quot;title&quot;: &quot;Sample Post 2&quot;<br>        }<br>      ],<br>      &quot;page&quot;: 1,<br>      &quot;count&quot;: 2,<br>      &quot;next&quot;: &quot;/post?page=2&quot;<br>  }<br>}</pre><p>Notice that this request is independent of other requests that we&#39;ll use to fetch page=2 response and vice versa. This is what stateless means.</p><p>While I tried to explain REST thoroughly, but REST requires a study of its own. I recommend you go through its guidelines and implementation before you implement it. As there is no defined framework, it becomes elementary to stray away from the general principles of REST.</p><p><em>I would also recommend you to go through some of the well-accepted specifications such as OpenAPI, HAL, and ION.</em></p><h4>Why use REST?</h4><ul><li>REST APIs are well designed and only perform atomic operations. Thus, making it easier for the client system to understand the pattern and scope of each API with no external documentation.</li><li>It is stateless, meaning the client maintains the context of the request rather than the server. Offloading tasks like session management and request context from the server.</li><li>Client and server systems can be built independently (loosely coupled systems), unlike in RPC, where clients need to be aware of all exposed functions.</li><li>Multiple version of the same API makes it easier to upgrade an API functionality at the same time, making it backward compatible.</li></ul><h4>Why REST sucks?</h4><ul><li>No defined specification or framework leads to a lot of interpretations.</li><li>Being atomic i.e., one API can only perform one operation, can lead to multiple API hits before fully rendering a particular feature. Increasing server hits and big payloads for the client.</li></ul><h4>Ideal applications?</h4><ul><li>REST is widely used for microservice and client-server architecture because of its loose coupling and easy to maintain traits.</li></ul><p><em>Note: If you find this article somewhat helpful and appreciated my effort, please leave some lovely claps. It&#39;ll inspire me to continue writing articles.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa5527a87483" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Crawl Instagram profiles and posts the most efficient way possible without FB Graph API]]></title>
            <link>https://medium.com/@vasu.pal/crawl-instagram-profiles-and-posts-the-most-efficient-way-possible-without-fb-graph-api-7ea270b11e74?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ea270b11e74</guid>
            <category><![CDATA[crawling]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[influencer-marketing]]></category>
            <category><![CDATA[instagram]]></category>
            <category><![CDATA[facebook]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Sat, 30 May 2020 17:53:56 GMT</pubDate>
            <atom:updated>2021-11-20T07:27:33.333Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jD0NEdZJhTI8MOm693KoSA.png" /></figure><p>In this article, we’re going to look at how you could fetch Instagram profiles and post data without the need to get into FB’s Graph API. Before we begin, let me be clear that this technique only works only on public accounts.</p><p><em>Before we begin, let me give you a context of the problem that leads to this solution. I worked on building a platform that could easily leverage Instagram influencers to connect with brands for paid promotion. What we needed was a database of Influencers. Initially, we thought of running a selenium script to scrape the data out of the Instagram website later to realize that it was both time-consuming and resource-intensive. Next, we thought of using FB Graph API, but that would require us to go through app review &amp; business verification first, which was impossible for the time. So, we needed to figure out an efficient and low-cost way to crawl the Instagram profile. We did a lot of googling here and there, looked at APIs of the Instagram website, and finally found a way to do this, which this article is really about.</em></p><p>So, What we’re going to do here is call some Instagram APIs which are open to all and do not require any token or authentication. So, it’s like browsing a profile, but instead of fancy rendering, you get JSON formatted data.</p><p>I’m going to separate this article into three sections</p><ol><li>getting profile data such as the username, profile image, profile description</li><li>getting post related data</li><li>some limitations of this technique</li></ol><h3>Get Profile Data</h3><p>Well, It’s relatively easy to get profile data. All you need to do is replace the placeholder from the URL below with the Instagram handle of the profile you want to crawl and hit the URL. And that’s it.</p><blockquote><em>https://www.instagram.com/&lt;username_here&gt;/?__a=1</em></blockquote><p>You’ll get a response similar to the one shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kq2Fk0F-3qAH3II7c5jphg.png" /><figcaption>JSON formatted Instagram profile</figcaption></figure><p>Look for <strong><em>edge_owner_to_timeline_media </em></strong>for post related data. Here you’ll only get a maximum of 12 recent posts. Shown below is a snippet of all data you get for an Instagram post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zd__KNqxujz2xAdevpNplQ.png" /><figcaption>Post info inside edge_owner_to_timeline_media.edges</figcaption></figure><h3>Get Post Data</h3><p>The above API gets you 12 recent posts, but if you want more than 12 or all of the posts, it gets tricky because there is a lot more in getting posts of a user than merely replacing the username.</p><p>I had to do a bit of research on the API pattern of Instagram’s main website and how it fetches posts. But I figured it.</p><p>Hit profile API (the one we just discussed). Look for key <strong><em>“id” </em></strong>inside <strong><em>“user” </em></strong>and key “<strong><em>end_cursor</em></strong>” inside “<strong><em>edge_owner_to_timeline_media.page_info”</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t1sQ0IXD9wcYdCj2Z_QI4A.png" /><figcaption>page info inside edge_owner_to_timeline_media</figcaption></figure><p>Suppose the value of “<strong><em>has_next_page” </em></strong>is True. Then, copy the <strong><em>“end_cursor” </em></strong>value and replace it with the <strong>end_cursor_here </strong>placeholder in the URL below without trailing “==”. Also, replace the <strong>instagram_account_id_here </strong>with the user id (which is 7370820917 in my case).</p><blockquote><em>https://www.instagram.com/graphql/query/?query_hash=58b6785bea111c67129decbe6a448951&amp;variables=%7B%22id%22%3A%22</em><strong>&lt;instagram_account_id_here&gt;</strong><em>%22%2C%22first%22%3A50%2C%22after%22%3A%22</em><strong>&lt;end_cursor_here&gt;</strong><em>%3D%3D%22%7D</em></blockquote><p>And your API should look like this.</p><blockquote><a href="https://www.instagram.com/graphql/query/?query_hash=58b6785bea111c67129decbe6a448951&amp;variables=%7B%22id%22%3A%227370820917%22%2C%22first%22%3A50%2C%22after%22%3A%22QVFDVzJXS19YVTJfNlBIdVVmYmU5dzNSVFFSMFRxc09aRzFJcmdqWHkwUWpFSFlOQUlmWmxzMjJPcUNmeWRxbDNrQW03NlM5SG9LR1B6ZlpEcC0tZGU3dw%3D%3D%22%7D">https://www.instagram.com/graphql/query/?query_hash=58b6785bea111c67129decbe6a448951&amp;variables=%7B%22id%22%3A%<strong><em>227370820917</em></strong>%22%2C%22first%22%3A50%2C%22after%22%3A%22<strong><em>QVFDVzJXS19YVTJfNlBIdVVmYmU5dzNSVFFSMFRxc09aRzFJcmdqWHkwUWpFSFlOQUlmWmxzMjJPcUNmeWRxbDNrQW03NlM5SG9LR1B6ZlpEcC0tZGU3dw</em></strong>%3D%3D%22%7D</a></blockquote><p>Hit the API, and you should get a response similar to the one shown below, containing a maximum of 50 posts and page_info to crawl posts further.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9a8MQQG7kLW576pnTRxjhQ.png" /><figcaption>posts info with max 50 posts and page_info</figcaption></figure><p>Typically this response has some extra info that you don’t see in profile API’s post data. Look for the screenshot below to get an idea of the data you can get with this API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62JwGQo_RcHC0AtrM6nofA.png" /><figcaption>Single post</figcaption></figure><p>Now let’s discuss some of the limitations or problems you may bump into using this technique.</p><ol><li><strong>Rate limiting:</strong> Instagram allows a maximum of around 200–300 request hits per hour. After reaching the limit, you’ll start getting the server error.</li><li><strong>No Insight Data: </strong>You cannot get Instagram insight data using this API. Look into <a href="https://developers.facebook.com/docs/instagram-api/guides/insights/"><em>FB Instagram Insight API</em></a>.</li><li><strong>No Private Account Data: </strong>Since this works on publically available APIs, you cannot get private account data.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ea270b11e74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Under The Hood [Garbage Collection]]]></title>
            <link>https://medium.com/analytics-vidhya/python-under-the-hood-garbage-collection-7cc55977c482?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cc55977c482</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[garbage-collection]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Thu, 07 May 2020 17:01:11 GMT</pubDate>
            <atom:updated>2020-05-07T17:01:11.780Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GPhtmRdktXK9Aldvyl02yw.png" /></figure><p>Hi, welcome to the 2nd part of Python: Under The Hood series, in this article, we’ll look at what garbage collection is and how it works in the context of Python language.</p><p>Let’s first talk about <strong><em>What is Garbage Collection?</em></strong> and <strong><em>What does it do?</em></strong></p><blockquote>Garbage collection is a technique in computer science, which is used to clear up memory occupied by the objects that are no longer required by the program by removing them from the memory.</blockquote><p>All HLL programming languages implement garbage collection internally as the core of the language so that developers don’t have a deal with managing memory allocation and de-allocation explicitly.</p><p>Now, coming to question <strong><em>What does it do?</em></strong></p><p>Majorly it does two things:</p><ol><li>Memory clean up</li><li>Heap fragmentation</li></ol><p>Okay, I understand memory clean up but <strong><em>what is heap fragmentation?</em></strong></p><p>Programs in python require contiguous memory blocks, and when an object is removed during the execution, a specific memory block gets freed and ready to be occupied by another object.</p><p>The only issue here is that not every object in python requires a single memory block. Some might take up multiple memory blocks such as a list in python which requires contiguous memory blocks.</p><p>So, what heap fragmentation does is,</p><blockquote>It aggregates all the free blocks inside memory to form a contiguous memory block.</blockquote><p>Let’s understand this with an example</p><p>Say, our program during execution has a memory structure like this.</p><figure><img alt="Heap memory representation" src="https://cdn-images-1.medium.com/max/154/1*SpNhsbM_FcFGIuE7gR9SSg.jpeg" /><figcaption>Heap memory representation</figcaption></figure><p>Let’s say our garbage collector has removed the 3rd object (300) from the memory and then, we try to create a list object containing 3 integer values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*Z-D9pgSCc7LH4SdJDqJSxA.jpeg" /><figcaption>Cannot add list due to unavailability of contiguous memory</figcaption></figure><p>Even though there are 3 blocks available to contain 3 integer objects but since the list requires contiguous memory blocks and only 2 blocks are available thus this operation cannot be performed on that specific memory block.</p><p>Clearly, this isn’t the most efficient way to manage memory. Thus, here comes heap fragmentation to the rescue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*om3Y42eNE4Ko3Uo2CtRHEw.jpeg" /><figcaption>add list operation after fragmentation</figcaption></figure><p>GC after deleting the object performs fragmentation and puts the free memory blocks contiguously. Thus allowing the “add” list operation to be performed.</p><p>Cool! Now let’s talk about <strong><em>how python implements garbage collection?</em></strong>.</p><p>Though are multiple garbage collection techniques or garbage collectors GCs. You can take a look at these types <a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Strategies">here</a>.</p><p>Interestingly, Python uses 2 garbage collectors named Reference Garbage Collector and Generational Garbage Collector.</p><h4>Reference Garbage Collector</h4><p>Every PyObject in python maintains a RefCount which counts the number of references it has i.e how many variables are pointing to that object.</p><p><em>In my first article, I discussed how RefCount changes during the program execution state.</em></p><p>Reference GC is the main garbage collection mechanism in python, and there is no way to disable or replace the implementation. However, we can use getrefcount(&lt;variable&gt;) from sys module to count the number of references an object holds.</p><pre>import sys<br>a = &quot;xyz&quot;<br>sys.getrefcount(a)<br>&gt;&gt;2</pre><p><em>Notice that there are two references. One is from creating the variable at line 2 and second is when we pass the variable “</em><strong><em>a”</em></strong><em> to the </em><strong><em>sys.getrefcount()</em></strong><em> function at line 3.</em></p><p>Despite having such robust implementation, it holds some drawbacks as well.</p><ul><li><strong>It does not detect cyclical references.</strong></li></ul><p>Cyclical references occur when two object references each other. Thus, the RefCount of these objects never reaches Zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*6nhMaQcQyPEtwhsSPNcP3w.jpeg" /><figcaption>Cyclical references between objects</figcaption></figure><ul><li><strong>It’s not thread-safe.</strong></li></ul><p>It is plausible in a multi-threaded environment that two threads can access/modify the same object simultaneously. And Reference GC has no mechanism to deal with it.</p><h4>Generational Garbage Collector</h4><p>The key concept behind generational GC is that most of the new objects become unreachable quickly. Thus, we can separate the objects based on their life and allocate in different generations.</p><blockquote>Python has 3 generation of objects. One object residing in only one generation at a time.</blockquote><p>During the execution, whenever an interpreter encounters a new object, it puts the object into generation-0.</p><p>For each generation, the garbage collector holds a limited number of objects i.e the threshold. When the number of objects exceeds that threshold, the garbage collection process is executed. If an object survives this process, the object moves up into the next generation (or stays in the same generation in the case of generation-3 objects).</p><p>Unlike reference GC, you can modify the generational GC behavior such as to modify the threshold, manually trigger the garbage collection process, or disable it altogether. Click <a href="https://docs.python.org/3/library/gc.html">here</a> to check out the GC docs to know more.</p><p>Alright! but <strong>does it solve</strong> <strong>the issue of the cyclical reference?</strong></p><blockquote>Yes, Generational GC keeps track of the object references across generations by observing the creation and overwriting of references and is able to detect objects that are unreachable without having to traverse the entire reference tree.</blockquote><p>And <strong>what about the threading issue?</strong></p><p>Well, Python solves this issue by dealing with multi-threading in a very interesting way. Python implements a mechanism called Global Interpreter Lock or GIL.</p><p>In the most basic sense,</p><blockquote>GIL allows only one thread that can be in a state of execution at any point in time.</blockquote><p>Thus, no need for a thread-safe environment.</p><p>We’ll learn more about GIL is my next article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cc55977c482" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/python-under-the-hood-garbage-collection-7cc55977c482">Python: Under The Hood [Garbage Collection]</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Under the hood [Memory Allocation & Management]]]></title>
            <link>https://medium.com/analytics-vidhya/python-under-the-hood-draft-2-7191d4da48aa?source=rss-c650134b1fa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/7191d4da48aa</guid>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[memory-allocation]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Vasu Pal]]></dc:creator>
            <pubDate>Sat, 02 May 2020 09:28:43 GMT</pubDate>
            <atom:updated>2020-10-02T14:22:34.035Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yEe9IbjHWD496KHqDVKuCg.jpeg" /></figure><p>Hi, in this article, We’ll be learning about some core concepts of python without getting into coding.</p><p>Firstly, why do we even care about under the hood stuffs when they are just meant to be “under the hood,” you may ask, right? Well, being a developer, it’s useful to know how a programming language works. As it could unravel the mystery of magical stuff happening in the background you don’t know of.</p><p>And for some reason, if your consciousness is telling you, that this is going to be challenging. Trust me it’s not. These are relatively simple concepts, just like the Python language itself.</p><p>Here is the list of the topics we are going to cover in this article series.</p><ol><li>Memory Allocation &amp; Management in Python</li><li>Garbage Collection</li><li>GIL (Global Interpreter Lock)</li></ol><h4>Memory Allocation &amp; Management in Python</h4><p>Knowing about memory management may help you write efficient code. Even though you may not have control over memory allocation, but you can optimize your programs to allocate it better.</p><p>Before we dig deep, remember this:</p><blockquote>In python, everything is object.</blockquote><p>Languages like C, C++, or Java store the value in memory, and there’s a variable that points to that memory location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MOHcGNwodrw6YxLvog9NdA.png" /><figcaption>Memory Allocation in C</figcaption></figure><p>Whereas In python, an entire object is stored in the memory (heap memory); the object could be integer, string, or a list, and variable pointing to an object. Confusing. Right?</p><p>To make sense, let’s understand what does a python object holds!</p><p>A PyObject in memory holds</p><ol><li><em>Type: integer, string, float, etc</em></li><li><em>Ref Count: number of references bound to that object</em></li><li><em>Value: value/data/information</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*O7jFdcJNRiP43MYExh-VDw.png" /><figcaption>PyObject (a=200)</figcaption></figure><p>So, whenever you create a variable, let’s say <strong><em>a = 200</em></strong>, a new PyObject is created in the memory, and its ref count is set to 1, and variable “<strong><em>a”</em></strong> points to it.</p><p>Okay. <strong>But what is ref count?</strong></p><p>Let’s take an example to understand it.</p><p>We have a variable “<strong><em>a”</em></strong> of type integer with value of 200. Let’s say I need to have another variable named “<strong><em>b”</em></strong> of type integer with a value of 200.</p><p>So, you’ve created two variables like this.</p><blockquote>a=200</blockquote><blockquote>b=200</blockquote><p>Now, you might be guessing; there must be two objects in the memory for variable “<strong><em>a”</em></strong> and “<strong><em>b”</em></strong>. But that’s not the case. Both “<strong><em>a”</em></strong> and “<strong><em>b”</em></strong> points to the same object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PgyDkzIVMNJeGIi93ueYAw.jpeg" /><figcaption>Variable a and b referencing to the same PyObject</figcaption></figure><p>Let’s validate this through code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YVtUGjIengkVj41xT-kgYQ.png" /></figure><p>As you can see, variable “<strong><em>a”</em></strong> and “<strong><em>b”</em></strong> have the same id (memory location) thus, representing the same object, while c has a different value of the id. Therefore, currently in memory, there two python objects.</p><p>So, what will happen if we assign a new value to “<strong><em>a”</em></strong> i.e., <strong><em>a = 3</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4QCy0R6hKAMP8smI8APUAA.png" /></figure><p><strong><em>“a”</em></strong> now points to a new object, but “<strong><em>b”</em></strong> stills points to the same object.</p><p>But let’s consider this example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pWgepWPa8Twz5zfhrXC1aw.png" /></figure><p>In this example, <strong><em>a =1</em></strong> but “<strong><em>b”</em></strong> has the value of “<strong><em>a”</em></strong>. When we change the value of “<strong><em>a”</em></strong>, is it going to affect “<strong><em>b”</em></strong> too? Let’s see.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9ZuCSIe795W51YWn--UliQ.png" /></figure><p><strong><em>“a”</em></strong> now points to a new object, but “<strong><em>b”</em></strong> still points to the older object. Quite Surprising.<strong> But why?</strong></p><p>Because “<strong><em>b”</em></strong> does not directly point to the variable “<strong><em>a”</em></strong> but the object of variable “<strong><em>a”</em></strong>. That’s what ref count does. It keeps track of the number of variables pointing to it.</p><p>Another intriguing question would be, <strong>What would happen if the ref count of a PyObject becomes zero?</strong></p><p>Before that, let’s find out how we can remove references to an object.</p><ol><li><strong>del</strong></li></ol><p><em>Python’s built-in del keyword helps us remove these references pointing to an object. </em>In fact, some people think that del removes the object from memory, which is not the case.</p><p>So, you could use del like this to remove a reference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PjF_bv2hk9DnM813pHOe5g.png" /><figcaption>Remove reference to an object</figcaption></figure><p>2. <strong>Going out of scope</strong></p><p>The reference to an object gets automatically deleted once it goes out of scope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RBXwADWmqpM3eQsB8cfccQ.png" /></figure><p>Let’s take the above code as an example, the object of variable “<strong><em>x”</em></strong> will have +1 ref count inside the scope() function, but as the interpreter leaves this function scope, the ref count of the PyObject reduce to -1 because “<strong><em>x” </em></strong>is a local variable and only have scope to its function.</p><p>And this is one of the reasons why python discourages using global variables since variables in global scope stick to the entirety of the program.</p><p><strong>3. Assigning a new object to a variable.</strong></p><p>When assigning a new object to an already existing variable, the ref count of the previous object reduces to -1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F5Mp2XnSuLqIaCYUIWgDfQ.png" /><figcaption>Reducing ref count of an object by assigning a new object</figcaption></figure><p>Now, coming back to the previous question, <strong><em>What happens when the ref count of an object is 0. Does it stay in memory?</em></strong></p><p>Well, the answer is No.</p><blockquote>As soon as ref count of an object turns to 0. It gets erased from the memory by Garbage Collector. You can think of garbage collector as a cleaner, which cleans all the leftovers and waste items which won’t be used in the future and this technique is called “Garbage Collection”.</blockquote><p>We’ll be discussing garbage collection in python in more detail in the next article.</p><p><em>Note: If you have any doubts or suggestions, I’ll be happy to reply through the comments, and I’ll also be needing those lovely claps to boost myself to write more articles, so please be sure to do that.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7191d4da48aa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/python-under-the-hood-draft-2-7191d4da48aa">Python: Under the hood [Memory Allocation &amp; Management]</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>