<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Syed Fawzul Azim on Medium]]></title>
        <description><![CDATA[Stories by Syed Fawzul Azim on Medium]]></description>
        <link>https://medium.com/@syed.fawzul.azim?source=rss-94e46dfb4db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*67N5pjG4VDPpKWLhxBVPzw.jpeg</url>
            <title>Stories by Syed Fawzul Azim on Medium</title>
            <link>https://medium.com/@syed.fawzul.azim?source=rss-94e46dfb4db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:00:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@syed.fawzul.azim/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mastering AWS CDK: Build, Deploy, and Manage Cloud Infrastructure]]></title>
            <link>https://medium.com/@syed.fawzul.azim/mastering-aws-cdk-build-deploy-and-manage-cloud-infrastructure-5dbf2ceb13b8?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/5dbf2ceb13b8</guid>
            <category><![CDATA[aws-cdk]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Sun, 23 Nov 2025 16:07:51 GMT</pubDate>
            <atom:updated>2025-11-23T16:09:38.323Z</atom:updated>
            <content:encoded><![CDATA[<h4>Simplify cloud infrastructure with AWS CDK: from constructs to deployment and stack management.</h4><p>Managing AWS resources manually or through lengthy configuration files can be tedious and prone to errors. <strong>Infrastructure as Code (IaC)</strong> addresses this by enabling you to define, manage, and automate cloud infrastructure using code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*lVT3NoPK6STGYfw-7j7_2g.jpeg" /></figure><h4><strong>There are several ways to do this:</strong></h4><p><strong>1. AWS Management Console</strong> — Create resources manually through the web interface.</p><ul><li># Manually create S3 via AWS Consol</li></ul><p><strong>2. CloudFormation (YAML/JSON templates)</strong> — AWS automatically provisions resources defined in templates.</p><pre>Resources:<br>  MyBucket:<br>    Type: AWS::S3::Bucket<br>    Properties:<br>      VersioningConfiguration:<br>        Status: Enabled</pre><p>Automated, but templates can be long and complex.</p><p><strong>3. Terraform</strong> — Multi-cloud IaC using HCL language.</p><pre>resource &quot;aws_s3_bucket&quot; &quot;my_bucket&quot; {<br>  bucket = &quot;my-example-bucket&quot;<br>  versioning { enabled = true }<br>}</pre><p><strong>4. AWS SDK / CLI</strong> — Script resource creation using code or commands.</p><pre>aws s3api create-bucket --bucket my-example-bucket</pre><p>Great for automation, but harder to manage a large infrastructure.</p><p><strong>5. AWS CDK (Cloud Development Kit)</strong> — Define AWS resources using programming languages like TypeScript or Python. CDK converts your code into CloudFormation automatically:</p><p><strong>Example:</strong> Creating an S3 bucket in CDK (TypeScript)</p><pre>new s3.Bucket(this, &#39;MyBucket&#39;, { versioned: true });</pre><p>Developer-friendly, reusable, and easier to maintain.</p><p>Among these, <strong>AWS CDK</strong> strikes a balance between simplicity, automation, and flexibility, a developer-friendly framework that lets you define AWS infrastructure using familiar programming languages like <strong>TypeScript, Python, Java, C#, and Go</strong>.</p><p>With AWS CDK, you can use loops, conditionals, and functions to build cloud environments programmatically, making your infrastructure <strong>easier to manage, test, and reuse</strong>.</p><p><strong>CDK vs CloudFormation</strong></p><ul><li>CloudFormation uses declarative YAML/JSON templates.</li><li>CDK lets you use programming languages, loops, and reusable constructs.</li><li>CDK generates CloudFormation automatically, combining the power of code with AWS-native IaC.</li></ul><p><strong>Real-World Use Cases</strong></p><ul><li>Deploy serverless applications (Lambda + API Gateway + DynamoDB).</li><li>Build CI/CD pipelines with CodePipeline.</li><li>Create VPCs, ECS clusters, and networking infrastructure.</li><li>Automate multi-environment deployments.</li></ul><h4>How AWS CDK Works:</h4><p>→ Prerequisites:<br>- Node<br>- AWS CLI + configure with credentials<br>- aws-cdk</p><p>→ Create a new CDK project:<br>1. Make a new folder and navigate into it:</p><pre>mkdir aws-cdk-app &amp;&amp; cd aws-cdk-app<br>mkdir first-app &amp;&amp; cd first-app</pre><p>2. Initialize a CDK project (TypeScript example):</p><pre>cdk init app --language typescript</pre><p>This creates a standard CDK folder structure:</p><pre>first-app/<br>├── bin/ # Entry point<br>├── lib/ # Your stacks go here<br>├── cdk.json # CDK config<br>├── package.json # Node dependencies<br>└── tsconfig.json # TypeScript config</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*JnCuHNMs39ST58ndY4aW_Q.png" /></figure><p>In a TypeScript AWS CDK project, the cdk.json file tells the CDK <strong>how to run your app</strong>. The &quot;app&quot; field is the command that CDK executes to start your stack. For example:</p><pre>&quot;app&quot;: &quot;npx ts-node --prefer-ts-exts bin/first-app.ts&quot;</pre><h3>App → Stack → Construct Hierarchy</h3><ol><li><strong>App</strong> — The root of your CDK project, representing your entire infrastructure.</li><li><strong>Stack</strong> — A deployable unit within the app, mapped to a CloudFormation stack.</li><li><strong>Construct</strong> — The building blocks of AWS resources, which can be simple (like an S3 bucket) or complex (like a serverless app).</li></ol><pre>const app = new cdk.App();<br>new FirstAppStack(app, &#39;FirstAppStack&#39;);<br><br>export class FirstAppStack extends cdk.Stack {<br>  constructor(scope: Construct, id: string, props?: cdk.StackProps) {<br>    super(scope, id, props);<br><br>    // The code that defines your stack goes here<br>  }<br>}</pre><h4>Bootstrapping, Synth, and Deploy in AWS CDK:</h4><p><strong>1. CDK Bootstrap<br></strong>Before deploying, AWS CDK requires a bootstrap stack in your account. This stack provisions resources CDK needs to deploy other stacks, such as:</p><ul><li>S3 bucket for storing assets (Lambda code, Docker images)</li><li>IAM roles for deployment</li></ul><p><strong>Command:</strong></p><pre>cdk bootstrap</pre><ul><li>Creates a CloudFormation stack CDKToolkit in your AWS account.</li><li>The CDKToolkit Stack provisions resources CDK needs for deployments.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MuFYcNTsXHBmJzse5zYQFA.png" /><figcaption>AWS Console: CloudFormation-&gt;Stacks</figcaption></figure><p>This sets up your AWS account to work with CDK. You only need to run this once per account/region.</p><p>Now that our CDK is bootstrapped, we can start with constructs for the stack.</p><p>Update the FirstAppStack with different levels of S3 bucket contracts:<br>AWS CDK uses <strong>constructs</strong> as the building blocks for cloud resources. They come in three levels:</p><ol><li><strong>L1 Constructs</strong> — Direct CloudFormation resources. They map 1:1 with AWS resources but require all properties to be manually defined.</li><li><strong>L2 Constructs</strong> — Higher-level, opinionated resources with sensible defaults, easier to use.</li><li><strong>L3 Constructs (Patterns)</strong> — Pre-built architecture patterns combining multiple L2 constructs for common use cases.</li></ol><pre>import * as cdk from &#39;aws-cdk-lib&#39;;<br>import { Construct } from &#39;constructs&#39;;<br>import {Bucket} from &#39;aws-cdk-lib/aws-s3&#39;;<br><br><br>// L3 Construct<br>class LambdaWithS3 extends Construct {<br>  constructor(scope: Construct, id: string, bucketName: string) {<br>    super(scope, id);<br><br>    const bucket = new Bucket(this, &#39;MyBucket&#39;, { bucketName, versioned: true });<br><br>    const lambda = new Function(this, &#39;MyLambda&#39;, {<br>      runtime: Runtime.NODEJS_18_X,<br>      handler: &#39;index.handler&#39;,<br>      code: Code.fromAsset(&#39;lambda&#39;),<br>      environment: { BUCKET_NAME: bucket.bucketName },<br>    });<br><br>    bucket.grantReadWrite(lambda);<br>  }<br>}<br><br>export class FirstAppStack extends cdk.Stack {<br>  constructor(scope: Construct, id: string, props?: cdk.StackProps) {<br>    super(scope, id, props);<br><br>    // The code that defines your stack goes here<br><br>    // L1 Construct<br>    new cfnBucket(this, &#39;MyL1Bucket&#39;, {<br>      licecycleConfiguration: {<br>        rules: [{<br>          expirationInDays: 3,<br>          status: &#39;Enabled&#39;,<br>        }],<br>      },<br>    });<br><br>    // L2 Construct<br>    new Bucket(this, &#39;MyL2Bucket&#39;, {<br>      lifecycleRules: [{<br>        expiration: cdk.Duration.days(3),<br>      }],<br>    });<br><br>    // L3 Construct<br>    new LambdaWithS3(this, &#39;MyLambdaS3Pattern&#39;, &#39;my-lambda-bucket&#39;);<br><br>  }<br>}</pre><ul><li><strong>L1 (</strong><strong>CfnBucket)</strong> – Full control, verbose configuration.</li><li><strong>L2 (</strong><strong>Bucket)</strong> – Easier to use, with default settings.</li><li><strong>L3 ( </strong><strong>LambdaWithS3)</strong> – Combines an S3 bucket and a Lambda function in a single reusable component.<br>- Encapsulates best practices: bucket creation, Lambda deployment, and permissions.<br>- Can be reused across stacks with different parameters, reducing repetitive code.</li></ul><blockquote>Note: When you create a Lambda function in AWS CDK, you need to provide the function code. Here’s a simple “Hello World” Lambda written in TypeScript</blockquote><pre>// create this inside first-app/lambda/index.ts<br>export const handler = async (event: any) =&gt; {<br>  return {<br>    message: &#39;Hello World!&#39;,<br>  };<br>};</pre><p>handler is the entry point for the Lambda function. event contains input data when the Lambda is triggered (can be from API Gateway, S3, or other AWS services).</p><p>After defining your stacks and constructs, you can preview and deploy your infrastructure using CDK commands.</p><p><strong>2. CDK Synth– Generate CloudFormation Template</strong></p><p>The cdk synth command converts your CDK code into a CloudFormation template, without actually creating any resources in AWS.</p><pre>cdk synth</pre><p><strong>What it does:</strong></p><ul><li>Translates all your L1, L2, and L3 constructs into a CloudFormation YAML template.</li><li>Let&#39;s you preview what AWS resources will be created or changed.</li><li>Helps catch errors before deployment.</li><li>Genereates: <em>aws-cdk-app/first-app/cdk.out/FirstAppStack.template.json</em></li></ul><p><strong>3. CDK Deploy – Deploy Resources to AWS</strong></p><p>Once you’re happy with your template, use cdk deploy to provision the resources in your AWS account.</p><pre>cdk deploy</pre><p><strong>What it does:</strong></p><ol><li>Compares your current stack with the last deployed stack (diff).</li><li>Shows a summary of changes and asks for confirmation.</li><li>Creates or updates AWS resources as defined in your CDK app.</li></ol><p>If deployment goes well, you will see something like this on CloudFormation:</p><p>FirstAppStack will be created with the necessary resources.<br> → MyL2Bucket<br> → MyL1Bucket<br> → MyLambdaS3Pattern: (MyBucket, MyLambda)</p><p>And others like: CDKMetadata</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SigMWsRdMYpC9KnPAkEkRQ.png" /></figure><p>Using cdk diff to Preview Changes:</p><p>When you make changes to your CDK code, such as renaming a Lambda function, you may want to see what will change in AWS before actually deploying. That’s where cdk diff comes in.</p><pre>cdk diff</pre><p><strong>What happens:</strong></p><ul><li>CDK compares your <strong>current deployed stack</strong> with the <strong>new code</strong>.</li><li>Shows a <strong>summary of differences</strong>, for example:</li><li>The old Lambda (MyLambda) will be <strong>deleted</strong>.</li><li>A new Lambda (MyRenamedLambda) will be <strong>created</strong>.</li></ul><blockquote><em>This is extremely helpful to </em><strong><em>preview changes</em></strong><em> and avoid accidental deletions or misconfigurations before running </em><em>cdk deploy.</em></blockquote><p><strong>→ Accessing Data from Deployed Resources:</strong></p><p>When you deploy a stack, you can expose values — such as an S3 bucket name — using CloudFormation Outputs. This allows other stacks, scripts, or manual processes to easily read those values.</p><pre>export class FirstAppStack extends cdk.Stack {<br>  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {<br>    super(scope, id, props);<br><br>    const bucket = new Bucket(this, &#39;MyBucket&#39;);<br><br>    new cdk.CfnOutput(this, &#39;BucketNameOutput&#39;, {<br>      value: bucket.bucketName,<br>      description: &#39;The name of the S3 bucket&#39;,<br>    });<br>  }<br>}</pre><p>After deploying, the output will appear in the terminal:</p><pre>cdk deploy</pre><pre>Outputs:<br>FirstAppStack.BucketNameOutput = mybucket-12345</pre><p>You can now use this value in other stacks, scripts, or manually.</p><p>How to use this value: <br>- Pass it into another CDK stack.<br>- Read it from scripts using AWS CLI.<br>- Manually copy it for testing or other integrations.</p><p>This is the standard way CDK exposes resource information after deployment.</p><p><strong>→ Using Deployment Parameters in CDK:</strong></p><p>You can define a deployment parameter Duration to control the S3 bucket lifecycle, including minimum, maximum, and default values.</p><pre>    // Define deployment parameter for duration<br>    const duration = new cdk.CfnParameter(this, &#39;Duration&#39;, {<br>      type: &#39;Number&#39;,<br>      default: 7,<br>      minValue: 1,<br>      maxValue: 365,<br>      description: &#39;Number of days before S3 objects expire&#39;,<br>    });<br><br>    // Create bucket with lifecycle rule using the parameter<br>    new Bucket(this, &#39;MyBucket&#39;, {<br>      lifecycleRules: [<br>        {<br>          expiration: cdk.Duration.days(duration.valueAsNumber),<br>        },<br>      ],<br>    });</pre><p><strong>Usage:</strong></p><ul><li>Default lifecycle: 7 days</li><li>Override at deploy time:</li></ul><pre>cdk deploy --parameters Duration=30</pre><ul><li>Result: Objects in the bucket will expire after <strong>30 days</strong>, respecting the min/max constraints (1–365 days).</li></ul><p><strong>→ Deleting a CDK Stack:</strong></p><p>One way is to delete using the AWS Console:<br> — AWS Console → CloudFormation → Select your stack → <strong>Delete</strong></p><p>Another way,<br>Deleting a stack means removing all resources created by that stack through AWS CloudFormation. CDK doesn’t delete anything directly — it relies entirely on CloudFormation.</p><pre>cdk destroy &lt;constructID&gt;</pre><p>For our example:</p><pre>cdk destroy FirstAppStack</pre><p>This command:</p><ul><li>Shows a confirmation prompt.</li><li>Removes the entire stack and its resources.</li><li>Uses the same permissions that originally created them.</li></ul><p>What Will Not Be Deleted Automatically:</p><p>Services like AWS Lambda, API Gateway, ECS, and others automatically create <strong>CloudWatch Log Groups</strong>.<br>However, when you delete a CDK stack:</p><p>✔ The resources (like Lambda functions) are removed.<br> ✖ Their <em>CloudWatch</em> Log Groups remain unless explicitly configured.</p><p>Some AWS services have protection behaviours to avoid accidental data loss. Even if the stack is deleted, these resources may remain.</p><p><strong>1. S3 Buckets With Data Inside</strong></p><p>If an S3 bucket still contains objects, CloudFormation <strong>cannot delete it</strong> unless:</p><ul><li>The bucket has <strong>autoDeleteObjects: true</strong> (CDK option)</li><li>OR you manually empty the bucket first</li></ul><blockquote>Note: Don’t delete the S3 bucket created by cdk, which has the resources for CloudFormation.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a9jpORzquC-jQAibCXEGzA.png" /><figcaption>S3 bucket by CDK</figcaption></figure><p><strong>2. Retained or Imported Resources</strong></p><p>If a resource is marked with RemovalPolicy.RETAIN, CDK tells CloudFormation to keep it, even during stack deletion.</p><p>Example cases:</p><ul><li>Databases</li><li>KMS keys</li><li>S3 buckets</li><li>SNS topics</li><li>Imported VPCs</li></ul><p><em>Github Link: </em><a href="https://github.com/syedfawzulazim/aws-cdk-app"><em>https://github.com/syedfawzulazim/aws-cdk-app</em></a></p><h4>Conclusion</h4><p>AWS CDK empowers developers to define, deploy, and manage cloud infrastructure using familiar programming languages, combining the flexibility of code with the power of CloudFormation. By understanding constructs (L1, L2, L3), using deployment parameters, accessing stack outputs, and safely managing stack deletion, you can build scalable, reusable, and maintainable AWS environments.</p><p>With CDK, infrastructure as code becomes <strong>easier, safer, and more automated</strong>, enabling you to focus on building applications rather than managing resources manually.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5dbf2ceb13b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Complete Guide to AWS Identity and Access Management (IAM)]]></title>
            <link>https://medium.com/@syed.fawzul.azim/the-complete-guide-to-aws-identity-and-access-management-iam-ed5787fcad88?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed5787fcad88</guid>
            <category><![CDATA[aws-iam]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[iam-policy]]></category>
            <category><![CDATA[iam-roles]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Tue, 21 Oct 2025 10:51:46 GMT</pubDate>
            <atom:updated>2025-10-25T13:09:01.482Z</atom:updated>
            <content:encoded><![CDATA[<p>In AWS, managing who can access what, is crucial for maintaining a secure cloud environment. Identity and Access Management (IAM) helps you control access to AWS resources, ensuring only authorized users and services can perform specific actions.</p><p><strong>Why IAM is critical:</strong> It protects your account from unauthorized access, accidental changes, and potential data breaches. With IAM, you can assign precise permissions and follow the principle of least privilege.</p><p><strong>Common mistakes:</strong> Many beginners use the root user for daily tasks, share credentials, or assign overly broad permissions. These practices increase security risks and should be avoided.</p><p><strong>Shared Responsibility Model:</strong> AWS secures the cloud infrastructure, while you’re responsible for securing your identities, configurations, and data. IAM is the key tool that helps you meet your side of that responsibility.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EvuoPIe2qDemsquf0vD-gA.png" /><figcaption>AWS Account Overview</figcaption></figure><h4>Introduction to AWS Accounts:</h4><p>To get started with AWS, you first need to create an AWS account using a unique email address and a credit card for billing.</p><ul><li><strong>Root User:</strong> When you create an AWS account, a root user is automatically created.</li><li><strong>Full Access:</strong> The root user has full administrative access to all resources.</li><li><strong>Limited Use:</strong> The root user should only be used for initial setup and critical tasks.</li><li><strong>IAM Users:</strong> For daily operations, create IAM users using AWS Identity and Access Management (IAM).</li><li><strong>IAM Capabilities:</strong> IAM allows you to define users, groups, roles, and policies to manage permissions securely.</li><li><strong>Access Methods:</strong> AWS can be accessed via:<br>- Management Console (browser)<br>- CLI (Command Line Interface)<br>- API (Application Programming Interface)</li><li><strong>Authentication:</strong> Access requires email/passwords (console) or access keys/SecretKeys (CLI/API).</li></ul><h4>IAM (Identity and Access Management) Overview:</h4><p>IAM is AWS’s service for managing identities, access, and permissions securely within an AWS account. Ensures that <strong>only authorized users, applications, or services</strong> can access specific AWS resources.</p><p><strong>Key Concepts:</strong></p><ul><li><strong>Users: </strong>Represents a person or service that interacts with AWS.</li><li><strong>Groups: </strong>A collection of IAM users. Useful for assigning permissions to multiple users at once. Permissions assigned to a group by policies apply to all its members.</li><li><strong>Roles:</strong> An IAM Role is an AWS identity with specific permissions that can be assumed temporarily by trusted entities (users or services), instead of being permanently assigned to a user. Often used for EC2 instances, Lambda, or cross-account access.</li><li><strong>Policies: </strong>JSON documents attached to users, groups, or roles that define who can do what on which resources. <br>Two main types:<br>- <strong>Managed Policies</strong>: Predefined by AWS or created by users, reusable.<br>- <strong>Inline Policies</strong>: Embedded directly into a user, group, or role, specific to that entity.</li><li><strong>Trust Relationships: </strong>Used by roles to define who (which users or services) can assume the role.</li><li>IAM is <strong>global</strong>, not tied to a specific AWS region, meaning users, roles, and policies work across all regions.</li><li>Allows <strong>secure delegation of administrative responsibilities</strong> without sharing root credentials.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jLOWOJ-QiHwImUt9KyqWPA.png" /><figcaption>IAM (Identity and Access Management) work flow</figcaption></figure><p><strong>How IAM Works: (Authentication and Authorization)</strong></p><p><strong>1. Access Methods:</strong> AWS accounts can be accessed via:<br>- AWS Management Console (web interface)<br>- AWS CLI (command-line interface)<br>- AWS API (programmatic access via code)</p><p><strong>2. Principals:</strong> Entities that can make requests to AWS resources:<br>- IAM Users<br>- IAM Roles<br>- Federated Users (from external identity providers like Google, Facebook)<br>- Applications using access keys or roles to authenticate programmatically</p><p><strong>3. Authentication:<br></strong>- All principals must authenticate before performing actions on AWS resources.</p><p><strong>4. Request Context:</strong> When a principal wants to perform an action or operation on AWS resources, a request context is created. AWS evaluates the request using context for authorization, which includes:<br>- Action being attempted (e.g., RunInstances, GetObject)<br>- Target resource (e.g., EC2 instance, S3 bucket)<br>- Principal identity<br>- Environment data (e.g., IP address)<br>- Resource-specific data</p><p><strong>5. Authorization:</strong> <br>AWS determines whether the request is allowed or denied based on policies and permissions.</p><p><strong>6. Permissions: <br> → Identity-Based Policies:</strong><br>- These are policies attached to IAM identities: users, groups, or roles.<br><strong> → Resource-Based Policies:</strong><br>- These are policies attached directly to AWS resources instead of identities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y72TYmBDr7ZxMOq6WAIsPg.png" /><figcaption>Users, Groups, Roles and Policies</figcaption></figure><p>Within your AWS account, you can create and manage Users, Groups, Roles, and Policies. These elements together define who can access what, and under what conditions.</p><h4>1. IAM Users</h4><ul><li>Represent individual identities within your AWS account.</li><li>Each user can have credentials to sign in and make requests:<br>- AWS Management Console: Username and password.<br>- CLI &amp; API: Access key and secret access key.</li><li>By default, new users have no permissions — access must be explicitly granted through policies.</li><li>Each user has:<br>- Friendly Name: Readable identifier (e.g., “Alice” or “DevUser1”).<br>- Amazon Resource Name(ARN): Globally unique identifier for the user, including the account ID.</li><li>AWS allows up to 5,000 IAM users per account.</li></ul><h4>2. IAM Groups</h4><ul><li>A group is a container for IAM users — used to organize users and assign permissions collectively.</li><li>Example groups: Admins, Developers, Operations.</li><li>Each group can have multiple users, and a user can belong to up to 10 groups.</li><li><strong>Policies</strong> are attached to groups — users inherit permissions from their group’s policies.</li><li>This structure simplifies management by applying permissions to groups rather than individual users.</li><li><strong>Example:<br>- </strong>Admin group: Full access to EC2 and S3.<br>- Developer group: Limited access to deploy code and manage specific resources.</li><li><strong>Best Practice:</strong> Use groups to reflect organizational roles or departments for cleaner permission management.</li></ul><h4>3. IAM Roles</h4><ul><li>A role is an identity with permissions that can be assumed temporarily by trusted entities (users, applications, or services).</li><li>Roles don’t have long-term credentials like passwords or access keys.</li><li>Instead, they provide temporary security credentials via the AWS Security Token Service (STS). The STS API (e.g., sts:AssumeRole) issues temporary credentials:<br>- Access Key ID<br>- Secret Access Key<br>- Session Token</li><li>Used for delegation of permissions — entities act “as the role” once assumed.</li><li>Permissions are defined in the permission policy.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6v7ey8Ia-IYUknNOkND06Q.png" /><figcaption>IAM Role with policies</figcaption></figure><ul><li><strong>Who can assume a role:<br>-</strong> It is defined in the trust policy.<br>- IAM users within the same account.<br>- Users or roles in another AWS account (cross-account access).<br>- AWS services like EC2, Lambda, or ECS (service roles).<br>- Federated identities (e.g., from Google, Okta, or SAML providers).</li><li><strong>How role assumption works:<br>- </strong>A user/application calls the API action sts:AssumeRole.<br>- AWS provides temporary credentials for the session.<br>- The identity gains permissions defined in the role’s policy for a limited time.</li><li><strong>Common use cases:<br>- </strong>EC2 instances accessing S3 buckets without storing credentials.<br>- Cross-account resource sharing.<br>- Federated user access from corporate directories.</li><li><strong>Best Practices:<br></strong>- Prefer roles over long-term IAM user credentials for applications and services.<br>- Assign only the minimum required permissions (principle of least privilege).<br>- Monitor role usage via AWS CloudTrail.</li></ul><h4>4. IAM Policies</h4><ul><li>Policies define permissions — determining which actions are allowed or denied on specific resources.</li><li>Written in JSON format and consists of statements with Effect, Action, Resource, and optional Condition.</li><li><strong>Two main types of policies:<br>a) Identity-Based Policies</strong><br>- Attached to users, groups, or roles.<br>- Grant permissions to those identities.<br>- Example: Allow s3:PutObject on a specific bucket.<br><strong>b) Resource-Based Policies<br></strong>- Attached directly to resources (e.g., S3 buckets, SNS topics, SQS queues).<br>- Specify who (principal) can access the resource and what actions are allowed.<br>- Example: S3 bucket policy allowing another account access.<br><strong>Others:</strong><br>- Inline Policies: A policy embedded directly into a single user, group, or role.<br>- Managed Policies: A separate, reusable policy that can be attached to multiple users, groups, or roles. (AWS-managed or Customer-managed)</li><li><strong>Key principle:<br></strong>- All actions are <strong><em>denied</em></strong> by default.<br>- You must explicitly allow access via a policy statement.</li><li><strong>Evaluation logic:<br>- </strong>If multiple policies apply, AWS evaluates all and denies override allows (explicit deny &gt; allow).</li></ul><h4>Access Control Methods:</h4><p><strong>1. RBAC — Role-Based Access Control:</strong></p><p>Access is granted based on roles or groups — each role has a set of predefined permissions. Users get permissions by being assigned to those roles/groups.</p><p>In AWS IAM, RBAC is typically implemented using:</p><ul><li><strong>IAM Groups</strong> → e.g., Developers, Testers, Admins</li><li><strong>IAM Roles</strong> → e.g., EC2AccessRole, ProdReadOnlyRole</li></ul><p><strong>2. ABAC — Attribute-Based Access Control:</strong></p><p>Access is granted based on attributes (tags or metadata) of:</p><ul><li>The user or role</li><li>The resource they are trying to access</li><li>And possibly the environment (like time, region, etc.)</li></ul><p>Instead of fixed roles, AWS IAM evaluates tags dynamically during each request.</p><p>Example:<br>Only DB admins can perform RDS operations <strong>on production resources</strong>.</p><pre>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;rds:DescribeDBInstances&quot;,<br>        &quot;rds:StartDBInstance&quot;,<br>        &quot;rds:StopDBInstance&quot;,<br>        &quot;rds:ModifyDBInstance&quot;<br>      ],<br>      &quot;Resource&quot;: &quot;*&quot;,<br>      &quot;Condition&quot;: {<br>        &quot;StringEquals&quot;: {<br>          &quot;aws:PrincipalTag/Department&quot;: &quot;DBAdmin&quot;,<br>          &quot;aws:ResourceTag/Environment&quot;: &quot;production&quot;<br>        }<br>      }<br>    }<br>  ]<br>}</pre><p><strong>Explanation</strong></p><ul><li>&quot;aws:PrincipalTag/Department&quot;: &quot;DBAdmin&quot;<br> → Grants access <strong>only if the IAM user or role</strong> has a tag Department=DBAdmin.</li><li>&quot;aws:ResourceTag/Environment&quot;: &quot;production&quot;<br> → Grants access <strong>only to RDS instances</strong> that are tagged with Environment=production.</li></ul><p><strong>Permission Boundary:</strong></p><p>A permission boundary is an advanced IAM feature that defines the maximum permissions an IAM user or role can have — <em>even if other policies try to grant more</em>.</p><h4><strong>AWS IAM policy structure:</strong></h4><ul><li><strong>Effect</strong> → Allow or Deny</li><li><strong>Action</strong> → What API calls are allowed/denied</li><li><strong>Resource</strong> → Where the policy applies</li><li><strong>Condition</strong> → Optional fine-grained rules</li><li><strong>Statement</strong> → Each permission block</li><li><strong>Version</strong> → Policy language version</li></ul><pre>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot; | &quot;Deny&quot;,<br>      &quot;Action&quot;: &quot;service:action&quot; | [&quot;service:action1&quot;, &quot;service:action2&quot;],<br>      &quot;Resource&quot;: &quot;arn:aws:service:region:account-id:resource&quot; | &quot;*&quot;,<br>      &quot;Condition&quot;: { /* optional */ }<br>    }<br>  ]<br>}</pre><p>Allow a developer to list and read S3 objects:</p><pre>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;s3:ListBucket&quot;,<br>        &quot;s3:GetObject&quot;<br>      ],<br>      &quot;Resource&quot;: [<br>        &quot;arn:aws:s3:::my-app-bucket&quot;,<br>        &quot;arn:aws:s3:::my-app-bucket/*&quot;<br>      ]<br>    }<br>  ]<br>}</pre><p>AWS Identity and Access Management (IAM) is the foundation of secure access control in AWS. It enables you to manage users, groups, roles, and permissions effectively — ensuring that only authorized entities can access specific resources. By applying the <strong>principle of least privilege</strong>, using <strong>roles instead of long-term credentials</strong>, and implementing <strong>identity- and resource-based policies</strong>, you can maintain a robust and scalable security model. Mastering IAM is essential for protecting your AWS environment and fulfilling your part of the <strong>Shared Responsibility Model</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed5787fcad88" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Java Keywords: Access Modifiers, Final, Static, and More]]></title>
            <link>https://medium.com/@syed.fawzul.azim/mastering-java-keywords-access-modifiers-final-static-and-more-7bdd50b455f5?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bdd50b455f5</guid>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Sat, 13 Sep 2025 11:19:07 GMT</pubDate>
            <atom:updated>2025-09-13T11:19:07.840Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Comprehensive Guide to Java’s Core Keywords, Default Behaviors, and Best Practices for Writing Clean, Maintainable Code</h4><h4>Access Modifiers:</h4><ul><li><strong>private</strong> → Accessible only within the same class.</li><li><strong>default (package-private)</strong> → Accessible within the same package (when no modifier is specified).</li><li><strong>protected</strong> → Accessible within the same class, same package, and in subclasses (even if they are in different packages).</li><li><strong>public</strong> → Accessible from anywhere: same class, same package, subclasses, and other packages.</li></ul><p><strong>Rule of Access Modifier Changes in Overriding:</strong></p><p>When overriding a method:</p><ul><li><strong>Cannot reduce visibility</strong> (narrower than the parent).</li><li><strong>Can keep the same visibility</strong>.</li><li><strong>Can broaden visibility</strong> (wider than the parent).</li></ul><p>Example:</p><pre>// File: animals/ParentProtected.java<br>package animals;<br><br>public class ParentProtected {<br>    protected void speak() {<br>        System.out.println(&quot;ParentProtected speaking...&quot;);<br>    }<br>}</pre><pre>// File: animals/ChildSame.java<br>package animals;<br><br>public class ChildSame extends ParentProtected {<br>    @Override<br>    protected void speak() {   // same visibility<br>        System.out.println(&quot;ChildSame speaking (protected)&quot;);<br>    }<br>}<br>// Broaden visibility<br>class ChildBroader extends ParentProtected {<br>    @Override<br>    public void speak() {   // broadened from protected → public<br>        System.out.println(&quot;ChildBroader speaking (public)&quot;);<br>    }<br>}</pre><pre>// File: animals/ChildReduced.java<br>package animals;<br><br>public class ChildReduced extends ParentProtected {<br>    @Override<br>    void speak() {   // ❌ Compile error: weaker access privileges<br>        System.out.println(&quot;ChildReduced speaking (default)&quot;);<br>    }<br>}</pre><p><strong>So, Visibility can stay the same or broaden, but it can never be reduced.</strong></p><h4>final &amp; static:</h4><p>The final keyword in Java can be used with <strong>variables, methods, and classes</strong>.</p><p>a) Final Variable<br>- Once assigned, its value cannot be changed.<br>- Commonly used for constants.</p><pre>class Constants {<br>    final double PI = 3.14159; // constant<br>}<br><br>public class TestFinalVariable {<br>    public static void main(String[] args) {<br>        Constants c = new Constants();<br>        // c.PI = 3.14; // ❌ Compile-time error<br>        System.out.println(&quot;PI = &quot; + c.PI);<br>    }<br>}</pre><p>b) Final Method<br>- Cannot be overridden by subclasses.</p><pre>class Parent {<br>    final void showMessage() {<br>        System.out.println(&quot;This is a final method&quot;);<br>    }<br>}<br><br>class Child extends Parent {<br>    // @Override<br>    // void showMessage() {} // ❌ Compile-time error<br>}</pre><p>C) Final Class<br>- Cannot be extended.<br>- Example: String class is final.</p><pre>final class Vehicle {<br>    void run() {<br>        System.out.println(&quot;Vehicle is running&quot;);<br>    }<br>}<br><br>// class Car extends Vehicle {} // ❌ Compile-time error</pre><p>The static keyword in Java is used to <strong>associate a variable, method, or block with the class instead of instances</strong>.</p><p>a) Static Variable<br>- Assigned during declaration or in a <strong><em>static</em></strong> block.<br>- Shared across all instances.</p><pre>class Counter {<br>    static int count = 0; // shared for all objects<br><br>  Counter() {<br>        count++;<br>    }<br>}<br>public class TestStaticVariable {<br>    public static void main(String[] args) {<br>        new Counter();<br>        new Counter();<br>        new Counter();<br>        System.out.println(&quot;Count = &quot; + Counter.count); // Count = 3<br>    }<br>}</pre><p>b) Static Method<br>- Can be called without creating an object.<br>- Cannot access instance variables directly.</p><pre>class MathUtil {<br>    static int square(int x) {<br>        return x * x;<br>    }<br>}<br><br>public class TestStaticMethod {<br>    public static void main(String[] args) {<br>        int result = MathUtil.square(5); // call without object<br>        System.out.println(&quot;Square = &quot; + result);<br>    }<br>}</pre><pre>class Test {<br>    int number = 10;       // instance variable<br>    static int staticNum = 20; // static variable<br><br>    static void staticMethod() {<br>        // System.out.println(number); // ❌ Compile-time error<br>        System.out.println(staticNum); // ✅ Allowed<br>    }<br>    void instanceMethod() {<br>        System.out.println(number);     // ✅ Allowed<br>        System.out.println(staticNum);  // ✅ Allowed<br>    }<br>}<br>public class Main {<br>    public static void main(String[] args) {<br>        Test.staticMethod(); // can call static method without object<br>        Test obj = new Test();<br>        obj.instanceMethod(); // can access both instance and static<br>    }<br>}</pre><p>c) Static Block<br>- Executes once when the class is loaded.</p><pre>class Init {<br>    static {<br>        System.out.println(&quot;Static block executed!&quot;);<br>    }<br>}<br><br>public class TestStaticBlock {<br>    public static void main(String[] args) {<br>        Init obj = new Init(); // Static block runs before main<br>    }<br>}</pre><p><strong>Default values of static variables</strong> when they are not explicitly assigned:</p><pre>class Defaults {<br>    static int number;         // default 0<br>    static boolean flag;       // default false<br>    static String text;        // default null<br>}<br><br>public class TestDefaults {<br>    public static void main(String[] args) {<br>        System.out.println(&quot;Default int: &quot; + Defaults.number);      // 0<br>        System.out.println(&quot;Default boolean: &quot; + Defaults.flag);   // false<br>        System.out.println(&quot;Default String: &quot; + Defaults.text);    // null<br>    }<br>}</pre><p>Together, static final creates a constant that is <strong><em>class-level</em> </strong>and <strong><em>immutable</em>.</strong></p><p>Initialization Rules:</p><ul><li>Must be initialized at declaration or in a static block.</li><li>Cannot be initialized in non-static methods or blocks.</li><li>For primitive types or String, the value is immutable and thread-safe.</li><li>For objects (e.g., static final List MY_LIST = new ArrayList&lt;&gt;()), the reference is immutable, but the object’s state can still change (e.g., elements can be added to the list).</li></ul><pre>import java.util.ArrayList;<br>import java.util.List;<br><br>class Example {<br>    // Static final primitive<br>    static final int MAX_USERS;<br>    <br>    // Static final object<br>    static final List&lt;String&gt; NAMES = new ArrayList&lt;&gt;();<br>    // Static block to initialize primitive<br>    static {<br>        MAX_USERS = 100;<br>    }<br>    static void modifyList() {<br>        NAMES.add(&quot;Alice&quot;);  // ✅ Allowed: modifying object state<br>        // NAMES = new ArrayList&lt;&gt;(); // ❌ Not allowed: cannot reassign reference<br>    }<br>}<br>public class TestStaticFinal {<br>    public static void main(String[] args) {<br>        System.out.println(&quot;Max Users: &quot; + Example.MAX_USERS);<br>        <br>        Example.modifyList();<br>        System.out.println(&quot;Names: &quot; + Example.NAMES);<br>    }<br>}</pre><h4>Default Access Modifiers: — interfaces &amp; abstract class</h4><p>Interfaces:</p><ul><li>All fields in an interface are implicitly public static final .</li></ul><pre>interface MyInterface { // Equivalent to: public interface MyInterface<br>    int MY_CONSTANT = 10; // Implicitly public static final<br>}</pre><ul><li>By default, methods declared without a body in an interface are implicitly public abstract.</li><li>Default and static methods (since Java 8): If explicitly marked with default or static, they are public by default.</li><li>No package-private or protected fields/methods unless explicitly specified (e.g., private methods since Java 9).</li></ul><pre>interface MyInterface {<br>    void myAbstractMethod(); // Implicitly public abstract<br>    default void myDefaultMethod() { // Implicitly public<br>        System.out.println(&quot;Default method&quot;);<br>    }<br>    static void myStaticMethod() { // Implicitly public<br>        System.out.println(&quot;Static method&quot;);<br>    }<br>}</pre><p>Abstract Classes:</p><ul><li>Fields in an abstract class have <strong>no implicit access modifier</strong>. They take the access modifier explicitly specified, or if none is provided, they default to <strong>package-private</strong>.</li></ul><pre>abstract class MyAbstractClass {<br>    int myField; // Package-private by default<br>    public int myPublicField = 10;<br>}</pre><ul><li>Methods in an abstract class have <strong>no implicit access modifier</strong>. They take the access modifier explicitly specified, or if none is provided, they default to <strong>package-private</strong>.</li><li>Both abstract and concrete (non-abstract) methods follow this rule.</li></ul><pre>abstract class MyAbstractClass {<br>    abstract void myAbstractMethod(); // Package-private by default<br>    void myConcreteMethod() { // Package-private by default<br>        System.out.println(&quot;Concrete method&quot;);<br>    }<br>    public abstract void myPublicAbstractMethod(); // Explicitly public<br>}</pre><p><strong>Interface: <br></strong>Fields: <br>- public static final (always) <br>Methods:<br>- public abstract (default for abstract methods); <br>- public (for default/static methods)</p><p><strong>Abstract Class: <br></strong>Fields/Methods: <br>- Package-private (default)</p><h4>Other Keywords:</h4><p>this — Current object instance<br>- Access instance variables, call constructors, and pass objects.</p><p>super — Parent class instance<br>- Call parent constructor, access parent methods/variables.</p><p>synchronized — Ensures thread safety by allowing only one thread to execute a block/method at a time</p><p>finalize — Called by the garbage collector before an object is removed.</p><p>volatile — Ensures a variable’s value is always read from main memory, not from thread cache.</p><p>var — Allows local variable type inference (Java 10).</p><p>instanceof — Checks if an object is an instance of a class.</p><p>transient — Used to ignore an attribute when serializing an object.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bdd50b455f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inheritance vs Composition: Choosing the Right Design Approach]]></title>
            <link>https://medium.com/@syed.fawzul.azim/inheritance-vs-composition-choosing-the-right-design-approach-94eadb9bd887?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/94eadb9bd887</guid>
            <category><![CDATA[composition]]></category>
            <category><![CDATA[inheritance]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Sun, 07 Sep 2025 10:29:27 GMT</pubDate>
            <atom:updated>2025-09-07T10:29:27.802Z</atom:updated>
            <content:encoded><![CDATA[<h3>Inheritance vs Composition in Java: Choosing the Right Design Approach</h3><h4>A practical guide to understanding IS-A vs HAS-A relationships, runtime behavior, and best practices for building flexible, maintainable Java applications.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*GOnUWE1xCchDre47aqcdMQ.png" /></figure><p>In object-oriented programming (OOP) with Java, <strong>inheritance</strong> and <strong>composition</strong> are two fundamental approaches to structuring code and achieving code reuse. Each has its strengths, weaknesses, and use cases, and choosing the right one is critical for building maintainable, flexible, and scalable applications.</p><p><strong>1. Inheritance (IS-A Relationship):</strong></p><p>Inheritance is an object-oriented programming mechanism in Java where one class (child/subclass) derives properties and behaviors from another class (parent/superclass) using the extends keyword.</p><p>It represents an “is-a” relationship.</p><ul><li>Example: A Dog is an Animal.</li></ul><pre>class Animal {<br>    void eat() {<br>        System.out.println(&quot;This animal eats food.&quot;);<br>    }<br>}<br><br>// Dog inherits from Animal<br>class Dog extends Animal {<br>    void bark() {<br>        System.out.println(&quot;The dog barks.&quot;);<br>    }<br>}<br><br>public class TestInheritance {<br>    public static void main(String[] args) {<br>        Dog dog = new Dog();<br>        dog.eat();   // inherited from Animal<br>        dog.bark();  // Dog&#39;s own method<br>    }<br>}<br><br>// out put<br>This animal eats food.<br>The dog barks.</pre><p><strong>2. Composition (HAS-A Relationship):</strong></p><p>Composition is a <strong>design technique</strong> in Java where a class contains references to other classes and delegates behavior to them.</p><p>It represents a <strong>“has-a” relationship</strong>.</p><ul><li>Example: A Car <strong>has an</strong> Engine.</li></ul><pre>class Engine {<br>    void start() {<br>        System.out.println(&quot;Engine starting...&quot;);<br>    }<br>}<br><br>class Car {<br>    private Engine engine; // Composition: Car has-an Engine<br><br>    public Car() {<br>        this.engine = new Engine();<br>    }<br><br>    void drive() {<br>        engine.start();  // delegation<br>        System.out.println(&quot;Car is driving.&quot;);<br>    }<br>}<br><br>public class TestComposition {<br>    public static void main(String[] args) {<br>        Car car = new Car();<br>        car.drive();<br>    }<br>}<br><br>//out put<br>Engine starting...<br>Car is driving.</pre><h4>Inheritance vs Composition:</h4><ul><li>Definition:<br><strong>- I:</strong> A class derives from another class (extends).<br><strong>- C: </strong>A class contains references to other classes.</li><li>Relationship:<br><strong>- I: </strong>Represents an <strong>IS-A</strong> relationship.<br><strong>- C: </strong>Represents a <strong>HAS-A</strong> relationship.</li><li>Code Reuse:<strong><br>- I: </strong>Reuses code by inheriting fields and methods.<br><strong>- C:</strong> Reuses code by delegating to component objects.</li><li>Flexibility:<br><strong>- I: </strong>Less flexible — tightly coupled to parent.<br><strong>- C:</strong> More flexible — components can be swapped or replaced.</li><li>Coupling:<br><strong>- I: </strong>Strong coupling between child and parent.<br><strong>- C: </strong>Loose coupling between classes.</li><li>Runtime Behavior:<br><strong>- I:</strong> Fixed at compile time (hierarchy defined).<br><strong>- C: </strong>Can be changed at runtime (swap dependencies).</li><li>Multiple Use:<br><strong>- I: </strong>Single inheritance only (no multiple parent classes).<br><strong>- C:</strong> Can combine many classes inside one.</li><li>Fragile Base Class Problem:<br><strong>- I: </strong>High risk: changes in parents affect children.<br><strong>- C: </strong>Low risk: changes isolated within components.</li></ul><p><strong>Examples: </strong>Runtime Behavior</p><ul><li><strong>Inheritance (Fixed at Compile Time):</strong></li></ul><pre>class Animal {<br>    void sound() {<br>        System.out.println(&quot;Some generic sound&quot;);<br>    }<br>}<br><br>class Dog extends Animal {<br>    @Override<br>    void sound() {<br>        System.out.println(&quot;Bark!&quot;);<br>    }<br>}<br><br>public class TestInheritance {<br>    public static void main(String[] args) {<br>        Animal animal = new Dog();  // upcasting works<br>        animal.sound();  // Output: Bark!<br>        <br>        // But the hierarchy is fixed: Dog always extends Animal.<br>        // You cannot &quot;swap&quot; Dog’s parent at runtime.<br>    }<br>}</pre><p>Once you decide Dog extends Animal, that relationship is <strong>fixed in code</strong> at compile time. You cannot change the inheritance hierarchy while running the program.</p><ul><li><strong>Composition (Can Change at Runtime)</strong></li></ul><pre>interface Engine {<br>    void start();<br>}<br><br>class PetrolEngine implements Engine {<br>    public void start() {<br>        System.out.println(&quot;Starting petrol engine...&quot;);<br>    }<br>}<br><br>class ElectricEngine implements Engine {<br>    public void start() {<br>        System.out.println(&quot;Starting electric engine...&quot;);<br>    }<br>}<br><br>class Car {<br>    private Engine engine;  // HAS-A relationship<br><br>    // Constructor injection allows swapping dependency<br>    public Car(Engine engine) {<br>        this.engine = engine;<br>    }<br><br>    void drive() {<br>        engine.start();<br>        System.out.println(&quot;Car is driving...&quot;);<br>    }<br>}<br><br>public class TestComposition {<br>    public static void main(String[] args) {<br>        Car petrolCar = new Car(new PetrolEngine());<br>        petrolCar.drive();<br>        // Output:<br>        // Starting petrol engine...<br>        // Car is driving...<br><br>        // Swap engine at runtime<br>        Car electricCar = new Car(new ElectricEngine());<br>        electricCar.drive();<br>        // Output:<br>        // Starting electric engine...<br>        // Car is driving...<br>    }<br>}</pre><p>With <strong>composition</strong>, you can swap out different implementations (PetrolEngine → ElectricEngine) <strong>at runtime</strong> without changing the Car class.</p><h4>Best Practices:</h4><ul><li><strong>Favor Composition Over Inheritance</strong>: Composition is generally more flexible and maintainable, as recommended by design principles like SOLID (e.g., the Open-Closed Principle).</li><li><strong>Use Interfaces for Flexibility</strong>: Define behavior with interfaces and implement them using composition to decouple code.</li><li><strong>Keep Inheritance Shallow</strong>: Avoid deep inheritance hierarchies to reduce complexity and fragility.</li><li><strong>Encapsulate Components</strong>: Use private fields and public methods to control access to composed objects.</li><li>Use <strong>inheritance</strong> sparingly, mainly for <strong>polymorphism</strong>.</li><li>Follow <strong>SOLID principles</strong>, especially:<br>- <strong>Liskov Substitution Principle (LSP)</strong> → subclass should behave like superclass.<br>- <strong>Dependency Inversion Principle (DIP)</strong> → depend on abstractions, not concrete classes.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94eadb9bd887" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authentication and Authorization — JWT (JSON Web Tokens)]]></title>
            <link>https://medium.com/@syed.fawzul.azim/authentication-and-authorization-jwt-json-web-tokens-43fa77d32afd?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/43fa77d32afd</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Fri, 05 Sep 2025 09:37:37 GMT</pubDate>
            <atom:updated>2025-09-05T09:37:37.633Z</atom:updated>
            <content:encoded><![CDATA[<h3>Authentication and Authorization — JWT (JSON Web Tokens)</h3><h4>Understanding JWT Structure, Use Cases, Challenges, and Security Considerations</h4><p><strong>Authentication</strong>: Verifies the identity of a user or system (e.g., confirming a user&#39;s identity by verifying their credentials).</p><p><strong>Authorization</strong>: Determines what an authenticated user is allowed to do (e.g., accessing specific APIs or resources).</p><h4>JSON Web Tokens (JWT)</h4><p><strong>Here is how it works:</strong></p><p>1) User Logs In: A user authenticates with their username and password.</p><p>2) JWT Creation: The Authentication Server (the JWT Issuer) creates a JWT token for that user.</p><p>3) Token Sent to Client: The server sends this encrypted JWT token back to the client (browser). The client then stores it, often in local storage or a cookie.</p><p>4) Client Sends Requests: For all subsequent requests, the client sends the JWT in the Authorization header, like this: Authorization: <br>Bearer &lt;jwt_token&gt;.</p><p>5) API Server Validation: The API server (the resource) receives the request and validates the token to ensure it hasn’t been tampered with.</p><p>This includes checking the signature and the token’s expiration.</p><p>6) Response: If the JWT is valid, the server approves the request and sends the client the requested information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*AVcNiiQFVV79gN9sgITMhQ.png" /><figcaption>JWT authentication architecture diagram</figcaption></figure><p><strong>The JWT structure:</strong></p><p>It has three parts, separated by dots: Header.Payload.Signature</p><h4>1. Header:</h4><p>Specifies the algorithm used for signing the token (e.g., HS256).</p><pre>{<br>  &quot;alg&quot;: &quot;HS256&quot;,<br>  &quot;typ&quot;: &quot;JWT&quot;<br>}</pre><h4>2. Payload:</h4><p>Carries the claims, which are statements about the user and additional data.</p><p>Registered claims: These are a set of predefined claims that are not mandatory but recommended to provide a set of useful, interoperable claims. Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), nbf(not before), iat(issued at), jti(jwt id).</p><pre>{<br>  &quot;iss&quot;: &quot;https://auth.myapp.com&quot;,     // Issuer: who issued the token<br>  &quot;sub&quot;: &quot;1234567890&quot;,                 // Subject: unique identifier for the user<br>  &quot;aud&quot;: &quot;https://api.myapp.com&quot;,      // Audience: intended recipient (your API)<br>  &quot;exp&quot;: 1735910400,                   // Expiration time (Unix timestamp)<br>  &quot;nbf&quot;: 1735906800,                   // Not valid before (Unix timestamp)<br>  &quot;iat&quot;: 1735906800,                   // Issued at (Unix timestamp)<br>  &quot;jti&quot;: &quot;abcde12345&quot;                  // JWT ID: unique identifier for the token<br>}</pre><p>Public Claims: It’s a custom claim, which can be shared and understood across different systems, but it should be unique to avoid collisions.</p><pre>{<br>  &quot;userId&quot;: &quot;12345&quot;,<br>  &quot;role&quot;: &quot;admin&quot;,<br>  &quot;department&quot;: &quot;finance&quot;<br>}<br><br>// Public Claims with Namespaces<br>{<br>  &quot;https://myapp.com/userId&quot;: &quot;12345&quot;,<br>  &quot;https://myapp.com/role&quot;: &quot;admin&quot;,<br>  &quot;https://myapp.com/department&quot;: &quot;finance&quot;<br>}</pre><p>Here, the claim names are namespaced with your domain to ensure uniqueness if the token is used in multiple systems.</p><p>Private Claims: Private claims are completely custom and only understood between the parties that exchange the token (usually your app + your backend). These are often app-specific flags, preferences, or session details.</p><pre>{<br>  &quot;themePreference&quot;: &quot;dark&quot;,<br>  &quot;sessionId&quot;: &quot;xyz123&quot;,<br>  &quot;shoppingCartId&quot;: &quot;cart789&quot;<br>}</pre><p><strong>Important: </strong>The payload is not encrypted, so you shouldn’t store sensitive information like passwords or credit card numbers here!</p><p>JWT payload is <strong>base64-encoded but not encrypted</strong>, so anyone with the token can read it.</p><h4>3) Signature:</h4><p>A hash of the header and payload, created using a secret key. This is the part that proves the token’s authenticity and that its contents haven’t been tampered with.</p><p>How the Signature is Created:</p><ol><li>Take the <strong>Base64URL-encoded header</strong> and <strong>payload</strong>.</li></ol><pre>base64UrlEncode(header) + &quot;.&quot; + base64UrlEncode(payload)</pre><p>2. Apply a <strong>signing algorithm</strong> (defined in the header, e.g., HS256, RS256).</p><p>3. Use a <strong>secret key</strong> for <strong>HMAC(symmetric cryptography) </strong>or a private key, if asymmetric cryptography, to generate a hash.</p><pre>HMACSHA256(<br>  base64UrlEncode(header) + &quot;.&quot; + base64UrlEncode(payload),<br>  secret_key<br>)</pre><p><em>Note:</em> Some library expects a <strong>raw string</strong> secret, some library expects a <strong>Base64-encoded</strong> secret.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*I6cptkTAKuYNrU7_NBJjeA.jpeg" /></figure><p>Types of Signing Algorithms</p><ul><li><strong>HMAC (HS256, HS512)</strong> → Symmetric; same secret for signing &amp; verifying.</li><li><strong>RSA/ECDSA (RS256, ES256)</strong> → Asymmetric; private key signs, public key verifies (better for distributed systems).</li></ul><p>Sample API request with JWT:</p><pre>GET /api/user/profile HTTP/1.1<br>Host: api.myapp.com<br>Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.<br>eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwicm9sZSI6InVzZXIiLCJpYXQiOjE3MjUyMDAwMDAsImV4cCI6MTcyNTIwMzYwMH0.<br>dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk</pre><p>Advantages of JWT:</p><ul><li><strong>Stateless</strong> → no server-side session storage needed.</li><li><strong>Compact</strong> → small, URL-safe, easy to send in headers/cookies.</li><li><strong>Secure</strong> → digitally signed, tamper-proof.</li><li><strong>Scalable</strong> → multiple services can verify independently.</li><li><strong>Built-in expiration</strong> → supports short-lived tokens + refresh flow.</li><li><strong>Custom claims</strong> → carry roles/permissions for authorization.</li><li><strong>Fast</strong> → only signature verification, no DB lookup.</li><li><strong>Cross-domain</strong> → widely used for SSO and OAut</li></ul><p><strong>Use Cases:</strong></p><p>1) Authorization — the most common use case. Once logged in, a user includes the JWT in each request to access protected routes, services, and resources.</p><p>2) Single Sign-On (SSO):</p><h4>JWT-based SSO:</h4><ul><li>User logs in <strong>once</strong> with a central <strong>Identity Provider (IdP)</strong>.</li><li>IdP authenticates the user and creates a <strong>server-side session</strong> to remember the login.</li><li>IdP issues a <strong>signed JWT</strong> containing identity and claims to the application (App A).</li><li>Other applications (App B, App C) also trust the IdP → they either accept the same JWT or request a new one from the IdP using the existing session.</li><li>Result: the user can access multiple apps without logging in again (<strong>Single Sign-On</strong>), as long as the IdP session is valid.</li></ul><p><strong>Some best practices:</strong></p><p>1) Keep JWTs secret — Treat your secret signing key like any other credential.</p><p>2) Keep Tokens Short-Lived — The expiration times should be short to minimize the risk of a compromised token being used by an attacker.</p><p>3) Validate the token — The API server must always validate the token’s signature, expiration, and other claims on every single request.</p><h4><strong>Challenges in JWT:</strong></h4><p><strong>1. Revocation / Immediate Logout</strong></p><ul><li>JWTs are <strong>stateless</strong>, so once issued, they remain valid until expiration.</li><li>Hard to revoke immediately without implementing a <strong>token blacklist</strong> or <strong>session tracking</strong>.</li></ul><p><strong>2. Token Theft / Security Risks</strong></p><ul><li>If a JWT is stolen (via XSS, localStorage, or network), it can be used until it expires.</li><li>Short-lived tokens and secure storage (HttpOnly cookies) help mitigate this.</li></ul><p><strong>3. Token Size / Bloat</strong></p><ul><li>JWTs can become large if many claims are included, which affects <strong>network latency</strong> and <strong>header size</strong>.</li></ul><p><strong>4. Expiration Handling:</strong></p><ul><li>Long-lived tokens increase risk if stolen.</li><li>Short-lived tokens require <strong>refresh token logic</strong>, adding complexity.</li></ul><h4>How to Implement Immediate Logout / Revocation:</h4><ol><li>Token Blacklisting:</li></ol><ul><li>Keep a <strong>server-side list of revoked tokens</strong> (in Redis, DB).</li><li>On logout, add the JWT’s <strong>jti (JWT ID)</strong> or token string to the blacklist.</li><li>On every request, check the blacklist:|<br>- If the token is blacklisted, it rejects the request, works instantly.</li><li><strong>Adds server-side state, loses the full stateless advantage.</strong></li></ul><p>2. Short-Lived Access Tokens + Refresh Tokens:</p><ul><li>Issue <strong>short-lived JWTs</strong> (e.g., 5–15 minutes) for access.</li><li>Use <strong>refresh tokens</strong> (stored in DB) to issue new access tokens.</li><li>On logout:<br>- Delete the refresh token → user cannot get new access tokens.</li><li>The existing access token will expire shortly.</li><li>Lightweight and scalable.</li><li><strong>Logout is not instantaneous (waits until the access token expires).</strong></li></ul><p>3. Token Versioning / Session Version:</p><ul><li>Add a <strong>tokenVersion or </strong><strong>sessionVersion</strong> field in your <strong>User table</strong>.</li><li>Include it in the JWT payload.</li><li>On logout:<br>- Increment the user’s tokenVersion.</li><li>On each request:<br>- Compare JWT’s tokenVersion with DB → reject if mismatched.</li><li>Immediate logout effect.</li><li><strong>Requires a DB lookup per request (can cache).</strong></li></ul><h4>Additional Security Considerations:</h4><ul><li><strong>Use HTTPS Everywhere</strong><br> Always serve JWTs over HTTPS to prevent token leakage through network sniffing. Sending JWTs over plain HTTP makes them vulnerable to Man-in-the-Middle (MITM) attacks.</li><li><strong>Store Tokens Securely<br>- Access Tokens</strong>: Typically stored in memory (JavaScript variable) so they vanish on page refresh.<br>- <strong>Refresh Tokens</strong>: Store in an HttpOnly, Secure cookie to prevent XSS attacks. Avoid localStorage/sessionStorage for long-lived tokens.</li><li><strong>Audience (</strong><strong>aud) and Issuer (</strong><strong>iss) Validation</strong><br> Make sure your backend validates that:<br>- The correct authority issued the token (iss).<br>- The token was intended for your API (aud).<br> This prevents misuse of tokens across different services.</li><li><strong>Key Rotation (KID Header)<br>- </strong>Include a kid (Key ID) in the JWT header.<br>- Maintain multiple signing keys and rotate them periodically.<br>- This allows old tokens to remain valid while new tokens are signed with the new key.</li><li><strong>Rate Limiting &amp; Brute Force Protection</strong><br>Protect your login and token refresh endpoints with rate limiting and monitoring to avoid brute-force attacks.</li></ul><p>JWTs are a powerful and lightweight way to implement authentication and authorization in modern applications. They provide stateless scalability, support cross-domain use cases like SSO, and are easy to integrate with APIs and microservices.</p><p>At the same time, JWTs come with challenges like revocation, token theft risks, and expiration handling. By combining best practices such as short-lived access tokens, refresh tokens, token versioning, secure storage, and key rotation, you can strike the right balance between security and usability.</p><p>In short:</p><ul><li>Use <strong>JWTs for stateless authentication</strong>.</li><li>Keep them <strong>short-lived and secure</strong>.</li><li>Add <strong>refresh tokens + revocation strategy</strong> for long-lived sessions.</li><li>Always <strong>validate, monitor, and rotate keys</strong>.</li></ul><p>With these practices, JWT becomes a reliable backbone for securing APIs and enabling smooth user experiences in distributed systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43fa77d32afd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fault-Tolerant Systems: Principles, Patterns, and Practices]]></title>
            <link>https://medium.com/@syed.fawzul.azim/fault-tolerant-systems-principles-patterns-and-practices-29867699744b?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/29867699744b</guid>
            <category><![CDATA[fault-tolerance]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[resilience4j]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Tue, 02 Sep 2025 22:31:41 GMT</pubDate>
            <atom:updated>2025-09-02T22:31:41.482Z</atom:updated>
            <content:encoded><![CDATA[<p>Ensuring System Resilience with Redundancy, Circuit Breakers, Timeouts, Rate Limiting, and Retry Strategies</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5-IerwosJmz3cUfKofrxJQ.jpeg" /></figure><p><strong>Failures are inevitable:</strong></p><ul><li>Hardware can fail unexpectedly (disk crashes, power failures).</li><li>Networks are unreliable (latency, packet loss, outages).</li><li>Software often contains bugs that only surface in production.</li><li>Distributed systems add more complexity, where partial failures and inconsistencies are normal.</li></ul><p><strong>Fault-tolerant vs. fault-free systems</strong></p><ul><li><strong>Fault-free systems:</strong> Aim for perfection, assume no failures will occur (theoretically ideal but practically impossible).</li><li><strong>Fault-tolerant systems:</strong> Assume failures will happen and are designed to continue operating despite them.</li></ul><p><strong>Fault Tolerance:</strong></p><ul><li>Fault tolerance is the ability of a system to continue functioning correctly in the presence of faults or failures.</li><li>Instead of preventing all failures (impossible in practice), the system is designed to withstand them and maintain availability.</li></ul><p>Different Levels of Fault Tolerance:</p><ul><li><strong>Hardware level</strong> → redundant power supplies, RAID storage, backup servers.</li><li><strong>Software level</strong> → exception handling, retries, rollbacks, resilient algorithms.</li><li><strong>Network level</strong> → load balancing, redundant communication paths, failover routing.</li><li><strong>Application level</strong> → microservices with isolation, circuit breakers, and graceful degradation.</li></ul><p>Fault tolerance ≠ perfection.</p><p>It’s about resilience → designing systems that expect failure and recover gracefully.</p><h4>Key Principles of Fault Tolerance:</h4><p><strong>1. Redundancy</strong></p><ul><li>Duplicate critical components to remove single points of failure.</li><li><strong>Examples: </strong>database replication, RAID storage, backup servers, multi-region cloud deployments.</li></ul><p><strong>2. Failover &amp; Recovery:</strong></p><ul><li><strong>Automatic Failover</strong>: Switching to a backup component when a failure is detected, as seen in database systems like MySQL with master-slave replication.</li><li><strong>Rollback and Checkpointing</strong>: Saving system states periodically to restore operations to a known good state.</li><li><strong>Graceful Degradation</strong>: Allowing the system to operate at reduced capacity rather than failing entirely.</li></ul><p><strong>3. Isolation &amp; Containment:</strong></p><ul><li>Prevent failures in one module from propagating to the entire system.</li><li><strong>Circuit Breakers:</strong> Temporarily halting requests to a failing component to avoid overloading it.</li><li><strong>Techniques:</strong> bulkheads, sandboxing, process isolation.</li></ul><p><strong>4. Graceful Degradation:</strong></p><ul><li>Instead of a total shutdown, the system continues in a reduced functionality mode.</li><li><strong>Example: </strong>a video streaming service may lower video quality if CDN nodes fail, but still plays content.</li></ul><p><strong>5. Monitoring &amp; Self-Healing:</strong></p><ul><li>Continuous monitoring to detect anomalies and failures.</li><li>Automated self-healing: restarting failed services, reallocating resources, triggering retries.</li><li><strong>Tools: </strong>Prometheus, Grafana, Kubernetes health checks, and auto-restart policies.</li></ul><h4><strong>Techniques and Patterns for Fault Tolerance:</strong></h4><p><strong>Replication:</strong></p><ul><li>Keep multiple copies of data/services to ensure availability.</li><li>Examples:<br>- Database <strong>master-slave / leader-follower</strong> replication.<br>- Multi-region data replication in cloud systems.</li></ul><p><strong>Checkpoints &amp; Rollback Recovery:</strong></p><ul><li>Periodically save the system state so recovery can restart from the last checkpoint instead of starting over.</li><li>Common in distributed computing, stream processing, and batch jobs (e.g., Apache Flink, Spark).</li></ul><p><strong>Consensus Algorithms:</strong></p><ul><li>Ensure agreement across distributed nodes, even in the case of failures.</li><li>Critical for leader election, configuration management, and replicated state machines.</li><li>Examples: Raft, Paxos, ZAB (used in ZooKeeper).</li></ul><p><strong>Rate Limiting &amp; Timeouts:</strong></p><ul><li><strong>Rate limiting</strong>: Control request flow to prevent overloading services (e.g., API gateway throttling).</li><li><strong>Timeouts</strong>: Define max wait times for requests → prevents hanging calls and frees resources quickly.</li></ul><p><strong>Retry &amp; Backoff Strategies:</strong></p><ul><li>Retrying failed operations with controlled delays to give systems time to recover.</li><li><strong>Exponential backoff</strong>: progressively increasing wait times between retries. This avoids overwhelming a failing service.</li></ul><p><strong>Circuit Breaker Pattern:</strong></p><ul><li>Prevents cascading failures when one service repeatedly calls a failing dependency.</li><li>Automatically “opens” the circuit after repeated failures, blocking requests until the service recovers.</li><li>Implementations: Netflix Hystrix, Resilience4j.</li></ul><p><strong>Bulkheads &amp; Isolation:</strong></p><ul><li>Partition system resources so that failure in one area doesn’t affect others.</li><li>Example: separating thread pools per service (so a failing API doesn’t exhaust all threads).</li></ul><p><strong>Chaos Engineering:</strong></p><ul><li>Proactively test system resilience by injecting controlled failures.</li><li>Example: Netflix Chaos Monkey kills random instances to verify auto-recovery.</li></ul><h4>Fault Tolerance in Practice for Microservices:</h4><p>Logical Order of different fault-tolerant strategies:</p><h4>RateLimiter → Bulkhead → TimeLimiter → CircuitBreaker → Retry</h4><ol><li>Controls how many requests can enter the system.</li><li>Ensures that even if one service is overloaded, it won’t consume all system resources.</li><li>Defines how long a call is allowed to take.</li><li>Monitors calls → if too many failures occur, the breaker “opens” and blocks further calls temporarily.</li><li>If the request fails due to transient errors (such as network hiccups), retry with a backoff strategy.</li></ol><p><strong>Examples: </strong>Resilience4j</p><ol><li>RateLimiter:</li></ol><pre>@Configuration<br>public class RateLimiterConfiguration {<br><br>    @Bean<br>    public RateLimiter customRateLimiter(RateLimiterRegistry rateLimiterRegistry) {<br>        RateLimiterConfig config = RateLimiterConfig.custom()<br>                .limitForPeriod(5)                           // Max 5 requests<br>                .limitRefreshPeriod(Duration.ofSeconds(1))   // Window = 1 second<br>                .timeoutDuration(Duration.ofMillis(0))       // No waiting if limit reached<br>                .writableStackTraceEnabled(false)            // Optimize performance<br>                .subscribeForEvents(true)                    // Enable event publishing<br>                .timeoutDuration(Duration.ofMillis(100))     // Can also allow short waits<br>                .build();<br><br>        return rateLimiterRegistry.rateLimiter(&quot;myRateLimiter&quot;, config);<br>    }<br>}</pre><pre>@Service<br>public class PaymentService {<br><br>    @RateLimiter(name = &quot;myRateLimiter&quot;, fallbackMethod = &quot;fallbackPayment&quot;)<br>    public String processPayment() {<br>        return &quot;Payment processed successfully!&quot;;<br>    }<br><br>    public String fallbackPayment(Throwable t) {<br>        return &quot;Too many requests! Please try again later.&quot;;<br>    }<br>}</pre><p>2. Bulkhead:</p><pre>public class BulkheadExample {<br>    public static void main(String[] args) {<br>        // Configure Bulkhead<br>        BulkheadConfig config = BulkheadConfig.custom()<br>                .maxConcurrentCalls(5)              // max 5 concurrent calls allowed<br>                .maxWaitDuration(Duration.ofMillis(500)) // wait up to 500ms for a slot<br>                .fairCallHandlingStrategyEnabled(true)   // optional: FIFO handling<br>                .build();<br><br>        // Create Bulkhead<br>        Bulkhead bulkhead = Bulkhead.of(&quot;myBulkhead&quot;, config);<br><br>        // Wrap a function<br>        Callable&lt;String&gt; backendCall = Bulkhead.decorateCallable(bulkhead, () -&gt; {<br>            Thread.sleep(100); // simulate work<br>            return &quot;Success from backend&quot;;<br>        });<br><br>        // Execute with Try<br>        for (int i = 0; i &lt; 10; i++) {<br>            int finalI = i;<br>            new Thread(() -&gt; {<br>                Try&lt;String&gt; result = Try.ofCallable(backendCall)<br>                        .recover(throwable -&gt; &quot;Rejected &quot; + finalI + &quot;: &quot; + throwable.getMessage());<br><br>                System.out.println(result.get());<br>            }).start();<br>        }<br>    }<br>}</pre><ul><li>maxConcurrentCalls(5) → Only 5 threads can run inside the bulkhead at once.</li><li>If a 6th request comes in:<br>- It waits up to maxWaitDuration (500ms here).<br>- If a slot frees, → request goes through.<br>- If not → request fails with BulkheadFullException.</li><li>You can recover gracefully (e.g., fallback message).</li><li>It prevents one slow dependency (e.g., external API) from blocking your entire system.</li><li>Each service/component can have its own “compartment”.</li></ul><p>3. TimeLimiter:</p><pre>public class TimeLimiterExample {<br><br>    private static final ExecutorService executorService = Executors.newCachedThreadPool();<br><br>    public static void main(String[] args) {<br>        // Configure TimeLimiter<br>        TimeLimiterConfig config = TimeLimiterConfig.custom()<br>                .timeoutDuration(Duration.ofSeconds(2))  // max 2 seconds<br>                .cancelRunningFuture(true)               // cancel task if timed out<br>                .build();<br><br>        // Create TimeLimiter<br>        TimeLimiter timeLimiter = TimeLimiter.of(config);<br><br>        //Define a long-running task<br>        Callable&lt;String&gt; longTask = () -&gt; {<br>            System.out.println(&quot;Task started...&quot;);<br>            Thread.sleep(4000); // Simulate delay (4s)<br>            return &quot;Finished successfully!&quot;;<br>        };<br><br>        // Wrap with TimeLimiter<br>        Callable&lt;String&gt; timeLimitedCall = TimeLimiter.decorateFutureSupplier(<br>                timeLimiter,<br>                () -&gt; executorService.submit(longTask)<br>        );<br><br>        // Execute with Try + fallback<br>        Try&lt;String&gt; result = Try.ofCallable(timeLimitedCall)<br>                .recover(throwable -&gt; &quot;Fallback: Task timed out! &quot; + throwable.getMessage());<br><br>        System.out.println(&quot;Result: &quot; + result.get());<br><br>        executorService.shutdown();<br>    }<br>}</pre><ul><li>timeoutDuration(2s) → The task must finish within 2 seconds.</li><li>Our longTask takes 4 seconds → gets timed out.</li><li>Because cancelRunningFuture(true) → the running thread is interrupted.</li><li>The recover() block provides a fallback message.</li></ul><p>4. CircuitBreaker:</p><pre>@Configuration<br>public class CircuitBreakerConfigClass {<br><br>    @Bean<br>    public CircuitBreaker defaultCircuitBreaker() {<br>        CircuitBreakerConfig config = CircuitBreakerConfig.custom()<br>                .registerHealthIndicator(true)                                 // registerHealthIndicator=true<br>                .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED) // COUNT_BASED<br>                .slidingWindowSize(10)                                         // last 10 calls<br>                .failureRateThreshold(50)                                      // open if &gt;50% failures<br>                .waitDurationInOpenState(Duration.ofSeconds(5))                // stay open for 5s<br>                .permittedNumberOfCallsInHalfOpenState(3)                     // allow 3 trial calls<br>                .automaticTransitionFromOpenToHalfOpenEnabled(true)            // auto transition<br>                .minimumNumberOfCalls(5)                                       // min 5 calls before evaluating<br>                .build();<br><br>        return CircuitBreaker.of(&quot;defaultCircuitBreaker&quot;, config);<br>    }<br>}</pre><ul><li>registerHealthIndicator(true) → Spring Boot exposes a <strong>health indicator</strong> for this circuit breaker.</li><li>slidingWindowType=COUNT_BASED → failure rate is calculated over the <strong>last N calls</strong> instead of time.</li><li>slidingWindowSize=10 → last 10 calls considered.</li><li>failureRateThreshold=50 → opens if &gt;50% of calls fail.</li><li>waitDurationInOpenState=5s → stays open for 5 seconds before trying again.</li><li>permittedNumberOfCallsInHalfOpenState=3 → 3 trial calls when moving to half-open.</li><li>automaticTransitionFromOpenToHalfOpenEnabled=true → automatically moves to half-open after wait.</li><li>minimumNumberOfCalls=5 → only evaluates failure rate after 5 calls.</li></ul><pre>@Service<br>public class PaymentService {<br><br>    // CircuitBreaker annotation applied<br>    @CircuitBreaker(name = &quot;paymentServiceCB&quot;, fallbackMethod = &quot;paymentFallback&quot;)<br>    public String processPayment(boolean fail) {<br>        if (fail) {<br>            throw new RuntimeException(&quot;Payment service failed!&quot;);<br>        }<br>        return &quot;Payment processed successfully!&quot;;<br>    }<br><br>    // Fallback method called when CircuitBreaker is open or exception occurs<br>    public String paymentFallback(boolean fail, Throwable t) {<br>        return &quot;Fallback: Payment service unavailable! Reason: &quot; + t.getMessage();<br>    }<br>}</pre><p>5. Retry:</p><pre>resilience4j:<br>  retry:<br>    instances:<br>      paymentRetry:<br>        maxAttempts: 3                 # retry 3 times<br>        waitDuration: 500ms            # wait 500ms between retries<br>        retryExceptions:<br>          - java.lang.RuntimeException # retry only on RuntimeException<br>        ignoreExceptions:<br>          - java.lang.IllegalArgumentException # ignore this exception</pre><pre>@Service<br>public class PaymentService {<br><br>    // Retry configured with name &quot;paymentRetry&quot;, fallback on failure<br>    @Retry(name = &quot;paymentRetry&quot;, fallbackMethod = &quot;paymentFallback&quot;)<br>    public String processPayment(boolean fail) {<br>        if (fail) {<br>            System.out.println(&quot;Processing failed, throwing exception...&quot;);<br>            throw new RuntimeException(&quot;Payment failed!&quot;);<br>        }<br>        return &quot;Payment processed successfully!&quot;;<br>    }<br><br>    // Fallback method<br>    public String paymentFallback(boolean fail, Throwable t) {<br>        return &quot;Fallback: Payment failed after retries! Reason: &quot; + t.getMessage();<br>    }<br>}</pre><p>Fault tolerance is no longer optional — it’s a necessity for modern software systems, especially in distributed architectures and microservices. By designing systems that <strong>expect failures</strong> and implementing strategies like <strong>RateLimiter, Bulkhead, TimeLimiter, CircuitBreaker, and Retry</strong>, organizations can:</p><ul><li>Maintain high availability even when components fail.</li><li>Prevent cascading failures that could bring down the entire system.</li><li>Provide a graceful user experience with fallback mechanisms.</li><li>Improve observability and proactive recovery with monitoring and self-healing tools</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29867699744b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Immutability in Java]]></title>
            <link>https://medium.com/@syed.fawzul.azim/immutability-in-java-6195780a0ce8?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/6195780a0ce8</guid>
            <category><![CDATA[immutability]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Mon, 01 Sep 2025 15:41:37 GMT</pubDate>
            <atom:updated>2025-09-04T10:05:56.107Z</atom:updated>
            <content:encoded><![CDATA[<h4>A practical guide covering strings, wrapper classes, custom immutable classes, streams, and more.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*ftG7SHHP6asAk3RBkZWAwQ.jpeg" /></figure><p>Immutability means that once an object is created, its state cannot be changed.</p><p><strong>Why Does Immutability Matter in Java?</strong></p><ul><li><strong>Thread Safety: </strong>Immutable objects can be shared safely across multiple threads without synchronization, as their state never changes.</li><li><strong>Predictable Behavior: </strong>Since the state of an immutable object is fixed, you can rely on it not being modified unexpectedly.</li><li><strong>Easier Debugging: </strong>Immutability reduces side effects, making your code more straightforward to understand, test, and debug.</li></ul><p><strong>Immutability vs Mutability:</strong></p><ul><li><strong>Mutable Objects: </strong>Their state can change after creation.</li></ul><p>Example:</p><pre>List&lt;String&gt; list = new ArrayList&lt;&gt;();<br>list.add(&quot;Java&quot;);   // state changes<br>list.add(&quot;Spring&quot;);</pre><p>Classes like ArrayList, HashMap, StringBuilder, and Date are mutable.</p><ul><li><strong>Immutable Objects: </strong>Their state cannot be changed once they are created. Any modification creates a new object instead of altering the existing one.</li></ul><p>Example:</p><pre>String str = &quot;Java&quot;; <br>str = str.concat(&quot; Rocks!&quot;);  // creates a new String object &quot;Java Rocks!&quot;, Original &quot;Java&quot; string still exists (in string pool).</pre><p>Classes like String, BigInteger , BigDecimal, LocalDate, and all wrapper classes Integer, Long, Short, Byte, Character, Boolean are immutable.</p><p>Examples:</p><p><strong>1. Primitive Types (when declared as final):</strong></p><pre>final int x = 10;<br>// This will throw a compile-time error.<br>x = 20;<br>x++;<br>x = x + 10;</pre><p><strong>2. String Objects are immutable ( not string variables, here s1 is mutable):</strong></p><pre>String s1 = &quot;Hello&quot;;<br>s1 = &quot;New&quot;;<br>String s2 = s1.concat(&quot; World&quot;);<br>System.out.println(s1); // Outputs: &quot;New&quot;<br>System.out.println(s2); // Outputs: &quot;New World&quot;</pre><p>a)<strong> String s1 = &quot;Hello&quot;;</strong></p><ul><li>A String object &quot;Hello&quot; is created in the <strong>String pool</strong>, and s1 points to it.</li></ul><p>b) <strong>s1 = &quot;New&quot;;</strong></p><ul><li>Now, &quot;New&quot; is created (if not already in the String pool).</li><li>s1 is reassigned to point to &quot;New&quot;.</li><li>The &quot;Hello&quot; object still exists in the pool, but s1 no longer references it.</li></ul><p>c) <strong>String s2 = s1.concat(&quot; World&quot;);</strong></p><ul><li>Since String is <strong>immutable</strong>, concat() does <strong>not</strong> change s1.</li><li>Instead, it creates a <strong>new String object</strong> with the value &quot;New World&quot;.</li><li>s2 references this new string, while s1 still points to &quot;New&quot;.</li></ul><pre>String a1 = &quot;Azim&quot;;<br>String a2 = &quot;Azim&quot;;<br><br>// Stack    -------  Heap Memory<br>// a1(105) --------&gt; &quot;Azim&quot;    [String a1 = &quot;Azim&quot;;]<br>// a2(105) --------&gt; &quot;Azim&quot;    [String a2 = &quot;Azim&quot;;]<br><br>// (doesn&#39;t create new string obj in heap memory, only points to the same string obj)<br><br>// hashed memory address prints the same value which means both are pointing to same String Obj<br>System.out.println(System.identityHashCode(a1));<br>System.out.println(System.identityHashCode(a2));<br><br>// after all that, if you want to create separate string obj(outside string pool)<br>// with the same (&quot;Azim&quot;)<br>String a3 = new String(&quot;Azim&quot;);<br>String a4 = new String(&quot;Azim&quot;);<br><br>System.out.println(a1 == a2); // true<br>System.out.println(a1 == a3); // false<br>System.out.println(a3 == a4); // false<br></pre><p><strong>3. Wrapper classes in Java:</strong></p><pre>Integer num = 100;   // wrapper object<br>System.out.println(&quot;Original: &quot; + num);<br><br>// &quot;Modify&quot; num by adding 50<br>num = num + 50;<br><br>System.out.println(&quot;After modification: &quot; + num);</pre><ul><li>Integer num = 100;<br>- num points to an Integer object with value 100.</li><li>num = num + 50;<br>- num is unboxed (converted to primitive int → 100).<br>- 100 + 50 = 150.<br>- The result is re-boxed into a new Integer(150) object.<br>- Now num points to a <strong>different object</strong>.</li><li>The original Integer(100) still exists in memory (possibly in cache *bonus), but num no longer refers to it.</li></ul><p>Every <strong>“modification”</strong> creates a new object.</p><p><strong>4. Immutable Class in Java:</strong></p><p>List the key rules for creating an immutable class in Java:</p><ol><li>Make the class final So it cannot be subclassed.</li><li>Declare all fields as private and final.</li><li>Do not provide setters or methods that modify fields.</li><li>Ensure constructor initializes all fields.</li><li>Assign deep copies of mutable objects to ensure the internal state cannot be altered from outside.</li><li>Return deep copies of mutable objects in getters to prevent external modification.</li><li>Ensure thread-safety for any shared mutable state (though rare in immutable objects).</li></ol><pre>public final class Student {<br>    private final String name;<br>    private final List&lt;String&gt; courses;<br><br>    public Student(String name, List&lt;String&gt; courses) {<br>        this.name = name;<br>        // Defensive copy to prevent external modification<br>        this.courses = new ArrayList&lt;&gt;(courses);<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public List&lt;String&gt; getCourses() {<br>        // Return an unmodifiable copy to prevent modification from outside<br>        return Collections.unmodifiableList(courses);<br>    }<br>}</pre><pre>public class Main {<br>    public static void main(String[] args) {<br>        List&lt;String&gt; courseList = Arrays.asList(&quot;Math&quot;, &quot;Physics&quot;);<br>        Student student = new Student(&quot;Alice&quot;, courseList);<br><br>        System.out.println(student.getName());      // Alice<br>        System.out.println(student.getCourses());   // [Math, Physics]<br><br>        // Modify the original courseList<br>        courseList.set(0, &quot;Biology&quot;);              // Change the first element<br>        System.out.println(&quot;Original list modified: &quot; + courseList); // [Biology, Physics]<br><br>        // Student&#39;s courses remain unchanged<br>        System.out.println(&quot;Student&#39;s courses: &quot; + student.getCourses()); // [Math, Physics]<br><br>        // Attempt to modify the courses<br>        List&lt;String&gt; courses = student.getCourses();<br>        courses.add(&quot;Chemistry&quot;); // Throws UnsupportedOperationException<br><br>        // Original courses remain unchanged<br>        System.out.println(student.getCourses());   // [Math, Physics]<br>    }<br>}</pre><p><strong>Explanation</strong></p><ol><li>courseList is modified after Student creation.</li><li>Student’s internal courses list is unaffected because of the defensive copy in the constructor.</li><li>Trying to modify the list returned by getCourses() throws UnsupportedOperationException because of Collections.unmodifiableList() in the getter.</li></ol><p><strong>5. Enums:</strong></p><ul><li>Enum instances are created once when the enum class is loaded.</li><li>You cannot create new instances or change the state of existing enum constants.</li><li>This makes enums naturally thread-safe and immutable.</li></ul><pre>enum Direction {<br>    NORTH, SOUTH, EAST, WEST<br>}<br><br>public class Main {<br>    public static void main(String[] args) {<br>        Direction dir = Direction.NORTH;<br><br>        System.out.println(dir); // Outputs: NORTH<br><br>        // Cannot modify dir; enums are immutable<br>        // dir = Direction.NORTH; // allowed (reassignment of reference)<br>        // But you cannot change the enum instance itself<br>    }<br>}</pre><p>Enum constants are <strong>final and static</strong> by default.</p><p>Enums can have fields, but you should make them <strong>final</strong> to preserve immutability.</p><p><strong>6. Record:</strong></p><p>Records are immutable by design (all fields private final).</p><pre>public record Student(String name, int age) {}</pre><p>This single line generates:</p><ul><li>private final String name;</li><li>private final int age;</li><li>Canonical constructor (Person(String name, int age))</li><li>Getters (name(), age())</li><li>toString()</li><li>equals() &amp; hashCode()</li></ul><p><strong>Bonus:</strong></p><p><strong>Arrays &amp; ArrayList&lt;&gt; &amp; Collections</strong></p><p>Arrays and ArrayList are mutable even if declared as final.</p><ul><li>Arrays and ArrayLists are mutable objects.</li><li>Declaring a reference final only prevents <strong>reassigning the reference</strong>, not modifying the contents.</li></ul><pre>final List&lt;String&gt; list = new ArrayList&lt;&gt;();<br>list.add(&quot;Java&quot;);<br>list.add(&quot;Python&quot;);<br><br>System.out.println(list); // [Java, Python]<br><br>list.set(0, &quot;C++&quot;); // Allowed! Modifies content<br>System.out.println(list); // [C++, Python]<br><br>// list = new ArrayList&lt;&gt;(); // NOT allowed, final prevents reassignment</pre><p><strong>Immutability in Functional Programming with Java Streams</strong></p><ul><li><strong>Source immutability:</strong> Streams don’t alter the original collection.</li><li><strong>Transformations</strong> → Use pure functions like map(), filter(), flatMap() to produce new values.</li><li><strong>No side effects</strong> → Encourages immutability and thread-safety.</li></ul><pre>import java.util.List;<br>import java.util.stream.Collectors;<br><br>public class Main {<br>    public static void main(String[] args) {<br>        List&lt;String&gt; names = List.of(&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;);<br><br>        // Transform to uppercase without modifying original<br>        List&lt;String&gt; upper = names.stream()<br>                                  .map(String::toUpperCase)<br>                                  .collect(Collectors.toList());<br><br>        System.out.println(names); // [Alice, Bob, Charlie]<br>        System.out.println(upper); // [ALICE, BOB, CHARLIE]<br>    }<br>}</pre><p>Streams allow developers to <strong>process data immutably</strong>, avoiding shared mutable state.</p><p><strong>String vs StringBuilder vs StringBuffer:</strong></p><ol><li><strong>String:</strong></li></ol><p>String is immutable, meaning that once a String object is created, its contents cannot be modified. Any modification (like concatenation or replacing characters) results in the creation of a new String object.</p><p>Thread-Safety: String is inherently thread-safe because its state cannot change once it is created.</p><pre>String s = &quot;Hello&quot;;<br>s = s.concat(&quot; World&quot;); // creates a new String object</pre><p><strong>2. StringBuilder:</strong></p><p>StringBuilder is mutable, meaning its contents can be modified after the object is created.<br>It provides an efficient way to handle string modifications without creating new objects.</p><p>Thread-Safety: StringBuilder is not thread-safe, meaning it should not be used in multi-threaded environments unless external synchronization is provided.</p><p>Memory and Performance: StringBuilder is much more memory-efficient and faster than String when performing frequent string concatenations or modifications (such as inside loops). It does not create new objects on each modification.</p><pre>StringBuilder sb = new StringBuilder(&quot;Hello&quot;);<br>sb.append(&quot; World&quot;); <br>System.out.println(sb); // &quot;Hello World&quot;</pre><p><strong>3. StringBuffer:</strong></p><p>Like StringBuilder, StringBuffer is mutable, and its contents can be modified after creation.</p><p>Thread-Safety: StringBuffer is thread-safe and slower than StringBuilder</p><pre>StringBuffer sb = new StringBuffer(&quot;Hello&quot;);<br>sb.append(&quot; World&quot;);<br>System.out.println(sb); // &quot;Hello World&quot;</pre><p><strong>Integer Cache in Java</strong></p><p>In Java, all wrapper classes are immutable, including Integer. To optimize memory and performance, the JVM caches Integer objects for small values.</p><ul><li>Integer objects in the range <strong>-128 to 127</strong> are <strong>pre-created and reused</strong>.</li><li>This means if you create multiple Integer objects with the value 100, they <strong>all point to the same cached object</strong>.</li><li>This avoids creating unnecessary duplicate objects for commonly used numbers.</li></ul><pre>Integer a = 100;<br>Integer b = 100;<br><br>System.out.println(a == b);  // true, same object in cache<br><br>Integer x = 200;<br>Integer y = 200;<br><br>System.out.println(x == y);  // false, outside cache → new objects</pre><ul><li>Only applies to values between -128 and 127 (by default).</li><li>Objects outside this range are always new, even if the value is the same.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6195780a0ce8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inversion of Control & Dependency Injection]]></title>
            <link>https://medium.com/@syed.fawzul.azim/inversion-of-control-dependency-injection-59af14ce294f?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/59af14ce294f</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[inversion-of-control]]></category>
            <category><![CDATA[dependency-inversion]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Sat, 30 Aug 2025 01:17:34 GMT</pubDate>
            <atom:updated>2025-08-30T01:17:34.803Z</atom:updated>
            <content:encoded><![CDATA[<h4>From tightly coupled classes to flexible, testable design: Understanding Dependency Injection in Java.</h4><p><strong>Inversion of Control (IoC)</strong> is a design principle where the control of creating and managing objects is shifted away from the application code to a container or framework. Instead of your classes being responsible for finding or creating their dependencies, the framework provides them.</p><p><strong>Dependency Injection (DI)</strong> is the most common way to achieve IoC. With DI, a class declares the dependencies it needs, and those dependencies are “injected” into it — typically via a constructor, setter, or field — by a container(IoC) or external framework (like Spring).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qr9chze2aDs_HBlrJYMxlA.png" /></figure><p><strong>Dependency Inversion Principle (DIP) </strong>is one of the SOLID principles. High-level modules should not depend on low-level modules; both should depend on abstractions. DI is a way to achieve DIP in practice.</p><p><strong>Inversion of Control Container (IoC Container) </strong>is a framework component (like Spring’s ApplicationContext) that manages object creation, dependency wiring, and lifecycle, enabling IoC and DI.</p><p>For example:</p><pre>class UserService {<br>    private UserRepository userRepository = new UserRepository(); // tightly coupled<br>}</pre><ul><li>Here, UserService is responsible for creating its own UserRepository.</li><li>If we want to switch UserRepository to a different implementation (e.g., in-memory vs. database), we must modify the UserService code.</li></ul><p>If we create and manage dependencies directly inside our classes, we end up with:</p><ul><li><strong>Tight coupling</strong> → Classes know too much about how their dependencies are constructed, making the system rigid and hard to change.</li><li><strong>Difficult testing</strong> → Since dependencies are hard-coded, replacing them with mocks or stubs for unit tests becomes painful.</li><li><strong>Reduced reusability</strong> → Code tied to specific implementations is harder to reuse in other contexts.</li></ul><p>With DI, we let the framework handle this:</p><pre>class UserService {<br>    private final UserRepository userRepository;<br><br>    public UserService(UserRepository userRepository) {<br>        this.userRepository = userRepository; // dependency injected<br>    }<br>}</pre><p>Now, UserService is flexible, easier to test, and not tied to a specific UserRepository implementation.</p><p>Let’s expand on <strong>why DI makes </strong><strong>UserService flexible and testable</strong>:</p><ol><li>Flexibility:</li></ol><p>When dependencies are injected, UserService does not need to know which UserRepository it is using.</p><ul><li>We could have a DatabaseUserRepository (talking to MySQL),</li><li>an InMemoryUserRepository (for quick prototyping),</li><li>or even a CachedUserRepository.</li></ul><p>Since the UserService only depends on the <strong>interface (</strong><strong>UserRepository)</strong>, you can swap implementations without changing UserService itself.</p><pre>interface UserRepository {<br>    User findById(Long id);<br>}<br><br>class DatabaseUserRepository implements UserRepository {<br>    // fetch from database<br>}<br><br>class InMemoryUserRepository implements UserRepository {<br>    // fetch from in-memory collection<br>}</pre><pre>class UserService {<br>    private final UserRepository userRepository;<br><br>    public UserService(UserRepository userRepository) {<br>        this.userRepository = userRepository; <br>    }<br>}</pre><p>Now UserService can work with <strong>any</strong> UserRepository implementation.</p><p>2. Easier Testing:</p><p>In unit tests, we don’t want to hit the real database. With DI, you can inject a <strong>mock or stub</strong> implementation.</p><p>For example, using a simple stub:</p><pre>class FakeUserRepository implements UserRepository {<br>    @Override<br>    public User findById(Long id) {<br>        return new User(id, &quot;TestUser&quot;); // return dummy data<br>    }<br>}<br><br>@Test<br>void testUserService() {<br>    UserRepository fakeRepo = new FakeUserRepository();<br>    UserService userService = new UserService(fakeRepo);<br><br>    User user = userService.getUser(1L);<br><br>    assertEquals(&quot;TestUser&quot;, user.getName());<br>}</pre><p>No database needed → test runs fast, isolated, and repeatable.</p><p>3. Reduced Coupling:</p><ul><li>Without DI: UserService must <strong>know how to create </strong><strong>UserRepository</strong>, and changes to repository construction force changes in service code.</li><li>With DI: UserService only <strong>knows what it needs</strong>, not how it is built. The container/framework handles wiring.</li></ul><h4>Types of Dependency Injection:</h4><p>1. Constructor Injection:</p><p>Dependencies are passed via the constructor. Best for required dependencies.</p><pre>class UserService {<br>    private final UserRepository repo;<br><br>    public UserService(UserRepository repo) { // Constructor Injection<br>        this.repo = repo;<br>    }<br>}</pre><p>2. Setter Injection:</p><p>Dependencies are set using setter methods. Good for optional/changeable dependencies.</p><pre>class UserService {<br>    private UserRepository repo;<br><br>    public void setUserRepository(UserRepository repo) { // Setter Injection<br>        this.repo = repo;<br>    }<br>}</pre><p>3. Field Injection:</p><p>Dependencies are injected directly into fields (commonly with Spring’s @Autowired). Quick but less testable.</p><pre>class UserService {<br>    @Autowired<br>    private UserRepository repo; // Field Injection<br>}</pre><p>4. Interface Injection (rare in Java):</p><p>Dependency is provided through an interface method.</p><pre>interface UserRepositoryAware {<br>    void setUserRepository(UserRepository repo);<br>}<br><br>class UserService implements UserRepositoryAware {<br>    private UserRepository repo;<br>    public void setUserRepository(UserRepository repo) { this.repo = repo; }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59af14ce294f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exception Handling in Java]]></title>
            <link>https://medium.com/@syed.fawzul.azim/exception-handling-in-java-e55d57ba5602?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/e55d57ba5602</guid>
            <category><![CDATA[exception-handling]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Thu, 28 Aug 2025 01:21:33 GMT</pubDate>
            <atom:updated>2025-08-28T01:21:33.887Z</atom:updated>
            <content:encoded><![CDATA[<h4>The difference between checked and unchecked exceptions.</h4><p>An exception is an event that disrupts normal program execution (e.g., dividing by zero, accessing a null object).</p><p>Types of Exceptions:</p><ul><li><strong>Checked Exceptions</strong>: Handled at compile-time (e.g., IOException, SQLException). Must be declared or caught.</li><li><strong>Unchecked Exceptions</strong>: Runtime exceptions (e.g., NullPointerException, ArrayIndexOutOfBoundsException). Not required to be caught.</li><li><strong>Errors</strong>: Serious issues, usually not handled by code (e.g., OutOfMemoryError, StackOverflowError).</li></ul><p><strong>Exception Hierarchy:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k2K8OcGQLWTkobzozs4ROw.png" /><figcaption>Exception Hierarchy</figcaption></figure><p>Examples:</p><ol><li>Unchecked / Runtime Exception (ArithmeticException):</li></ol><pre>public class UncheckedExample {<br>    public static void main(String[] args) {<br>            int result = 10 / 0; // risky code<br>    }<br>}<br></pre><pre>Exception in thread &quot;main&quot; java.lang.ArithmeticException: / by zero<br>    at Example.main(Example.java:4)</pre><p><strong>Why unchecked?</strong><br>Because ArithmeticException is a subclass of RuntimeException.<br>The compiler does <strong>not</strong> force you to handle it. It only fails at runtime.</p><p>2. Checked / Compile Time Exception (ClassNotFoundException):</p><pre>public class CheckedExample {<br>    public static void main(String[] args) {<br>        method1();<br>    }<br>    public static void method1(){<br>        throw new ClassNotFoundException();<br>    }<br>}</pre><p><strong>What happens?</strong></p><ul><li>ClassNotFoundException is a checked exception.</li><li>In Java, checked exceptions must either be caught or declared with throws.</li><li>In this code, method1() throws ClassNotFoundException but does not declare throws and does not handle it with try-catch.</li><li>The compiler forces you to handle it.</li></ul><p>Result: <strong>Compilation Error</strong></p><pre>error: unreported exception ClassNotFoundException; must be caught or declared to be thrown<br>        throw new ClassNotFoundException();<br>        ^</pre><p><strong>Correct Versions:</strong></p><p>1. Using throws in method signature</p><pre>public static void method1() throws ClassNotFoundException {<br>    throw new ClassNotFoundException();<br>}</pre><p>But then, main must also handle it or declare it:</p><pre>public static void main(String[] args) throws ClassNotFoundException {<br>    method1();<br>}</pre><p>2. Using try-catch:</p><pre>public static void method1() {<br>    try {<br>        throw new ClassNotFoundException();<br>    } catch (ClassNotFoundException e) {<br>        // handle this exception<br>        System.out.println(&quot;Caught: &quot; + e);<br>    }<br>}</pre><p>→ Unchecked exceptions (RuntimeException) → compiler doesn’t force you.</p><p>→ Checked exceptions (like ClassNotFoundException) → compiler forces you to handle or declare.</p><h4>Handle Exceptions:</h4><p>1. Using try-catch:</p><pre>// catch block can only catch exception which can be thrown by try block<br>public class Example1 {<br>    public static void main(String[] args) {<br>        try {<br>            int result = 10 / 0;  // risky code<br>        } catch (ArithmeticException e) {<br>            System.out.println(&quot;Handled: &quot; + e.getMessage());<br>        }<br>    }<br>}<br><br>// Multiple catch blocks [first put specific exception (ArithmeticException) then generic exception]<br>public class MultipleCatchExample {<br>    public static void main(String[] args) {<br>        try {<br>            // Risky code<br>            int[] numbers = new int[5];<br>            numbers[5] = 30 / 0;  // causes ArithmeticException first<br>        } <br>        catch (ArithmeticException e) {<br>            System.out.println(&quot;Arithmetic Exception caught: &quot; + e.getMessage());<br>        } <br>        catch (ArrayIndexOutOfBoundsException e) {<br>            System.out.println(&quot;Array Index Out Of Bounds Exception caught: &quot; + e.getMessage());<br>        } <br>        catch (Exception e) {<br>            System.out.println(&quot;General Exception caught: &quot; + e.getMessage());<br>        }<br>        <br>        System.out.println(&quot;Program continues...&quot;);<br>    }<br>}</pre><p>2. Using try-catch-finally:</p><p>finally always executes (good for cleanup, closing DB/file connections).</p><p>Only if JVM-related issues like out of memory, system shutdown down or the process is forcefully killed, then finally block does not get executed.</p><pre>public class Example2 {<br>    public static void main(String[] args) {<br>        try {<br>            int result = 10 / 0;<br>        } catch (ArithmeticException e) {<br>            System.out.println(&quot;Handled: &quot; + e.getMessage());<br>        } finally {<br>            System.out.println(&quot;Finally block always executes.&quot;);<br>        }<br>    }<br>}</pre><p>3. Declaring with throws (propagation):</p><pre>public class Example {<br>    public static void main(String[] args) throws Exception {<br>        method1();<br>    }<br><br>    public static void method1() throws Exception {<br>        throw new Exception(&quot;Something went wrong!&quot;);<br>    }<br>}</pre><p>4. Custom Exception Handling:</p><pre>class InvalidAgeException extends Exception {<br>    public InvalidAgeException(String msg) {<br>        super(msg);<br>    }<br>}<br><br>public class Example6 {<br>    public static void main(String[] args) {<br>        try {<br>            validateAge(15);<br>        } catch (InvalidAgeException e) {<br>            System.out.println(&quot;Custom Exception caught: &quot; + e.getMessage());<br>        }<br>    }<br><br>    public static void validateAge(int age) throws InvalidAgeException {<br>        if (age &lt; 18) throw new InvalidAgeException(&quot;Age must be 18+&quot;);<br>    }<br>}</pre><h4>What is Error?</h4><ul><li>Error is a subclass of Throwable in Java.</li><li>It represents serious problems that applications usually should not try to handle.</li><li>These are usually outside the control of the program and indicate JVM-level issues.</li><li>Example: OutOfMemoryError , StackOverflowError , VirtualMachineError</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e55d57ba5602" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Apache Kafka Explained: Core Concepts, Use Cases, and Architecture]]></title>
            <link>https://medium.com/@syed.fawzul.azim/apache-kafka-explained-core-concepts-use-cases-and-architecture-62eceaec2efe?source=rss-94e46dfb4db------2</link>
            <guid isPermaLink="false">https://medium.com/p/62eceaec2efe</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Syed Fawzul Azim]]></dc:creator>
            <pubDate>Tue, 26 Aug 2025 01:07:34 GMT</pubDate>
            <atom:updated>2025-08-26T01:07:34.524Z</atom:updated>
            <content:encoded><![CDATA[<h4>A deep dive into how Kafka achieves scalability, fault-tolerance, and high throughput</h4><p>Modern applications generate <strong>huge volumes of real-time data</strong>—clicks, orders, payments, logs, metrics, IoT signals, etc. Traditional systems like databases or message queues struggle when data needs to be:</p><ul><li><strong>Scalable: </strong>handling millions of events per second.</li><li><strong>Durable: </strong>reliably storing events for later replay.</li><li><strong>Distributed: </strong>working across multiple servers and data centers.</li><li><strong>Real-time: </strong>delivering data instantly to multiple consumers.</li></ul><p>Kafka solves these problems by acting as a <strong>high-throughput, fault-tolerant, distributed event streaming platform</strong> that can store, publish, and subscribe to event data at scale.</p><h4><strong>Message Queue VS Kafka:</strong></h4><p>A Message Queue (MQ) (like RabbitMQ, ActiveMQ, SQS) follows a point-to-point model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*eIqCuob6d1SjDI7uH_XklQ.png" /><figcaption>Message Queue</figcaption></figure><ul><li><strong>Producers</strong> send messages to a queue.</li><li><strong>Consumers</strong> read messages from the queue.</li><li>Once a message is consumed, it’s <strong>removed</strong> from the queue.</li></ul><p>Kafka is not just a message queue — it’s a <strong>distributed event streaming platform</strong>. Key differences:</p><p><strong>1. Message Retention</strong></p><ul><li>MQ: Messages are deleted after consumption.</li><li>Kafka: Messages are stored for a configured <strong>retention period</strong> (e.g., 7 days), even after being consumed. Allowing replay, historical analysis, or late-joining consumers</li></ul><p><strong>2. Consumption Model:</strong></p><ul><li>MQ: Often point-to-point (one consumer per message).</li><li>Kafka: Uses a pub-sub model with consumer groups. Multiple consumers in a group share work, but different groups can read the same messages independently (e.g., one for analytics, another for alerting).</li></ul><p><strong>3. Throughput &amp; Scalability:</strong></p><ul><li>MQ: Designed for moderate workloads.</li><li>Kafka: Handles <strong>millions of messages/sec</strong>, horizontally scalable.</li></ul><h4><strong>Top Use Cases of Apache Kafka:</strong></h4><p><strong>1. Log Aggregation:</strong></p><p>Kafka centralizes logs from distributed systems, applications, and servers, making it easier to collect, store, and process them for debugging, auditing, or compliance. This is common in large-scale environments like cloud infrastructures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VFlf0-r7pnFa1lHxbpKwBA.png" /><figcaption>Log Aggregation — Source: ByteByteGo</figcaption></figure><p><strong>2. Real-Time Machine Learning Pipelines:</strong></p><p>Kafka collects raw streaming data (user clicks, transactions, sensor readings, logs).</p><p>Using <strong>Kafka Streams</strong> or <strong>Apache Flink</strong> to process, aggregate, and enrich data into ML-ready features.</p><p>Kafka feeds processed data into training pipelines (Spark, Flink, TensorFlow).</p><p>Kafka routes incoming events to <strong>ML inference services</strong> that serve predictions instantly. Predictions and outcomes are written back into Kafka all in real time.</p><p>Example: Fraud Detection, Recommendation Engines, Predictive Maintenance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-02IQQysrWJS-yuOBn0i9w.png" /><figcaption>Real-Time Machine Learning Pipelines — Source: ByteByteGo</figcaption></figure><p>Kafka integrates with tools like Kafka Streams for real-time data transformations, filtering, and aggregations, enabling applications such as fraud detection or live ETL (Extract, Transform, Load) pipelines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*l5SN3oTXm3xmV0bwZQy_kQ.png" /><figcaption>Stream Processing using Kafka Streams — Source: ByteByteGo</figcaption></figure><p><strong>3. Real-Time System Monitoring &amp; Alerting:</strong></p><p>While log analysis helps to investigate past events, this use case is different.</p><p>It’s about immediate, proactive system health tracking and alerting.</p><p>Applications, containers, servers, and network devices produce logs and metrics. Kafka Streams, Apache Flink, or Spark Streaming process the data in real time. Examples: detect error spikes, unusual latency, or failed login attempts.</p><p>Processed data is sent to monitoring systems like <strong>Prometheus, Grafana, or ELK (Elasticsearch, Logstash, Kibana)</strong>.</p><p>Alerts are triggered via Slack, PagerDuty, or Email when thresholds are breached.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*nfr2PzT6mk1mcBrpDHv3vQ.png" /><figcaption>Real-Time System Monitoring &amp; Alerting — Source: ByteByteGo</figcaption></figure><p><strong>4. Change Data Capture (CDC) with Kafka:</strong></p><p>CDC is the process of <strong>capturing changes (INSERT, UPDATE, DELETE)</strong> from a database and delivering them to other systems in the same order as they occurred. Instead of running expensive batch jobs, CDC streams database changes as they happen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*jHOlicyYFGWirPpwMEpgmg.png" /><figcaption>Database transaction log(includes data modifications) feeds into kafka — Source: ByteByteGo</figcaption></figure><p>Kafka, with <strong>Kafka Connect</strong> and CDC tools (like <strong>Debezium</strong>), makes this seamless.</p><p><strong>Kafka Connect</strong> is a framework for <strong>integrating Kafka with external systems</strong> (databases, cloud storage, search indexes, etc.) — without writing custom code. Instead of building producers/consumers manually, Kafka Connect uses <strong>pre-built connectors</strong> to stream data <strong>into</strong> and <strong>out of</strong> Kafka.</p><ul><li>Kafka Connect connects Kafka to systems like MySQL, PostgreSQL, MongoDB, Elasticsearch, S3, Snowflake, etc.</li><li>Source Connectors (Data <em>into</em> Kafka): Capture data changes from source systems. Example: Debezium connector streams row-level changes from MySQL into Kafka topics.</li><li>Sink Connectors (Data <em>out of</em> Kafka): Push data from Kafka topics into target systems.</li><li>No coding required. Connectors are configured with JSON properties.</li><li>In <strong>Kafka Connect</strong>, a <strong>connector</strong> is a <strong>pre-built or custom plugin</strong> that handles moving data <strong>into</strong> or <strong>out of Kafka</strong> from external systems — without writing custom producer/consumer code.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q_K6IfuYpUHAm6r9v1ZQrA.png" /><figcaption>Kafka with Kafka Connect(diffrent connectors)</figcaption></figure><p><strong>5. System Migration:</strong></p><p>Kafka can simplify <strong>system or database migration</strong> by streaming data in <strong>real time</strong> from the old system to the new one. Kafka enables <strong>zero-downtime, reliable, and scalable migrations</strong>.</p><h4>Why Kafka is Fast?</h4><p>Kafka is optimized for high throughput, with low latency being good but secondary. It excels at processing massive data streams reliably and efficiently.</p><ul><li><strong>Sequential I/O and Append-Only Logs:</strong> Kafka stores data in immutable, append-only logs, enabling sequential writes and reads that minimize disk seeks and leverage contiguous memory blocks. This is far faster than random I/O used in traditional databases.</li><li><strong>Batching and Compression:</strong> Producers batch multiple messages before sending, reducing network round-trips and I/O operations.</li><li><strong>Zero Copy: </strong>It<strong> is </strong>a technique where Kafka sends data directly from disk to the network without copying it through application memory.</li></ul><p>Normally, sending data over the network involves multiple copies:<br>=&gt; Disk → OS buffer → Application buffer → Socket buffer → NIC Buffer -&gt; Consumer.</p><p>With <strong>zero copy</strong>, Kafka skips the application buffer &amp; Socket buffer:<br>=&gt; Disk → OS buffer → NIC Buffer -&gt; Consumer.</p><ul><li>Reduces <strong>CPU usage</strong></li><li>Improves <strong>throughput</strong></li><li>Makes Kafka <strong>faster and more efficient</strong></li></ul><h4>Kafka Core Concepts:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*NiPwEU7WfuAZ3q69lw0VKw.png" /><figcaption><strong>Kafka Architechture</strong></figcaption></figure><p>1. Messages (Events or Records):</p><ul><li>The fundamental unit of data in Kafka, representing an immutable fact about something that happened (e.g., a user click, payment transaction, or sensor reading).</li><li>Each message/event consists of a key (optional, for partitioning), value (the actual data, often in JSON, text, or binary format), timestamp, and headers. Events are appended to logs in a sequential, time-ordered manner, enabling replayability and real-time processing.</li></ul><p>2. Producers:</p><ul><li>Clients or applications that publish (write) events to Kafka topics.</li><li>They decide which topic (and sometimes partition) the message goes to.</li></ul><p>3. Consumer:</p><ul><li>Clients or applications that subscribe to topics and read (consume) events.</li><li>Consumers pull data at their own pace, processing events in real-time or batch. They maintain their position using offsets and can rewind/replay streams. Supports at-least-once, at-most-once, or exactly-once semantics</li><li>Consumers can be grouped into <strong>consumer groups</strong> for load balancing.</li></ul><p>4. Topics:</p><ul><li>A logical abstraction or category for organizing streams of events.</li><li>Producers publish events to topics, and consumers subscribe to them. Topics are multi-subscriber (pub-sub model) and can handle massive volumes of data.</li></ul><p>5. Partitions:</p><ul><li>Each topic is split into <strong>partitions</strong> (ordered logs).</li><li>Each message in a partition has an <strong>offset</strong> (unique ID), so the events are ordered, and immutable sequences of events (logs) help parallelism.</li><li>No guaranteed order across different partitions.</li><li>Events with the same key are assigned to the same partition (via hashing), ensuring order within partitions but not across them.</li></ul><p>6. Consumer Groups:</p><ul><li>A group of consumers that <strong>shares the load</strong> of reading from a topic.</li><li>Each partition is consumed by only <strong>one consumer in a group</strong>.</li><li>If a consumer fails, partitions are reassigned (rebalancing). Multiple groups can consume the same topic independently, enabling fan-out.</li></ul><p>7. Broker:</p><ul><li>A Kafka <strong>server</strong> that stores data and serves client requests.</li><li>A Kafka cluster has multiple brokers.</li><li>Kafka spreads partitions across different <strong>brokers</strong> in the cluster for <strong>scalability</strong> and <strong>fault tolerance</strong>.</li><li>Each partition has:<br>- Leader replica → the main copy that handles all reads/writes.<br>- Follower replicas → backup copies stored on other brokers for redundancy.</li></ul><p>8. ZooKeeper / KRaft (Kafka Raft):</p><ul><li>ZooKeeper (legacy) handles leader election, topic configuration, and broker registration.</li><li>KRaft (introduced in Kafka 2.8, default since 3.3) uses a Raft-based protocol for self-managed metadata, eliminating ZooKeeper dependency for simpler operations and better scalability.</li></ul><p>Additional Key Components:</p><ul><li><strong>Kafka Cluster: </strong>A Kafka cluster is a group of Kafka brokers (servers) working together to store, manage, and distribute messages.</li><li><strong>Kafka APIs</strong>: Core interfaces include Producer API (publishing), Consumer API (subscribing), Streams API (processing), and Connect API (integration with external systems).</li><li><strong>Kafka Streams</strong>: A library for building stream processing applications, supporting operations like filtering, aggregating, and joining directly on Kafka data.</li><li><strong>Kafka Connect</strong>: A framework for reliably moving data between Kafka and external systems (e.g., databases, files) using connectors.</li></ul><h4>Kafka Delivery Semantics:</h4><ul><li><strong>At-Most-Once:<br>- </strong>Messages may be lost but are never redelivered.<br>- Risk: Some messages may be <strong>lost</strong> if there’s a failure.<br>- Example: Non-critical logging where occasional message loss is acceptable.</li><li><strong>At-Least-Once:</strong><br>- Messages are never lost but may be redelivered.<br>- Ensured by <strong>retries on producer failures</strong> and <strong>acknowledgments from brokers</strong>.<br>- Example: Payment transactions where a missing message is unacceptable, but duplicates can be handled in downstream logic.</li><li><strong>Exactly-Once:<br>- </strong>Each message is <strong>processed only once</strong>.<br>- Achieved with <strong>idempotent producers</strong> + <strong>transactions</strong> across partitions and topics.<br>- Example: Financial transfers, stock trading, or billing pipelines where duplicates must be avoided.</li></ul><h4>Serialization and Schema Registry in Kafka:</h4><p><strong>Serialization:</strong> Kafka transmits data as <strong>bytes</strong>, but applications usually work with objects (Java POJOs).<br>Serialization is the process of <strong>converting objects into byte arrays</strong> before sending them to Kafka.</p><ul><li><strong>Producer side:</strong> Object → Serialized (bytes) → Sent to Kafka topic</li><li><strong>Consumer side:</strong> Serialized (bytes) → Deserialized → Object</li></ul><p>Kafka supports multiple serializers, such as:</p><ul><li><strong>StringSerializer</strong> — for plain text</li><li><strong>ByteArraySerializer</strong> — for raw bytes</li><li><strong>Avro, Protobuf, JSON</strong> — for structured data with schemas</li></ul><p><strong>Schema Registry: </strong>Without schema management, producers and consumers might <strong>break compatibility</strong>:</p><ul><li>The producer adds/removes a field.</li><li>Consumer expects an old format → fails to parse.</li></ul><p>This leads to <strong>serialization errors, data loss, or runtime crashes</strong>.</p><p>The <strong>Confluent Schema Registry</strong> (or any compatible registry) solves this by:</p><ul><li>Storing schemas centrally (Avro, Protobuf, JSON).</li><li>Assigning a unique ID to each schema version.</li><li>Allowing producers and consumers to check for compatibility before sending/reading data.</li><li>Enforces schema evolution rules (backward, forward, or full compatibility).</li><li>Prevents breaking changes between producers and consumers.</li></ul><h4><strong>How Kafka Ensures Durability &amp; Consistency:</strong></h4><h4>Durability:</h4><p>Durability in Kafka ensures that once a message is written to a topic, it is not lost, even in the face of failures like broker crashes or network issues.</p><p>Key mechanisms include:</p><ol><li>Append-Only Log Storage:</li></ol><ul><li>Kafka stores events in immutable, append-only logs on disk for each topic partition. Messages are written sequentially, leveraging fast sequential I/O, and persist for a configurable retention period (e.g., hours, days, or indefinitely) or size limit. This ensures data remains available even if consumers are offline or brokers fail temporarily.</li></ul><p>2. Replication Across Brokers:</p><ul><li>Each partition is replicated across multiple brokers based on the replication factor (e.g., 3), creating multiple copies of the data. One broker acts as the leader (handling reads/writes), while others are followers (replicating the leader’s log) (handling reads).</li><li>If the leader broker fails, an in-sync replica (ISR) (a follower fully synchronized with the leader) is promoted to leader, ensuring no data loss.</li></ul><p>3. Acknowledgment Modes:</p><p>Producers can configure the <strong>acknowledgment (acks)</strong> setting to control durability:</p><ul><li>acks=0: No acknowledgment; fastest but risks loss if the broker fails before writing.</li><li>acks=1: Leader acknowledges after writing to its log; balances speed and durability, but risks loss if the leader fails before replication.</li><li>acks=all: Leader waits for all ISRs to replicate the message before acknowledging, maximizing durability at the cost of higher latency.</li></ul><p>Using acks=all with min.insync.replicas &gt; 1 ensures messages are durable across multiple brokers.</p><p>Kafka Acknowledgment Flow:</p><ol><li>The producer sends a message to a Kafka broker (the leader of the partition).</li><li>Broker writes a message to the log and stores it on disk and/or replicates it to followers.</li><li>Broker sends ack to producer, and depending on the acks setting:</li></ol><ul><li>acks=0 → No ack (fastest, unreliable).</li><li>acks=1 → Leader broker confirms write.</li><li>acks=all → All in-sync replicas confirm before ack (most durable).</li></ul><p>The producer knows the message is safely in Kafka once it receives the ack from the broker.</p><h4>Consistency</h4><p>Consistency in Kafka ensures that data is delivered correctly and predictably across producers, brokers, and consumers, maintaining order and avoiding duplicates or gaps.</p><p>Kafka achieves this through:</p><p>1. Ordered Delivery Within Partitions:</p><ul><li>Kafka guarantees that messages within a single partition are stored and delivered in the order they were written, based on their <strong>offsets</strong> (unique, monotonically increasing identifiers). This ensures strong consistency for applications relying on event order (e.g., transaction logs). Note that order is not guaranteed across partitions.</li></ul><p>2. In-Sync Replicas (ISRs):</p><ul><li>Kafka maintains consistency by ensuring only ISRs (replicas fully synchronized with the leader) are used for failover.</li></ul><p>3. Consumer Group Rebalancing:</p><ul><li>Kafka’s consumer groups ensure consistent data distribution across consumers. When a consumer joins or leaves, partitions are reassigned (rebalanced) to maintain even load and consistent processing.</li><li>Offsets ensure no messages are skipped or reprocessed unnecessarily during rebalancing.</li><li><strong>Note: </strong>This works because each partition is consumed by only one consumer per group — if multiple consumers in the same group tried to read one partition, it would break ordering and lead to inconsistencies.</li></ul><h4>Monitoring:</h4><ul><li>Broker metrics: under-replicated/offline partitions, ISR, disk/CPU/network usage.</li><li>Producer/consumer metrics: lag, commit rates, errors, throughput.</li><li>Tools: Prometheus + Grafana, Confluent Control Center, Datadog, ELK Stack.</li><li>Set alerts for thresholds (e.g., consumer lag &gt; 1 min).</li><li>Track JVM metrics (GC, heap) and topic compaction/dedup buffers.</li></ul><h4>Producer-Side Storing/ WAL — When to Use</h4><ul><li><strong>Broker failures</strong>: Protects messages if Kafka(all brokers) is temporarily down.</li><li><strong>Retries may fail</strong>: Ensures messages aren’t lost if the producer crashes or max retries are exceeded.</li><li><strong>End-to-end reliability</strong>: Guarantees every event reaches Kafka before processing.</li></ul><p><strong>Solution: </strong>Before sending the event to Kafka, store messages in a local durable log (DB or file). If retries fail, the message can be replayed later.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62eceaec2efe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>