<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Masoud Golestaneh</title>
    <description>Masoud's Personal Website</description>
    <link>https://cypherspark.github.io/</link>
    <atom:link href="https://cypherspark.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 03 Dec 2023 12:00:05 +0000</pubDate>
    <lastBuildDate>Sun, 03 Dec 2023 12:00:05 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>PostgreSQL HA using Patroni</title>
        <description>&lt;h1 id=&quot;overcoming-challenges-with-high-availability-postgresql-using-patroni&quot;&gt;Overcoming Challenges with High-Availability PostgreSQL Using Patroni&lt;/h1&gt;

&lt;p&gt;Welcome to the latest post on my academic blog, where today, we’re diving into the world of databases—specifically, achieving high availability (HA) with PostgreSQL using Patroni.&lt;/p&gt;

&lt;p&gt;High availability is critical for modern applications, as downtime can be costly and damaging to a business’s reputation. PostgreSQL, being an open-source relational database, is widely recognized for its robust features, reliability, and flexibility. However, ensuring that it remains available and resilient in the face of hardware failures, maintenance, or unexpected surges in traffic poses a set of challenges.&lt;/p&gt;

&lt;p&gt;Enter Patroni, a template for you to create your customized, high-availability solution using PostgreSQL. It leverages the capabilities of etcd, Consul, or ZooKeeper for consensus and coordination between database instances.&lt;/p&gt;

&lt;h2 id=&quot;the-ha-challenges-with-postgresql-and-how-patroni-addresses-them&quot;&gt;The HA Challenges with PostgreSQL and How Patroni Addresses Them:&lt;/h2&gt;

&lt;h3 id=&quot;challenge-1-failover-and-leader-election&quot;&gt;Challenge 1: Failover and Leader Election&lt;/h3&gt;
&lt;p&gt;One of the most significant challenges with any HA system is managing failover—the process where a standby server takes over when the primary server fails. Patroni automates this process and handles leader election, ensuring that there’s always a designated primary node ready to serve client requests.&lt;/p&gt;

&lt;h3 id=&quot;challenge-2-replication-management&quot;&gt;Challenge 2: Replication Management&lt;/h3&gt;
&lt;p&gt;Maintaining consistent data across multiple database instances is essential. Patroni takes charge of setting up and managing replication between the primary and standby nodes in PostgreSQL, reducing the complexity of keeping these processes synced.&lt;/p&gt;

&lt;h3 id=&quot;challenge-3-configuration-and-orchestration&quot;&gt;Challenge 3: Configuration and Orchestration&lt;/h3&gt;
&lt;p&gt;When working with a cluster of PostgreSQL servers, managing configurations across nodes can be daunting. Patroni’s use of a distributed configuration store simplifies managing settings and state across all PostgreSQL instances, making it easier to orchestrate changes and updates.&lt;/p&gt;

&lt;h3 id=&quot;challenge-4-automated-failback-and-switchover&quot;&gt;Challenge 4: Automated Failback and Switchover&lt;/h3&gt;
&lt;p&gt;Once a failed primary Node is back online, reintegrating it into the cluster should be smooth and automatic. Patroni’s automation covers the switchover process as well, ensuring that nodes can be switched painlessly and the system can recover without human intervention.&lt;/p&gt;

&lt;h3 id=&quot;challenge-5-downtime-during-maintenance&quot;&gt;Challenge 5: Downtime during Maintenance&lt;/h3&gt;
&lt;p&gt;Scheduled maintenance should not result in significant downtime. Patroni’s capability to perform scheduled failovers to standby nodes means maintenance can occur with minimal impact on the application’s availability.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;Creating a high-availability environment with PostgreSQL involves meeting the challenges of failover, replication, configuration management, and automated recovery. Patroni emerges as a powerful ally, offering PostgreSQL users a way to handle these obstacles with grace and efficiency.&lt;/p&gt;

&lt;p&gt;However, setting up and tuning Patroni to suit specific needs is a task that requires a deep understanding of both PostgreSQL and the environment it operates within. As database systems grow in complexity, the role of solutions like Patroni continues to become more integral in the quest for maintaining uninterrupted service.&lt;/p&gt;

&lt;p&gt;The journey to achieving high availability is continuous, requiring constant vigilance and adaptation to new situations. But with tools like Patroni and the community’s collective knowledge, we are now better equipped than ever to keep our systems robust and resilient.&lt;/p&gt;

&lt;p&gt;If you’re looking to implement HA PostgreSQL with Patroni in your infrastructure or are navigating the challenges that come with such a setup, I hope this post sheds light on the path to persistent availability. Stay tuned for more deep dives into database technologies and solutions!&lt;/p&gt;

&lt;h2 id=&quot;until-next-time-keep-your-data-secure-and-your-servers-running&quot;&gt;Until next time, keep your data secure and your servers running!&lt;/h2&gt;
</description>
        <pubDate>Mon, 21 Dec 2020 00:00:00 +0000</pubDate>
        <link>https://cypherspark.github.io/database/2020/12/21/test-code-snippets.html</link>
        <guid isPermaLink="true">https://cypherspark.github.io/database/2020/12/21/test-code-snippets.html</guid>
        
        <category>Postgresql</category>
        
        <category>Patroni</category>
        
        
        <category>Database</category>
        
      </item>
    
      <item>
        <title>Understanding Pass by Reference vs. Pass by Value in Python</title>
        <description>&lt;h1 id=&quot;understanding-pass-by-reference-vs-pass-by-value-in-python&quot;&gt;Understanding Pass by Reference vs. Pass by Value in Python&lt;/h1&gt;

&lt;p&gt;Programming languages often differ in how they handle the passing of arguments to functions. The terms commonly used to describe this behavior are “pass by reference” and “pass by value.” In this blog post, we’ll explore these concepts with code examples and clarify a common misconception: Python’s argument passing mechanism is actually “pass by assignment.”&lt;/p&gt;

&lt;h2 id=&quot;pass-by-value&quot;&gt;Pass by Value&lt;/h2&gt;

&lt;p&gt;In languages that use pass by value, a copy of the argument’s value is passed to the function. Changes made to this copy do not affect the original variable. Let’s simulate what this would look like if Python used pass by value:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;modify_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Inside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;modify_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Outside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Output:
Inside function: 10
Outside function: 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If Python were to use pass by value, the modify_value function would not be able to alter the original x. However, Python does not behave in this way—as we will see next.&lt;/p&gt;

&lt;h2 id=&quot;pass-by-reference&quot;&gt;Pass by Reference&lt;/h2&gt;
&lt;p&gt;In pass by reference, a reference to the actual argument is passed to the function. Any changes made in the function affect the argument outside the function. In languages that use pass by reference, we would see something like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;modify_reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Inside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;modify_reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Outside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Output:
Inside function: [1, 2, 3, 4]
Outside function: [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In pass by reference, changes inside the function affect the original list. Now, let’s see how Python actually handles these situations.&lt;/p&gt;

&lt;h2 id=&quot;pass-by-assignment-in-python&quot;&gt;Pass by Assignment in Python&lt;/h2&gt;
&lt;p&gt;Python uses a mechanism known as “pass by assignment.” Variables in Python are names that point to objects, and when you pass an argument to a function, a new name (the parameter) is assigned to the original object. If that object is mutable (like a list), mutations will reflect in both the original and local names. Immutable types (like integers) seem to act as pass by value because reassigning within the function creates a new object.&lt;/p&gt;

&lt;p&gt;Here’s an example with a mutable object (list):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;append_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Inside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;append_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Outside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Output:
Inside function: [1, 2, 3, 4]
Outside function: [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And here’s an example with an immutable object (integer):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Inside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Outside function:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Output:
Inside function: 2
Outside function: 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In both scenarios, Python “passes by assignment.” The list example reflects changes outside the function because lists are mutable. The integer example does not because integers are immutable, and n += 1 creates a new integer object inside the function.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;Python’s pass by assignment is an elegant approach that simplifies how we reason about argument passing. Whether dealing with mutable or immutable objects, understanding this behavior allows Python programmers to write functions that behave predictably and avoid unintended side effects.&lt;/p&gt;

&lt;p&gt;It’s crucial to recognize the type of object you’re dealing with and how changes to it inside a function can affect its state outside the function. With this knowledge, you can manipulate Python’s object model to achieve your desired outcomes in a clear and Pythonic way.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;hr /&gt;
</description>
        <pubDate>Tue, 14 May 2019 00:00:00 +0000</pubDate>
        <link>https://cypherspark.github.io/python/2019/05/14/python.html</link>
        <guid isPermaLink="true">https://cypherspark.github.io/python/2019/05/14/python.html</guid>
        
        <category>Python</category>
        
        <category>Programming</category>
        
        
        <category>Python</category>
        
      </item>
    
  </channel>
</rss>
