<?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 Shanuka Lakshan on Medium]]></title>
        <description><![CDATA[Stories by Shanuka Lakshan on Medium]]></description>
        <link>https://medium.com/@shanukalakshan9817?source=rss-a9c9c4806dbb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eAkFlZxJ0ACVQC9BwdL9zg.png</url>
            <title>Stories by Shanuka Lakshan on Medium</title>
            <link>https://medium.com/@shanukalakshan9817?source=rss-a9c9c4806dbb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:08:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shanukalakshan9817/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[Zero-Downtime Schema Changes for Enterprise SQL Databases at 50M+ Rows]]></title>
            <link>https://medium.com/@shanukalakshan9817/zero-downtime-schema-changes-for-enterprise-sql-databases-at-50m-rows-80cd0ded85f8?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/80cd0ded85f8</guid>
            <category><![CDATA[enterprise-software]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[enterprise-architecture]]></category>
            <category><![CDATA[relational-databases]]></category>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Sun, 22 Mar 2026 13:47:08 GMT</pubDate>
            <atom:updated>2026-03-22T13:47:08.045Z</atom:updated>
            <content:encoded><![CDATA[<p>In large systems, schema changes are rarely just database tasks. They are availability decisions.</p><p>I recently looked at a common but deceptively risky scenario: adding a new column to a SQL table with more than 50 million rows, while the system remains live.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2rVDBMgMtNdSHlzotjGaLQ.png" /></figure><p>The table was simple in purpose but large in scale:</p><ul><li>users table held a small number of records</li><li>user_activity_logs was a write-heavy table with <strong>50M+</strong> rows</li><li>The system had to remain <strong>highly available</strong> during the change</li></ul><p>The new requirement was to add:</p><pre>device_type ENUM(&#39;WEB&#39;, &#39;MOBILE&#39;, &#39;TABLET&#39;)</pre><p>At first glance, this looks like a basic ALTER TABLE. Architecturally, it is not.</p><h3>The real problem</h3><p>At this scale, the danger is not syntax. <strong>The danger is execution path.</strong></p><p>A schema change can be:</p><ul><li><strong>metadata-only and nearly instant</strong></li><li><strong>partially online but still disruptive</strong></li><li><strong>a full table rewrite with operational risk</strong></li></ul><p>That is why schema evolution on hot tables should be treated as a staged rollout, not a one-step database command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I7_i8-NLYFlOnNaFd9NB2w.png" /></figure><h3>The correct pattern</h3><p>The safest approach is to separate three concerns:</p><ol><li>schema introduction</li><li>historical data migration</li><li>default behavior for future writes</li></ol><h3>Step 1: add the column safely</h3><pre>ALTER TABLE user_activity_logs<br>ADD COLUMN device_type ENUM(&#39;WEB&#39;, &#39;MOBILE&#39;, &#39;TABLET&#39;) DEFAULT NULL,<br>ALGORITHM=INSTANT;</pre><p>This keeps the initial change focused on structure only.</p><h3>Step 2: backfill existing rows in batches</h3><p>Historical rows needed to resolve to WEB, but updating 50M rows in one statement would be a bad operational decision.</p><p>Instead, the backfill should run in small batches:</p><pre>UPDATE user_activity_logs<br>SET device_type = &#39;WEB&#39;<br>WHERE device_type IS NULL<br>LIMIT 50000;</pre><p>This keeps transactions smaller, reduces lock pressure, and makes the rollout observable and pausable.</p><h3>Step 3: set the default for future writes</h3><pre>ALTER TABLE user_activity_logs<br>ALTER device_type SET DEFAULT &#39;WEB&#39;;</pre><p>his ensures that new rows behave correctly without coupling future-write behavior to the historical migration.</p><h3>Why this matters architecturally</h3><p>The core principle is simple:</p><p><strong>Schema change is not the same as data migration.</strong></p><p>That distinction matters more as systems scale.</p><p>When teams combine structural change, backfill, and future defaults into one operation, they increase the chance of:</p><ul><li>unexpected locking</li><li>write latency spikes</li><li>rollback complexity</li><li>availability incidents</li></ul><p>When those concerns are separated, the change becomes safer, clearer, and easier to operate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sMxmYYJQZBcm6Hh8hOUERg.png" /></figure><h3>Practical takeaway</h3><p>For large SQL tables in live systems, the right pattern is:</p><ul><li>add structure first</li><li>migrate old data gradually</li><li>define new default behavior last</li></ul><p>This is not just a database optimization. It is an availability first design decision.</p><p>That is the difference between executing a migration and engineering one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80cd0ded85f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection with FastAPI: A Forward-Looking Guide]]></title>
            <link>https://medium.com/@shanukalakshan9817/dependency-injection-with-fastapi-a-forward-looking-guide-1adfc143a277?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1adfc143a277</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Sat, 03 May 2025 12:00:51 GMT</pubDate>
            <atom:updated>2025-05-03T12:00:51.979Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IMVrg-m_7OnucLWWNEEX1w.png" /></figure><p>Dependency Injection (DI) is a design pattern that decouples the creation of an object from its behavior, promoting modularity, testability, and maintainability. FastAPI, a modern, high‑performance Python web framework, embraces DI as a first‑class concept making it effortless to inject services, configurations, and security layers into your route handlers.</p><p><strong>Why Dependency Injection Matters</strong></p><ul><li><strong>Modularity &amp; Maintainability</strong>: Dependencies are defined separately and injected where needed, making components easier to swap or upgrade.</li><li><strong>Testability</strong>: You can override dependencies during tests, isolating units without cumbersome mocks or complex setup.</li><li><strong>Readability</strong>: FastAPI’s declarative syntax makes it obvious what each route depends on, improving developer onboarding and collaboration.</li></ul><p><strong>Core Concepts in Dependency Injection</strong></p><ul><li><strong>Dependencies</strong><br> A “dependency” in FastAPI is any callable (function, class, or coroutine) that provides a value.</li><li><strong>Injection via Parameters</strong><br> Simply annotate your path operation function parameters with types and Depends(...) to declare a dependency.</li></ul><p><strong>Scopes</strong></p><ul><li><strong>Function-scoped</strong> (default): A new dependency instance is created per request.</li><li><strong>Session-scoped</strong>: Using singletons or caching, you can share state across requests.</li><li><strong>Custom scopes</strong>: By crafting a generator with yield, you can control startup and teardown logic.</li></ul><h3><strong>Without Dependency Injection</strong></h3><p>In many simple scripts, you might construct service objects directly within your route. While this works, it quickly becomes repetitive and hard to test.</p><pre>from fastapi import FastAPI, HTTPException<br><br>app = FastAPI()<br><br>class AuthService:<br>    def authenticate(self, token: str):<br>        if token == &quot;valid-token&quot;:<br>            return True<br>        raise HTTPException(status_code=401, detail=&quot;Unauthorized&quot;)<br><br>@app.get(&quot;/secure-data&quot;)<br>def get_secure_data_no_di(token: str):<br>    # Direct instantiation inside the route<br>    auth_service = AuthService()<br>    if auth_service.authenticate(token):<br>        return {&quot;data&quot;: &quot;This is secure data (no DI)&quot;}</pre><p><strong>Drawbacks</strong></p><ul><li>Every route that needs AuthService must instantiate it.</li><li>You cannot override AuthService behavior in tests without modifying global state.</li><li>Harder to introduce common setup (logging, metrics) or teardown logic.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gOTJVIAZo22XtMjix5STZA.png" /></figure><h3>Embracing DI with Annotated + Depends</h3><p>FastAPI’s Dependency Injection system revolves around callables (functions, classes, coroutines) that return values or generators. You declare dependencies via the Depends marker. Since Python 3.9+, you can use Annotated for a cleaner signature:</p><pre>from typing import Annotated<br>from fastapi import Depends, FastAPI, HTTPException<br><br>app = FastAPI()<br><br>class AuthService:<br>    def authenticate(self, token: str):<br>        if token == &quot;valid-token&quot;:<br>            return True<br>        raise HTTPException(status_code=401, detail=&quot;Unauthorized&quot;)<br><br># 1️⃣ Define a provider function<br>def get_auth_service() -&gt; AuthService:<br>    # Could include setup logic, logging, or shared state here<br>    return AuthService()<br><br># 2️⃣ Create an Annotated alias for readability<br>AuthServiceDep = Annotated[AuthService, Depends(get_auth_service)]<br><br>@app.get(&quot;/secure-data&quot;)<br>def get_secure_data(token: str, auth_service: AuthServiceDep):<br>    if auth_service.authenticate(token):<br>        return {&quot;data&quot;: &quot;This is secure data (with DI)&quot;}</pre><p><strong>Key Points</strong></p><ol><li><strong>get_auth_service</strong> is invoked once per request (by default).</li><li>FastAPI inspects the signature, recognizes Depends, and injects the returned AuthService.</li><li>Your route stays focused on authorization logic, without worrying about instantiation.</li></ol><h3><strong>Advanced DI Patterns</strong></h3><ol><li>Database Sessions</li></ol><p>Use generator dependencies to manage lifecycle:</p><pre>from sqlalchemy.orm import Session<br>from fastapi import Depends<br><br>def get_db_session() -&gt; Session:<br>    session = SessionLocal()<br>    try:<br>        yield session<br>    finally:<br>        session.close()<br><br>DBSessionDep = Annotated[Session, Depends(get_db_session)]<br><br>@app.post(&quot;/users/&quot;)<br>def create_user(user: UserCreate, db: DBSessionDep):<br>    db_user = UserModel(**user.dict())<br>    db.add(db_user)<br>    db.commit()<br>    db.refresh(db_user)<br>    return db_user</pre><p><strong>Forward‑Looking</strong>: As async ORMs like SQLModel or Tortoise ORM mature, refactor these to async def get_db_session() and yield async sessions fully embracing FastAPI’s async event loop.</p><p>2. Authorization &amp; Security</p><p>Centralize token verification and user retrieval:</p><pre>from fastapi import Security, status<br>from fastapi.security import OAuth2PasswordBearer<br><br>oauth2_scheme = OAuth2PasswordBearer(tokenUrl=&quot;/token&quot;)<br><br>def get_current_user(token: str = Depends(oauth2_scheme)) -&gt; User:<br>    user = verify_token_and_fetch_user(token)<br>    if not user:<br>        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)<br>    return user<br><br>CurrentUserDep = Annotated[User, Depends(get_current_user)]<br><br>@app.get(&quot;/profile/&quot;)<br>def read_profile(current_user: CurrentUserDep):<br>    return {&quot;username&quot;: current_user.username, &quot;email&quot;: current_user.email}</pre><h3>MongoDB Integration</h3><h4>Asynchronous with Motor</h4><p>Using Motor (the official async MongoDB driver) lets you keep your endpoints fully non‑blocking:</p><pre>from typing import Annotated<br>from fastapi import Depends, FastAPI<br>from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase<br>from pydantic import BaseModel<br><br>app = FastAPI()<br><br># Global client instance (reused across requests)<br>MONGODB_URL = &quot;mongodb://localhost:27017&quot;<br>motor_client = AsyncIOMotorClient(MONGODB_URL)<br><br># 1️⃣ Define an async provider that returns the database<br>async def get_mongo_db() -&gt; AsyncIOMotorDatabase:<br>    return motor_client[&quot;mydatabase&quot;]<br><br># 2️⃣ Create an alias for cleaner signatures<br>MongoDBDep = Annotated[AsyncIOMotorDatabase, Depends(get_mongo_db)]<br><br>class Item(BaseModel):<br>    name: str<br>    description: str<br><br>@app.post(&quot;/items/&quot;, response_model=Item)<br>async def create_item(item: Item, db: MongoDBDep):<br>    result = await db[&quot;items&quot;].insert_one(item.dict())<br>    created = await db[&quot;items&quot;].find_one({&quot;_id&quot;: result.inserted_id})<br>    # Convert ObjectId to str<br>    created[&quot;id&quot;] = str(created.pop(&quot;_id&quot;))<br>    return created<br><br>@app.get(&quot;/items/{item_id}&quot;, response_model=Item)<br>async def read_item(item_id: str, db: MongoDBDep):<br>    raw = await db[&quot;items&quot;].find_one({&quot;_id&quot;: item_id})<br>    if not raw:<br>        raise HTTPException(status_code=404, detail=&quot;Item not found&quot;)<br>    raw[&quot;id&quot;] = str(raw.pop(&quot;_id&quot;))<br>    return raw</pre><ul><li><strong>MotorClient</strong> is created once at import time, avoiding reconnect overhead.</li><li><strong>get_mongo_db</strong> is declared as an async dependency, so FastAPI injects db per request without blocking.</li></ul><h4>Synchronous with PyMongo</h4><p>If you’re using a sync driver (PyMongo), wrap it in a generator to ensure proper teardown:</p><pre>from typing import Annotated, Generator<br>from fastapi import Depends, FastAPI<br>from pymongo import MongoClient<br>from pymongo.database import Database<br>from pydantic import BaseModel<br><br>app = FastAPI()<br><br>MONGODB_URL = &quot;mongodb://localhost:27017&quot;<br><br>def get_sync_mongo_db() -&gt; Generator[Database, None, None]:<br>    client = MongoClient(MONGODB_URL)<br>    db = client[&quot;mydatabase&quot;]<br>    try:<br>        yield db<br>    finally:<br>        client.close()<br><br>SyncMongoDep = Annotated[Database, Depends(get_sync_mongo_db)]<br><br>class Item(BaseModel):<br>    name: str<br>    description: str<br><br>@app.post(&quot;/sync-items/&quot;, response_model=Item)<br>def create_item_sync(item: Item, db: SyncMongoDep):<br>    result = db[&quot;items&quot;].insert_one(item.dict())<br>    raw = db[&quot;items&quot;].find_one({&quot;_id&quot;: result.inserted_id})<br>    raw[&quot;id&quot;] = str(raw.pop(&quot;_id&quot;))<br>    return raw<br><br>@app.get(&quot;/sync-items/{item_id}&quot;, response_model=Item)<br>def read_item_sync(item_id: str, db: SyncMongoDep):<br>    raw = db[&quot;items&quot;].find_one({&quot;_id&quot;: item_id})<br>    if not raw:<br>        raise HTTPException(status_code=404, detail=&quot;Item not found&quot;)<br>    raw[&quot;id&quot;] = str(raw.pop(&quot;_id&quot;))<br>    return raw</pre><ul><li>The generator ensures that client.close() is called after each request.</li><li>Use this pattern if you cannot adopt async (e.g. legacy code or blocking integrations).</li></ul><h3>Conclusion</h3><p>Dependency Injection in FastAPI keeps your code clean and testable by externalizing setup (e.g., auth, SQL or MongoDB) into reusable providers you inject with Depends, making it easy to swap, mock, or scale services.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1adfc143a277" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event-Driven vs. Request-Driven Architectures]]></title>
            <link>https://medium.com/@shanukalakshan9817/event-driven-vs-request-driven-architectures-083ee46ba36a?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/083ee46ba36a</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Thu, 01 May 2025 08:19:11 GMT</pubDate>
            <atom:updated>2025-05-01T08:19:11.070Z</atom:updated>
            <content:encoded><![CDATA[<p>As software systems become increasingly complex and distributed, architects face the critical choice of selecting the right communication approach between system components. Two primary paradigms dominate this space: Event-Driven Architecture (EDA) and Request-Driven Architecture (often REST or RPC-based). Understanding their differences, strengths, and weaknesses can help you build more efficient, scalable, and maintainable software solutions.</p><h3>Understanding Request-Driven Architectures</h3><p>Request-driven architectures are the traditional, widely-used paradigm where communication occurs via explicit requests and responses. Typically implemented with REST APIs, RPC calls, or GraphQL queries, this approach involves synchronous interaction between client and server, creating a clear dependency chain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nNz-YocAg-kHXOc84QNFyA.png" /></figure><p><strong>Pros:</strong></p><ul><li><strong>Simplicity &amp; Predictability:</strong> Easy to understand, implement, and debug.</li><li><strong>Immediate Feedback:</strong> Direct response ensures instant validation of requests.</li><li><strong>Well-Established Patterns &amp; Tools:</strong> Rich ecosystem of tooling and best practices.</li></ul><p><strong>Cons:</strong></p><ul><li><strong>Tight Coupling:</strong> Services depend directly on each other’s availability.</li><li><strong>Potential for Latency:</strong> Blocking requests may slow down systems under heavy load.</li><li><strong>Limited Scalability:</strong> Difficulties arise when scaling synchronous services horizontally.</li></ul><h3>Exploring Event-Driven Architectures (EDA)</h3><p>Event-driven architectures, on the other hand, operate by asynchronously communicating events between loosely-coupled components through messaging systems like Kafka, RabbitMQ, or AWS SNS/SQS. Systems react to events without waiting explicitly for responses, promoting decoupling and parallel processing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4-n75vbUn5US3VVDiwAS8A.png" /></figure><p><strong>Pros:</strong></p><ul><li><strong>High Scalability &amp; Flexibility:</strong> Components can scale independently, allowing better resource management.</li><li><strong>Loose Coupling:</strong> Components can evolve, deploy, and fail independently without impacting overall system health.</li><li><strong>Improved Resilience:</strong> Asynchronous communication enhances fault tolerance and recovery.</li></ul><p><strong>Cons:</strong></p><ul><li><strong>Complexity &amp; Debugging Difficulty:</strong> Asynchronous interactions make debugging and tracing harder.</li><li><strong>Eventual Consistency:</strong> Data consistency becomes more complex, as immediate synchronization isn’t guaranteed.</li><li><strong>Learning Curve:</strong> Requires sophisticated design and tooling, increasing initial setup complexity.</li></ul><h3>Choosing the Right Approach</h3><p>The decision between event-driven and request-driven architectures hinges primarily on the specific needs and constraints of your project. Here are several considerations to help guide your choice:</p><h4>Consider Request-Driven if:</h4><ul><li>Your system demands immediate consistency and quick responses (e.g., financial transactions, synchronous user interactions).</li><li>The application structure and interactions are straightforward and less likely to scale dramatically.</li><li>Rapid development and simple debugging workflows are critical.</li></ul><h4>Consider Event-Driven if:</h4><ul><li>Your system requires high scalability, fault tolerance, and asynchronous communication (e.g., distributed logging, analytics pipelines).</li><li>You can tolerate eventual consistency and asynchronous updates.</li><li>Your architecture needs to be flexible, adaptable, and independently scalable components.</li></ul><h3>Practical Tips for Adopting an Event-Driven Architecture:</h3><ul><li><strong>Invest in Observability:</strong> Strong logging, tracing, and monitoring practices are essential to mitigate complexity.</li><li><strong>Implement Idempotency:</strong> Ensure your event handlers are idempotent, allowing them to safely handle repeated event deliveries.</li><li><strong>Start Small:</strong> Begin with isolated, non-critical components to ease the transition to EDA.</li></ul><h3>Practical Tips for Adopting a Request-Driven Architecture:</h3><ul><li><strong>Embrace Caching and Load Balancing:</strong> Improve response times and scalability through intelligent caching strategies.</li><li><strong>Keep APIs Simple:</strong> Adhering to standard patterns (like RESTful principles) facilitates maintainability and ease of use.</li><li><strong>Consider API Gateways:</strong> Centralizing API management simplifies security, routing, and scalability.</li></ul><h3>Conclusion</h3><p>Neither event-driven nor request-driven architecture is universally superior, each excels in different contexts. Thoughtful consideration of your project’s unique requirements and constraints will guide you toward the optimal choice. Moreover, hybrid approaches often emerge naturally as solutions evolve, offering the benefits of both worlds.</p><p>Making an informed decision early on can significantly influence your system’s scalability, resilience, and maintainability setting the foundation for a robust architecture that supports sustainable growth.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=083ee46ba36a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secure VPC with AWS and Terraform]]></title>
            <link>https://medium.com/@shanukalakshan9817/secure-vpc-with-aws-and-terraform-36aa6bdc1e5d?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/36aa6bdc1e5d</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Fri, 17 Jan 2025 23:24:33 GMT</pubDate>
            <atom:updated>2025-01-17T23:24:33.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t2xT_H87mFqM5AJYedgPLQ.png" /></figure><p>Building a secure and scalable network infrastructure on AWS starts with configuring a <strong>Virtual Private Cloud (VPC)</strong>. Using <strong>Terraform</strong>, you can automate this process efficiently, ensuring a repeatable and version-controlled environment. This article outlines the steps to create a VPC with <strong>public and private subnets</strong>, <strong>Elastic IP (EIP)</strong>, <strong>NAT Gateway</strong>, and <strong>Internet Gateway</strong>, leveraging Terraform for streamlined deployment.</p><p>Complete code : <a href="https://github.com/ShanukaLakshan/aws-vps-terraform.git">https://github.com/ShanukaLakshan/aws-vps-terraform.git</a></p><h3>Why Use Terraform for AWS VPC Creation?</h3><p>AWS VPCs provide flexibility and control over your network environment, but manually configuring them can be tedious. <strong>Terraform</strong> simplifies the process by allowing you to:</p><ul><li><strong>Version control your infrastructure.</strong></li><li><strong>Reproduce environments consistently.</strong></li><li><strong>Automate cloud infrastructure provisioning.</strong></li><li><strong>Easily update and modify your infrastructure</strong> when requirements change.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_vlE4iy8yAiyaVxj-yL03w.png" /></figure><h3>The Components of Our VPC Architecture</h3><p>we will be working with the following key components:</p><ol><li><strong>VPC (Virtual Private Cloud):</strong> The isolated network where your AWS resources reside.</li><li><strong>Subnets (Public and Private):</strong> The division of your network into public subnets (for internet-facing resources) and private subnets (for backend services that need not be exposed to the internet).</li><li><strong>Internet Gateway (IGW):</strong> Provides internet access for resources in the public subnet.</li><li><strong>NAT Gateway:</strong> Allows instances in the private subnet to securely access the internet, while preventing inbound traffic from the internet to the private subnet.</li><li><strong>Elastic IP (EIP):</strong> A static, public IPv4 address that can be associated with the NAT Gateway, enabling it to route internet traffic for private subnet resources.</li><li><strong>Route Tables:</strong> Define how traffic is routed between subnets and to the internet.</li></ol><h4>1. Setting Up the VPC</h4><p>The first step is creating the <strong>VPC</strong>. The VPC is the network boundary within which all your resources will live. By creating a VPC, you define its CIDR block, DNS settings, and ensure that it’s ready to host your subnets and other resources.</p><h4>2. Creating Public and Private Subnets</h4><p>Next, create <strong>public</strong> and <strong>private subnets</strong> within the VPC. Public subnets will host resources that require internet access, like web servers, while private subnets will host sensitive resources, like databases, that should not be directly accessed from the internet.</p><ul><li><strong>Public Subnet:</strong> Connects directly to the Internet Gateway, allowing internet-facing traffic.</li><li><strong>Private Subnet:</strong> Does not have direct internet access but will route traffic through a <strong>NAT Gateway</strong> to access the internet for updates and patches.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8nG20lClU_ngA2rAP8RBZQ.png" /></figure><h4>3. Configuring the Internet Gateway</h4><p>An <strong>Internet Gateway (IGW)</strong> is essential for enabling resources in the public subnet to access the internet. The gateway allows bi-directional communication between the VPC and the internet. Attach the Internet Gateway to your VPC and update the route table to route outbound traffic from the public subnet to the IGW.</p><h4>4. Setting Up the NAT Gateway with Elastic IP</h4><p>While the public subnet has direct access to the internet, private subnet instances need a secure path to reach the internet. This is achieved by using a <strong>NAT Gateway</strong>.</p><p>To ensure that the NAT Gateway can handle internet-bound traffic, it needs an <strong>Elastic IP (EIP)</strong>, which is a static, public IP address. This allows the instances in the private subnet to communicate with the internet through the NAT Gateway, while maintaining security by not exposing them directly to the internet.</p><p>Here’s how the process works:</p><ul><li>The NAT Gateway is launched in the public subnet.</li><li>The <strong>Elastic IP</strong> is associated with the NAT Gateway, ensuring that private subnet resources can reach the internet securely.</li></ul><h4>5. Configuring Route Tables</h4><p>To control traffic flow within the VPC, route tables are essential. You’ll need two route tables:</p><ul><li><strong>Public Route Table:</strong> This table ensures that resources in the public subnet route their outbound traffic to the <strong>Internet Gateway</strong> for internet access.</li><li><strong>Private Route Table:</strong> The private subnet needs to route internet-bound traffic through the <strong>NAT Gateway</strong>. This ensures that resources in the private subnet can reach the internet for tasks like downloading patches or updates without exposing them to direct internet access.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ND6iLFXr76f79BsgBftGIg.png" /></figure><h3>Benefits of Using Elastic IP and NAT Gateway</h3><p>This architecture provides several key benefits:</p><ul><li><strong>Security:</strong> By placing sensitive resources in private subnets and only routing outbound traffic via the NAT Gateway, you ensure that your backend services are not exposed to the internet, while still allowing access to necessary external services.</li><li><strong>Cost-Effective and Scalable:</strong> Using <strong>Elastic IP</strong> ensures a static public IP address, which is useful for NAT Gateway functionality. This design also scales with your workload by allowing instances in private subnets to leverage the internet for outbound traffic.</li></ul><p>VPC architecture with the key components needed to ensure high availability, security, and scalability. By leveraging <strong>Elastic IPs</strong> and <strong>NAT Gateways</strong>, you ensure that resources in private subnets remain secure while still able to access the internet for external communications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36aa6bdc1e5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering SonarQube Integration for Node.js Applications]]></title>
            <link>https://medium.com/@shanukalakshan9817/mastering-sonarqube-integration-for-node-js-applications-fbec559f91ea?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbec559f91ea</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Tue, 07 Jan 2025 13:52:29 GMT</pubDate>
            <atom:updated>2025-01-07T13:52:29.700Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z4_SPIYIKT79wY5ALAv7vA.png" /></figure><h4><strong>Introduction</strong></h4><p>SonarQube is a powerful tool for continuous inspection of code quality, providing static code analysis to identify bugs, vulnerabilities, and code smells. Integrating SonarQube with your Node.js project can ensure that your code is not only functional but also clean, maintainable, and secure.</p><h4>Prerequisites</h4><p>Before you start, ensure the following:</p><ol><li><strong>SonarQube Installation</strong>:</li></ol><ul><li>Use Docker for a quick local setup:</li></ul><pre>docker run -d --name sonarqube -p 9000:9000 sonarqube</pre><p>Alternatively, access a hosted SonarQube instance.</p><p><strong>2. Node.js Installed</strong>: Ensure you have Node.js (v14+) and npm or yarn installed. (set up with your preferred framework (Express, NestJS, etc.))</p><p><strong>3. Sonar Scanner Installed</strong>: Download and configure Sonar Scanner (Sonar Scanner documentation).</p><h4>Step 1: Create a Project in SonarQube</h4><ol><li>Log in to your SonarQube dashboard.</li><li>Navigate to <strong>Projects &gt; Create Project</strong>.</li><li>Enter the project name and key.</li><li>Generate a token for authentication and save it for later steps.</li></ol><h4>Step 2: Set Up Your Node.js Environment</h4><ol><li>Navigate to your environment root directory.</li><li>Ensure your project is initialized with a package.json file. If not, run:</li></ol><pre>npm init -y</pre><ol><li>Install necessary dependencies:</li></ol><pre>npm install --save-dev eslint jest</pre><h4>Step 3: Configure SonarQube Properties</h4><ol><li>Create a sonar-project.properties file in the root directory of your project:</li></ol><pre># Project details<br>sonar.projectKey=your_project_key<br>sonar.projectName=Your Project Name<br><br># Source code configuration<br>sonar.sources=src<br>sonar.exclusions=**/node_modules/**,**/*.test.js,**/*.spec.js<br><br># Language and framework settings<br>sonar.javascript.lcov.reportPaths=coverage/lcov.info<br><br># Host details<br>sonar.host.url=http://localhost:9000<br>sonar.login=your_generated_token</pre><p>Replace your_project_key and your_generated_token with the values from SonarQube.</p><h4>Step 4: Set Up Code Coverage</h4><ol><li>Configure Jest to generate a code coverage report. Update or create a jest.config.js file:</li></ol><pre>module.exports = {<br>  collectCoverage: true,<br>  collectCoverageFrom: [<br>    &quot;src/**/*.{js,ts}&quot;,<br>    &quot;!src/**/*.d.ts&quot;,<br>  ],<br>  coverageDirectory: &quot;coverage&quot;,<br>  coverageReporters: [&quot;lcov&quot;, &quot;text&quot;],<br>};</pre><p>2. Run tests to generate the coverage report:</p><pre>npm test -- --coverage</pre><h4>Step 5: Run Sonar Scanner</h4><p>Add a script to package.json for convenience:</p><pre>&quot;scripts&quot;: {   &quot;sonar&quot;: &quot;sonar-scanner&quot; }</pre><p>Run the scanner:</p><pre>npm run sonar</pre><p>View the analysis results on your SonarQube dashboard.</p><h4>Step 6: Integrate with CI/CD</h4><p>In here you can use github actions.</p><p>Create a .github/workflows/sonarqube.yml file:</p><pre>name: SonarQube Analysis<br><br>on:<br>  push:<br>    branches:<br>      - main<br><br>jobs:<br>  sonarqube:<br>    runs-on: ubuntu-latest<br><br>    steps:<br>      - name: Checkout Code<br>        uses: actions/checkout@v3<br><br>      - name: Set up Node.js<br>        uses: actions/setup-node@v3<br>        with:<br>          node-version: &#39;16&#39;<br><br>      - name: Install Dependencies<br>        run: npm install<br><br>      - name: Run Tests with Coverage<br>        run: npm test -- --coverage<br><br>      - name: Run SonarQube Scanner<br>        env:<br>          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}<br>        run: |<br>          npx sonar-scanner \<br>            -Dsonar.projectKey=your_project_key \<br>            -Dsonar.host.url=http://your-sonarqube-server \<br>            -Dsonar.login=${{ secrets.SONAR_TOKEN }}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SG8kL-zGWxUPCLIGFtocsw.png" /></figure><p>Add your SonarQube token as a secret (SONAR_TOKEN) in your GitHub repository settings.</p><h4>Troubleshooting</h4><ul><li><strong>Authentication Errors</strong>: Double-check your token and server URL.</li><li><strong>Coverage Report Issues</strong>: Ensure the lcov.info file is generated and paths are correctly set in the properties file.</li><li><strong>Parsing Errors</strong>: Verify your Node.js version and ESLint configuration are compatible.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbec559f91ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Rate Limiting is Crucial for Microservices Security]]></title>
            <link>https://medium.com/@shanukalakshan9817/why-rate-limiting-is-crucial-for-microservices-security-e721d20e5998?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/e721d20e5998</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Wed, 25 Dec 2024 06:59:38 GMT</pubDate>
            <atom:updated>2024-12-25T06:59:38.100Z</atom:updated>
            <content:encoded><![CDATA[<p>Rate limiting is an essential mechanism in securing microservices and APIs. It helps protect against various types of malicious attacks and unintended usage spikes. Here’s why:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i4S_T4vmE_uhvaPAUR9Cjw.png" /></figure><ol><li><strong>Protection Against DDoS Attacks</strong><br>Rate limiting restricts the number of requests a client can send within a specific time frame, mitigating Distributed Denial of Service (DDoS) attacks.</li><li><strong>Preventing Resource Exhaustion</strong><br>APIs and microservices have finite resources. Rate limiting ensures fair usage and prevents a single client from consuming all resources, which could lead to downtime for other users.</li><li><strong>Authentication and Brute Force Protection</strong><br>By limiting login attempts, rate limiting helps defend against brute force attacks that try to guess credentials or tokens.</li><li><strong>Cost Control</strong><br>For cloud-based services with pay-per-use models, rate limiting helps avoid unexpected costs by controlling excessive usage.</li><li><strong>Improved Performance and Stability</strong><br>Limiting requests reduces the risk of performance degradation under heavy loads, maintaining consistent service quality for all users.</li></ol><h4>Rate Limiting in Microservices with Spring Cloud Gateway</h4><p>Rate limiting is a critical feature when building scalable, secure microservices. In this article, we’ll explore how to implement rate limiting using <strong>Spring Cloud Gateway</strong>, an API Gateway that supports modern security and scalability patterns.</p><p><strong>Scenario Setup</strong></p><p>We have the following architecture:</p><ol><li><strong>API Gateway</strong> running on port 8082</li><li><strong>Microservice</strong> running on port 8080</li></ol><p>The gateway routes requests to the microservice and enforces rate limits. We’ll use Redis as the backing store for the rate limiter.</p><h4>1. Setting Up the API Gateway</h4><p>First, we create a Spring Boot application for the API Gateway. Here’s the implementation:</p><pre>package com.ratelimit.rate_limit;<br>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br>import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;<br>import org.springframework.context.annotation.Bean;<br>import reactor.core.publisher.Mono;<br><br>@SpringBootApplication<br>public class RateLimitApplication {<br> public static void main(String[] args) {<br>  SpringApplication.run(RateLimitApplication.class, args);<br> }<br> @Bean<br> public KeyResolver keyResolver() {<br>  // Use the client&#39;s IP address as the key for rate limiting<br>  return exchange -&gt; Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());<br> }<br>}</pre><p>Here, we define a KeyResolver to identify clients based on their IP addresses.</p><h4>2. API Gateway Configuration (application.yml)</h4><p>We configure the gateway in application.yml:</p><pre>server:<br>  port: 8082<br>spring:<br>  application:<br>    name: ratelimiter-example<br>  cloud:<br>    gateway:<br>      routes:<br>        - id: route1<br>          uri: http://localhost:8080/hello<br>          predicates:<br>            - Path=/hello<br>          filters:<br>            - name: RequestRateLimiter<br>              args:<br>                redis-rate-limiter.replenishRate: 5<br>                redis-rate-limiter.burstCapacity: 10<br>                redis-rate-limiter.requestedTokens: 1<br>  data:<br>    redis:<br>      port: 6379<br>      host: localhost</pre><p>This configuration:</p><ul><li>Defines a route for /hello requests.</li><li>Adds a rate limiter with:</li><li>replenishRate: 5 → Allows 5 requests per second.</li><li>burstCapacity: 10 → Allows a burst of up to 10 requests.</li><li>requestedTokens: 1 → Each request consumes 1 token.</li></ul><h4>3. Setting Up the Microservice</h4><p>Here’s a simple microservice responding to /hello requests:</p><pre>package com.example.demo;<br><br>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RestController;<br><br>@SpringBootApplication<br>public class DemoApplication {<br> public static void main(String[] args) {<br>  SpringApplication.run(DemoApplication.class, args);j<br> }<br> @RestController<br> public class HelloController {<br>  @GetMapping(&quot;/hello&quot;)<br>  public String hello() {<br>   return &quot;Hello, World!&quot;;<br>  }<br> }<br>}</pre><p>The microservice runs on port 8080.</p><h4>4. Running Redis</h4><p>Ensure that Redis is running locally on localhost:6379. You can use Docker to set up Redis:</p><pre>docker run -d -p 6379:6379 --name redis redis</pre><h4>5. Testing the Rate Limiter</h4><p>Use a tool like <strong>Postman</strong> or <strong>curl</strong> to test the /hello endpoint via the API Gateway:</p><pre>curl -X GET http://localhost:8082/hello</pre><p>You should receive responses until the rate limit is exceeded, after which the gateway will return a 429 Too Many Requests error</p><pre>#!/bin/bash<br><br># URL of the API Gateway endpoint<br>URL=&quot;http://localhost:8082/hello&quot;<br><br># Number of requests to send<br>REQUEST_COUNT=20<br><br>echo &quot;Sending $REQUEST_COUNT requests to $URL...&quot;<br>echo &quot;---------------------------------------&quot;<br><br># Loop to send requests<br>for i in $(seq 1 $REQUEST_COUNT)<br>do<br>  RESPONSE=$(curl -s -o /dev/null -w &quot;%{http_code}&quot; $URL)<br>  <br>  if [ &quot;$RESPONSE&quot; -eq 200 ]; then<br>    echo &quot;Request $i: ✅ Allowed (200 OK)&quot;<br>  elif [ &quot;$RESPONSE&quot; -eq 429 ]; then<br>    echo &quot;Request $i: 🚫 Rate Limited (429 Too Many Requests)&quot;<br>  else<br>    echo &quot;Request $i: ❓ Unexpected Response ($RESPONSE)&quot;<br>  fi<br>done<br><br>echo &quot;---------------------------------------&quot;<br>echo &quot;Test completed!&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c-dQ0JT3dt9AOdzCJa7_Eg.png" /></figure><p>The completed source code can be found in this GitHub repository:</p><p><a href="https://github.com/ShanukaLakshan/SpringBoot-Rate-Limit---Api-Gateway.git">https://github.com/ShanukaLakshan/SpringBoot-Rate-Limit---Api-Gateway.git</a></p><h4>Conclusion</h4><p>Implementing rate limiting is essential for protecting APIs and maintaining consistent service quality. Using <strong>Spring Cloud Gateway</strong> and <strong>Redis</strong>, you can achieve a robust and scalable solution.</p><h4>Further Enhancements</h4><ul><li>Use custom keys for specific users or applications.</li><li>Integrate with monitoring tools to track rate limiter metrics.</li><li>Apply rate limits per API or user role.</li></ul><p>By adopting rate limiting, you ensure your microservices remain secure, reliable, and performant.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e721d20e5998" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding and Fixing CORS Errors]]></title>
            <link>https://medium.com/@shanukalakshan9817/understanding-and-fixing-cors-errors-baf9ff77a7a9?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/baf9ff77a7a9</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Tue, 24 Dec 2024 05:48:43 GMT</pubDate>
            <atom:updated>2024-12-24T05:48:43.338Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RjBwo_UGInuS3QurQsnL5w.png" /><figcaption><a href="https://supertokens.com/blog/what-is-cross-origin-resource-sharing#">https://supertokens.com/blog/what-is-cross-origin-resource-sharing#</a></figcaption></figure><p>1. What is CORS?</p><p>CORS stands for <strong>Cross-Origin Resource Sharing</strong> a security feature built into browsers.</p><p>It restricts requests made from one origin (domain, protocol, or port) to another unless explicitly allowed by the server.</p><p><strong>Example:</strong></p><ul><li>Your micro frontend is hosted at <a href="https://frontend.com./">https://microfrontend.com</a></li><li>Your micro service is hosted at <a href="https://api.backend.com./">https://api.microservice.com</a></li></ul><p>The browser identifies these as different origins and blocks the request unless the server permits it.</p><h4>2. Why Does It Happen?</h4><p>CORS errors occur because of the <strong>Same-Origin Policy</strong>. This policy is designed to prevent malicious websites from making unauthorized API calls using your credentials.</p><p>When the backend server doesn’t include the correct CORS headers, the browser blocks the response and throws an error like:</p><blockquote>Access to fetch at ‘<a href="https://api.microservice.com/">https://api.microservice.com</a>&#39; from origin ‘https://microfrontend.com&#39; has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present.</blockquote><p>As a developer, it’s important to understand that the browser isn’t blocking the request itself it’s blocking the response for security reasons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1glRLzD5yVNcl-3ri4gUxQ.png" /></figure><h4>3. The Real Problem Behind CORS</h4><ul><li><strong>It’s not a micro-frontend side error.</strong></li><li><strong>It’s a server-side configuration issue.</strong></li><li><strong>Many developers mistakenly blame the browser.</strong></li><li><strong>Configuring the server to handle CORS properly is the real solution.</strong></li></ul><p>Sometimes, these errors don’t appear in development environments but show up after deployment. This is often because development setups may bypass certain restrictions or use proxies that mask the issue. Always test CORS configurations in a production-like environment to catch such problems early.</p><h4>4. How Do You Fix It?</h4><p><strong>Step 1: Update the Micro-service Configurations</strong></p><p>Ensure the server sends the correct headers:</p><ul><li>Access-Control-Allow-Origin: * (Allows all origins).</li><li>Or specify trusted domains, e.g., Access-Control-Allow-Origin: <a href="https://frontend.com./">https://microfrontend.com</a> or <a href="https://frontend.com./">http://localhost</a>:3000</li></ul><p><strong>Step 2: Handle Preflight Requests (OPTIONS)</strong></p><p>For complex requests (e.g., POST with custom headers), browsers send a preflight request before the actual request. The server must handle this by responding with headers like:</p><ul><li>Access-Control-Allow-Methods: GET, POST, OPTIONS</li><li>Access-Control-Allow-Headers: Content-Type, Authorization</li></ul><p><strong>Step 3: Use a Proxy for Local Development</strong></p><p>During development, if the backend isn’t configured yet, set up a proxy to forward requests through the same origin as your frontend. For example:</p><ul><li>Use Webpack DevServer or Vite to proxy requests.</li><li>Use a reverse proxy like Nginx or a middleware in Express.</li></ul><h4>5. Can You Bypass CORS?</h4><blockquote>Short answer : <strong>No</strong></blockquote><p>As tempting as it may be to use browser extensions or disable CORS in the browser, these workarounds won’t function in production. The correct and scalable solution is to fix the server configuration.</p><h4>6. Why Is This Important for Developers?</h4><p>Understanding and resolving CORS errors showcases your ability to:</p><ul><li>Debug backend and frontend interactions effectively.</li><li>Identify the root cause of an issue instead of patching symptoms.</li><li>Grasp the principles of web security and the Same-Origin Policy.</li></ul><h4>Conclusion</h4><p>By understanding the root causes of CORS errors and knowing how to fix them, you’ll be well-equipped to tackle these issues confidently. This knowledge not only makes you a better developer but also highlights your ability to debug and solve real-world problems effectively.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=baf9ff77a7a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying a Micro-service (Spring Boot Java) on Kubernetes]]></title>
            <link>https://medium.com/@shanukalakshan9817/deploying-a-micro-service-spring-boot-java-on-kubernetes-1b62199fb22b?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b62199fb22b</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Fri, 13 Dec 2024 10:19:49 GMT</pubDate>
            <atom:updated>2025-01-07T08:40:36.753Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XXnCEuIs-JrQR6MdTNkkhQ.png" /></figure><p>Kubernetes is a powerful platform for managing containerized applications, and deploying a Spring Boot microservice on it is a great way to scale your Java applications in the cloud. In this guide, we’ll walk through the process of deploying a Spring Boot Java microservice on Kubernetes, from creating the microservice to deploying it on a Kubernetes cluster.</p><h3>Prerequisites</h3><p>Before you begin, ensure you have the following installed:</p><ol><li><strong>Docker</strong>: To containerize the Spring Boot microservice.</li><li><strong>Kubernetes (minikube or a cloud Kubernetes cluster)</strong>: For managing the deployment.</li><li><strong>kubectl</strong>: The command-line tool for interacting with Kubernetes clusters.</li><li><strong>Maven/Gradle</strong>: For building the Spring Boot application.</li><li><strong>Java Development Kit (JDK 11 or above)</strong>: Required for developing the Spring Boot microservice. ()</li></ol><h3>Step 1: Create a Spring Boot Microservice</h3><p>If you don’t have an existing Spring Boot microservice, you can quickly create one using <a href="https://start.spring.io/">Spring Initializr</a>.</p><p><a href="https://github.com/ShanukaLakshan/k8s-microservice.git">https://github.com/ShanukaLakshan/k8s-microservice.git</a></p><h3>Step 2: Containerize the Application with Docker</h3><p>Now that the Spring Boot application is ready, the next step is to package it into a Docker container.</p><ol><li><strong>Create a Dockerfile</strong> in the root of your project</li><li><strong>Build the JAR file</strong> using Maven</li><li><strong>Build the Docker image</strong></li><li><strong>Run the Docker container</strong> to make sure it works locally</li></ol><h3>Step 3: Push the Docker Image to a Container Registry</h3><p>You’ll need to upload the Docker image to a container registry (e.g., Docker Hub or Google Container Registry) to make it accessible for Kubernetes.</p><p><strong>Tag your Docker image</strong> with your Docker Hub repository:</p><p>You can use version code or /latest tag for image name,</p><pre>docker tag microservice:1.0 &lt;your-docker-username&gt;/microservice:1.0</pre><p><strong>Push the image to Docker Hub</strong>:</p><pre>docker push &lt;your-docker-username&gt;/microservice:1.0</pre><h3>Step 4: Set Up Kubernetes Deployment Files</h3><p>Now, let’s create the necessary Kubernetes configuration files to deploy the microservice.</p><ol><li><strong>Create a Kubernetes Deployment YAML file</strong> (deployment.yaml):</li></ol><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: microservice<br>spec:<br>  replicas: 2<br>  selector:<br>    matchLabels:<br>      app: microservice<br>  template:<br>    metadata:<br>      labels:<br>        app: microservice<br>    spec:<br>      containers:<br>      - name: microservice<br>        image: &lt;your-docker-username&gt;/microservice:1.0<br>        ports:<br>        - containerPort: 8080</pre><p><strong>2. Create a Kubernetes Service YAML file</strong> (service.yaml):</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: microservice<br>spec:<br>  selector:<br>    app: microservice<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 8080<br>  type: LoadBalancer</pre><h3>Step 5: Deploy the Microservice to Kubernetes</h3><ol><li><strong>Apply the Kubernetes deployment and service files</strong>:</li></ol><pre>kubectl apply -f deployment.yaml<br>kubectl apply -f service.yaml</pre><p><strong>Check the status of your pods and services</strong>:</p><pre>kubectl get pods<br>kubectl get services</pre><h3>Step 6: Verify the Deployment</h3><p>Once the pods are up and running, you can access the Spring Boot microservice through the service’s external IP (or Kubernetes service if running locally). Use curl or a browser to verify that the microservice is accessible:</p><pre>curl &lt;external-ip&gt;/hello</pre><h3>Conclusion</h3><p>Congratulations! You’ve successfully deployed a Spring Boot Java microservice to Kubernetes. Kubernetes makes it easy to manage and scale your microservices, providing features like self-healing, scaling, and service discovery. By following this guide, you can deploy any Java microservice on Kubernetes and ensure it runs smoothly in production environments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b62199fb22b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying Cloud Run Service on GCP with Terraform]]></title>
            <link>https://medium.com/@shanukalakshan9817/deploying-cloud-run-service-on-gcp-with-terraform-1234889b9b3f?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1234889b9b3f</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Fri, 29 Nov 2024 03:41:41 GMT</pubDate>
            <atom:updated>2024-11-29T03:41:41.167Z</atom:updated>
            <content:encoded><![CDATA[<p>Google Cloud Run is a powerful platform that allows developers to deploy containerized applications effortlessly. This tutorial will guide you through deploying a public Cloud Run service using Terraform. You’ll set up the GCP project, configure the deployment, grant public access, and output the service URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SzGLEDer9UwqUspTrXPz8A.jpeg" /></figure><h3>1. Prerequisites</h3><p>Before we dive into the configuration, ensure you have:</p><ul><li>A Google Cloud Platform (GCP) account.</li><li>Terraform installed on your local computer.</li><li>Basic familiarity with Terraform and containerized applications.</li><li>A containerized application ready for deployment. In this example, we’ll use the gcr.io/cloudrun/hello image provided by Google.</li></ul><h3>2. Project Setup</h3><p>Create a Terraform configuration file (e.g., main.tf) and configure the Google provider with your project details.</p><pre># Configure GCP project<br>provider &quot;google&quot; {<br>  project = var.project_id<br>  region  = var.region<br>}</pre><p>This section ensures Terraform knows which GCP project and region to use. You’ll need to pass the project ID and region as variables.</p><h3>3. Deploying the Cloud Run Service</h3><p>Define the Cloud Run service in your Terraform configuration.</p><pre># Deploy image to Cloud Run<br>resource &quot;google_cloud_run_service&quot; &quot;default&quot; {<br>  name     = &quot;hello-world&quot;<br>  location = var.region<br>  template {<br>    spec {<br>      containers {<br>        image = &quot;gcr.io/cloudrun/hello&quot;<br>      }<br>    }<br>  }<br>  traffic {<br>    percent         = 100<br>    latest_revision = true<br>  }<br>}</pre><p>Here’s what each section does:</p><ul><li><strong>name</strong>: Specifies the name of the Cloud Run service.</li><li><strong>location</strong>: Determines the region to deploy in.</li><li><strong>template.spec.containers.image</strong>: Points to the container image to deploy.</li><li><strong>traffic</strong>: Routes 100% of the traffic to the latest revision.</li></ul><h3>4. Enabling Public Access</h3><p>To make the service publicly accessible, we need to grant the roles/run.invoker role to all users.</p><h4>Define an IAM Policy:</h4><pre># Create public access<br>data &quot;google_iam_policy&quot; &quot;noauth&quot; {<br>  binding {<br>    role = &quot;roles/run.invoker&quot;<br>    members = [<br>      &quot;allUsers&quot;,<br>    ]<br>  }<br>}</pre><p>This IAM policy binds the roles/run.invoker role to allUsers.</p><h4>Apply the IAM Policy:</h4><pre># Enable public access on Cloud Run service<br>resource &quot;google_cloud_run_service_iam_policy&quot; &quot;noauth&quot; {<br>  location    = google_cloud_run_service.default.location<br>  project     = google_cloud_run_service.default.project<br>  service     = google_cloud_run_service.default.name<br>  policy_data = data.google_iam_policy.noauth.policy_data<br>}</pre><p>This resource links the policy to your Cloud Run service.</p><h3>5. Output the Service URL</h3><p>Finally, output the service URL so you can easily access it after deployment.</p><pre># Return service URL<br>output &quot;url&quot; {<br>  value = google_cloud_run_service.default.status[0].url<br>}</pre><p>This will display the Cloud Run service’s public URL when you run terraform apply.</p><h3>6. Deploy the Service</h3><p>Follow these steps to deploy your Cloud Run service:</p><ol><li><strong>Initialize Terraform:</strong><br>Run the following command in your terminal to initialize Terraform and download the necessary plugins:</li></ol><pre>terraform init</pre><p><strong>2. Plan the Deployment:</strong><br>Check the resources Terraform will create or modify:</p><pre>terraform plan -var=&quot;project_id=&lt;your-project-id&gt;&quot; -var=&quot;region=&lt;your-region&gt;&quot;</pre><p>3. <strong>Apply the Configuration:</strong><br>Deploy the resources by applying the configuration:</p><pre>terraform apply -var=&quot;project_id=&lt;your-project-id&gt;&quot; -var=&quot;region=&lt;your-region&gt;&quot;</pre><h3>8. Clean Up</h3><p>To avoid incurring unnecessary charges, destroy the resources when no longer needed:</p><pre>terraform destroy -var=&quot;project_id=&lt;your-project-id&gt;&quot; -var=&quot;region=&lt;your-region&gt;&quot;</pre><h3>Conclusion</h3><p>In here, we used Terraform to deploy a public Cloud Run service on GCP. This approach simplifies infrastructure management, allowing you to define and manage your GCP resources declaratively.</p><p>Happy World! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1234889b9b3f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing Spring Boot Microservices: The Essential Role of AbstractController]]></title>
            <link>https://medium.com/@shanukalakshan9817/optimizing-spring-boot-microservices-the-essential-role-of-abstractcontroller-3b7a71b78c38?source=rss-a9c9c4806dbb------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b7a71b78c38</guid>
            <dc:creator><![CDATA[Shanuka Lakshan]]></dc:creator>
            <pubDate>Sun, 29 Sep 2024 08:04:17 GMT</pubDate>
            <atom:updated>2024-09-29T08:04:17.882Z</atom:updated>
            <content:encoded><![CDATA[<p>In modern software development, particularly within microservices architecture, maintaining clean, reusable, and efficient code is paramount. One effective way to achieve this in Spring Boot applications is through the use of an AbstractController. This approach not only enhances code organization but also simplifies exception handling and response generation across multiple controllers. In this article, we will explore the concept of AbstractController, its implementation, and the benefits it brings to Spring Boot microservices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*g75cAD8gShdpwU4ksbOhJg.png" /></figure><h3>What is AbstractController?</h3><p>An AbstractController is a base class that contains common functionalities shared among multiple controllers in a Spring Boot application. By extending this class, concrete controllers can inherit and reuse methods, promoting <strong>DRY</strong> (Don&#39;t Repeat Yourself) principles. In the context of microservices, where multiple controllers often have similar patterns for handling requests and responses, this structure significantly reduces redundancy.</p><h3>Key Features of AbstractController</h3><ol><li><strong>Centralized Response Handling</strong>: The AbstractController provides a unified way to send responses back to clients. This includes methods for sending successful responses, created responses, and no content responses. By standardizing these methods, the code remains consistent across different controllers.</li><li><strong>Exception Handling</strong>: The class can include exception handling methods that handle various types of exceptions, such as validation errors and custom exceptions. This centralized error handling means that common error responses can be defined in one place, reducing the likelihood of inconsistent responses across different endpoints.</li><li><strong>Logging</strong>: Using logging frameworks like SLF4J (as demonstrated in the example), the AbstractController can log errors, making it easier to debug and monitor applications.</li></ol><h3>Example Implementation</h3><p>Here is a simplified implementation of the AbstractController and its derived controller, AuthController, that demonstrates how to handle authentication-related requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yQkGM4jvQJNEg_hpkGpqGw.png" /></figure><h3>AbstractController Implementation</h3><pre>@Slf4j<br>public abstract class AbstractController {<br><br>    protected &lt;T&gt; ResponseEntity&lt;ResponseObject&gt; sendResponse(T response, HttpStatus httpStatus) {<br>        ResponseObject responseBody = new ResponseObject();<br>        responseBody.setData(response);<br>        responseBody.setStatus(httpStatus);<br>        return new ResponseEntity&lt;&gt;(responseBody, httpStatus);<br>    }<br><br>    protected ResponseEntity&lt;Void&gt; sendNoContentResponse() {<br>        return ResponseEntity.noContent().build();<br>    }<br><br>    protected &lt;T&gt; ResponseEntity&lt;ResponseObject&gt; sendSuccessResponse(T response) {<br>        return sendResponse(response, HttpStatus.OK);<br>    }<br><br>    protected &lt;T&gt; ResponseEntity&lt;ResponseObject&gt; sendCreatedResponse(T response) {<br>        return sendResponse(response, HttpStatus.CREATED);<br>    }<br><br>    @ResponseBody<br>    @ExceptionHandler(value = BookingException.class)<br>    protected ResponseEntity&lt;ResponseObject&gt; handleBookingException(BookingException exception) {<br>        log.error(&quot;Exception occurred&quot;, exception);<br>        Map&lt;String, Object&gt; errorData = new HashMap&lt;&gt;();<br>        errorData.put(&quot;message&quot;, exception.getMessage());<br>        errorData.put(&quot;errors&quot;, exception.getErrors());<br>        errorData.put(&quot;timeStamp&quot;, exception.getTimestamp());<br><br>        ResponseObject responseBody = new ResponseObject();<br>        responseBody.setData(errorData);<br>        responseBody.setStatus(exception.getHttpStatusCode());<br><br>        return new ResponseEntity&lt;&gt;(responseBody, exception.getHttpStatusCode());<br>    }<br><br>    @ResponseBody<br>    @ExceptionHandler(value = MethodArgumentNotValidException.class)<br>    protected ResponseEntity&lt;ResponseObject&gt; handleMethodArgumentNotValidException(<br>            MethodArgumentNotValidException exception) {<br>        log.error(&quot;Exception occurred&quot;, exception);<br>        List&lt;String&gt; errors = exception.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage)<br>                .collect(Collectors.toList());<br>        Map&lt;Object, Object&gt; errorData = new HashMap&lt;&gt;();<br>        errorData.put(&quot;errors&quot;, errors);<br>        errorData.put(&quot;message&quot;, ErrorMessages.ERR_MSG_BAD_REQUEST);<br>        errorData.put(&quot;timeStamp&quot;, new Date());<br><br>        ResponseObject responseBody = new ResponseObject();<br>        responseBody.setData(errorData);<br>        responseBody.setStatus(HttpStatus.BAD_REQUEST);<br><br>        return new ResponseEntity&lt;&gt;(responseBody, HttpStatus.BAD_REQUEST);<br>    }<br>}</pre><h3>BookingController Implementation (sample controller)</h3><p>The AuthController class extends AbstractController and uses its methods for handling bookings.</p><pre>@RestController<br>@RequestMapping(&quot;${api.path.booking}&quot;)<br>public class BookingController extends AbstractController {<br><br>    @Autowired<br>    private BookingService bookingService;<br><br>    @PostMapping(&quot;/book&quot;)<br>    public ResponseEntity&lt;ResponseObject&gt; bookVehicle(@Valid @RequestBody BookingRequest bookingRequest) {<br>        return sendCreatedResponse(bookingService.bookVehicle(bookingRequest));<br>    }<br><br>    @GetMapping(&quot;/history/{userId}&quot;)<br>    public ResponseEntity&lt;ResponseObject&gt; getBookingHistory(@PathVariable String userId) {<br>        return sendSuccessResponse(bookingService.getBookingHistory(userId));<br>    }<br><br>}</pre><h3>Advantages of Using AbstractController in Spring Boot</h3><h3>1. Code Reusability</h3><p>By centralizing common functionalities in AbstractController, you promote code reuse. This approach minimizes the amount of duplicated code across your controllers, making maintenance easier and reducing the potential for errors.</p><h3>2. Consistency</h3><p>Having a centralized mechanism for response generation and error handling ensures that all controllers respond in a consistent manner. This is crucial in microservices architectures, where different services might be communicating with each other or with front-end applications.</p><h3>3. Improved Readability</h3><p>With common functionalities extracted to a base class, concrete controllers like AuthController become cleaner and more focused on their specific business logic. This separation of concerns improves code readability and maintainability.</p><h3>4. Simplified Error Handling</h3><p>By handling exceptions in the AbstractController, you can ensure that all controllers handle errors in a unified manner. This not only improves the consistency of error responses but also simplifies the debugging process.</p><h3>5. Enhanced Logging</h3><p>Centralized logging in exception handling allows for easier tracking of issues and operational metrics. It helps developers to quickly identify problems across multiple services.</p><h3>Conclusion</h3><p>Using an AbstractController in Spring Boot microservices is a best practice that can significantly improve the structure, maintainability, and readability of your code. By centralizing common functionalities such as response handling and exception management, you can streamline the development process and ensure a more consistent and error-free experience for users. As your application scales, this approach will make it easier to manage complexity while fostering code quality and collaboration among team members.</p><p>In a world where microservices are becoming increasingly prevalent, adopting such design patterns will set the foundation for robust and maintainable applications.</p><h3>Example GitHub Repository</h3><p>For a practical implementation, you can refer to this GitHub repository:</p><ul><li><a href="https://github.com/ShanukaLakshan/abstract_exampe_medium.git"><strong>Spring Boot AbstractController Example</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b7a71b78c38" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>