<?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 Aahana Khanal on Medium]]></title>
        <description><![CDATA[Stories by Aahana Khanal on Medium]]></description>
        <link>https://medium.com/@aahana.khanal11?source=rss-ccee6dbb1ec7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*P3L0pwDfE-wUIKPQ</url>
            <title>Stories by Aahana Khanal on Medium</title>
            <link>https://medium.com/@aahana.khanal11?source=rss-ccee6dbb1ec7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 23:13:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aahana.khanal11/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[Hailo-8 AI Module Documentation]]></title>
            <link>https://medium.com/@aahana.khanal11/hailo-8-ai-module-documentation-182d2dd5c9d0?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/182d2dd5c9d0</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Thu, 03 Jul 2025 04:48:12 GMT</pubDate>
            <atom:updated>2025-07-03T05:25:10.226Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>The <strong>Hailo-8 AI Module</strong> is an edge-AI accelerator in a compact M.2 form factor, designed for low-power devices that require high-performance deep-learning inference. It brings data-center-class performance to embedded and IoT applications without the power and thermal constraints of traditional GPUs.</p><h3>2. What Is the Hailo-8?</h3><ul><li><strong>Form Factor</strong>: M.2 module (22×42 mm standard, breakable to 22×60 mm or 22×80 mm)</li><li><strong>Core</strong>: Proprietary Hailo-8 deep-learning inference ASIC</li><li><strong>Interface</strong>: PCIe Gen3.0 (×4 lanes on M-key; ×2 lanes on B+M/A+E)</li><li><strong>Power</strong>: Typical 2.5 W; maximum ~8.65 W</li></ul><h3>3. Key Features</h3><p><strong>High Compute Throughput</strong><br> — Up to <strong>26 TOPS</strong> (INT8 precision)</p><p><strong>Exceptional Power Efficiency</strong><br> — Over <strong>10 TOPS/W</strong> (typical draw: 2.5 W)</p><p><strong>Concurrent Multi-Stream &amp; Multi-Model</strong><br> — Run several video feeds or neural networks in parallel</p><p><strong>Wide Framework Support</strong><br> — TensorFlow, TensorFlow Lite, PyTorch, ONNX, Keras</p><p><strong>Robust Software Stack</strong><br> — HailoWare SDK: compiler, runtime, and APIs for Python &amp; C/C++</p><h3>4. Technical Specifications</h3><p>The Hailo-8 AI module centers on a custom Hailo-8 ASIC that delivers up to 26 INT8 TOPS of compute while drawing just 2.5 W under typical loads (peaking at about 8.65 W), all within a compact M.2 form factor (22 × 42 mm, with break-away extensions to 60 mm or 80 mm). It connects over PCIe Gen3.0 — four lanes on an M-key slot or two lanes on B+M/A+E keys — and supports both Linux and Windows hosts. Rated for operation from –40 °C to +85 °C, it meets CE and FCC certifications for use in rugged edge and IoT environments.</p><h3>5. Software Ecosystem</h3><p><strong>HailoWare Compiler</strong><br> Converts models into Hailo-optimized binaries</p><p><strong>Runtime &amp; SDK</strong><br> Python &amp; C/C++ libraries for loading models and running inference</p><p><strong>Pre-Built Demos</strong><br> Examples for object detection, classification, and segmentation</p><p><strong>Monitoring Tools</strong><br> Power, performance, and latency profiling</p><h3>6. Typical Use Cases</h3><ul><li><strong>Smart Surveillance</strong>: On-device person detection, facial recognition</li><li><strong>Robotics</strong>: Real-time navigation and obstacle avoidance</li><li><strong>Industrial IoT</strong>: Anomaly detection, quality inspection</li><li><strong>Automotive Prototyping</strong>: Driver monitoring, sensor fusion</li></ul><h3>7. Getting Started</h3><p><strong>Hardware Installation</strong><br> Insert the M.2 module into a compatible slot on your board</p><p><strong>Software Setup</strong><br> Install Hailo kernel module, udev rules, and HailoWare SDK</p><p><strong>Model Preparation</strong></p><ul><li>Train your model (e.g., in TensorFlow or PyTorch)</li><li>Compile with Hailoc compiler</li></ul><p><strong>Deployment &amp; Inference</strong></p><ul><li>Load the compiled model via Python/C++ API</li><li>Run inference and monitor performance</li></ul><h3>8. Audio &amp; Sound Detection with Hailo-8</h3><p>The Hailo-8 module is fully capable of accelerating audio and sound classification models, enabling on-device inference for tasks such as keyword spotting, environmental sound detection, or alarm-beep recognition. The end-to-end workflow consists of:</p><p><strong>Audio Pre‑processing (Host CPU)</strong><br>• Capture audio (e.g., 16 kHz mono) and compute features such as Mel-spectrograms or MFCCs (e.g., 40 Mel bands, 25 ms window, 10 ms hop).<br>• Normalize and reshape into a tensor of shape [1, time_frames, freq_bins, 1] ready for inference.</p><p><strong>Model Design &amp; Training</strong><br>• Choose or build a lightweight CNN, CRNN (CNN + RNN), or transformer-based audio classifier (e.g., for speech commands).<br>• Train in TensorFlow, PyTorch, or Keras on your labeled dataset.<br>• (Optional) Apply pruning or quantization-aware training to improve INT8 accuracy.</p><p><strong>Export &amp; Quantization</strong><br>• Export the trained model to a supported format (TensorFlow SavedModel, TFLite, or ONNX).<br>• Perform post-training static quantization to INT8, calibrating with representative audio samples.</p><p><strong>Compilation with HailoWare</strong></p><pre>hailoc compiler \  <br>  --input my_audio_model.onnx \  <br>  --input-shapes [1,100,40,1] \  <br>  --output sound_model.hef</pre><p>This generates an optimized binary (.hef) tailored for the Hailo-8’s dataflow engine.</p><p><strong>Deployment &amp; Inference</strong></p><pre>import numpy as np<br>from hailo_platform import Device, Config<br><br># Load pre-processed feature tensor<br>features = np.load(&#39;test_melspec.npy&#39;).astype(np.int8)<br><br># Initialize device and network<br>with Device(Config(&#39;config_hailo.json&#39;)) as device:<br>    sound_net = device.networks[&#39;sound_model&#39;]<br><br>    # Run inference<br>    outputs = sound_net.infer({&#39;input.1&#39;: features})<br>    prediction = np.argmax(outputs[&#39;output.1&#39;], axis=1)<br>    print(&#39;Detected class:&#39;, prediction)</pre><p><strong>Performance &amp; Latency</strong></p><ol><li>Typical end-to-end inference latency is sub-millisecond per frame, enabling real-time audio detection.</li><li>Monitor performance via hailoc monitor --target hailo-8 throughput, power, and utilization.</li></ol><h3>8. References</h3><ul><li><strong>Hailo Developer Portal</strong>: <a href="https://hailo.ai/">https://hailo.ai</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=182d2dd5c9d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Documentation on Hugging Face Models]]></title>
            <link>https://medium.com/@aahana.khanal11/documentation-on-hugging-face-models-0748a39b8d79?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/0748a39b8d79</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Fri, 27 Jun 2025 06:52:15 GMT</pubDate>
            <atom:updated>2025-06-27T06:52:15.218Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview</h3><p>Hugging Face is an open-source platform that offers advanced machine learning tools, models, and libraries for various artificial intelligence tasks. It focuses on Natural Language Processing (NLP), Computer Vision, Audio processing, and multimodal applications, providing pre-trained models, datasets, and user-friendly interfaces.</p><h3>1. NLP (Text) Models</h3><h4>Encoder Models</h4><ul><li><strong>Purpose:</strong> Create contextual embeddings for tasks such as classification and Named Entity Recognition (NER).</li></ul><p><strong>Examples:</strong></p><ul><li><strong>BERT:</strong> Used for classification, sentiment analysis, and NER.</li><li><strong>RoBERTa:</strong> An improved version of BERT that excels at semantic tasks.</li><li><strong>DistilBERT:</strong> A lightweight model suitable for quick inference.</li></ul><h4>Decoder Models</h4><ul><li><strong>Purpose:</strong> Autoregressive models are used in text generation tasks.</li></ul><p><strong>Examples:</strong></p><ul><li><strong>GPT (GPT-2, GPT-3, GPT-4):</strong> Utilized for conversational agents and text completion.</li><li><strong>LLaMA:</strong> Open-source alternatives to GPT models that offer powerful generative capabilities.</li></ul><h4>Encoder-Decoder Models</h4><ul><li><strong>Purpose:</strong> Translation, summarization, and question-answering tasks.</li></ul><p><strong>Examples:</strong></p><ul><li><strong>T5:</strong> Flexible text-to-text tasks.</li><li><strong>BART:</strong> Ideal for summarization and text generation.</li><li><strong>PEGASUS:</strong> Specifically optimized for summarization.</li></ul><h4>Multilingual Models</h4><ul><li><strong>Purpose:</strong> NLP tasks across multiple languages.</li></ul><p><strong>Examples:</strong></p><ul><li><strong>mBERT:</strong> Supports about 100 languages.</li><li><strong>XLM-R:</strong> Cross-lingual tasks, highly accurate.</li></ul><h3>2. Vision Models</h3><h4>Image Classification</h4><ul><li><strong>Vision Transformer (ViT):</strong> Image classification via transformer architecture.</li><li><strong>ResNet/EfficientNet:</strong> CNN models for efficient and rapid image classification.</li></ul><h4>Object Detection and Segmentation</h4><ul><li><strong>DETR:</strong> End-to-end object detection using transformers.</li><li><strong>YOLOS:</strong> Vision transformer-based object detection.</li><li><strong>SegFormer &amp; MaskFormer:</strong> Pixel-level segmentation for detailed visual tasks.</li></ul><h4>Vision-Language Models</h4><ul><li><strong>CLIP:</strong> Image-text embedding for retrieval and classification.</li><li><strong>BLIP:</strong> Image captioning and question-answering.</li></ul><h3>3. Audio Models</h3><h4>Automatic Speech Recognition (ASR)</h4><ul><li><strong>Whisper:</strong> Multilingual speech-to-text model.</li><li><strong>Wav2Vec 2.0:</strong> Audio transcription directly from raw audio data.</li></ul><h4>Text-to-Speech (TTS)</h4><ul><li><strong>Tacotron 2, Bark, VITS:</strong> Realistic speech synthesis from textual input.</li></ul><h4>Audio Classification</h4><ul><li><strong>AST:</strong> Audio event detection and classification.</li></ul><h3>4. Generative &amp; Diffusion Models</h3><h4>Text Generation</h4><ul><li><strong>GPT Series:</strong> Conversational agents and creative writing.</li></ul><h4>Image Generation</h4><ul><li><strong>Stable Diffusion, SDXL:</strong> Generating high-quality images from text prompts.</li></ul><h3>5. Multimodal Models</h3><ul><li><strong>GPT-4 Vision:</strong> Integrates image understanding with text generation.</li><li><strong>BLIP:</strong> Multimodal understanding and image captioning.</li></ul><h3>6. Reinforcement Learning with Human Feedback (RLHF)</h3><ul><li><strong>ChatGPT, LLaMA-2-chat, Alpaca:</strong> Models optimized through human feedback for safe and effective interactions.</li></ul><h3>Conclusion</h3><p>Hugging Face provides comprehensive tools and pre-trained models enabling efficient, innovative AI-driven applications across NLP, vision, audio, and multimodal domains. Its ease of use, broad coverage, and active community make it invaluable for developers and researchers alike.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0748a39b8d79" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing Sentence Embedding Model to Reduce Application Size]]></title>
            <link>https://medium.com/@aahana.khanal11/optimizing-sentence-embedding-model-to-reduce-application-size-f934972bb7ca?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f934972bb7ca</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Fri, 20 Jun 2025 04:32:08 GMT</pubDate>
            <atom:updated>2025-06-20T04:35:30.269Z</atom:updated>
            <content:encoded><![CDATA[<h3>Objective</h3><p>To reduce the Docker image size and application footprint by replacing the heavy<em> all-MiniLM-L6-v2</em> embedding model with a lightweight alternative: <em>paraphrase-MiniLM-L3-v2</em>. This is achieved while maintaining performance and functionality.</p><h3>Requirement</h3><p>The current application uses an <em>all-Mini-L6-v2</em> model from sentence-transformers, which significantly increases the Docker image size. Evaluate and integrate a lighter alternative from Hugging Face that offers comparable performance with reduced size.</p><h3>Step-by-Step Implementation</h3><h3>1. Update .env</h3><p>Add the following line to your .env file:</p><pre>EMBEDDING_MODEL_NAME=sentence-transformers/paraphrase-MiniLM-L3-v2</pre><p>This allows you to dynamically change the embedding model name without touching the code.</p><h3>2. Create Lazy Model Loader</h3><p>File: app/utils/model_loader.py</p><pre>from sentence_transformers import SentenceTransformer<br>import os</pre><pre>_model = None  # cached singleton</pre><pre>def get_model():<br>    global _model<br>    if _model is None:<br>        model_name = os.getenv(&quot;EMBEDDING_MODEL_NAME&quot;, &quot;sentence-transformers/paraphrase-MiniLM-L3-v2&quot;)<br>        print(f&quot;Loading sentence-embedding model: {model_name}&quot;)<br>        _model = SentenceTransformer(model_name)<br>    return _model</pre><p>Benefits:</p><ul><li>Loads the model only when needed (lazy loading)</li><li>Caches model for reuse</li><li>Respect the configuration from .env</li></ul><h3>3. Modify the recommendation.py</h3><p>Location: app/services/recommendation.py</p><h4>3a. Remove global model loading</h4><p><strong>Before:</strong></p><pre>from sentence_transformers import SentenceTransformer, util<br>print(&quot;Loading sentence transformer model...&quot;)<br>model = SentenceTransformer(&#39;all-MiniLM-L6-v2&#39;)<br>tokenizer = model.tokenizer<br>print(&quot;Model loaded successfully.&quot;)</pre><p><strong>After:</strong></p><pre>from sentence_transformers import util<br>from app.utils.model_loader import get_model</pre><h4>🔧 3b. Load the model inside the function</h4><p>Inside <em>recommend_profiles(...)</em>, near the top:</p><pre>model = get_model()<br>tokenizer = model.tokenizer</pre><p>This defers model loading until the function is actually called.</p><h3>4. Remove Old Cache Files (Optional)</h3><p>The following files may have cached embeddings for the old model:</p><ul><li>app/storage/asset_embeddings.npy</li><li>app/storage/cached_asset_types.json</li></ul><p>Run:</p><pre>rm app/storage/asset_embeddings.npy app/storage/cached_asset_types.json 2&gt;/dev/null || true</pre><p>These will regenerate automatically the first time the new model runs.</p><h3>5. Update requirements.txt</h3><p>Pin the transformer version to ensure consistency:</p><pre>sentence-transformers==2.6.1<br>PyJWT[crypto]</pre><h3>6. Create or Update .dockerignore</h3><p>File: .dockerignore</p><pre>__pycache__/<br>*.npy<br>*.json<br>*.csv<br>*.xlsx<br>.env</pre><p>This prevents unnecessary or sensitive files from bloating your Docker image.</p><h3>7. Update Dockerfile</h3><p>Replace the existing line:</p><pre>RUN apt-get update &amp;&amp; apt-get install -y</pre><p>With:</p><pre>RUN apt-get update &amp;&amp; apt-get install -y git &amp;&amp; rm -rf /var/lib/apt/lists/*</pre><p>This reduces image size and ensures Git is available to pull models if needed.</p><h3>8. Rebuild and Run the App</h3><p>Use the following commands:</p><pre>docker-compose build<br>docker-compose up</pre><p>Watch for this message in your logs:</p><pre>Loading sentence-embedding model: sentence-transformers/paraphrase-MiniLM-L3-v2</pre><p>This confirms successful integration of the new model.</p><h3>Outcome Summary</h3><p>Change Result Replaced model From<em> all-MiniLM-L6-v2</em> (~420MB) to <em>paraphrase-MiniLM-L3-v2</em>(~33MB) Lazy model loading, Faster startup, lower memory usage, Model config in <em>.env, </em>Easy switching without code edits, Clean Docker image, Smaller image, faster builds, Maintained logic, No impact on core recommendation functionality</p><h3>Final Result</h3><p>We have now:</p><ul><li>Reduced Docker image size by 90%</li><li>Made model configurable via .env</li><li>Modularized model loading logic</li><li>Improved system startup time and memory efficiency</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f934972bb7ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of Message Queue for Skill Profile Excel Import API]]></title>
            <link>https://medium.com/@aahana.khanal11/implementation-of-message-queue-for-skill-profile-excel-import-api-45802f792f3a?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/45802f792f3a</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Wed, 18 Jun 2025 02:16:26 GMT</pubDate>
            <atom:updated>2025-06-18T02:19:08.040Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to offloading heavy Grok-based extraction and recommendations to a background worker for a scalable “Skill Profile” API.</p><h3>Introduction</h3><p>Processing large, multi-sheet Excel files to extract technician profiles, call AI models, generate CSV/JSON outputs, and compute embedding-based recommendations can block API threads and degrade user experience. By integrating Celery with RabbitMQ (as broker) and Redis (as result backend), we can:</p><ul><li><strong>Decouple</strong> heavy work from HTTP requests</li><li><strong>Scale</strong> worker processes independently</li><li><strong>Keep</strong> API responses fast and non-blocking</li><li><strong>Provide</strong> simple polling for results</li></ul><p>This documentation walks through a production-ready implementation using FastAPI.</p><h3>1. Dependencies</h3><pre>fastapi<br>uvicorn<br>pandas<br>python-dotenv<br>python-multipart<br>aiohttp<br>openpyxl<br>tenacity<br>tiktoken<br>httpx<br>sentence-transformers<br><br>celery&gt;=5.3.0<br>redis&gt;=4.5.0</pre><p><strong>Logic:</strong> Lists core FastAPI, AI, Excel-processing, and Celery/Redis libraries needed for the project.</p><h3>2. Celery Configuration (celery_app.py)</h3><pre>import os<br>from celery import Celery<br><br>BROKER  = os.getenv(&quot;CELERY_BROKER_URL&quot;, &quot;amqp://guest:guest@rabbitmq:5672//&quot;)<br>BACKEND = os.getenv(&quot;CELERY_RESULT_BACKEND&quot;, &quot;redis://redis:6379/0&quot;)<br><br>celery_app = Celery(<br>    &quot;skill_profile_grok&quot;,<br>    broker=BROKER,<br>    backend=BACKEND,<br>    include=[&quot;app.tasks&quot;],<br>)<br><br>celery_app.conf.update(<br>    task_serializer=&quot;json&quot;,<br>    result_serializer=&quot;json&quot;,<br>    accept_content=[&quot;json&quot;],<br>    timezone=&quot;Asia/Kathmandu&quot;,<br>    enable_utc=True,<br>)</pre><p><strong>Logic:</strong> Sets up Celery to use RabbitMQ as the broker and Redis as the result backend with JSON serialization and UTC timestamps.</p><h3>3. Defining the Background Task (app/tasks.py)</h3><pre>import json, asyncio<br>from io import BytesIO<br>from celery_app import celery_app<br>from fastapi.datastructures import UploadFile<br>from app.services.skill_profile_onboarding import generate_skill_profile<br><br>@celery_app.task(name=&quot;skill_profile.process&quot;)<br>def process_skill_profile(asset_types_json, site_name, file_bytes, filename):<br>    asset_types = json.loads(asset_types_json)<br>    upload = UploadFile(<br>        filename=filename,<br>        file=BytesIO(file_bytes),<br>        content_type=(<br>            &quot;application/vnd.openxmlformats-officedocument.&quot;<br>            &quot;spreadsheetml.sheet&quot;<br>        )<br>    )<br>    return asyncio.run(<br>        generate_skill_profile(asset_types, site_name, upload)<br>    )</pre><p><strong>Logic:</strong> Registers a Celery task that reconstructs the Excel upload and invokes the existing async processing function synchronously, returning its result.</p><h3>4. FastAPI Endpoints (app/routes/routes.py)</h3><pre>from fastapi import APIRouter, UploadFile, File, Form, HTTPException<br>from app.tasks import process_skill_profile<br><br>router = APIRouter(prefix=&quot;/api/skill-profile&quot;, tags=[&quot;Skill Profile&quot;])<br><br>@router.post(&quot;/generate&quot;)<br>async def generate_skill_profile_from_excel(<br>    asset_types: str = Form(...),<br>    site_name:    str = Form(...),<br>    file:         UploadFile = File(...)<br>):<br>    if not file.filename.endswith((&quot;.xls&quot;, &quot;.xlsx&quot;)):<br>        raise HTTPException(400, &quot;Invalid Excel file&quot;)<br>    data_bytes = await file.read()<br>    task = process_skill_profile.delay(<br>        asset_types, site_name, data_bytes, file.filename<br>    )<br>    return {&quot;task_id&quot;: task.id, &quot;status&quot;: &quot;queued&quot;}<br><br>@router.get(&quot;/tasks/{task_id}&quot;)<br>def get_task_status(task_id: str):<br>    from celery.result import AsyncResult<br>    from celery_app import celery_app<br><br>    res = AsyncResult(task_id, app=celery_app)<br>    if res.state == &quot;PENDING&quot;:<br>        return {&quot;state&quot;: res.state}<br>    if res.state == &quot;SUCCESS&quot;:<br>        return {&quot;state&quot;: res.state, &quot;result&quot;: res.result}<br>    if res.state == &quot;FAILURE&quot;:<br>        raise HTTPException(500, detail=str(res.result))<br>    return {&quot;state&quot;: res.state}</pre><p><strong>Logic:</strong></p><p><strong>/generate:</strong> Validates and enqueues the task, immediately returning a task_id.</p><p><strong>/tasks/{task_id}:</strong> Polls Celery/Redis for task state and result, relaying it back to the client.</p><h3>5. Environment Variables (.env)</h3><pre>XAI_API_KEY=YOUR_XAI_API_KEY<br>CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672//<br>CELERY_RESULT_BACKEND=redis://redis:6379/0</pre><p><strong>Logic:</strong> Stores secrets and broker/backend URLs separate from code, loaded via python-dotenv.</p><h3>6. Containerization</h3><h4>Dockerfile:</h4><pre>FROM python:3.11-slim<br>WORKDIR /app<br>RUN apt-get update &amp;&amp; apt-get install -y build-essential &amp;&amp; rm -rf /var/lib/apt/lists/*<br>COPY requirements.txt .<br>RUN pip install --no-cache-dir -r requirements.txt<br>COPY . .<br>EXPOSE 8000<br>CMD [&quot;uvicorn&quot;, &quot;app.utils.main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]</pre><p><strong>Logic:</strong> Creates a lightweight image, installs dependencies, and runs the FastAPI server.</p><h4>docker-compose.yml</h4><pre>version: &quot;3.8&quot;<br>services:<br>  rabbitmq:<br>    image: rabbitmq:3-management<br>    ports: [&quot;5672:5672&quot;,&quot;15672:15672&quot;]<br><br>  redis:<br>    image: redis:latest<br>    ports: [&quot;6379:6379&quot;]<br><br>  api:<br>    build: .<br>    command: uvicorn app.utils.main:app --host 0.0.0.0 --port 8000 --reload<br>    ports: [&quot;8000:8000&quot;]<br>    env_file: [.env]<br>    depends_on: [rabbitmq, redis]<br><br>  worker:<br>    build: .<br>    command: celery -A celery_app.celery_app worker --loglevel=info --concurrency=4<br>    env_file: [.env]<br>    depends_on: [rabbitmq, redis]</pre><p><strong>Logic:</strong> Orchestrates RabbitMQ, Redis, FastAPI, and the Celery worker containers, wiring them together.</p><h3>7. Running &amp; Testing</h3><ol><li><strong>Start all services</strong></li></ol><pre>docker-compose up --build</pre><p><strong>What to watch for</strong>:</p><ul><li><strong>rabbitmq</strong> container logs: “Server startup complete”</li><li><strong>redis</strong> container: no errors</li><li><strong>API </strong>(FastAPI) logs: “Uvicorn running on <a href="http://localhost:8000/docs">http://localhost:8000/docs</a>”</li><li><strong>worker </strong>logs: “Connected to redis://…”, ready to receive tasks</li></ul><h4>2. Verify the Queue</h4><ol><li>Open <strong>RabbitMQ Management UI</strong> at <a href="http://localhost:15672/">http://localhost:15672</a> (guest/guest).</li><li>Click <strong>Queues</strong> → You should see a queue named something like celery.</li></ol><h4>3. Enqueue a job</h4><p>Use a simple test Excel — e.g., <strong>test_profiles.xlsx</strong> with two sheets (“Jan”, “Feb”) containing a handful of rows.</p><pre>curl -X POST http://localhost:8000/api/skill-profile/generate \<br>  -F &#39;asset_types=[&quot;Wiring&quot;,&quot;Testing&quot;]&#39; \<br>  -F &#39;site_name=SampleSite&#39; \<br>  -F &#39;file=@/path/to/your.xlsx&#39;</pre><p><strong>Expected response</strong> (HTTP 200):</p><pre>{<br>  &quot;task_id&quot;: &quot;bce287b6-8d9a-4bc8-86fe-49c34e3dfd8e&quot;,<br>  &quot;status&quot;: &quot;queued&quot;<br>}</pre><h4>4. Monitor the Job</h4><ul><li><strong>In RabbitMQ UI</strong>: you should see <strong>Ready: 0, Unacked: 1</strong>, then back to <strong>Ready: 0</strong> as the worker picks it up.</li><li><strong>In the worker logs:</strong> look for lines like:</li></ul><pre>Received task: skill_profile.process[bce287b6...]<br>Merged 2 sheets with total 10 rows.<br>Task skill_profile.process[...] succeeded in 12.3s</pre><h4>5. Poll for results</h4><p>Wait a few seconds, then run:</p><pre>curl http://localhost:8000/api/skill-profile/tasks/&lt;task_id&gt;</pre><p><strong>PENDING</strong>:</p><pre>{ &quot;state&quot;: &quot;PENDING&quot; }</pre><p><strong>SUCCESS</strong>:</p><pre>{<br>  &quot;state&quot;: &quot;SUCCESS&quot;,<br>  &quot;result&quot;: {<br>    &quot;profiles&quot;: [ /* extracted profiles */ ],<br>    &quot;recommendations&quot;: { &quot;Wiring&quot;: [...], &quot;Testing&quot;: [...] }<br>  }<br>}</pre><p><strong>FAILURE</strong> (HTTP 500):</p><pre>{ &quot;detail&quot;: &quot;Skill profile generation failed: &lt;error message&gt;&quot; }</pre><p><strong>Logic:</strong> Demonstrates the full lifecycle — upload, enqueue, process, and retrieve — using simple cURL commands.</p><h3>Conclusion</h3><p>By integrating Celery with RabbitMQ and Redis, we can transform a blocking Excel-import API into a scalable, asynchronous pipeline. Clients enjoy instant acknowledgements and can poll for AI-extracted profiles and recommendations at their convenience.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45802f792f3a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Step-by-Step RabbitMQ Setup Using Docker]]></title>
            <link>https://medium.com/@aahana.khanal11/step-by-step-rabbitmq-setup-using-docker-5fd7572c2e7f?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fd7572c2e7f</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Tue, 10 Jun 2025 10:17:43 GMT</pubDate>
            <atom:updated>2025-06-11T04:40:25.380Z</atom:updated>
            <content:encoded><![CDATA[<p>Steps:-</p><h4>Step 1: Create or update Docker-Compose.yml</h4><p>Add this to your root project folder (docker-compose.yml):</p><pre>version: &#39;3&#39;<br><br>services:<br>  rabbitmq:<br>    image: rabbitmq:3-management<br>    container_name: rabbitmq<br>    ports:<br>      - &quot;5672:5672&quot;      # main RabbitMQ port<br>      - &quot;15672:15672&quot;    # management UI port<br>    environment:<br>      RABBITMQ_DEFAULT_USER: guest<br>      RABBITMQ_DEFAULT_PASS: guest</pre><p>This uses <strong>RabbitMQ with Management UI</strong> so that you can see queues and messages visually at:<a href="http://localhost:15672/"><strong>http://localhost:15672</strong></a><br><em>(</em><strong><em>username:</em></strong><em> guest, </em><strong><em>password:</em></strong><em> guest)</em></p><h4>Step 2: Start RabbitMQ</h4><p>Run this from your project root in terminal:</p><pre>docker-compose up -d</pre><p>You should see a container named rabbitmq running. You can verify with:</p><pre>docker ps</pre><p>Then visit: <strong>http://localhost:15672</strong> and log in with:</p><ul><li><strong>Username:</strong> guest</li><li><strong>Password: </strong>guest</li></ul><h4>Step 3: Install Python RabbitMQ Library (aio-pika)</h4><p>Run:</p><pre>pip install aio-pika</pre><p>Add to requirements.txt :</p><pre>aio-pika</pre><h4><strong>Step 4: Create a Basic Producer to Send a Message</strong></h4><p>In producer_test.py , add:</p><pre>import asyncio<br>import aio_pika<br><br>async def main():<br>    connection = await aio_pika.connect_robust(&quot;amqp://guest:guest@localhost/&quot;)<br>    channel = await connection.channel()<br><br>    queue = await channel.declare_queue(&quot;profile_jobs&quot;, durable=True)<br><br>    message = aio_pika.Message(body=b&quot;Hello from FastAPI!&quot;)<br>    await channel.default_exchange.publish(message, routing_key=&quot;profile_jobs&quot;)<br><br>    print(&quot;Message sent to RabbitMQ!&quot;)<br><br>    await connection.close()<br><br>asyncio.run(main())</pre><p>Then run:</p><pre>python producer_test.py</pre><p>Now, go to your RabbitMQ dashboard → Queues → Click on profile_jobs → You’ll see 1 message.</p><h4>Step 5: Create a Simple Consumer</h4><p>In worker_test.py , add:</p><pre>import asyncio<br>import aio_pika<br><br>async def process_message(message: aio_pika.IncomingMessage):<br>    async with message.process():<br>        print(f&quot;Received: {message.body.decode()}&quot;)<br><br>async def main():<br>    connection = await aio_pika.connect_robust(&quot;amqp://guest:guest@localhost/&quot;)<br>    channel = await connection.channel()<br><br>    queue = await channel.declare_queue(&quot;profile_jobs&quot;, durable=True)<br>    await queue.consume(process_message)<br><br>    print(&quot;Waiting for messages...&quot;)<br>    await asyncio.Future()  # keeps the script running<br><br>asyncio.run(main())</pre><p>Then run:</p><pre>python worker_test.py</pre><p>Now, re-run producer_test.py — The worker will instantly consume the message and print it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fd7572c2e7f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Title: Implementing Asynchronous Message Queue with Celery and Redis in FastAPI Chatbot]]></title>
            <link>https://medium.com/@aahana.khanal11/title-implementing-asynchronous-message-queue-with-celery-and-redis-in-fastapi-chatbot-615128a9fa49?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/615128a9fa49</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Fri, 30 May 2025 12:03:03 GMT</pubDate>
            <atom:updated>2025-05-30T12:03:03.521Z</atom:updated>
            <content:encoded><![CDATA[<h3>Objective</h3><p>To enhance the scalability and reliability of the /fetch-files endpoint in the Sclera AI Chatbot by integrating a message queue system that:</p><ul><li>Handles concurrent file upload requests</li><li>Processes files asynchronously without blocking the API</li><li>Ensures fault-tolerance and non-blocking performance under high load</li></ul><h3>Initial Codebase (Given by Team)</h3><pre># app/routers/data_routes.py<br>@router.post(&quot;/fetch-files&quot;)<br>async def fetch_files(request: FileRequest):<br>    try:<br>        return await process_files(request)  # Direct synchronous processing<br>    except Exception as e:<br>        raise HTTPException(status_code=500, detail=str(e))</pre><h3>Problem</h3><ul><li>The synchronous approach directly processes uploaded files within the FastAPI route.</li><li>Under concurrent requests, this leads to:</li><li>Slow response times</li><li>Thread blocking</li><li>Scalability issues</li></ul><h3>What I Did: Queue-Based Architecture Integration</h3><p>Redesigned the endpoint to use <strong>Celery + Redis</strong> as a background task system.</p><h4>1. Celery Worker Configuration</h4><pre># app/celery_worker.py<br>from celery import Celery</pre><pre>celery_app = Celery(<br>    &quot;chatbot_tasks&quot;,<br>    broker=&quot;redis://redis:6379/0&quot;,<br>    backend=&quot;redis://redis:6379/0&quot;<br>)</pre><pre>celery_app.conf.task_routes = {<br>    &quot;app.tasks.*&quot;: {&quot;queue&quot;: &quot;default&quot;},<br>}</pre><h4>2. Background Task</h4><pre># app/tasks.py<br>from app.services.file_service import process_files_sync<br>from app.celery_worker import celery_app</pre><pre>@celery_app.task(name=&quot;app.tasks.process_files_task&quot;)<br>def process_files_task(request_data):<br>    return process_files_sync(request_data)</pre><h4>3. Synchronous Processing Function</h4><pre># app/services/file_service.py<br>from app.model.file_request import FileRequest</pre><pre>def process_files_sync(request_data):<br>    request = FileRequest(**request_data)<br>    # Processing steps: Download PDFs, embed, store vectors<br>    return {&quot;message&quot;: &quot;All files processed successfully&quot;}</pre><h4>4. Updated Endpoint</h4><pre># app/routers/data_routes.py<br>from app.tasks import process_files_task</pre><pre>@router.post(&quot;/fetch-files&quot;)<br>async def fetch_files(request: FileRequest):<br>    try:<br>        process_files_task.delay(request.model_dump())  # Queued to Celery<br>        return {&quot;message&quot;: &quot;Your files are being processed in the background&quot;}<br>    except Exception as e:<br>        raise HTTPException(status_code=500, detail=str(e))</pre><h3>How It Works — End-to-End Flow</h3><ol><li>User sends a POST request to /fetch-files with JSON.</li><li>FastAPI validates the data using Pydantic.</li><li>Celery task is enqueued via process_files_task.delay(...).</li><li>Redis holds the message until a worker is ready.</li><li>Celery Worker picks the task, processes the PDF, embeds, and stores vectors.</li><li>FastAPI immediately responds to the user without delay.</li></ol><h3>Docker-based Infrastructure</h3><p><strong>docker-compose.yml</strong>:</p><pre>services:<br>  web:<br>    build: .<br>    command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload<br>    ports:<br>      - &quot;8000:8000&quot;<br>    depends_on:<br>      - redis<br>    volumes:<br>      - .:/app</pre><pre>  redis:<br>    image: redis:7</pre><pre>  celery_worker:<br>    build: .<br>    command: celery -A app.celery_worker.celery_app worker --loglevel=info<br>    depends_on:<br>      - redis<br>    volumes:<br>      - .:/app</pre><p><strong>requirements.txt</strong> (Added):</p><pre>celery==5.3.4<br>redis==5.0.1</pre><h3>Why Celery + Redis (instead of AWS SQS/Kafka)?</h3><p>Metric Celery + Redis Amazon SQS Kafka Language Support Native Python SDK required Java-native Simplicity Very easy for internal systems Moderate Complex setup Latency Low Moderate Ultra-low Use Case Fit Background processing Decoupled services High-throughput streaming</p><blockquote><em>Since our stack is FastAPI (Python), Celery+Redis is the simplest, most integrated, and reliable toolset.</em></blockquote><h3>Outcome</h3><ul><li>Requests are now offloaded to Celery for background processing.</li><li>FastAPI remains responsive under concurrent load.</li><li>Redis ensures durable message queueing.</li><li>Docker ensures environment parity and ease of deployment.</li></ul><h3>How to Test</h3><pre>curl -X POST http://localhost:8000/fetch-files \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{<br>        &quot;manufacturer&quot;: &quot;Amerex&quot;,<br>        &quot;model&quot;: &quot;ABC&quot;,<br>        &quot;s3_urls&quot;: [&quot;s3://your-bucket/path/to/file.pdf&quot;]<br>      }&#39;</pre><p><strong>Expected Response:</strong></p><pre>{&quot;message&quot;: &quot;Your files are being processed in the background&quot;}</pre><h4>My O<strong>utcome:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hp2_kGTKE3Gr2tb-6sjeVQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ni_3wgBnOM3eaKF598YBmQ.png" /></figure><h3>Final Result</h3><ul><li>Requests are offloaded to Celery.</li><li>FastAPI is non-blocking.</li><li>Redis acts as a durable queue.</li><li>Docker ensures deployment consistency.</li></ul><h3>Summary</h3><p>I integrated a background task system using Celery and Redis to handle concurrent file uploads to the /fetch-files endpoint. Instead of processing the files directly in FastAPI (which was blocking), we now enqueue them to Celery. Redis stores the task queue, and a Celery worker processes it in the background. This makes our chatbot more scalable, non-blocking, and production-ready. I used this approach because it&#39;s Python-native, simpler to manage with FastAPI, and avoids the overhead of Kafka or AWS SQS for our current scale.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=615128a9fa49" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Task: Identify Current Issues for /fetch-files]]></title>
            <link>https://medium.com/@aahana.khanal11/task-identify-current-issues-for-fetch-files-1707705f6e3d?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1707705f6e3d</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Mon, 26 May 2025 05:19:59 GMT</pubDate>
            <atom:updated>2025-05-26T05:23:36.899Z</atom:updated>
            <content:encoded><![CDATA[<h3>Objective:</h3><p>Evaluate how the system currently handles concurrent uploads and highlight architectural or performance problems.</p><h3><strong>1. Synchronous Design</strong> (Blocking Requests)</h3><h4><strong>Code:</strong></h4><pre>@router.post(&quot;/fetch-files&quot;)<br>async def fetch_files(request: FileRequest):<br>    return await process_files(request)</pre><h3>Problem:</h3><ul><li>The function <strong>process_files(request)</strong> is fully executed within the request-response cycle.</li><li>It waits for each file to be downloaded, parsed, embedded, and saved before returning a response.</li><li>If multiple users hit <strong>/fetch-files </strong>at the same time, FastAPI must:</li><li>Spin up a thread per request (or reuse from a limited pool)</li><li>Block that thread for potentially 10+ seconds per PDF</li></ul><h3>Consequence:</h3><ul><li>Poor user experience (high latency)</li><li>Threads get exhausted → 503 errors</li><li>Uvicorn/Gunicorn workers become overwhelmed under load</li></ul><h3>2. Heavy Operations Done in Real-Time</h3><h4>Observed in:</h4><pre>pages = download_pdf_from_s3(...)               # S3 network I/O<br>index.insert_nodes(nodes, embed_model=...)     # OpenAI API calls<br>index.storage_context.persist(...)             # Disk writes</pre><h4>Problem:</h4><p>These are <strong>I/O-heavy and slow</strong>:</p><ul><li>S3 downloads depend on file size + network</li><li>OpenAI embedding APIs take seconds per chunk</li><li>Disk writes compete with the OS for performance</li></ul><h4>Consequence:</h4><ul><li>Even one slow file upload can delay others.</li><li>Multiple users uploading large PDFs may crash the server or cause very high memory/CPU usage.</li></ul><h3>3. <strong>Thread Blocking</strong></h3><h4>Root Cause:</h4><p>await used for all async functions, but:</p><ul><li>Many of the functions being called are <strong>not truly async</strong> under the hood (e.g.,<strong> boto3, os.makedirs,</strong> OpenAI’s embedding).</li><li><strong>boto3.get_object(…)</strong> and<strong> index.persist(...)</strong> are <strong>sync I/O</strong> pretending to be async.</li></ul><h4>Consequence:</h4><ul><li>Thread-blocking occurs during:</li><li>PDF downloads</li><li>Embedding</li><li>Saving index to disk</li><li>This blocks FastAPI worker threads even if <strong>async def </strong>is used.</li></ul><h3>4. No Queue or Background Worker</h3><h4>Observation:</h4><ul><li>Processing logic is not offloaded to a background system (e.g., Celery, RQ).</li><li>No <strong>task_id</strong>, no job tracking, no way to retry failed jobs or resume partial progress.</li></ul><h4>Consequence:</h4><ul><li>If the server restarts, or embedding fails halfway, all progress is lost.</li><li>Users get error 500s with no retry or resume.</li></ul><h3><strong>Race Conditions</strong></h3><h4>Checked:</h4><ul><li>The use of <strong>storage_dir = f&quot;./app/storage/{asset_id}”</strong> per request is safe.</li><li>There’s no in-memory shared state being mutated across requests.</li></ul><h4>Good:</h4><p>No classic race condition exists between users <strong>unless</strong> two users upload for the same model at the same time.</p><h4>But:</h4><p>If two users upload PDFs for the same <strong>manufacturer + model</strong> concurrently, both will write to the same:</p><pre>/app/storage/{manufacturer}_{model}</pre><p><strong>That can cause:</strong></p><ul><li>Overwritten vector index</li><li>Corrupted disk files</li><li>Inconsistent document embedding</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1707705f6e3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Modern Message Queue Architectures: Apache Kafka vs AWS SQS/SNS/EventBridge]]></title>
            <link>https://medium.com/@aahana.khanal11/mastering-modern-message-queue-architectures-apache-kafka-vs-aws-sqs-sns-eventbridge-ba7d716b69e7?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba7d716b69e7</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Wed, 21 May 2025 02:40:26 GMT</pubDate>
            <atom:updated>2025-05-21T02:40:26.651Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In today’s event-driven architecture and microservices world, message queues are essential for decoupling components, improving scalability, and increasing reliability. This comprehensive guide explores three major players in this space:</p><ul><li><strong>Apache Kafka</strong> (open-source, high-throughput distributed event streaming platform)</li><li><strong>Amazon SQS</strong> (Simple Queue Service for message queuing)</li><li><strong>Amazon SNS</strong> (Simple Notification Service for pub/sub messaging)</li><li><strong>AWS EventBridge</strong> (advanced event bus for serverless integration)</li></ul><p>We’ll break down their architecture, performance traits, and real-world use cases to help you decide what best fits your system.</p><h3>Apache Kafka Overview</h3><h3>Kafka Architecture: Core Components</h3><ul><li><strong>Brokers</strong>: Store and forward messages. Think of them as scalable mail hubs.</li><li><strong>Producers</strong>: Send messages to Kafka topics.</li><li><strong>Consumers</strong>: Subscribe and read messages from topics.</li><li><strong>Partitions</strong>: Divide topics for parallelism and scalability.</li><li><strong>Topics</strong>: Logical categories/messages are grouped here.</li><li><strong>Zookeeper</strong>: Coordinates and manages brokers and partitions.</li></ul><h3>Kafka Workflow Diagram (Producer → Broker/Partition → Consumer)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Vw-DlfUHZElMz5tlYgzQw.webp" /></figure><h3>Message Retention, Durability, and Ordering</h3><ul><li><strong>Retention</strong>: Configurable time/size-based storage even after consumption.</li><li><strong>Durability</strong>: Data replicated across brokers to prevent loss.</li><li><strong>Ordering</strong>: Guaranteed per-partition.</li></ul><h3>Fault Tolerance &amp; Performance</h3><ul><li><strong>Fault Tolerance</strong>: Data replication + broker failover</li><li><strong>Performance</strong>: Handles millions of events per second (ideal for big data and analytics)</li></ul><h3>Kafka Streams &amp; Kafka Connect</h3><ul><li><strong>Kafka Streams</strong>: Enables real-time stream processing within the application.</li><li><strong>Kafka Connect</strong>: Bridges Kafka with external systems like RDBMS, S3, or Elasticsearch.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bC5_S7fgWrCspP3MUrykZg.png" /></figure><h3>Kafka Python Prototype (Enqueue &amp; Dequeue)</h3><pre># Producer<br>from kafka import KafkaProducer<br>import json<br>producer = KafkaProducer(bootstrap_servers=&#39;localhost:9092&#39;, value_serializer=lambda v: json.dumps(v).encode(&#39;utf-8&#39;))<br>producer.send(&#39;order-events&#39;, {&quot;order_id&quot;: 1, &quot;status&quot;: &quot;Placed&quot;})<br>producer.flush()<br><br># Consumer<br>from kafka import KafkaConsumer<br>consumer = KafkaConsumer(&#39;order-events&#39;, bootstrap_servers=&#39;localhost:9092&#39;, group_id=&#39;order-consumers&#39;, value_deserializer=lambda x: json.loads(x.decode(&#39;utf-8&#39;)))<br>for message in consumer:<br>    print(f&quot;Received: {message.value}&quot;)</pre><h3>Amazon SQS (Simple Queue Service)</h3><h3>Core Workflow</h3><ul><li><strong>Producer → SQS Queue → Consumer</strong></li><li>Messages stored across multiple Availability Zones</li><li><strong>Dead-letter Queue</strong>: For failed messages</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g0LW9TledtrYT9pLyfuCQA.png" /></figure><h3>Queue Types</h3><p><strong>Standard Queue</strong>:</p><ul><li>At-least-once delivery</li><li>Best-effort ordering</li><li>High throughput</li></ul><p><strong>FIFO Queue</strong>:</p><ul><li>Exactly-once delivery</li><li>Strict ordering</li><li>Lower throughput (3000 msg/sec with batching)</li></ul><h3>Key Concepts</h3><ul><li><strong>Visibility Timeout</strong>: Temporarily hides a message after delivery</li><li><strong>DLQs</strong>: Helps debug unprocessed messages</li><li><strong>Long Polling</strong>: Reduces cost by waiting for messages</li></ul><h3>Pros &amp; Cons</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*ARjoQtDdxOc8HuI6LSRokw.png" /></figure><h3>Python Demo (Using boto3)</h3><pre>import boto3<br>sqs = boto3.client(&#39;sqs&#39;)<br>queue_url = &#39;https://sqs.us-east-1.amazonaws.com/123456789012/my-queue&#39;<br>sqs.send_message(QueueUrl=queue_url, MessageBody=&#39;Hello from Python!&#39;)</pre><h3>Amazon SNS (Simple Notification Service)</h3><h3>What is Pub/Sub?</h3><ul><li>Publisher → Topic</li><li>Multiple Subscribers receive the message</li><li>Enables loose coupling + scalability</li></ul><h3>SNS Components</h3><ul><li><strong>Topic</strong>: Logical channel</li><li><strong>Publisher</strong>: Sends to topic</li><li><strong>Subscriber</strong>: Receives messages (SQS, Lambda, HTTP, SMS, etc.)</li></ul><h3>Fan-Out Architecture</h3><ul><li>One event → Multiple endpoints</li><li>Example: Order placed</li><li>Lambda: inventory</li><li>SQS: shipping</li><li>Email: customer</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j5zObv7J6RnSoEKwxg0hEg.png" /></figure><h3>SNS + SQS Integration (Reliable Queueing)</h3><ul><li>Durable + async delivery</li><li>Retry, buffering, DLQs</li></ul><h3>SNS + Lambda Integration (Serverless Processing)</h3><ul><li>Real-time triggers for lightweight business logic</li></ul><h3>Message Filtering</h3><ul><li>Route messages based on attributes</li></ul><pre>{ &quot;type&quot;: [&quot;shipping&quot;] }</pre><h3>SNS Python Integration CLI Example</h3><pre>aws sns create-topic --name OrderEvents<br>aws sqs create-queue --queue-name OrderQueue<br>aws sns subscribe --topic-arn &lt;TOPIC_ARN&gt; --protocol sqs --notification-endpoint &lt;SQS_ARN&gt;</pre><h3>AWS EventBridge (Advanced Use Case)</h3><h3>When to Use EventBridge</h3><ul><li>Content-based filtering</li><li>Integration with 20+ AWS services</li><li>Third-party SaaS support (Zendesk, Shopify, Auth0)</li><li>Schema registry + event contract validation</li></ul><p><strong>Example</strong>: GitHub webhook → Only pull_request events → Lambda → RDS</p><h3>EventBridge vs SNS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*9qdUExq_eZ5wC4SqAaX2cQ.png" /></figure><h3>Comparison Table</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*YfmjqMEzTVuJgRdGC_vjmg.png" /></figure><h3>Use Case Mapping</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*-_8UY307Yx8E2ID3iTOHZg.png" /></figure><h3>Cost &amp; Operational Considerations</h3><ul><li><strong>Kafka</strong>: Free, but requires infra + ops team</li><li><strong>SQS/SNS</strong>: Pay-per-request, managed infra</li><li><strong>EventBridge</strong>: Higher cost for event bus, but less ops</li></ul><h3>Final Recommendations</h3><ul><li>Use <strong>Kafka</strong> for large-scale stream processing and custom retention needs.</li><li>Use <strong>SQS</strong> when you need durable message queues and visibility timeouts.</li><li>Use <strong>SNS</strong> when you want simple pub/sub with multi-protocol fan-out.</li><li>Use <strong>EventBridge</strong> for fine-grained routing and integrations.</li></ul><h3>Security Best Practices</h3><ul><li>Use IAM roles &amp; policies</li><li>Enable KMS-based encryption</li><li>Use DLQs with Lambda/SQS</li></ul><h3>Conclusion</h3><p>Apache Kafka and AWS messaging tools (SQS, SNS, and EventBridge) each serve distinct use cases. Whether you’re building real-time data pipelines, serverless automation, or distributed microservices, choosing the right tool depends on delivery guarantees, performance, and operational complexity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba7d716b69e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Amazon SNS (Simple Notification Service) — A Deep Dive for Distributed Messaging]]></title>
            <link>https://medium.com/@aahana.khanal11/amazon-sns-simple-notification-service-a-deep-dive-for-distributed-messaging-f9cbef64f4f2?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9cbef64f4f2</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Fri, 16 May 2025 02:08:02 GMT</pubDate>
            <atom:updated>2025-05-16T02:08:02.301Z</atom:updated>
            <content:encoded><![CDATA[<h3>Amazon SNS (Simple Notification Service) — A Deep Dive for Distributed Messaging</h3><h3>Overview</h3><p><strong>Amazon Simple Notification Service (SNS)</strong> is a fully managed Pub/Sub messaging service by AWS. It allows applications, microservices, and distributed systems to communicate asynchronously, scalably, and reliably by decoupling message producers from message consumers.</p><p>SNS is primarily used for:</p><ul><li>Real-time fan-out messaging</li><li>Decoupled application architecture</li><li>Event-driven serverless integrations</li></ul><h3>1. Understanding the Pub/Sub Messaging Model</h3><h4>What is Pub/Sub?</h4><p><strong>In Publish-Subscribe (Pub/Sub) architecture:</strong></p><ul><li>A publisher sends messages to a logical channel (<strong>topic</strong>).</li><li>One or more subscribers register interest in that topic.</li><li>When a message is published, all subscribers receive a copy of the message.</li></ul><p><strong>This allows:</strong></p><ul><li>Loose coupling between services</li><li>Parallel processing</li><li>High scalability</li></ul><h4><strong>SNS Implementation</strong></h4><p>In Amazon SNS, the Topic acts as the central communication hub — a logical access point where messages are sent. A Publisher is any service or application that generates and sends a message to this topic. Once the message is published, it is automatically distributed to all configured Subscribers, which can be services like Amazon SQS queues, AWS Lambda functions, HTTP endpoints, or even email/SMS receivers. This design allows SNS to decouple components by enabling multiple subscribers to consume the same message independently and in parallel.</p><p><strong>SNS Topics can support multiple subscribers with different protocols:</strong></p><ul><li>HTTP/HTTPS</li><li>Email/Email-JSON</li><li>SMS</li><li>Amazon SQS</li><li>AWS Lambda</li><li>Application endpoints (e.g., Firebase, APNs)</li></ul><h3>2. Fan-out Architecture Explained</h3><p>The fan-out pattern is one of SNS’s most powerful features. It allows a single event to be broadcasted to multiple consumers, each handling the event in a different way.</p><h4>Key Benefits:</h4><ul><li><strong>Parallel Processing</strong>: All subscribers process the same event independently.</li><li><strong>Reliability</strong>: If one consumer fails, others are not affected.</li><li><strong>Flexibility</strong>: Mix and match protocols (e.g., Lambda + SQS + Email).</li></ul><h4><strong>Real Use Case:</strong></h4><p>When an order is placed on an e-commerce site:</p><ul><li>A <strong>Lambda</strong> function handles inventory reduction.</li><li>An <strong>SQS Queue</strong> triggers the shipping process.</li><li>An <strong>email</strong> is sent to the customer.</li><li>A <strong>Slack webhook</strong> logs the order in real-time.</li></ul><h4>Amazon SNS Fan-Out Architecture Diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZIb1LKmOK4ZvMGqj2Oo3pw.png" /></figure><h3>3. Integrating SNS with Amazon SQS</h3><h4>Why Integrate SNS with SQS?</h4><ul><li><strong>Durability</strong>: SQS ensures message persistence even if downstream systems are down.</li><li><strong>Backpressure handling</strong>: Queues buffer messages when the system is overwhelmed.</li><li><strong>Retry Logic</strong>: SQS offers automatic redelivery and visibility timeout.</li></ul><h4><strong>How It Works:</strong></h4><ul><li>SNS Topic publishes to SQS Queue.</li><li>SQS stores the message.</li><li>Consumer polls SQS and processes the message.</li></ul><p><strong>Example: SNS to SQS Integration (AWS CLI)</strong></p><pre># Create SNS Topic<br>aws sns create-topic --name OrderEvents<br><br># Create SQS Queue<br>aws sqs create-queue --queue-name OrderQueue<br><br># Get ARN for both<br>aws sns list-topics<br>aws sqs get-queue-attributes --queue-url &lt;QUEUE_URL&gt; --attribute-name QueueArn<br><br># Subscribe SQS to SNS<br>aws sns subscribe \<br>  --topic-arn &lt;SNS_TOPIC_ARN&gt; \<br>  --protocol sqs \<br>  --notification-endpoint &lt;SQS_QUEUE_ARN&gt;</pre><p>Important: Ensure the <strong>SQS queue policy</strong> allows the SNS topic to send messages.</p><h3>4. Integrating SNS with AWS Lambda</h3><h4>Why Use Lambda?</h4><ul><li>Real-time event-driven execution.</li><li>Serverless and scalable.</li><li>Ideal for lightweight business logic, transformations, notifications, etc.</li></ul><h4>Message Flow:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D1glMH_xBM36_MLChpMhjA.png" /></figure><h4>Use Case:</h4><p>For each new support ticket:</p><ul><li>Lambda creates a ticket in DynamoDB.</li><li>Sends an acknowledgment email.</li><li>Logs event to CloudWatch.</li></ul><h4>Lambda Integration Steps (CLI):</h4><pre># Create SNS Topic<br>aws sns create-topic --name TicketTopic<br><br># Create Lambda function and get its ARN<br><br># Add permission for SNS to invoke Lambda<br>aws lambda add-permission \<br>  --function-name ProcessTicketFunction \<br>  --statement-id snsInvoke \<br>  --action lambda:InvokeFunction \<br>  --principal sns.amazonaws.com \<br>  --source-arn &lt;SNS_TOPIC_ARN&gt;<br><br># Subscribe Lambda to Topic<br>aws sns subscribe \<br>  --topic-arn &lt;SNS_TOPIC_ARN&gt; \<br>  --protocol lambda \<br>  --notification-endpoint &lt;LAMBDA_ARN&gt;</pre><h3>5. Advanced Topic Features</h3><h4>Message Filtering</h4><p>Subscribers can define filter policies to receive only specific messages. Example: A shipping queue should only receive messages where “type”: “shipping”.</p><pre>{<br>  &quot;type&quot;: [&quot;shipping&quot;]<br>}</pre><p>Filters are set per subscriber and allow efficient routing within a single topic.</p><h3>6. (Optional) AWS EventBridge — When You Need More Than SNS</h3><h4>What is EventBridge?</h4><p>AWS EventBridge is a serverless event bus service designed for application integration and event-driven workflows.</p><p><strong>Key Differences vs SNS</strong></p><p>Amazon SNS and AWS EventBridge are both powerful event messaging services offered by AWS, but they serve different architectural needs. SNS follows a basic Pub/Sub (publish-subscribe) model, where publishers send messages to a topic, and those messages are pushed to all subscribed endpoints. In contrast, EventBridge operates as an event bus, offering advanced capabilities for routing and transformation of events based on specific rules. SNS supports attribute-based filtering, where subscribers filter messages using predefined message attributes. EventBridge, on the other hand, enables content-based filtering, allowing far more granular control over event routing using the full event payload. When it comes to target integration, SNS supports limited endpoints such as Amazon SQS, AWS Lambda, and HTTP/S endpoints. In contrast, EventBridge can route events to over 20 AWS services and integrated SaaS applications, such as Zendesk, Shopify, and Auth0, thanks to its native support for third-party events. Additionally, while SNS does not offer any built-in schema registry, EventBridge includes a schema registry and discovery features, making it ideal for complex event-driven systems that require strict event contracts and observability.</p><h3>Example Use Case:</h3><ul><li>You need to route GitHub webhook events → filter only “pull_request” type → send to Lambda → log in RDS.</li></ul><p>This is best suited for EventBridge, not SNS.</p><h3>Use Case Mapping</h3><p>Choosing between Amazon SNS and AWS EventBridge depends largely on the specific use case and the complexity of the system architecture. For scenarios where you need to broadcast a message to multiple systems simultaneously, Amazon SNS is the ideal choice due to its simple and scalable Pub/Sub model. If your goal is to decouple producers and consumers while ensuring message durability and retry capabilities, the combination of SNS with Amazon SQS is recommended. For applications requiring real-time serverless execution, such as triggering functions in response to specific events, using SNS with AWS Lambda offers a lightweight and reactive solution. However, when your system demands fine-grained routing and content-based filtering, EventBridge provides superior flexibility and control. Additionally, for integrating with third-party SaaS tools like Zendesk or Auth0, EventBridge is the more appropriate tool, as it offers built-in support for external event sources and advanced event bus capabilities.</p><h3>Security Best Practices</h3><ul><li>Use IAM policies and resource policies to control publishing and subscribing.</li><li>Enable encryption (SNS supports SSE with AWS KMS).</li><li>Use Dead Letter Queues (DLQs) with SQS and Lambda to capture failed messages.</li></ul><p>Amazon SNS is a core building block for real-time, event-driven architecture on AWS. When used with SQS and Lambda, it creates a powerful, scalable, and decoupled ecosystem. For advanced scenarios, EventBridge enhances flexibility with dynamic routing and third-party integration.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9cbef64f4f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering AWS Messaging Services: Deep Dive into Amazon SQS (Simple Queue Service)]]></title>
            <link>https://medium.com/@aahana.khanal11/mastering-aws-messaging-services-deep-dive-into-amazon-sqs-simple-queue-service-5256e15d7919?source=rss-ccee6dbb1ec7------2</link>
            <guid isPermaLink="false">https://medium.com/p/5256e15d7919</guid>
            <dc:creator><![CDATA[Aahana Khanal]]></dc:creator>
            <pubDate>Mon, 12 May 2025 15:55:52 GMT</pubDate>
            <atom:updated>2025-05-13T15:57:09.869Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In today’s microservices and serverless ecosystem, decoupling components is key to building scalable and reliable systems. Amazon Simple Queue Service (SQS) is AWS’s fully managed message queuing solution that helps you do just that. Whether you’re handling task queues, async workflows, or buffering requests, SQS ensures your messages are stored and processed reliably.</p><h3>Core Workflow of Amazon SQS</h3><h4>Architecture Overview:</h4><ol><li><strong>Producers</strong> send messages to an SQS queue.</li><li><strong>SQS</strong> stores messages securely and durably across multiple Availability Zones.</li><li><strong>Consumers</strong> retrieve and process messages.</li><li><strong>Dead-letter Queue</strong> : captures failed messages after max retries.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KFzabloa4vTTYuaUGyzriA.png" /></figure><h3>Amazon SQS Queue Types</h3><h4>Standard Queue</h4><ul><li><strong>Delivery:</strong> At-least-once</li><li><strong>Order:</strong> Best-effort</li><li><strong>Throughput:</strong> Nearly unlimited</li><li><strong>Duplicates:</strong> Possible</li><li><strong>Use Case:</strong> High-throughput background jobs like logging, metrics, chat messages</li></ul><h4>FIFO (First-In-First-Out) Queue</h4><ul><li><strong>Delivery:</strong> Exactly-once</li><li><strong>Order:</strong> Guaranteed</li><li><strong>Throughput:</strong> 3000 msg/sec (with batching)</li><li><strong>Duplicates:</strong> Not allowed</li><li><strong>Use Case:</strong> Transactional workflows, payment processing, order fulfillment</li></ul><h3>Key Concepts</h3><h4>Visibility Timeout</h4><p>After a message is received, it’s hidden for a time (default: 30s). If the consumer doesn’t delete it within that time, it becomes visible again for reprocessing.</p><h4>Dead-letter Queues (DLQs)</h4><p>When a message fails to be processed after a set number of retries, it moves to the DLQ. This helps isolate and debug failed processes.</p><h4>Long Polling</h4><p>Instead of checking the queue every few seconds (short polling), long polling waits (up to 20s) until a message becomes available. This reduces API calls and improves efficiency.</p><h3>Delivery Guarantees &amp; Scaling</h3><p>Amazon SQS supports two queue types: Standard and FIFO, which differ across multiple dimensions:</p><p><strong>Delivery Guarantee</strong>: Standard queues offer at-least-once delivery, meaning a message may be delivered more than once. FIFO queues provide exactly-once delivery.</p><p><strong>Message Order</strong>: Standard queues do not preserve the order of messages. FIFO queues strictly preserve the order.</p><p><strong>Throughput:</strong> Standard queues offer nearly unlimited throughput, while FIFO queues are limited to about 3000 messages/second with batching.</p><p><strong>Duplication Handling: </strong>In standard queues, duplication must be handled by the client. FIFO queues manage it automatically.</p><p><strong>Durability: </strong>Messages are stored across multiple AZs.</p><p><strong>Retention Period: </strong>1 minute — 14 days (default: 4 days).</p><h3>Pros &amp; Cons</h3><h3>Standard Queue</h3><p><strong>Pros:</strong></p><ul><li>High throughput</li><li>Scalable</li><li>Lower cost</li></ul><p><strong>Cons:</strong></p><ul><li>Potential message duplication</li><li>No strict order guarantee</li></ul><h3>FIFO Queue</h3><p><strong>Pros:</strong></p><ul><li>Strict message order</li><li>Exactly-once processing</li></ul><p><strong>Cons:</strong></p><ul><li>Lower throughput</li><li>Higher complexity &amp; cost</li></ul><h3>Use Case Recommendation</h3><ul><li><strong>Standard SQS</strong> for high-speed, non-order dependent tasks like real-time notifications or logging.</li><li><strong>FIFO SQS</strong> when order and duplication control are critical, such as order processing or inventory updates.</li></ul><h3>Python Demo Snippet</h3><pre>import boto3<br><br>sqs = boto3.client(&#39;sqs&#39;)<br>queue_url = &#39;https://sqs.us-east-1.amazonaws.com/123456789012/my-queue&#39;<br><br># Send a message<br>sqs.send_message(QueueUrl=queue_url, MessageBody=&#39;Hello from Python!&#39;)<br><br># Receive a message<br>response = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=1, WaitTimeSeconds=10)<br>message = response[&#39;Messages&#39;][0]<br>receipt_handle = message[&#39;ReceiptHandle&#39;]<br><br># Delete the message<br>sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=receipt_handle)</pre><h3>Diagram: Data Flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0yFq06C3IHlPx4vrs5wXMA.png" /></figure><h3>Conclusion</h3><p>Amazon SQS is a powerful tool in your AWS toolkit. Whether you’re designing scalable microservices or building resilient background processes, mastering SQS can help you build for the future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5256e15d7919" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>