<?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>Forem: Anwar</title>
    <description>The latest articles on Forem by Anwar (@anwaar).</description>
    <link>https://forem.com/anwaar</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%2F1919465%2F186e092f-b773-4f54-9bc7-552d429677a3.png</url>
      <title>Forem: Anwar</title>
      <link>https://forem.com/anwaar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anwaar"/>
    <language>en</language>
    <item>
      <title>Agentic AI: Governance, Guardrails and Security</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Mon, 06 Apr 2026 05:00:00 +0000</pubDate>
      <link>https://forem.com/anwaar/agentic-ai-governance-guardrails-and-security-57h0</link>
      <guid>https://forem.com/anwaar/agentic-ai-governance-guardrails-and-security-57h0</guid>
      <description>&lt;p&gt;If a standard Chatbot like ChatGPT is like a high-end GPS giving you directions and info, an &lt;code&gt;Agentic AI&lt;/code&gt; is the self-driving car that actually turns the wheel. It doesn’t just tell you how to book a flight; it logs into your corporate card, navigates the portal, and buys the ticket. Giving AI the keys without a license, a seatbelt, or a map leads to expensive and sometimes irreversible failures.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Rouge Agentic AI: Real Stories without Safeguards&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.envive.ai/post/case-study-of-air-canadas-chatbot" rel="noopener noreferrer"&gt;Air Canada Chatbot (2022)&lt;/a&gt;&lt;/strong&gt; : A chatbot &lt;code&gt;hallucinated&lt;/code&gt; a bereavement refund policy that didn’t exist leading to financial and legal consequences. &lt;em&gt;Air Canada argued in court that the chatbot was a "separate legal entity" responsible for its own actions. The British Columbia Civil Resolution Tribunal rejected this, forcing the airline to honor the agent's "offer" and pay damages, So AI outputs can become legally binding statements&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://fortune.com/2025/07/23/ai-coding-tool-replit-wiped-database-called-it-a-catastrophic-failure/" rel="noopener noreferrer"&gt;Replit AI Incident (2025)&lt;/a&gt;&lt;/strong&gt;
An AI coding agent ignored a code freeze instruction and deleted a production database. &lt;em&gt;Replit’s CEO publicly &lt;code&gt;apologized&lt;/code&gt; and had to implement stronger &lt;code&gt;separation between dev/production environments&lt;/code&gt; and better enforcement of instructions&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.wiz.io/blog/exposed-moltbook-database-reveals-millions-of-api-keys" rel="noopener noreferrer"&gt;Moltbook Exposure (2026)&lt;/a&gt;&lt;/strong&gt;: A misconfigured AI-agent platform exposed millions of API keys, enabling &lt;code&gt;prompt injection&lt;/code&gt; attacks and data theft. &lt;em&gt;Autonomous systems amplify security failures at scale.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;These are few incidents which demonstrate how agentic systems lacking guardrails, governance, and security can cause rapid, autonomous harm, turning powerful tools into vectors for destruction or fraud.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Three Foundations to build Safe Agentic AI Platforms&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Governance&lt;/strong&gt; is your constitution. It defines the boundaries of autonomy. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guardrails&lt;/strong&gt; enforce behavior. It ensures the agent behaves predictably even in unpredictable scenarios.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt; restricts capability. It ensures that even if something goes wrong, the blast radius is limited.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;If any one of these fails, it creates unbounded risk.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;I. Governance: The Constitution / Policy Layer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It’s the "who, what, and why" of your AI strategy. It also defines the scope of agency and the human-agent social contract. Before building anything, define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Human-in-the-Loop&lt;/strong&gt;: What actions are safe to automate, and what requires approval? For instance, an agent may triage 1000 emails but requires a human approval for any transaction exceeding a specific value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Audit Trails&lt;/strong&gt;: In an agentic workflow, &lt;code&gt;I don't know why it did that&lt;/code&gt; is an unacceptable answer. Governance ensures a persistent audit trail capturing the prompt, the model’s reasoning, decisions, tool it used and the tool output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Accountability&lt;/strong&gt;: Defining who is legally and professionally responsible when the agent makes a mistake. Governance aligns AI behavior with corporate, legal and brand standards.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;II. Guardrails: Seatbelts / Constraint Layer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Guardrails are the "Operational Layer." They are the real-time enforcement mechanisms that prevent an agent from drifting outside its intended behavioral "latent space".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Semantic Filtering / Input Guardrails&lt;/strong&gt;: Detect &lt;code&gt;Prompt Injections&lt;/code&gt; or malicious intent (e.g., a user trying to trick the AI into ignoring its rules) before they reach the Agent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deterministic Validation / Output Guardrails&lt;/strong&gt;: Prevent the AI from &lt;code&gt;hallucinating&lt;/code&gt; facts, leaking sensitive data, or using toxic language. Checking AI generated code or API calls against a set of hard rules (e.g., ensuring a SQL query doesn't contain a DROP TABLE command) before execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Action Guardrails&lt;/strong&gt;: They prevent an agent from executing a command that looks suspicious like &lt;code&gt;Delete All Records&lt;/code&gt;, &lt;code&gt;Execute unauthorized, non-compliant trade&lt;/code&gt; even if the AI thinks it's a good idea.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tool Abuse Protection&lt;/strong&gt;: Prevent agents from chaining tools in unsafe ways.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;III. Security: The Infrastructure Layer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Security is the "Hardened Layer." It treats the AI Agent as a &lt;code&gt;Privileged User&lt;/code&gt; and applies the principles of Zero Trust.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Least Privilege&lt;/strong&gt;: An agent should not have &lt;code&gt;God Mode&lt;/code&gt; access to the enterprise. The agent should only access what it absolutely needs nothing more.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sandboxing&lt;/strong&gt;: Run agentic actions especially code execution within isolated environments to contain failures. If an agent is compromised, the &lt;code&gt;blast radius&lt;/code&gt; is confined to that single container, protecting the broader corporate network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Egress Control&lt;/strong&gt;: Hardening the network, ensuring the AI can only talk to approved websites/resources/tools. Restrict where the agent can send data, preventing exfiltration.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why You Must Set This Up Before You Build&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Building an agentic platform without these foundations is like building a skyscraper without a foundation. Organizations that skip this step face:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Financial Loss&lt;/strong&gt;: From &lt;code&gt;unauthorized&lt;/code&gt; or &lt;code&gt;hallucinated&lt;/code&gt; transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reputational Damage&lt;/strong&gt;: From agents making &lt;code&gt;legally binding promises&lt;/code&gt; that the company can't keep.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security Breaches&lt;/strong&gt;: From over-permissioned agents being &lt;code&gt;manipulated by external attackers&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Agentic systems don’t operate in clean, isolated environments. Like humans, They operate in the real world i.e. (messy, ambiguous, and adversarial).&lt;/p&gt;

&lt;p&gt;The future of AI isn’t defined by how much it can do, but by how safely and reliably it can do it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Sources and Further Reading
&lt;/h3&gt;

&lt;p&gt;For more insights and detailed overview, please explore the following resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.protecto.ai/blog/ai-agents-excessive-agency-risks/" rel="noopener noreferrer"&gt;https://www.protecto.ai/blog/ai-agents-excessive-agency-risks/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.obsidiansecurity.com/blog/security-for-ai-agents" rel="noopener noreferrer"&gt;https://www.obsidiansecurity.com/blog/security-for-ai-agents&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.swept.ai/post/ai-customer-service-hallucinations-prevention-guide" rel="noopener noreferrer"&gt;https://www.swept.ai/post/ai-customer-service-hallucinations-prevention-guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fortune.com/2025/07/23/ai-coding-tool-replit-wiped-database-called-it-a-catastrophic-failure/" rel="noopener noreferrer"&gt;https://fortune.com/2025/07/23/ai-coding-tool-replit-wiped-database-called-it-a-catastrophic-failure/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.envive.ai/post/case-study-of-air-canadas-chatbot" rel="noopener noreferrer"&gt;https://www.envive.ai/post/case-study-of-air-canadas-chatbot&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>cloud</category>
      <category>security</category>
      <category>cybersecurity</category>
    </item>
    <item>
      <title>AWS Incident Response: SecurityAudit Access</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Mon, 30 Mar 2026 12:30:00 +0000</pubDate>
      <link>https://forem.com/anwaar/aws-incident-response-securityaudit-access-dcb</link>
      <guid>https://forem.com/anwaar/aws-incident-response-securityaudit-access-dcb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;In the heat of a security incident, your team needs eyes everywhere. Without the right visibility into permissions, logs, and account activity, teams may struggle to confirm what happened, limit damage, and recover with confidence. That’s exactly where AWS’s &lt;code&gt;SecurityAudit&lt;/code&gt; managed IAM policy shines. It’s the read-only &lt;code&gt;detective badge&lt;/code&gt; that gives your Incident Response (IR) team instant, safe visibility into your entire AWS environment without letting anyone touch a single configuration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is SecurityAudit?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/aws-managed-policy/latest/reference/SecurityAudit.html" rel="noopener noreferrer"&gt;SecurityAudit&lt;/a&gt; is an official AWS-managed policy (ARN: &lt;code&gt;arn:aws:iam::aws:policy/SecurityAudit&lt;/code&gt;) that delivers broad read-only access to security configuration metadata across dozens of services. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IAM configurations&lt;/li&gt;
&lt;li&gt;CloudTrail logs&lt;/li&gt;
&lt;li&gt;GuardDuty findings&lt;/li&gt;
&lt;li&gt;Security Hub insights&lt;/li&gt;
&lt;li&gt;AWS Config rules&lt;/li&gt;
&lt;li&gt;Inspector scan results&lt;/li&gt;
&lt;li&gt;S3, EC2, KMS metadata&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key principle is simple: Auditing and investigation &amp;amp; no Create, Update, Delete, or Put actions allowed. Just &lt;code&gt;Get*&lt;/code&gt;, &lt;code&gt;List*&lt;/code&gt;, and &lt;code&gt;Describe*&lt;/code&gt; everywhere you need them.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use SecurityAudit for Incident Response?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Principle of Least Privilege&lt;/strong&gt;: During an active incident, the last thing you want is a responder accidentally (or a compromised account intentionally) viewing sensitive PII in an S3 bucket. SecurityAudit strips away data-plane access while keeping the control-plane visible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Built-in compliance&lt;/strong&gt;: Perfect for &lt;code&gt;PCI-DSS&lt;/code&gt;, &lt;code&gt;ISO 27001&lt;/code&gt;, &lt;code&gt;CIS Benchmarks&lt;/code&gt;, or &lt;code&gt;SOC 2&lt;/code&gt; audits. Auditors love it because it’s auditable and least-privileged by design.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Audit logs &amp;amp; Evidence&lt;/strong&gt;: It’s about understanding what happened and why. &lt;code&gt;SecurityAudit&lt;/code&gt; gives you Security group configurations, Logging status, Policy structures, and timeline reconstruction. This is why it's the core of root cause analysis and forensic investigation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lightning-fast visibility&lt;/strong&gt;: One role = instant access to the full security picture. No hunting for individual permissions or waiting for admin approvals.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Use Cases That Save the Day&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Breach Investigation&lt;/strong&gt;: Attacker in your environment? Your IR team immediately runs &lt;code&gt;iam:GetAccountAuthorizationDetails&lt;/code&gt; and &lt;code&gt;cloudtrail:LookupEvents&lt;/code&gt; to see exactly which roles were compromised and what they touched—without touching anything themselves.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Misconfiguration Triage&lt;/strong&gt;: GuardDuty screams about suspicious S3 access? The team checks bucket policies, ACLs, and public access blocks in seconds, then cross-references with Security Hub and Config for context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insider Threat or Credential Abuse&lt;/strong&gt;: Suspect a compromised access key? Pull IAM user policies, last-used timestamps, and Access Analyzer findings instantly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Third-Party or MSSP Collaboration&lt;/strong&gt;: Give external responders temporary assume-role access to the &lt;code&gt;SecurityAudit&lt;/code&gt; &amp;amp; no need to grant them broader permissions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Make the &lt;strong&gt;SecurityAudit&lt;/strong&gt; IAM role a core part of your AWS incident response playbook. In a world where attackers move at cloud speed, ensuring your team can see everything without touching anything is no longer optional; it’s a competitive advantage in cybersecurity resilience. Your incident response team deserves the clearest possible view during chaos. &lt;strong&gt;SecurityAudit&lt;/strong&gt; gives them exactly that and your team will thank you during the next breach hunt.&lt;/p&gt;

</description>
      <category>security</category>
      <category>cybersecurity</category>
      <category>aws</category>
      <category>cloud</category>
    </item>
    <item>
      <title>AWS Incident Response: ReadOnly vs ViewOnly access</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Sun, 22 Mar 2026 20:57:13 +0000</pubDate>
      <link>https://forem.com/anwaar/aws-incident-response-readonly-vs-viewonly-access-37il</link>
      <guid>https://forem.com/anwaar/aws-incident-response-readonly-vs-viewonly-access-37il</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;TL;DR&lt;/strong&gt;:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;ViewOnlyAccess&lt;/strong&gt;: You can see the infrastructure (settings/tags) but not the data (files/records). It is useful for high-level visibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ReadOnlyAccess&lt;/strong&gt;: You can see the infrastructure and the &lt;strong&gt;data&lt;/strong&gt;, which is essential for &lt;strong&gt;deep investigation&lt;/strong&gt;, &lt;strong&gt;forensic analysis and evidence&lt;/strong&gt;. It also supports &lt;strong&gt;CLI-driven IR&lt;/strong&gt; which wins hands-down on usability and speed.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Imagine you are the Lead Incident Responder for a fintech company. At 2:00 AM, your GuardDuty alerts scream: &lt;code&gt;An unauthorized IP address is listing objects in your "Customer-Tax-Records" S3 bucket&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "&lt;strong&gt;ViewOnly&lt;/strong&gt;" Fail
&lt;/h3&gt;

&lt;p&gt;Your junior analyst logs in with &lt;code&gt;ViewOnlyAccess&lt;/code&gt;. They can see the bucket exists. They see the encryption is turned on (AES-256). They see the bucket policy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;: They try to check if the sensitive PDF files inside the bucket have been modified or if a &lt;code&gt;Canary file&lt;/code&gt; has been tripped.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Result&lt;/strong&gt;: Access Denied. Because they only have &lt;code&gt;View&lt;/code&gt; permissions, they can't see the content of the bucket. They are essentially a detective standing outside a house with a thermal camera; they know people are inside, but they can't see what they’re doing with the jewelry.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The "&lt;strong&gt;ReadOnly&lt;/strong&gt;" Save
&lt;/h3&gt;

&lt;p&gt;You quickly swap to a &lt;code&gt;ReadOnlyAccess&lt;/code&gt; role.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Action:&lt;/strong&gt; You can now see the individual objects. You notice a 5GB .zip file created 10 minutes ago that shouldn't be there.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Forensic Win&lt;/strong&gt;: Because you have &lt;code&gt;s3:GetObject&lt;/code&gt;, you can pull the metadata of that specific zip file to see the &lt;code&gt;Last Modified&lt;/code&gt; timestamp and the IAM User/Role that created it. You realize the attacker didn't just look at the data they staged it for exfiltration.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1. The Core Differences
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;AWS ViewOnlyAccess&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Think of this as &lt;em&gt;Look but don't touch (and don't look at the data)&lt;/em&gt;.&lt;br&gt;
This policy allows a user to see the list of resources and their configurations (metadata) but explicitly denies access to the actual data stored within those resources.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Can do:&lt;/strong&gt; See that an S3 bucket exists, see its tags, and see its encryption settings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cannot do:&lt;/strong&gt; Read the objects inside the S3 bucket or look at the contents of a DynamoDB table.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;AWS ReadOnlyAccess&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Think of this as &lt;em&gt;Look at everything, including the data.&lt;/em&gt;&lt;br&gt;
This is a much broader permission set. It allows the user to see the metadata and read the underlying data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Can do:&lt;/strong&gt; List the S3 bucket and download the files inside it to check for malware or unauthorized data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cannot do:&lt;/strong&gt; Delete, create, or modify resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. CLI: Command-driven investigation support
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;When using the AWS CLI for Incident Response or audits, the difference between ReadOnlyAccess and ViewOnlyAccess becomes very practical.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;CLI Command&lt;/th&gt;
&lt;th&gt;ViewOnlyAccess&lt;/th&gt;
&lt;th&gt;ReadOnlyAccess&lt;/th&gt;
&lt;th&gt;Why It Matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;aws s3api get-object&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;ViewOnly cannot download or read actual file content (bytes)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;aws cloudtrail lookup-events&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;ViewOnly can see the trail exists but cannot read log data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  3. Which is needed for Incident Response?
&lt;/h2&gt;

&lt;p&gt;For a primary Incident Responder, you almost always need &lt;code&gt;ReadOnlyAccess&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Evidence Collection&lt;/strong&gt;: To determine if data was exfiltrated or tampered with, you need to see the data itself. If you suspect a database has been injected with malicious code, &lt;code&gt;ViewOnlyAccess&lt;/code&gt; won't let you see the records to confirm it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log Analysis&lt;/strong&gt;: Some logs or specialized configurations are treated as data. An investigator needs to be able to pull these without being blocked by &lt;code&gt;ViewOnlyAccess&lt;/code&gt; restrictions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root Cause Analysis&lt;/strong&gt;: To understand the impact of a breach, you have to see what was accessed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. The Catch
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In a real-world IR scenario, even &lt;code&gt;ReadOnlyAccess&lt;/code&gt; might not be enough. You may need specific &lt;code&gt;SecurityAudit&lt;/code&gt; permissions to create &lt;strong&gt;EBS Snapshots&lt;/strong&gt; (which requires a &lt;code&gt;Write&lt;/code&gt; action to create the snapshot), so you can move a disk image to a sandbox for &lt;strong&gt;forensics&lt;/strong&gt; without touching the live production environment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I would love to hear your thoughts. Let me know in the comments. Thanks&lt;/p&gt;

</description>
      <category>cybersecurity</category>
      <category>security</category>
      <category>aws</category>
      <category>cloud</category>
    </item>
    <item>
      <title>C4 Model real world example with Google Maps</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Tue, 18 Feb 2025 14:00:00 +0000</pubDate>
      <link>https://forem.com/anwaar/c4-model-real-world-example-with-google-maps-3ano</link>
      <guid>https://forem.com/anwaar/c4-model-real-world-example-with-google-maps-3ano</guid>
      <description>&lt;p&gt;To illustrate C4 with a real world example, let's break down Google Maps as a case study and show how the same principles can apply to other applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://mermaid.live/edit" rel="noopener noreferrer"&gt;Mermaid Live&lt;/a&gt;&lt;/strong&gt; (not sponsored 😊) is a powerful tool that enables developers and architects to create C4 model diagrams in a simple, text-based format and can automatically generate Context, Container, Component, and Code-level diagrams without needing complex drawing tools. I love &lt;strong&gt;Mermaid&lt;/strong&gt; and have used it extensively to generate below images.&lt;/p&gt;

&lt;p&gt;and for an interesting overview, to understand fundamentals and beyond please visit &lt;a href="https://dev.to/anwaar/c4-model-1263"&gt;&lt;strong&gt;C4 Model&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Context Diagram (C1):&lt;/strong&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt; : Who uses the system and why?&lt;br&gt;
At the highest level, &lt;strong&gt;Google Maps&lt;/strong&gt; interacts with its &lt;strong&gt;users&lt;/strong&gt; and &lt;strong&gt;external systems&lt;/strong&gt; like &lt;strong&gt;location services&lt;/strong&gt; and &lt;strong&gt;traffic data providers&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&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%2Fngpkzq8iu1dlxmaveogb.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%2Fngpkzq8iu1dlxmaveogb.png" alt="C4 Context Diagram" width="800" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Users interact with Google Maps via a web app or mobile app.&lt;/li&gt;
&lt;li&gt;Google Maps integrates with external systems to provide location, traffic, and weather data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. Container Diagram (C2):&lt;/strong&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Details the major pieces of the system: "What are the main applications or services?"&lt;br&gt;
This level breaks &lt;strong&gt;Google Maps&lt;/strong&gt; into its &lt;strong&gt;main containers&lt;/strong&gt;, such as &lt;strong&gt;frontend&lt;/strong&gt;, &lt;strong&gt;backend services&lt;/strong&gt;, and &lt;strong&gt;databases&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&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%2Fbnpun2o60156mkc79hxy.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%2Fbnpun2o60156mkc79hxy.png" alt="C4 Container Diagram" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Frontend: The interface where users interact.&lt;/li&gt;
&lt;li&gt;API Gateway: Processes requests and routes them to the appropriate services.&lt;/li&gt;
&lt;li&gt;Map Rendering Service: Retrieves and serves map tiles.&lt;/li&gt;
&lt;li&gt;Routing Service: Calculates the best routes.&lt;/li&gt;
&lt;li&gt;Traffic Service: Analyzes real-time traffic data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. Component Diagram (C3):&lt;/strong&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Focuses on the internals of a container: "What modules exist in this service?"&lt;br&gt;
At this level, we zoom into the &lt;strong&gt;Routing Service&lt;/strong&gt; to see how it handles &lt;strong&gt;route calculations&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&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%2Fr6jtjtmphrwfky7lguzo.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%2Fr6jtjtmphrwfky7lguzo.png" alt="C4 Component Diagram" width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Route Planner&lt;/strong&gt;: Orchestrates the selected and dynamic route calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traffic Adjuster&lt;/strong&gt;: Modifies routes based on real-time traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distance Calculator:&lt;/strong&gt; Computes distances between locations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;4. Code Diagram (C4):&lt;/strong&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: At the code level, we describe the logic of a &lt;strong&gt;single component&lt;/strong&gt;, such as the &lt;strong&gt;Distance Calculator&lt;/strong&gt;. It can be a squence, workflow or any standard UML diagram.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;calculate:&lt;/strong&gt; Public method to calculate distances.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;haversineFormula:&lt;/strong&gt; Private method for geographical distance calculation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;adjustForTerrain:&lt;/strong&gt; Adjusts distance based on terrain type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The C4 model is more than just a set of diagrams; it's a framework for understanding and communicating complex software architectures. Its role in software development cannot be overstated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C4&lt;/strong&gt; is ideal for bridging communication between technical and non-technical teams, providing clarity on both the big picture and the nitty-gritty. It's widely used for system documentation, architecture design, and onboarding teams.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Reference&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/anwaar/c4-model-1263?preview=d785bd6e4175311a086cb598ee0b07996e0c516664bb1487e94fc0ad1fcdc505305eaf28ef98afb4b3c326f150a9f66bcde01e9c43b0fe5bcc0603b0"&gt;C4 Model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mermaid.live" rel="noopener noreferrer"&gt;Mermaid Live Editor: Online FlowChart &amp;amp; Diagrams Editor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://miro.com/diagramming/c4-model-for-software-architecture/" rel="noopener noreferrer"&gt;C4 Model for software Architecture&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c4model</category>
      <category>architecture</category>
      <category>design</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>C4 Model</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Tue, 18 Feb 2025 13:55:00 +0000</pubDate>
      <link>https://forem.com/anwaar/c4-model-1263</link>
      <guid>https://forem.com/anwaar/c4-model-1263</guid>
      <description>&lt;p&gt;Ever been in a design meeting where someone draws a &lt;strong&gt;high-level&lt;/strong&gt; architecture on a whiteboard, and by the end, it looks more like an ancient treasure map than a system diagram? 🤯 One person draws boxes labeled "Microservices" and "DB", while another sketches some arrows that somehow represent real-time, async, and batch processing all at once. &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%2F4aeq02hmgmcitxmb1irh.jpg" 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%2F4aeq02hmgmcitxmb1irh.jpg" alt="C4 Chaos" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, a product manager walks in and asks, "But where is the business logic?"&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%2Fafwujkpp86p1zmh2lkhv.jpg" 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%2Fafwujkpp86p1zmh2lkhv.jpg" alt="Need help" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;— and that’s when the architect decides it's time for coffee. ☕&lt;/p&gt;

&lt;p&gt;Software design often turns into a guessing game where different stakeholders see the system from different angles—developers worry about implementation, architects think in patterns, product managers want business clarity, and clients just want to know, "Will this work?" 🤔&lt;/p&gt;

&lt;p&gt;This is where the C4 Model saves the day! 🦸‍♂️&lt;/p&gt;

&lt;h2&gt;
  
  
  1️⃣ &lt;strong&gt;What is the C4 Model?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The C4 Model is a set of hierarchical diagrams (Context, Container, Component, Code) and provides a structured way to visualize and describe software architecture.  It helps to bridge the communication gap between technical and non-technical teams, providing clarity on both the big picture and the nitty-gritty. It's widely used for system documentation, architecture design and to analyze dependencies, internal and external integrations.&lt;/p&gt;

&lt;h2&gt;
  
  
  2️⃣ &lt;strong&gt;Breaking it Down: The Four C’s&lt;/strong&gt;
&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%2Fw6ku3z9mg1l07valvabf.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%2Fw6ku3z9mg1l07valvabf.png" alt="C4 Static" width="774" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Context Diagram&lt;/strong&gt; – The "Big Picture"&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What does the system do?, Who uses the system and why? &lt;br&gt;
High-level overview of the system and its interactions with users and external systems.&lt;br&gt;
&lt;strong&gt;Audience&lt;/strong&gt;: Business, Architect, Product, Developer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example (Google Maps Context)&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Users:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;End users (regular users using the mobile or web app)&lt;/li&gt;
&lt;li&gt;Businesses using Google Maps API for location services&lt;/li&gt;
&lt;li&gt;Delivery services (Uber Eats, DoorDash) integrating Maps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;External Systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Search (for place lookups)&lt;/li&gt;
&lt;li&gt;Google Places API (for location-based business details)&lt;/li&gt;
&lt;li&gt;Google Cloud (for data storage, compute services)&lt;/li&gt;
&lt;li&gt;Payment services (Google Pay for ride-hailing, parking, toll payments)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ &lt;strong&gt;Container Diagram&lt;/strong&gt; – The high-level building blocks &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What are the main applications or services? &lt;br&gt;
Helps us see how the system is set up and how it works with other integrations (Web App, Mobile App, DBs, Cache, Cloud, etc.)&lt;br&gt;
&lt;strong&gt;Audience&lt;/strong&gt;: Architect, Product, Developer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example (Google Maps Containers)&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Frontend (Mobile/Web Apps)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Maps Mobile App (Android/iOS)&lt;/li&gt;
&lt;li&gt;Google Maps Web Application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Backend Services&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maps Data Service (stores roads, buildings, locations)&lt;/li&gt;
&lt;li&gt;Routing Service (handles navigation, traffic updates)&lt;/li&gt;
&lt;li&gt;Geolocation Service (converts latitude/longitude to addresses)&lt;/li&gt;
&lt;li&gt;Street View Service (provides street-level images)&lt;/li&gt;
&lt;li&gt;Google Places API (fetches restaurant, hotel, gas station data)&lt;/li&gt;
&lt;li&gt;Traffic Analysis Service (integrates real-time traffic, historical trends)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Storage &amp;amp; Infrastructure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Cloud Storage (for map tile data, cached locations)&lt;/li&gt;
&lt;li&gt;BigQuery (to process location-based data)&lt;/li&gt;
&lt;li&gt;Firebase (syncs real-time data for mobile apps)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ &lt;strong&gt;Component Diagram&lt;/strong&gt; – The internals of a container&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What components/modules exist in application / service? &lt;br&gt;
Overview of specific parts doing specialized jobs&lt;br&gt;
&lt;strong&gt;Audience&lt;/strong&gt;: Architect, Developer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example (Google Maps Routing Service)&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Route Calculation Module: Finds shortest path using algorithms like Dijkstra’s/A*.&lt;/li&gt;
&lt;li&gt;Traffic Predictor: Uses historical + real-time data to estimate delays.&lt;/li&gt;
&lt;li&gt;Alternative Route Finder: Suggests different routes.&lt;/li&gt;
&lt;li&gt;User Location Service: Tracks real-time movement.&lt;/li&gt;
&lt;li&gt;Third-Party Data Integrator: Fetches public transit, ride-hailing data (Uber, Lyft).&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ &lt;strong&gt;Code Diagram&lt;/strong&gt; – Zooming in on implementation details&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;At the code level, we describe the logic of a single component&lt;br&gt;
&lt;strong&gt;Audience&lt;/strong&gt;: Developer, Designer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example (Google Maps Routing Code)&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;RouteController.java (exposes REST API)&lt;/li&gt;
&lt;li&gt;RouteService.java (business logic for computing routes)&lt;/li&gt;
&lt;li&gt;TrafficAnalysisService.java (predicts delays)&lt;/li&gt;
&lt;li&gt;GraphProcessor.java (Dijkstra’s algorithm implementation)&lt;/li&gt;
&lt;li&gt;GoogleMapsDB (stores precomputed routes for performance)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each level helps different people get the information they need—without unnecessary complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  3️⃣&lt;strong&gt;Audience Perspectives: How Different Stakeholders See the C4 Model&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;💰 Clients/Business Owners: "As long as it doesn’t crash, Add values to customer and 💵 /💲I’m happy :)"&lt;br&gt;
📈 Product Managers: "So this is how our product actually works?!"&lt;br&gt;
🏗️ Architects: "Finally, a structured way to describe systems across teams!"&lt;br&gt;
👨‍💻 Developers/Designers: "Ah, now I know which service calls what and where my code/design fits!"&lt;/p&gt;

&lt;h2&gt;
  
  
  4️⃣&lt;strong&gt;Why C4 Model is Better Than Chaos&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;No more ambiguous arrows.&lt;/li&gt;
&lt;li&gt;No more lost in UML nightmares.&lt;/li&gt;
&lt;li&gt;Consistent design across suites of products and teams.&lt;/li&gt;
&lt;li&gt;Common nomenclature for technical and non-technical audience.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5️⃣&lt;strong&gt;Real-World Example: C4 Model in Action&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Please review a short case study of architecture diagram using &lt;a href="https://dev.to/anwaar/c4-model-real-world-example-with-google-maps-3ano?preview=0004a21c7c3c86f6acd97ba83b6eaeebbe4e5da4c21024488bc4543eb99efd1594acb05be22e57ae031dd8edc45a02869c2b615052fa8c6fb9202ead"&gt;C4 and Google Maps&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  6️⃣&lt;strong&gt;Best Practices for Using the C4 Model&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Works across any language, framework, or platform, making it universally applicable.&lt;/li&gt;
&lt;li&gt;Maintain consistency across teams.&lt;/li&gt;
&lt;li&gt;Simplifies Complex Systems.&lt;/li&gt;
&lt;li&gt;Supports Agile Development.&lt;/li&gt;
&lt;li&gt;Facilitates Onboarding&lt;/li&gt;
&lt;li&gt;Adjust details based on the audience.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Final Thoughts&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Next time someone asks you to "&lt;strong&gt;just sketch the architecture real quick&lt;/strong&gt;," resist the urge to draw random boxes and arrows. Instead, use the C4 Model to keep things clear, structured, and (most importantly) understandable!&lt;/p&gt;

&lt;p&gt;What do you think? Would you like me to expand on any section? 😊. Let me know in comments. Thanks&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;A huge thanks to the online documentation, contributors, community and all the resources available that made this write-up possible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://c4model.com/introduction" rel="noopener noreferrer"&gt;C4 Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.easyread.co/c4-model-in-software-architecture-6587ab4a2d0f" rel="noopener noreferrer"&gt;Making C4-Model in Software Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/how-to-create-software-architecture-diagrams-using-the-c4-model/" rel="noopener noreferrer"&gt;Create Software Architecture using C4 Model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/icepanel/visualizing-software-architecture-with-the-c4-model-54e7"&gt;Visualizing software architecture with the C4 model&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>softwaredevelopment</category>
      <category>design</category>
      <category>c4model</category>
    </item>
    <item>
      <title>SQL PATTERNS : Pivot and Unpivot in SQL - Transforming Data Effectively</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Tue, 18 Feb 2025 05:00:00 +0000</pubDate>
      <link>https://forem.com/anwaar/sql-patterns-pivot-and-unpivot-in-sql-transforming-data-effectively-25k4</link>
      <guid>https://forem.com/anwaar/sql-patterns-pivot-and-unpivot-in-sql-transforming-data-effectively-25k4</guid>
      <description>&lt;h2&gt;
  
  
  📌 &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Handling data transformations is a crucial skill in SQL, especially for reporting and analytics. PIVOT and UNPIVOT operations help in restructuring data efficiently:&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;PIVOT&lt;/strong&gt;: Converts row-based data into a column-based format.&lt;br&gt;
🔹 &lt;strong&gt;UNPIVOT&lt;/strong&gt;: Converts column-based data into row-based format.&lt;/p&gt;

&lt;p&gt;In this article, we will explore both operations with real-world examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛠 &lt;strong&gt;Understanding PIVOT in SQL&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: You have sales data in a normalized format where each row represents sales for a specific month. You want to convert this into a columnar format to generate reports.&lt;/p&gt;

&lt;h3&gt;
  
  
  📌 &lt;strong&gt;Example: PIVOT Sales Data by Month&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;📊 Sample Table: Sales&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Month&lt;/th&gt;
&lt;th&gt;Sales&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;Jan&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;Feb&lt;/td&gt;
&lt;td&gt;7000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;Mar&lt;/td&gt;
&lt;td&gt;8000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;Jan&lt;/td&gt;
&lt;td&gt;3000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;Feb&lt;/td&gt;
&lt;td&gt;4500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;Mar&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  🔍 &lt;strong&gt;PIVOT Query&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;Month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Sales&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Sales&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;SourceTable&lt;/span&gt;
&lt;span class="n"&gt;PIVOT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Sales&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;Month&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;Jan&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Feb&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Mar&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;PivotTable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;Output&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Jan&lt;/th&gt;
&lt;th&gt;Feb&lt;/th&gt;
&lt;th&gt;Mar&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;td&gt;7000&lt;/td&gt;
&lt;td&gt;8000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;3000&lt;/td&gt;
&lt;td&gt;4500&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  🧐 &lt;strong&gt;Explanation&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We used &lt;code&gt;SUM(Sales)&lt;/code&gt; to aggregate values.&lt;/li&gt;
&lt;li&gt;The FOR Month IN &lt;code&gt;([Jan], [Feb], [Mar])&lt;/code&gt; clause dynamically transforms row values into column headers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔄 &lt;strong&gt;Understanding UNPIVOT in SQL&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: You receive a dataset where sales are already pivoted by months, but you need to transform it back into a normalized row-based format.&lt;/p&gt;

&lt;h3&gt;
  
  
  📌 &lt;strong&gt;Example: UNPIVOT Sales Data&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;📊 Pivoted Table: Sales_Pivoted&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Jan&lt;/th&gt;
&lt;th&gt;Feb&lt;/th&gt;
&lt;th&gt;Mar&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;td&gt;7000&lt;/td&gt;
&lt;td&gt;8000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;3000&lt;/td&gt;
&lt;td&gt;4500&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  🔍 &lt;strong&gt;UNPIVOT Query&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;Month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Sales&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Jan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Feb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Mar&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Sales_Pivoted&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;PivotTable&lt;/span&gt;
&lt;span class="n"&gt;UNPIVOT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Sales&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;Month&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Jan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Feb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Mar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;UnpivotTable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;Output&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Month&lt;/th&gt;
&lt;th&gt;Sales&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;Jan&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;Feb&lt;/td&gt;
&lt;td&gt;7000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;Mar&lt;/td&gt;
&lt;td&gt;8000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;Jan&lt;/td&gt;
&lt;td&gt;3000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;Feb&lt;/td&gt;
&lt;td&gt;4500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;td&gt;Mar&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  🧐 &lt;strong&gt;Explanation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The UNPIVOT operator converts multiple column values (Jan, Feb, Mar) back into row values under the Month column.&lt;br&gt;
This makes it easier to analyze or join with other normalized tables.&lt;/p&gt;

&lt;h3&gt;
  
  
  📌 &lt;strong&gt;When to Use PIVOT and UNPIVOT?&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Convert rows into columns (e.g., monthly reports)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;PIVOT&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Convert columns into rows (e.g., normalizing data)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;UNPIVOT&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;🚀 Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;PIVOT and UNPIVOT are powerful SQL techniques that help in data transformation for better reporting and analysis. Mastering them will enable you to handle structured data more effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  💡 &lt;strong&gt;What are your thoughts?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Have you used PIVOT and UNPIVOT in your SQL queries? Let’s discuss in the comments! ⬇️&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>softwareengineering</category>
      <category>datascience</category>
    </item>
    <item>
      <title>High Availability Mathematics for Mission-Critical Systems</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Tue, 21 Jan 2025 05:00:00 +0000</pubDate>
      <link>https://forem.com/anwaar/high-availability-mathematics-and-mission-critical-systems-51j9</link>
      <guid>https://forem.com/anwaar/high-availability-mathematics-and-mission-critical-systems-51j9</guid>
      <description>&lt;p&gt;&lt;strong&gt;High availability&lt;/strong&gt; is a critical metric that determines the reliability of a system or service. Often expressed as percentages, terms like "&lt;strong&gt;five nines&lt;/strong&gt;" &lt;strong&gt;(99.999%)&lt;/strong&gt; and "&lt;strong&gt;four nines&lt;/strong&gt;" &lt;strong&gt;(99.99%)&lt;/strong&gt; are used to describe uptime guarantees, typically in year. But what do these numbers mean, and how can they be achieved? Let’s explore.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Availability&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Downtime (Yearly)&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Systems Needed&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Trade-offs&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;99.0%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;87.6 hours i.e. 3 days 15 hours 39 minutes&lt;/td&gt;
&lt;td&gt;Basic setup, single region, minimal redundancy&lt;/td&gt;
&lt;td&gt;Low cost, high risk of downtime, minimal complexity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;99.9%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;8.76 hours&lt;/td&gt;
&lt;td&gt;Single region, load balancers, backup, monitoring, scaling&lt;/td&gt;
&lt;td&gt;Moderate cost and complexity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;99.99%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;52.6 minutes&lt;/td&gt;
&lt;td&gt;Multi-AZ/Region, cross-region replication, real-time monitoring&lt;/td&gt;
&lt;td&gt;Higher cost, more complexity, slight latency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;99.999%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;5.26 minutes&lt;/td&gt;
&lt;td&gt;Global multi-region, advanced replication, zero downtime deployment&lt;/td&gt;
&lt;td&gt;High cost, complex management, some latency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;99.9999%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;31.5 seconds&lt;/td&gt;
&lt;td&gt;Multi-cloud, federated load balancing, AI-driven monitoring&lt;/td&gt;
&lt;td&gt;Extremely high cost, significant operational burden&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Difference Between Uptime and Availability?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Uptime and availability are both critical concepts when discussing system reliability, but they are distinct and used in slightly different contexts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Uptime&lt;/strong&gt; is about the time the system is actually running, and is often expressed as a percentage of total time&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Uptime=(TotalUpTime/TotalTime)∗100Uptime  = (Total UpTime / Total Time ) * 100 &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;U&lt;/span&gt;&lt;span class="mord mathnormal"&gt;pt&lt;/span&gt;&lt;span class="mord mathnormal"&gt;im&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="mord mathnormal"&gt;U&lt;/span&gt;&lt;span class="mord mathnormal"&gt;pT&lt;/span&gt;&lt;span class="mord mathnormal"&gt;im&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="mord mathnormal"&gt;lT&lt;/span&gt;&lt;span class="mord mathnormal"&gt;im&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∗&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;100&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Availability&lt;/strong&gt; is a broader concept that includes uptime, measurement whether the system is &lt;strong&gt;operational&lt;/strong&gt; and &lt;strong&gt;accessible&lt;/strong&gt; and it also incorporates factors like system resilience, failover mechanisms, redundancy, and how quickly the system can recover from a failure and downtime during maintenance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Availability=(TotalUpTime/(TotalUpTime+TotalDownTime))∗100Availability = (Total_UpTime / (Total_UpTime + Total_DownTime) ) * 100&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ai&lt;/span&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="mord mathnormal"&gt;abi&lt;/span&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="mord mathnormal"&gt;i&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;U&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;pT&lt;/span&gt;&lt;span class="mord mathnormal"&gt;im&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;U&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;pT&lt;/span&gt;&lt;span class="mord mathnormal"&gt;im&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;D&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mord mathnormal"&gt;im&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∗&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;100&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How to Calculate Uptime!&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s the total time your system has been up and running. To find the availability percentage, divide the uptime by the total hours in the measured period and multiply by 100.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here’s an example: Imagine a website that experienced 10 hours of downtime over the course of a year.&lt;/p&gt;

&lt;p&gt;1️⃣ Total Hours in a Year: 8,760 hours&lt;br&gt;
2️⃣ Downtime Experienced: 20 hours (Consider there are 4 quarterly product releases/maintenance upgrades that were deployed over the weekend and each took 5 hours to upgrade the PROD region)&lt;br&gt;
3️⃣ Actual Uptime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="mi"&gt;8760&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="n"&gt;downtime&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;740&lt;/span&gt; &lt;span class="n"&gt;Uptime&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4️⃣ Availability Percentage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8740&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;8760&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;77168&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Simplified View&lt;/strong&gt;:
&lt;/h3&gt;

&lt;p&gt;📅 Total Annual Hours: 8,760&lt;br&gt;
🚫 Downtime: 20 hours&lt;br&gt;
🆙 Operational Time: 8,740 hours&lt;br&gt;
📈 Availability: 🎯 99.77%&lt;/p&gt;

&lt;p&gt;This shows how a small amount of downtime, such as 20 hours in a year, can slightly lower the availability percentage, but still result in a highly reliable system.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Examples of Applications by Availability Level&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;99.00000% (Two Nines)&lt;/strong&gt;: Suitable for non-critical systems or internal tools where occasional downtime is acceptable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;99.90000% (Three and a Half Nines)&lt;/strong&gt;: Common for basic online services or small business websites.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;99.99000% (Four Nines)&lt;/strong&gt;: Ideal for high-availability applications like e-commerce platforms or SaaS products.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;99.99900% (Five Nines)&lt;/strong&gt;: Essential for mission-critical systems like banking, healthcare, or telecom.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;99.99990% and Above&lt;/strong&gt;: Ultra-high availability, often required for safety-critical systems (e.g., aviation control).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Both 99.999% and 99.99% availability represent high reliability standards, but the choice depends on the criticality of your service and budget constraints. While achieving five nines may be essential for mission-critical systems, four nines may suffice for most business applications. The key is to &lt;strong&gt;balance cost&lt;/strong&gt;, &lt;strong&gt;complexity&lt;/strong&gt;, and &lt;strong&gt;user expectations&lt;/strong&gt; while implementing robust strategies to minimize downtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What did I miss?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you have suggestions/feedback worth mentioned here, please share them in the comment below. Consider liking and sharing if you find this helpful.&lt;br&gt;
Thank you. Have a good day!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;References&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A huge thanks to the documentation, community and all the resources available that made this write-up possible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.sedai.io/blog/system-availability-calculation-definition-and-measurement" rel="noopener noreferrer"&gt;System availability calculation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://uptimerobot.com/blog/how-to-calculate-uptime/" rel="noopener noreferrer"&gt;Calculate Uptime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freepik.com/" rel="noopener noreferrer"&gt;Infographics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>highavailability</category>
      <category>disasterrecovery</category>
      <category>uptime</category>
      <category>architecture</category>
    </item>
    <item>
      <title>SQL PATTERNS: ROW_NUMBER, RANK and DENSE_RANK</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Thu, 05 Dec 2024 02:37:29 +0000</pubDate>
      <link>https://forem.com/anwaar/sql-rownumber-rank-and-denserank-7ik</link>
      <guid>https://forem.com/anwaar/sql-rownumber-rank-and-denserank-7ik</guid>
      <description>&lt;p&gt;Imagine you're tasked with building a ranking system for a gaming platform. &lt;strong&gt;How do you handle ties in scores&lt;/strong&gt;? Take a look at the below image, where players on rank 3 have the same scores.&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%2F6o61dg2t7opemnniwp1d.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%2F6o61dg2t7opemnniwp1d.png" alt="Score ties" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if you want to identify and remove duplicate entries from a user database while keeping only the latest record? Or perhaps you need to paginate millions of rows in a web application efficiently. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is where &lt;strong&gt;ROW_NUMBER&lt;/strong&gt;, &lt;strong&gt;RANK&lt;/strong&gt;, and &lt;strong&gt;DENSE_RANK&lt;/strong&gt; step in as your go-to SQL window functions, turning daunting problems into manageable queries. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;They assign ranks or numbers to rows within a result set&lt;/u&gt;. These functions are often paired with &lt;strong&gt;ORDER BY&lt;/strong&gt; and &lt;strong&gt;PARTITION BY&lt;/strong&gt; clauses.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ORDER BY defines how rows are ordered for ranking.&lt;/li&gt;
&lt;li&gt;PARTITION BY divides rows into groups (optional). Ranking restarts for each group.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ready to uncover how these SQL superheroes can transform your data operations? Let’s dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Sample data
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Department&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;EmployeeName&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Salary&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;9000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;8500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;8500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;td&gt;8000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;9500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;8900&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;Emma&lt;/td&gt;
&lt;td&gt;8900&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;Frank&lt;/td&gt;
&lt;td&gt;8700&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Query Using ROW_NUMBER, RANK, and DENSE_RANK&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Let's write a query to rank employees within each department based on their salaries in descending order
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;Department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;EmployeeName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ROW_NUMBER&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
      &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;Department&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;RowNum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;RANK&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
     &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;Department&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;Rank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;DENSE_RANK&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
     &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;Department&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;DenseRank&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Department&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;EmployeeName&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Salary&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;RowNum&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Rank&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;DenseRank&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;9000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;8500&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;8500&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HR&lt;/td&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;td&gt;8000&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;9500&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;8900&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;Emma&lt;/td&gt;
&lt;td&gt;8900&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;Frank&lt;/td&gt;
&lt;td&gt;8700&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Key Differences&lt;/strong&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Function&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Unique Sequence&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Skips Ranks After Ties&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Continuous Ranks&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ROW_NUMBER&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;N/A&lt;/td&gt;
&lt;td&gt;N/A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RANK&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DENSE_RANK&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Explanation&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ROW_NUMBER&lt;/strong&gt; generates a unique sequence for every row, ignoring ties.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RANK&lt;/strong&gt; assigns the same rank for ties but skips the next ranks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DENSE_RANK&lt;/strong&gt; assigns the same rank for ties without skipping subsequent ranks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know if you’d like any other adjustments! 🚀&lt;/p&gt;

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

&lt;p&gt;The versatility of &lt;strong&gt;ROW_NUMBER&lt;/strong&gt;, &lt;strong&gt;RANK&lt;/strong&gt;, and &lt;strong&gt;DENSE_RANK&lt;/strong&gt; makes them indispensable for scenarios like &lt;strong&gt;pagination&lt;/strong&gt;, &lt;strong&gt;deduplication&lt;/strong&gt;, &lt;strong&gt;pattern analysis&lt;/strong&gt;, &lt;strong&gt;prioritization&lt;/strong&gt;, and much more. Let me know if you'd like more examples or further clarification on any of these use cases! 😊&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Script: Creating and Populating the Employees Table&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Table Creation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;EmployeeID&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Department&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;EmployeeName&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Salary&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Inserting Sample Data
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EmployeeID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EmployeeName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'HR'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'HR'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8500&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'HR'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Eve'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8500&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'HR'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Tom'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'IT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Charlie'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9500&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'IT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'David'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8900&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'IT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Emma'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8900&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'IT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Frank'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8700&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;References&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blix.gg/news/cs-2/how-does-the-rating-system-work-in-cs2/" rel="noopener noreferrer"&gt;CS2 leaderboard&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/64420584/when-to-choose-rank-over-dense-rank-or-row-number" rel="noopener noreferrer"&gt;Stackoverflow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>ranking</category>
      <category>database</category>
      <category>snowflake</category>
    </item>
    <item>
      <title>Multithreading Concepts Part 3 : Deadlock</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Mon, 04 Nov 2024 12:12:34 +0000</pubDate>
      <link>https://forem.com/anwaar/multithreading-concepts-part-3-deadlock-4ip6</link>
      <guid>https://forem.com/anwaar/multithreading-concepts-part-3-deadlock-4ip6</guid>
      <description>&lt;p&gt;Welcome to Part 3 of our multithreading series! &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;a href="https://dev.to/anwaar/multithreading-key-concepts-for-engineers-part-1-4g73"&gt;Part 1&lt;/a&gt;, we explored &lt;strong&gt;Atomicity&lt;/strong&gt; and &lt;strong&gt;Immutability&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;In &lt;a href="https://dev.to/anwaar/multithreading-concepts-part-2-starvation-1abb"&gt;Part 2&lt;/a&gt;, we discussed &lt;strong&gt;Starvation&lt;/strong&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this part, we’ll dive into the mechanics of &lt;strong&gt;Deadlock&lt;/strong&gt; in multithreading. What causes it, How to identify and preventive strategies you can use to avoid turning your code into a gridlocked intersection. Application grinds to a halt, often without any visible errors, leaving developers puzzled and systems frozen.&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%2Fk6n8738d9h04lzkrqj09.gif" 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%2Fk6n8738d9h04lzkrqj09.gif" alt="Deadlock" width="600" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Navigating the Complex Tracks of Concurrency
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A useful analogy to understand deadlock is to imagine a railway network with multiple trains on intersecting tracks.&lt;/p&gt;

&lt;p&gt;Since each train is waiting for the next one to move, none can proceed, leading to a deadlock. In this scenario, the inefficient signaling system allowed each train to enter its respective section without first confirming that the next section would be free, trapping all trains in an unbreakable cycle.&lt;/p&gt;

&lt;p&gt;This train example illustrates a typical deadlock in multithreading, where threads (like the trains) hold onto resources (track sections) while waiting for other resources to be freed, but none can progress. To prevent this kind of deadlock in software, effective resource management strategies—analogous to smarter railway signaling—must be implemented to avoid circular dependencies and ensure safe passage for each thread.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  1. What is Deadlock?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Deadlock&lt;/strong&gt; is a situation in which threads (or processes) are indefinitely blocked, waiting for resources that other threads hold. This scenario leads to an unbreakable cycle of dependencies, where no involved thread can make progress. Understanding the basics of deadlock is essential before exploring the methods for detection, prevention, and resolution.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Conditions for Deadlock
&lt;/h1&gt;

&lt;p&gt;For a deadlock to occur, four conditions must be met simultaneously, known as the Coffman conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mutual Exclusion:&lt;/strong&gt; At least one resource must be held in a non-shareable mode, meaning only one thread can use it at a time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hold and Wait:&lt;/strong&gt; A thread must hold one resource and wait to acquire additional resources that other threads hold.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Preemption:&lt;/strong&gt; Resources cannot be forcibly taken away from threads. They must be released voluntarily.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Circular Wait:&lt;/strong&gt; A closed chain of threads exists, where each thread holds at least one resource needed by the next thread in the chain.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fshz1y9khfisabmhgqcsz.gif" 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%2Fshz1y9khfisabmhgqcsz.gif" alt="Deadlock detected and waiting!" width="683" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand as a sequence diagram&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%2Fwjldkinugjkz84yi4i3w.gif" 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%2Fwjldkinugjkz84yi4i3w.gif" alt="Deadlock sequence diagram" width="753" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In above animation, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thread A holds Resource 1 and waits for Resource 2&lt;/li&gt;
&lt;li&gt;While Thread B holds Resource 2 and waits for Resource 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All four conditions shared above for deadlock are present, which results indefinite block. Breaking any one of them can prevent deadlock.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Detect / Monitor deadlock
&lt;/h1&gt;

&lt;p&gt;Detecting deadlocks, especially in large-scale applications, can be challenging. However, the following approaches can help identify deadlocks&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tooling:&lt;/strong&gt; Java's JConsole, VisualVM, and thread analyzers in IDEs can detect deadlocks in real-time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread Dumps and Logs:&lt;/strong&gt; Analyzing thread dumps can reveal waiting threads and the resources they’re holding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For detailed overview to understand how to debug/monitor deadlocks please visit &lt;a href="https://dev.to/anwaar/debug-and-monitor-java-app-with-visualvm-and-jstack-aoa"&gt;Debug and Monitor Deadlock using VisualVM and jstack&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Strategies for Deadlock Prevention
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Applying the Wait-Die and Wound-Wait Schemes&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Wait-Die Scheme&lt;/code&gt;: When a thread requests a lock held by another thread, the database assesses the relative priority (usually based on the timestamp of each thread). If the requesting thread has a higher priority, it waits; otherwise, it dies (restarts).&lt;br&gt;
&lt;code&gt;Wound-Wait Scheme&lt;/code&gt;: If the &lt;code&gt;requesting thread&lt;/code&gt; has a &lt;code&gt;higher priority&lt;/code&gt;, it &lt;strong&gt;wounds&lt;/strong&gt; (&lt;code&gt;preempts&lt;/code&gt;) the &lt;code&gt;lower-priority&lt;/code&gt; thread by forcing it to release the lock.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Immutable Objects for Shared State&lt;/strong&gt;&lt;br&gt;
Design shared state as immutable wherever possible. Since immutable objects cannot be modified, they require &lt;strong&gt;no locks&lt;/strong&gt; for concurrent access, reducing the risk of deadlock and simplifying code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Using tryLock with Timeout for Lock Acquisition&lt;/strong&gt;: Unlike a standard &lt;code&gt;synchronized block&lt;/code&gt;, &lt;code&gt;ReentrantLock&lt;/code&gt; allows using &lt;code&gt;tryLock(timeout, unit)&lt;/code&gt; to attempt acquiring a lock within a specified period. If the lock isn’t acquired within that time, it releases resources, preventing indefinite blocking.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ReentrantLock&lt;/span&gt; &lt;span class="n"&gt;lock1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReentrantLock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;ReentrantLock&lt;/span&gt; &lt;span class="n"&gt;lock2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReentrantLock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;acquireLocks&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lock1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tryLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MILLISECONDS&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lock2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tryLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MILLISECONDS&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="c1"&gt;// Critical section&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;lock2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;lock1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lock Ordering and Releasing&lt;/strong&gt;
Set a strict, global order for lock acquisition. If all threads acquire locks in a consistent order, cyclic dependencies are less likely to form, thus avoiding deadlocks. For example, always acquire &lt;code&gt;lock1&lt;/code&gt; before &lt;code&gt;lock2&lt;/code&gt; throughout the codebase. This practice can be challenging in larger applications but is very effective in reducing deadlock risk.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.locks.Lock&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.locks.ReentrantLock&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LockOrderingExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Lock&lt;/span&gt; &lt;span class="n"&gt;lock1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReentrantLock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Lock&lt;/span&gt; &lt;span class="n"&gt;lock2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReentrantLock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;acquireLocksInOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lock1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lock2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;

        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;acquireLocksInOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lock1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lock2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;

        &lt;span class="n"&gt;thread1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;thread2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;acquireLocksInOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lock&lt;/span&gt; &lt;span class="n"&gt;firstLock&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Lock&lt;/span&gt; &lt;span class="n"&gt;secondLock&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;firstLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" acquired lock1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;secondLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" acquired lock2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Perform some operations&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;secondLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" released lock2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;firstLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" released lock1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Thread-Safe/Concurrent Collections&lt;/strong&gt;: Java’s &lt;code&gt;java.util.concurrent&lt;/code&gt; package provides thread-safe implementations of common data structures (&lt;code&gt;ConcurrentHashMap&lt;/code&gt;, &lt;code&gt;CopyOnWriteArrayList&lt;/code&gt;, etc.) that handle synchronization internally, reducing the need for explicit locks. These collections minimize deadlocks as they’re designed to avoid the need for explicit locking, using techniques like internal partitioning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Nested Locks&lt;/strong&gt;&lt;br&gt;
Minimize acquiring multiple locks within the same block to avoid circular dependencies. If nested locks are necessary, use consistent lock ordering&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Key Takeaways for Software Engineers
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Whenever you create a design that requires locking, you open up the possibility for deadlocks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deadlock&lt;/strong&gt; is a &lt;strong&gt;blocking issue&lt;/strong&gt; caused by a cycle of dependencies between processes. No process can make progress because each one is waiting for a resource held by another, and none can proceed to release resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deadlock&lt;/strong&gt; is more severe, as it completely halts the involved processes and requires breaking the deadlock cycle for recovery.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deadlock&lt;/strong&gt; can occur only when there are &lt;code&gt;two different locks&lt;/code&gt;, i.e. when you are &lt;code&gt;holding a lock&lt;/code&gt; and &lt;code&gt;waiting for another lock to release&lt;/code&gt;. (There are more conditions on deadlocks, however).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread-safety&lt;/strong&gt; does not mean &lt;strong&gt;deadlock free&lt;/strong&gt;. It only guarantees that the code will operate according to its interface, even when called from multiple threads. Making a class thread-safe usually includes adding locks to guarantee safe execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Outro
&lt;/h1&gt;

&lt;p&gt;Whether you're a beginner or a seasoned developer, understanding deadlock is crucial for writing robust, efficient code in concurrent systems. In this article, we explored what deadlocks are, their causes, and practical ways to prevent them. By implementing effective resource allocation strategies, analyzing task dependencies, and utilizing tools like thread dumps and deadlock detection tools, developers can minimize deadlock risk and optimize their code for smooth concurrency.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As we continue our journey through the core concepts of multithreading, stay tuned for the next articles in this series. We’ll dive into &lt;strong&gt;Critical Sections&lt;/strong&gt;, understanding how to manage shared resources safely among multiple thread. We will also discuss the concept of &lt;strong&gt;Race Conditions&lt;/strong&gt;, a common concurrency issue that can lead to unpredictable behavior and bugs if left unchecked.&lt;/p&gt;

&lt;p&gt;With each step, you’ll gain deeper insights into how to make your applications thread-safe, efficient, and resilient. Keep pushing the boundaries of your multithreading knowledge to build better, more performant software!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/40219985/thread-safe-but-does-not-prevent-deadlock" rel="noopener noreferrer"&gt;Stackoverflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/pulse/deadlock-vs-livelock-demystifying-distributed-systems-pronnoy-goswami-jocjc" rel="noopener noreferrer"&gt;Infographics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=b4Scxi7sJBc" rel="noopener noreferrer"&gt;How to detect and fix deadlock&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>multithreading</category>
      <category>java</category>
      <category>deadlock</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Debug and Monitor Java App with VisualVM and jstack</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Sun, 03 Nov 2024 22:45:20 +0000</pubDate>
      <link>https://forem.com/anwaar/debug-and-monitor-java-app-with-visualvm-and-jstack-aoa</link>
      <guid>https://forem.com/anwaar/debug-and-monitor-java-app-with-visualvm-and-jstack-aoa</guid>
      <description>&lt;p&gt;Java applications, especially those running in production, can develop performance &lt;strong&gt;bottlenecks&lt;/strong&gt;, &lt;strong&gt;deadlocks&lt;/strong&gt;, and &lt;strong&gt;memory leaks&lt;/strong&gt; that can be challenging to trace. When these issues arise, quick and effective debugging is essential to keep applications running smoothly and to maintain a positive user experience. This is where VisualVM and JStack become invaluable tools for Java developers. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;VisualVM&lt;/strong&gt;, with its powerful profiling and monitoring capabilities, and &lt;strong&gt;jstack&lt;/strong&gt;, offering detailed thread dumps, are a dynamic duo that enables developers to detect, diagnose, and debug complex issues with ease.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Diagnosing and resolving &lt;strong&gt;deadlocks&lt;/strong&gt; is critical, especially in high-performance applications. This guide will explore how to &lt;strong&gt;detect deadlocks&lt;/strong&gt; and obtain &lt;strong&gt;thread dumps&lt;/strong&gt; using two powerful Java tools: &lt;strong&gt;VisualVM&lt;/strong&gt; and &lt;strong&gt;jstack&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Understanding Deadlocks and Thread Dumps
&lt;/h1&gt;

&lt;p&gt;Before diving into the tools, let's clarify a few basics:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deadlock&lt;/strong&gt;: A situation where threads are waiting for each other to release resources, leading to an indefinite blocking cycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thread Dump&lt;/strong&gt;: A snapshot of the active threads within a Java application at a given time, including details on their states and any locks they are holding or waiting on. A thread dump is crucial in analyzing and identifying deadlock sources in Java applications, allowing developers to pinpoint the cause of blocked threads.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Introduction to VisualVM
&lt;/h1&gt;

&lt;p&gt;VisualVM is a visual tool integrating several command-line JDK tools to deliver a comprehensive overview of Java applications. It’s widely used to monitor and profile applications, diagnose memory leaks, and analyze performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Live monitoring and profiling&lt;/li&gt;
&lt;li&gt;Thread analysis&lt;/li&gt;
&lt;li&gt;Memory and CPU usage insights&lt;/li&gt;
&lt;li&gt;Thread dumps for deadlock analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Debug using VisualVM (for Java applications)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Download &lt;a href="https://visualvm.github.io/download.html" rel="noopener noreferrer"&gt;VisualVM&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Open VisualVM.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Connect to the running JVM process.&lt;br&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%2Fo0nc5h5u0q61utnoh0v9.jpg" 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%2Fo0nc5h5u0q61utnoh0v9.jpg" alt=" " width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the monitoring tools, you can click thread dump or view thread details in real time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fwwdcv4rve31a0hyw7krz.jpg" 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%2Fwwdcv4rve31a0hyw7krz.jpg" alt=" " width="800" height="647"&gt;&lt;/a&gt;&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%2Fncmp8kt3ckib94mcxyn9.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%2Fncmp8kt3ckib94mcxyn9.png" alt="Deadlock Thread Dump details" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Introduction to jstack
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;jstack&lt;/code&gt; is an invaluable command-line tool for any Java developer dealing with complex applications that rely heavily on threads. By providing detailed insights into thread states, locking behavior, and execution flow, &lt;code&gt;jstack&lt;/code&gt; simplifies the debugging process, making it easier to spot and resolve issues like deadlocks, performance bottlenecks, and application freezes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;jstack&lt;/code&gt; shows the state of each thread, such as &lt;strong&gt;RUNNABLE&lt;/strong&gt;, &lt;strong&gt;BLOCKED&lt;/strong&gt;, &lt;strong&gt;WAITING&lt;/strong&gt;, or &lt;strong&gt;TIMED_WAITING&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Capture stack traces for live or hung processes to help in debugging complex threading issues.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jstack&lt;/code&gt; marks deadlocked threads in the thread dump, allowing you to quickly identify them.&lt;/li&gt;
&lt;li&gt;Obtain a thread dump, which lists all active threads in the JVM.&lt;/li&gt;
&lt;li&gt;Paired with other tools like &lt;strong&gt;VisualVM&lt;/strong&gt; or &lt;strong&gt;jmap&lt;/strong&gt;, &lt;code&gt;jstack&lt;/code&gt; can help trace memory leaks back to threads responsible for excessive object creation or holding onto references.&lt;/li&gt;
&lt;li&gt;By analyzing thread dumps, developers can identify problematic synchronization, excessive blocking, or opportunities to reduce contention, leading to improved concurrency and responsiveness.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using jstack to Obtain and Analyze Thread Dumps
&lt;/h3&gt;

&lt;p&gt;To capture a thread dump of a running Java application using &lt;code&gt;jstack&lt;/code&gt;, you’ll need the &lt;code&gt;process ID (PID)&lt;/code&gt; of the &lt;code&gt;Java process&lt;/code&gt;. Here’s a step-by-step guide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; Find the Process ID (PID) of the Java Application
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;C:&lt;span class="se"&gt;\P&lt;/span&gt;rogram Files&lt;span class="se"&gt;\J&lt;/span&gt;ava&lt;span class="se"&gt;\j&lt;/span&gt;dk-21&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;jps &lt;span class="nt"&gt;-l&lt;/span&gt;
12912
22480 org.springframework.ide.vscode.boot.app.BootLanguageServerBootApp
24020 jdk.jcmd/sun.tools.jps.Jps
14344 org/netbeans/Main
21944 deadlock/deadlock.DeadlockExample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Use &lt;code&gt;jstack&lt;/code&gt; to Capture the Thread Dump
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;C:&lt;span class="se"&gt;\P&lt;/span&gt;rogram Files&lt;span class="se"&gt;\J&lt;/span&gt;ava&lt;span class="se"&gt;\j&lt;/span&gt;dk-21&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;jstack 21944
2024-11-02 11:12:18
Full thread dump Java HotSpot&lt;span class="o"&gt;(&lt;/span&gt;TM&lt;span class="o"&gt;)&lt;/span&gt; 64-Bit Server VM &lt;span class="o"&gt;(&lt;/span&gt;21.0.5+9-LTS-239 mixed mode, sharing&lt;span class="o"&gt;)&lt;/span&gt;:

Threads class SMR info:
&lt;span class="nb"&gt;.&lt;/span&gt;
..
....
...

&lt;span class="s2"&gt;"Thread-0"&lt;/span&gt; &lt;span class="c"&gt;#29 [18484] prio=5 os_prio=0 cpu=0.00ms elapsed=1896.34s tid=0x000001bb3395ac40 nid=18484 waiting for monitor entry  [0x00000099227ff000]&lt;/span&gt;
   java.lang.Thread.State: BLOCKED &lt;span class="o"&gt;(&lt;/span&gt;on object monitor&lt;span class="o"&gt;)&lt;/span&gt;
        at deadlock.DeadlockExample.lambda&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;deadlock/DeadlockExample.java:23&lt;span class="o"&gt;)&lt;/span&gt;
        - waiting to lock &amp;lt;0x000000070d03c740&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        - locked &amp;lt;0x000000070d05a828&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        at deadlock.DeadlockExample&lt;span class="nv"&gt;$$&lt;/span&gt;Lambda/0x000001bb350019f8.run&lt;span class="o"&gt;(&lt;/span&gt;deadlock/Unknown Source&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.runWith&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1596&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.run&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1583&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="s2"&gt;"Thread-1"&lt;/span&gt; &lt;span class="c"&gt;#30 [23240] prio=5 os_prio=0 cpu=0.00ms elapsed=1896.34s tid=0x000001bb3395b2a0 nid=23240 waiting for monitor entry  [0x00000099228ff000]&lt;/span&gt;
   java.lang.Thread.State: BLOCKED &lt;span class="o"&gt;(&lt;/span&gt;on object monitor&lt;span class="o"&gt;)&lt;/span&gt;
        at deadlock.DeadlockExample.lambda&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;deadlock/DeadlockExample.java:41&lt;span class="o"&gt;)&lt;/span&gt;
        - waiting to lock &amp;lt;0x000000070d05a828&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        - locked &amp;lt;0x000000070d03c740&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        at deadlock.DeadlockExample&lt;span class="nv"&gt;$$&lt;/span&gt;Lambda/0x000001bb35001c08.run&lt;span class="o"&gt;(&lt;/span&gt;deadlock/Unknown Source&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.runWith&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1596&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.run&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1583&lt;span class="o"&gt;)&lt;/span&gt;

...
.....
..
Found one Java-level deadlock:
&lt;span class="o"&gt;=============================&lt;/span&gt;
&lt;span class="s2"&gt;"Thread-0"&lt;/span&gt;:
  waiting to lock monitor 0x000001bb117727a0 &lt;span class="o"&gt;(&lt;/span&gt;object 0x000000070d03c740, a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;,
  which is held by &lt;span class="s2"&gt;"Thread-1"&lt;/span&gt;

&lt;span class="s2"&gt;"Thread-1"&lt;/span&gt;:
  waiting to lock monitor 0x000001bb11772500 &lt;span class="o"&gt;(&lt;/span&gt;object 0x000000070d05a828, a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;,
  which is held by &lt;span class="s2"&gt;"Thread-0"&lt;/span&gt;

Java stack information &lt;span class="k"&gt;for &lt;/span&gt;the threads listed above:
&lt;span class="o"&gt;===================================================&lt;/span&gt;
&lt;span class="s2"&gt;"Thread-0"&lt;/span&gt;:
        at deadlock.DeadlockExample.lambda&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;deadlock/DeadlockExample.java:23&lt;span class="o"&gt;)&lt;/span&gt;
        - waiting to lock &amp;lt;0x000000070d03c740&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        - locked &amp;lt;0x000000070d05a828&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        at deadlock.DeadlockExample&lt;span class="nv"&gt;$$&lt;/span&gt;Lambda/0x000001bb350019f8.run&lt;span class="o"&gt;(&lt;/span&gt;deadlock/Unknown Source&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.runWith&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1596&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.run&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1583&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="s2"&gt;"Thread-1"&lt;/span&gt;:
        at deadlock.DeadlockExample.lambda&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;deadlock/DeadlockExample.java:41&lt;span class="o"&gt;)&lt;/span&gt;
        - waiting to lock &amp;lt;0x000000070d05a828&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        - locked &amp;lt;0x000000070d03c740&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;a java.lang.Object&lt;span class="o"&gt;)&lt;/span&gt;
        at deadlock.DeadlockExample&lt;span class="nv"&gt;$$&lt;/span&gt;Lambda/0x000001bb35001c08.run&lt;span class="o"&gt;(&lt;/span&gt;deadlock/Unknown Source&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.runWith&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1596&lt;span class="o"&gt;)&lt;/span&gt;
        at java.lang.Thread.run&lt;span class="o"&gt;(&lt;/span&gt;java.base@21.0.5/Thread.java:1583&lt;span class="o"&gt;)&lt;/span&gt;

Found 1 deadlock.

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;jstack&lt;/strong&gt; is available in the JDK, so ensure that the JDK is installed and accessible in your environment.&lt;/li&gt;
&lt;li&gt;Using &lt;strong&gt;jstack&lt;/strong&gt; on some systems may require administrator privileges.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;jstack -l 12345 &amp;gt; threaddump.txt&lt;/code&gt; saves the thread dump to a file called threaddump.txt.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Outro
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;VisualVM&lt;/strong&gt; and &lt;strong&gt;jstack&lt;/strong&gt; are two essential tools in the Java developer’s toolkit for diagnosing and troubleshooting Java applications, particularly when dealing with performance issues, deadlocks, and thread bottlenecks.&lt;/p&gt;

&lt;p&gt;Together, &lt;strong&gt;VisualVM&lt;/strong&gt; and &lt;strong&gt;jstack&lt;/strong&gt; offer a comprehensive approach to debugging Java applications, with &lt;strong&gt;VisualVM&lt;/strong&gt; providing broad, real-time performance insights and &lt;strong&gt;jstack&lt;/strong&gt; enabling deep thread-level analysis. Their combined usage allows developers to effectively diagnose and resolve complex Java issues in both development and production environments.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reference
&lt;/h1&gt;

&lt;p&gt;A huge thanks to the online documentation, community and all the resources available that made this write-up possible.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://visualvm.github.io/gettingstarted.html" rel="noopener noreferrer"&gt;How to use VisualVM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/anwaar/multithreading-key-concepts-for-engineers-part-1-4g73"&gt;Multithreading Concepts Part 1: Atomicity and Immutability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/technotes/guides/visualvm/index.html" rel="noopener noreferrer"&gt;Java VisualVM&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>devops</category>
      <category>deadlock</category>
      <category>mulithreading</category>
    </item>
    <item>
      <title>Multithreading Concepts Part 2 : Starvation</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Mon, 21 Oct 2024 17:11:48 +0000</pubDate>
      <link>https://forem.com/anwaar/multithreading-concepts-part-2-starvation-1abb</link>
      <guid>https://forem.com/anwaar/multithreading-concepts-part-2-starvation-1abb</guid>
      <description>&lt;p&gt;Welcome to Part 2 of our multithreading series! &lt;br&gt;
In this part, we’ll dive deeper to understand &lt;strong&gt;starvation&lt;/strong&gt; as it is essential for engineers, because it ensures fairness and system performance. By recognizing starvation risks, engineers can design systems that prioritize fairness, prevent resource monopolization, and ensure all threads get the necessary CPU time and resources to function optimally.&lt;/p&gt;
&lt;h1&gt;
  
  
  The Never-Ending Wait in Game Lobbies
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Imagine you’re playing an online multiplayer game, and you’re trying to join a lobby for a popular game mode, like a team battle. You’ve been waiting in the lobby for a while, but every time a match starts, a new batch of players with faster internet or higher skill ranking gets prioritized and added to the game before you. You see matches starting over and over, but you never seem to get in!&lt;/p&gt;

&lt;p&gt;You are technically in the queue, but because other players with faster response times or higher ranks keep getting into games first, you are left in the lobby indefinitely. Despite having a perfectly functional system, you’re denied the chance to play because of how the matchmaking algorithm unfairly prioritizes others.&lt;/p&gt;
&lt;/blockquote&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%2Fyduxm1edn98chf1ucbee.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%2Fyduxm1edn98chf1ucbee.png" alt=" " width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  1. Starvation
&lt;/h1&gt;

&lt;p&gt;Starvation is a situation in which a process is continuously denied access to the resources it needs to proceed, even though those resources are available. The process remains in a waiting state because higher-priority processes or other resource allocation policies prevent it from acquiring the necessary resources. Unlike deadlock, the resources are not entirely unavailable, but the process is unable to access them due to unfair scheduling.&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Causes
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Priority Inversion&lt;/strong&gt;: When a higher-priority process is waiting for a resource held by a lower-priority process, the lower-priority process may be starved of CPU time if other higher-priority processes keep arriving.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resource Allocation Policies&lt;/strong&gt;: Some scheduling algorithms may favor certain processes (usually those with higher priorities), leading to a situation where lower-priority processes are rarely allocated resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Poorly Designed Algorithms&lt;/strong&gt;: If the resource allocation algorithm is not balanced or fair, it may lead to certain processes being continuously overlooked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;High Resource Demand&lt;/strong&gt;: If a few processes demand an excessive amount of resources, they can starve other processes by monopolizing those resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Long Wait Times&lt;/strong&gt;: Processes that are frequently preempted or that find themselves in competition for limited resources may experience starvation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  3. Preventing Starvation
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using ReentrantLock with Fairness&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Java’s &lt;strong&gt;ReentrantLock&lt;/strong&gt; provides an option to enforce fairness. By using the &lt;strong&gt;ReentrantLock&lt;/strong&gt; constructor with the true argument, we can ensure that threads acquire the lock in a first-come, first-served (FCFS) manner, preventing starvation.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Lock&lt;/span&gt; &lt;span class="n"&gt;lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReentrantLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Fair lock&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using Semaphore for Fair Resource Sharing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;Semaphore&lt;/strong&gt; is used to control access to a limited number of resources. By using a semaphore with fairness enabled, we can ensure that threads acquire permits in a fair order, avoiding starvation.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Semaphore&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Semaphore&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Fair semaphore&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The Semaphore(1, true) is a semaphore with only one permit and fairness enabled. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Avoiding Starvation in Producer-Consumer Problem (Using BlockingQueue)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In traditional producer-consumer problems, starvation may occur if the producer overwhelms the consumer or if consumers are denied access to the shared resource. &lt;strong&gt;BlockingQueue&lt;/strong&gt; prevents this because it automatically handles synchronization between producers and consumers. Both the producer and consumer are blocked when the queue is full or empty, respectively. This ensures a fair balance between the two and prevents one from overwhelming the other, thus avoiding starvation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using Java’s ForkJoinPool for Fair Task Scheduling&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In scenarios where multiple tasks are forked and joined, &lt;strong&gt;ForkJoinPool&lt;/strong&gt; in Java provides a way to balance the work fairly among threads. It ensures work-stealing, preventing starvation of less active threads. Java’s ForkJoinPool efficiently handles task splitting and balancing, ensuring that no thread starves for work. This is achieved using a work-stealing algorithm, where idle threads &lt;code&gt;steal&lt;/code&gt; tasks from busy threads to keep everything moving smoothly&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  4. How Operating System prevents Starvation
&lt;/h1&gt;

&lt;p&gt;Operating systems (OS) use various techniques to avoid starvation—a situation where certain processes or threads are denied necessary resources (such as CPU time, memory, or I/O access) for prolonged periods because higher-priority tasks dominate. Here are some of the common ways an OS prevents starvation:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;No.&lt;/th&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Prevents Starvation By&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Aging&lt;/td&gt;
&lt;td&gt;Gradually increases priority of waiting processes.&lt;/td&gt;
&lt;td&gt;Prevents long waits by adjusting priority based on wait time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Round-Robin Scheduling&lt;/td&gt;
&lt;td&gt;Allocates CPU time in a fixed cyclic order.&lt;/td&gt;
&lt;td&gt;Ensures all processes get CPU time, avoiding starvation.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Completely Fair Scheduler&lt;/td&gt;
&lt;td&gt;Allocates CPU based on fairness, independent of priority.&lt;/td&gt;
&lt;td&gt;Ensures fair distribution of CPU time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Priority Boosting&lt;/td&gt;
&lt;td&gt;Temporarily raises the priority of starved processes holding important resources.&lt;/td&gt;
&lt;td&gt;Prevents priority inversion and ensures high-priority tasks get needed resources.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Multilevel Feedback Queues&lt;/td&gt;
&lt;td&gt;Dynamically adjusts process priorities based on behavior.&lt;/td&gt;
&lt;td&gt;Promotes long-waiting processes to higher-priority queues.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Semaphores with Fairness&lt;/td&gt;
&lt;td&gt;Ensures fair access to resources through FIFO queues.&lt;/td&gt;
&lt;td&gt;Prevents low-priority tasks from being perpetually blocked by higher-priority tasks.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Fair Resource Allocation&lt;/td&gt;
&lt;td&gt;Distributes system resources like CPU and memory based on process demand and need.&lt;/td&gt;
&lt;td&gt;Prevents resource-hogging processes from starving others.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Fair I/O Scheduling&lt;/td&gt;
&lt;td&gt;Prioritizes I/O requests to ensure timely completion for all processes.&lt;/td&gt;
&lt;td&gt;Prevents disk I/O starvation for processes making low-priority requests.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By implementing these strategies, operating systems can ensure that no process or thread is indefinitely starved for resources, promoting a more fair and efficient use of system resources.&lt;/p&gt;

&lt;h1&gt;
  
  
  Key Takeaways for Software Engineers
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Starvation&lt;/strong&gt; is a &lt;strong&gt;progress issue&lt;/strong&gt; where a process keeps being denied access to a resource, but doesn’t necessarily involve other processes blocking it in a deadlock cycle. It is often caused by unfair scheduling or resource allocation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Starvation&lt;/strong&gt; happens even though the resource it needs is available, but it never gets the chance to acquire it. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Starvation&lt;/strong&gt; also happens even when there is no deadlock on the system. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Starvation&lt;/strong&gt; can be a side effect of deadlock prevention (e.g., prioritizing certain processes to avoid deadlock), so it’s crucial to balance fairness with deadlock avoidance strategies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Reference
&lt;/h1&gt;

&lt;p&gt;A huge thanks to the online documentation, community and all the resources available that made this write-up possible.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/anwaar/multithreading-key-concepts-for-engineers-part-1-4g73"&gt;Multithreading Concepts Part 1: Atomicity and Immutability &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/search?q=starvation&amp;amp;s=92824ecf-e7c1-406b-92b3-748d0d8791f7" rel="noopener noreferrer"&gt;Stackoverflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.sportskeeda.com/esports/cs2-server-reserved-game-lobby-error-possible-fixes-reasons" rel="noopener noreferrer"&gt;Info-graphics&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>multithreading</category>
      <category>starvation</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Multithreading Concepts Part 1: Atomicity and Immutability</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Mon, 30 Sep 2024 01:39:30 +0000</pubDate>
      <link>https://forem.com/anwaar/multithreading-key-concepts-for-engineers-part-1-4g73</link>
      <guid>https://forem.com/anwaar/multithreading-key-concepts-for-engineers-part-1-4g73</guid>
      <description>&lt;p&gt;Understanding key multithreading concepts is crucial for software developers, as it not only enhances skill set but also directly impacts application development, scalability, and the overall quality of software solutions. &lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Atomicity&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In the context of multithreading, atomic operations ensure that a thread can execute a series of actions without interruption from other threads. Multiple threads may attempt to read or write shared data simultaneously. Without atomicity, concurrent modifications can lead to inconsistent or unexpected results, commonly known as race conditions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Java Specification guarantees that 'reading' and 'writing' are atomic operations not their combinations. so an operation which 'reads, adds 1 and then writes the result back' is not atomic as per specification. such operations are called compound operations and they usually need to be atomic in context of their usage in our code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Examples of Atomic Operations:&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Incrementing a counter&lt;/strong&gt;: If two threads increment a counter at the same time without atomicity, they may both read the same value and write back the same incremented value, leading to a loss of one increment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Updating a shared variable&lt;/strong&gt;: If one thread is reading a value while another is modifying it, without atomicity, the reading thread may get an inconsistent value.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Achieving Atomicity:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Atomic Classes&lt;/strong&gt;: Many programming languages provide atomic classes (e.g., &lt;code&gt;AtomicInteger&lt;/code&gt;in Java) that encapsulate operations that are guaranteed to be atomic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronized Methods/Blocks&lt;/strong&gt;: In languages like Java, you can use the synchronized keyword to ensure that only one thread can execute a block of code or a method at a time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Locks&lt;/strong&gt;: Using explicit locks (e.g., &lt;code&gt;ReentrantLock&lt;/code&gt;in Java) to manage access to shared resources.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Classes in &lt;code&gt;java.util.concurrent.atomic&lt;/code&gt; also provide a lock-free approach to ensure thread safety, making them a preferred choice in many scenarios.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt; Using atomic classes simplifies code, as developers don’t need to manage locks and can focus on the logic of the program.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread Safety:&lt;/strong&gt; Atomic operations ensure that variables are safely updated across multiple threads without the risk of data corruption or race conditions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Immutability&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Immutability refers to the property of an object whose state cannot be modified after it is created. In programming, immutable objects are those that, once initialized, cannot be changed or altered. Instead of modifying an immutable object, a new object is created with the desired changes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Immutable means that once the constructor for an object has completed execution that instance can't be altered.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Characteristics of Immutable Objects&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No State Change:&lt;/strong&gt; Once an immutable object is created, its state (attributes or fields) remains constant throughout its lifetime.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Thread-Safe:&lt;/strong&gt; Immutable objects can be safely shared between multiple threads without the need for synchronization, as they cannot be modified.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hashcode Stability:&lt;/strong&gt; The &lt;code&gt;hashcode&lt;/code&gt; of an immutable object remains the same throughout its lifetime, making it suitable for use in hash-based collections like &lt;code&gt;HashMap&lt;/code&gt; or &lt;code&gt;HashSet&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Achieving Immutability:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use of Records (in &lt;code&gt;Java 14+&lt;/code&gt;):&lt;/strong&gt; In &lt;code&gt;Java&lt;/code&gt;, the record feature provides a concise way to create immutable data classes.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;ImmutablePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use Immutable Data Structures:&lt;/strong&gt; Utilize existing immutable data structures provided by the programming language or libraries, such as:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Java:&lt;/strong&gt; &lt;code&gt;Collections.unmodifiableList()&lt;/code&gt;, &lt;code&gt;List.of()&lt;/code&gt;, &lt;code&gt;Set.of()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt; &lt;code&gt;ImmutableList&lt;/code&gt;, &lt;code&gt;ImmutableArray&lt;/code&gt; from &lt;code&gt;System.Collections.Immutable&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Python:&lt;/strong&gt; &lt;code&gt;Tuples&lt;/code&gt;are inherently immutable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Final Fields:&lt;/strong&gt; Declare the fields of a class as &lt;code&gt;final&lt;/code&gt;. This ensures that the fields can only be assigned once, during object construction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Setters:&lt;/strong&gt; Avoid providing setter methods for mutable fields. This prevents external code from changing the state of an object after it's been constructed.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImmutablePoint&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ImmutablePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Static Factory Methods:&lt;/strong&gt; Instead of providing a public constructor, use static factory methods that return new instances of the object, making it clear that the state cannot be changed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Builder Pattern (for complex objects):&lt;/strong&gt; For objects that require many parameters, use the builder pattern to create immutable objects. The builder accumulates the parameters and constructs an immutable instance at the end.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Concurrency:&lt;/strong&gt; If the internal structure of an immutable object is valid, it will always be valid. There's no chance that different threads can create an invalid state within that object. Hence, immutable objects are Thread Safe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Garbage collection:&lt;/strong&gt; It's much easier for the garbage collector to make logical decisions about immutable objects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Outro
&lt;/h1&gt;

&lt;p&gt;Arming yourself with this knowledge not only enhances your ability to write high-performance code but also prepares you for the challenges of modern software development, where responsiveness and scalability are paramount. As you continue your journey into the world of multithreading, remember that each concept you master will contribute to your growth as a developer and your capacity to create applications that meet and exceed user expectations.&lt;/p&gt;

&lt;p&gt;Stay tuned as we will focus on &lt;code&gt;starvation&lt;/code&gt;, &lt;code&gt;deadlock&lt;/code&gt;, &lt;code&gt;race-condition&lt;/code&gt;, &lt;code&gt;OS scheduling&lt;/code&gt; and much more in upcoming write-up, that would elevate your programming skills and boost your career!&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;A huge thanks to the online documentation, community and all the resources available that made this write-up possible.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://hackaday.com/wp-content/uploads/2024/06/DALL-E-abstract-multithreading.png?w=800" rel="noopener noreferrer"&gt;Info-graphic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/cd/E19455-01/806-5257/6je9h032e/index.html" rel="noopener noreferrer"&gt;Understanding Basic Multithreading Concepts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://naveen-metta.medium.com/atomic-operations-in-java-mastering-thread-safety-and-concurrency-7c3360ec0bc5" rel="noopener noreferrer"&gt;Atomicity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/279507/what-is-meant-by-immutable" rel="noopener noreferrer"&gt;What is immutable&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>multithreading</category>
      <category>atomic</category>
      <category>java</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
