<?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"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://programminguides.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 19 Jun 2026 08:36:55 GMT</lastBuildDate><atom:link href="https://programminguides.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How We Built a Scalable Log Analytics Platform with OpenSearch]]></title><description><![CDATA[Introduction
OpenSearch is a powerful, open-source search and analytics suite derived from Elasticsearch 7.10 and Kibana 7.10, developed and maintained by the OpenSearch Project, originally started by Amazon Web Services (AWS). It enables developers ...]]></description><link>https://programminguides.hashnode.dev/how-we-built-a-scalable-log-analytics-platform-with-opensearch</link><guid isPermaLink="true">https://programminguides.hashnode.dev/how-we-built-a-scalable-log-analytics-platform-with-opensearch</guid><category><![CDATA[opensearch]]></category><category><![CDATA[System Design]]></category><category><![CDATA[distributed system]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Sun, 05 Oct 2025 13:16:09 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p><em>OpenSearch is a powerful, open-source search and analytics suite derived from Elasticsearch 7.10 and Kibana 7.10, developed and maintained by the OpenSearch Project, originally started by Amazon Web Services (AWS). It enables developers and organizations to ingest, search, analyze, and visualize large volumes of data in near real-time.</em></p>
<p><em>Designed to be scalable, extensible, and vendor-neutral, OpenSearch supports a wide range of use cases such as log analytics, application monitoring, and business intelligence. With its hot-warm-cold architecture, it also offers cost-effective storage tiering for managing large data volumes across different performance levels.</em></p>
<h2 id="heading-advantages-of-using-opensearch">Advantages of Using OpenSearch</h2>
<ol>
<li><p><em>Multi-AZ Deployment Support. Data is replicated across AZs to ensure resilience during infrastructure failures.</em></p>
</li>
<li><p><em>Index Lifecycle Management: Time &amp; Size-Based Splitting. Prevents index bloat and improves search performance.</em></p>
</li>
<li><p><em>Automatic Snapshots. Supports scheduled snapshots of indices to remote storage (like Amazon S3).</em></p>
</li>
<li><p><em>Rich Visualization with OpenSearch Dashboards</em></p>
</li>
<li><p><em>Hot-Warm-Cold Index Tiering with Retention Policies</em></p>
</li>
</ol>
<h2 id="heading-opensearch-storage-tiers">OpenSearch Storage Tiers</h2>
<h3 id="heading-purpose">Purpose</h3>
<ol>
<li><p><em>Master Node: Manages the cluster metadata and state.</em></p>
</li>
<li><p><em>Data Node: Stores and processes frequently accessed and actively written data.</em></p>
</li>
<li><p><em>Warm Node: Stores less frequently accessed but still queryable data.</em></p>
</li>
<li><p><em>Cold Node: Stores rarely accessed, long-term archival data.</em></p>
</li>
</ol>
<h3 id="heading-responsibilities"><strong>Responsibilities</strong></h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong><em>Master Node</em></strong></td><td><strong><em>Data Node</em></strong></td><td><strong><em>Warm Node</em></strong></td><td><strong><em>Cold Node</em></strong></td></tr>
</thead>
<tbody>
<tr>
<td><em>Creating/deleting indices</em></td><td><em>Real-time indexing and querying</em></td><td><em>Older logs (e.g., 1 week–1 month old)</em></td><td><em>Historical logs (e.g., several months or years old)</em></td></tr>
<tr>
<td><em>Tracking all nodes in the cluster</em></td><td><em>Recent logs, current metrics, etc.</em></td><td><em>Read-only Node</em></td><td><em>Read &amp; Write not possible until &amp; unless moved to Data or Warm Node.</em></td></tr>
<tr>
<td><em>Managing shard allocation</em></td><td><em>Active writes happen in this Node.</em></td><td><em>-</em></td><td><em>-</em></td></tr>
<tr>
<td><em>Electing new master if the current one fails</em></td><td><em>-</em></td><td><em>-</em></td><td><em>-</em></td></tr>
</tbody>
</table>
</div><h3 id="heading-characteristics">Characteristics</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong><em>Master Node</em></strong></td><td><strong><em>Data Node</em></strong></td><td><strong><em>Warm Node</em></strong></td><td><strong><em>Cold Node</em></strong></td></tr>
</thead>
<tbody>
<tr>
<td><em>Does not store data or handle search/index requests directly (though it can if configured)</em></td><td><em>High-performance storage (e.g., SSDs)</em></td><td><em>Larger but slower disks (e.g., HDDs or lower-tier SSDs)</em></td><td><em>Very inexpensive storage (e.g., large HDDs)</em></td></tr>
<tr>
<td><em>Typically kept dedicated and lightweight</em></td><td><em>More CPU and RAM to handle fast operations</em></td><td><em>Less CPU-intensive than hot nodes</em></td><td><em>Minimal compute resources</em></td></tr>
<tr>
<td><em>Usually a small number of them (odd number, like 3, for quorum)</em></td><td><em>Fast response time for queries and aggregations</em></td><td><em>Cheaper infrastructure</em></td><td><em>-</em></td></tr>
<tr>
<td><em>-</em></td><td><em>Generally expensive but fast hardware</em></td><td><em>Slower response time is acceptable</em></td><td><em>-</em></td></tr>
</tbody>
</table>
</div><h2 id="heading-what-is-an-index-in-opensearch">What Is an Index in OpenSearch</h2>
<p><em>In OpenSearch, an index is a logical namespace where documents are stored. You can think of an index as a database table in a relational system:</em></p>
<ul>
<li><p><em>Each index contains documents, and each document is a JSON object.</em></p>
</li>
<li><p><em>Documents are stored in shards, which are distributed across the cluster’s data nodes.</em></p>
</li>
<li><p><em>Indexes support full-text search, filtering, aggregation, and more.</em></p>
</li>
</ul>
<h3 id="heading-what-are-hot-warm-and-cold-indices">What Are Hot, Warm, and Cold Indices</h3>
<p><em>Hot, warm, and cold indices refer to data lifecycle stages and where data is physically stored based on its age, access pattern, and importance. This approach helps balance cost vs performance.</em></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong><em>Hot Index</em></strong></td><td><strong><em>Warm Index</em></strong></td><td><strong><em>Cold Index</em></strong></td></tr>
</thead>
<tbody>
<tr>
<td><em>Actively written to and frequently queried.</em></td><td><em>No longer written to but still queried occasionally.</em></td><td><em>Rarely accessed but stored for compliance or archival.</em></td></tr>
<tr>
<td><em>Stored on: Hot data nodes (fast SSDs, high-performance)</em></td><td><em>Stored on: Warm nodes (cheaper, slower storage)</em></td><td><em>Stored on: Cold nodes (very cheap, slow storage)</em></td></tr>
</tbody>
</table>
</div><h3 id="heading-what-is-an-alias-in-opensearch">What Is an Alias in OpenSearch</h3>
<p><em>An</em> <strong><em>alias</em></strong> <em>in OpenSearch is like a</em> <strong><em>pointer or virtual name</em></strong> <em>that refers to one or more indices. Instead of interacting with an index directly (e.g.,</em> <code>logs-2025.06.16</code><em>), clients can read from or write to an</em> <strong><em>alias</em></strong> <em>like</em> <code>logs-current</code><em>.</em></p>
<p><strong>Key Concepts of Aliases:</strong></p>
<ol>
<li><p><em>Aliases Can point to one or more indices</em></p>
</li>
<li><p><em>Aliases Can be used for search (read) and indexing (write) operations</em></p>
</li>
<li><p><em>An alias used for writing must point to exactly one index.</em></p>
<ol>
<li><p><em>Common pattern: logs-write → logs-000001</em></p>
</li>
<li><p><em>When you roll over, the alias is updated to point to a new index, e.g. logs-000002.</em></p>
</li>
</ol>
</li>
<li><p><em>Read (search) aliases can point to one or many indices.</em></p>
<ol>
<li><p><em>Example: An alias logs-read could point to logs-000001, logs-000002, and so on.</em></p>
</li>
<li><p><em>This allows you to search across a series of indices seamlessly.</em></p>
</li>
</ol>
</li>
</ol>
<h2 id="heading-managing-index-growth-with-rollover-in-opensearch">Managing Index Growth with Rollover in OpenSearch</h2>
<h3 id="heading-what-is-rollover-in-opensearch">What Is <strong>Rollover</strong> in OpenSearch</h3>
<p><em>In OpenSearch, a rollover is the process of automatically creating a new index when the current index reaches a certain threshold, such as:</em></p>
<ul>
<li><p><em>Maximum size (e.g., 50GB)</em></p>
</li>
<li><p><em>Maximum number of documents (e.g., 1 million)</em></p>
</li>
<li><p><em>Maximum age (e.g., 1 day old)</em></p>
</li>
</ul>
<h3 id="heading-why-use-rollover">Why Use Rollover</h3>
<ul>
<li><p><em>Avoid performance degradation in large indices</em></p>
</li>
<li><p><em>Split data efficiently across smaller, manageable indices</em></p>
</li>
<li><p><em>Work with an alias, so applications always write to the current active index</em></p>
</li>
</ul>
<h3 id="heading-how-it-works">How It Works</h3>
<ul>
<li><p><em>You define a write alias, such as</em> <code>logs-write</code><em>.</em></p>
</li>
<li><p><em>The alias points to an index, e.g.,</em> <code>logs-000001</code><em>.</em></p>
</li>
<li><p><em>You set rollover conditions (e.g.,</em> <code>max_age: 1d</code><em>,</em> <code>max_size: 50gb</code><em>).</em></p>
</li>
<li><p><em>When conditions are met, a new index is created:</em> <code>logs-000002</code><em>.</em></p>
</li>
<li><p><em>The alias</em> <code>logs-write</code> <em>is automatically updated to point to the new index.</em></p>
</li>
</ul>
<h2 id="heading-understanding-shards-in-opensearch">Understanding Shards in OpenSearch</h2>
<p><em>In OpenSearch, shards are the fundamental units of data storage and processing. Each index is split into shards so that large datasets can be distributed across multiple nodes, enabling parallel operations.</em></p>
<p><em>A shard is a low-level, self-contained slice of an index that stores a portion of the data. Shards allow OpenSearch to:</em></p>
<ul>
<li><p><em>Scale horizontally across multiple servers (nodes)</em></p>
</li>
<li><p><em>Handle large volumes of data</em></p>
</li>
<li><p><em>Perform parallel queries for faster response times</em></p>
</li>
</ul>
<h3 id="heading-primary-shard">Primary Shard</h3>
<ul>
<li><p><em>Holds the original copy of documents.</em></p>
</li>
<li><p><em>Every index operation (create/update/delete) happens on the primary shard first.</em></p>
</li>
</ul>
<h3 id="heading-replica-shard">Replica Shard</h3>
<ul>
<li><p><em>Acts as a copy of a primary shard.</em></p>
</li>
<li><p><em>Provides failover protection: if the primary shard or its node fails, a replica can be promoted.</em></p>
</li>
</ul>
<h3 id="heading-what-does-52-replication-strategy-mean">What Does 5:2 Replication Strategy Mean</h3>
<p><em>The 5:2 replication strategy is a common shard configuration in OpenSearch. It refers to:</em></p>
<ul>
<li><p><em>5 primary shards</em></p>
</li>
<li><p><em>2 replica shards per primary</em></p>
</li>
<li><p><em>Breakdown:</em></p>
<ul>
<li><p><em>Primary Shards: 5</em></p>
</li>
<li><p><em>Replica Shards: 5 × 2 = 10</em></p>
</li>
<li><p><em>Total Shards: 15</em></p>
</li>
</ul>
</li>
</ul>
<p><em>Sharding and replication are critical to OpenSearch’s ability to scale and remain highly available. By understanding how primary and replica shards work—and how to use strategies like 5:2 replication—you can design clusters that are both efficient and resilient.</em></p>
<h2 id="heading-snapshots-in-opensearch">Snapshots in OpenSearch</h2>
<p><em>A snapshot in OpenSearch is a backup of your index or cluster metadata and data. It captures the state of indices at a given point in time and allows you to restore that data if needed. Think of snapshots as point-in-time backups that are incremental (efficient) and stored externally—typically on object storage like Amazon S3.</em></p>
<ul>
<li><p><strong><em>Disaster Recovery</em></strong> <em>– Restore lost data in case of accidental deletion or system failure.</em></p>
</li>
<li><p><strong><em>Cluster Migration</em></strong> <em>– Move data from one cluster to another.</em></p>
</li>
<li><p><strong><em>Rolling Back Changes</em></strong> <em>– Revert to a previous state after unwanted modifications.</em></p>
</li>
</ul>
<h3 id="heading-how-snapshots-work">How Snapshots Work</h3>
<ol>
<li><p><em>Snapshot Repository: Before taking snapshots, you need to register a snapshot repository, which is the storage location for backups like s3 bucket.</em></p>
</li>
<li><p><em>Snapshot Process: Snapshots are incremental, meaning only new or changed data is backed up after the first snapshot.</em></p>
</li>
<li><p><em>Restore: You can restore a snapshot at any time to: a new index, the original index, a new cluster (for migration).</em></p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Dissecting Apache Kafka]]></title><description><![CDATA[Introduction to Kafka: The Need for a Distributed Messaging System
Kafka is a distributed messaging system that plays a crucial role in modern data pipelines by addressing the need for high-throughput, low-latency communication between different serv...]]></description><link>https://programminguides.hashnode.dev/dissecting-apache-kafka</link><guid isPermaLink="true">https://programminguides.hashnode.dev/dissecting-apache-kafka</guid><category><![CDATA[kafka]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Thu, 24 Apr 2025 10:44:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/ZAbIO5eas9Q/upload/534ca22816d8c09188373a3af1a6df50.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction-to-kafka-the-need-for-a-distributed-messaging-system">Introduction to Kafka: The Need for a Distributed Messaging System</h2>
<p><em>Kafka is a distributed messaging system that plays a crucial role in modern data pipelines by addressing the need for high-throughput, low-latency communication between different services in a scalable and fault-tolerant manner. Traditional messaging systems, such as RabbitMQ and JMS, while capable, often struggle with scaling to handle large volumes of data or ensuring data consistency across distributed systems.</em></p>
<p><em>Kafka overcomes these challenges by enabling real-time streaming and providing a durable, distributed event log that acts as a central data hub for all services. It is particularly well-suited for use cases such as log aggregation, stream processing, and event sourcing, where data must be collected, processed, and consumed in real time. Kafka’s ecosystem is designed to provide a seamless experience for managing large-scale data flows.</em></p>
<p><em>This includes Kafka itself, which handles message brokering, ZooKeeper (or KRaft in newer versions) for managing the Kafka cluster, Kafka Connect for integrating external systems, and Kafka Streams for stream processing and real-time analytics. Together, these components create a powerful platform for building scalable, resilient, and fault-tolerant data-driven applications.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745402729992/74ef2047-5e8e-4227-8b2a-2b26e5f11a41.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-kafka-cluster-brokers-topics-and-partitions-the-backbone">Kafka Cluster / Brokers, Topics, and Partitions — The Backbone</h2>
<ul>
<li><p><em>Broker / Cluster: A Kafka server. Each broker handles part of the data load.</em></p>
</li>
<li><p><em>Topic: Logical channel to which producers send messages and consumers read from.</em></p>
</li>
<li><p><em>Partition: Unit of parallelism within a topic. Each message goes to one partition.</em></p>
</li>
<li><p><em>Replication: For each partition, Kafka can create multiple replicas (one leader + followers) for high availability.</em></p>
</li>
</ul>
<h2 id="heading-kafka-data-flow-from-producer-to-consumer">Kafka Data Flow — From Producer to Consumer</h2>
<ol>
<li><p><em>Producer sends a record to a topic.</em></p>
</li>
<li><p><em>Kafka determines the partition:</em></p>
<ul>
<li><p><em>If a key is provided → Hash(key) % partitions.</em></p>
</li>
<li><p><em>If no key → Round-robin or custom partitioner.</em></p>
</li>
</ul>
</li>
<li><p><em>The record is stored sequentially in the target partition's log.</em></p>
</li>
<li><p><em>Leader broker of that partition writes the record, then replicates it to follower brokers.</em></p>
</li>
<li><p><em>Consumers in a consumer group fetch records from their assigned partitions.</em></p>
</li>
<li><p><em>Kafka tracks the offset each consumer has read. (Like a bookmark in a book.)</em></p>
</li>
</ol>
<h2 id="heading-kafka-partitions-scaling-increasing-and-decreasing">Kafka Partitions — Scaling, Increasing, and Decreasing</h2>
<p><em>A partition in Kafka is a fundamental unit of parallelism and storage. Each topic can be divided into multiple partitions, which helps distribute records across them. Partitions allow Kafka to scale and handle large volumes of data efficiently, enabling high throughput and parallel processing.</em></p>
<p><em>When it comes to increasing the number of partitions for an existing topic, Kafka supports this operation after topic creation. This can offer several benefits:</em></p>
<ul>
<li><p><em>Improved parallelism: More partitions mean more consumers can read from the topic concurrently.</em></p>
</li>
<li><p><em>Higher throughput: Producers and consumers can scale independently of each other.</em></p>
</li>
</ul>
<p><em>The partition count directly impacts consumer behavior within a consumer group. Here’s how different scenarios play out:</em></p>
<ul>
<li><p><em>With 3 partitions and 2 consumers, one consumer will handle 2 partitions, and the other will handle 1.</em></p>
</li>
<li><p><em>With 3 partitions and 3 consumers, the partitions will be evenly distributed (one-to-one mapping).</em></p>
</li>
<li><p><em>With 3 partitions and 4 consumers, one consumer will remain idle since there are more consumers than partitions.</em></p>
</li>
<li><p><em>With 3 partitions and 1 consumer, that single consumer will handle all 3 partitions.</em></p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745403116589/58831f2f-15b9-40cb-a598-8d257d9b6eed.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-kafka-consumer-group-offset-polling-and-auto-commit-explained">Kafka Consumer Group, Offset, Polling, and Auto-Commit Explained</h2>
<h3 id="heading-consumer-group-concept">Consumer Group Concept</h3>
<p><em>A consumer group in Kafka is a logical grouping of consumers that work together to consume messages from a topic. Each consumer in a group is assigned a subset of partitions, ensuring that each message is processed only once per group.</em></p>
<ul>
<li><p><em>Each partition is consumed by exactly one consumer in a group.</em></p>
</li>
<li><p><em>Multiple groups can independently consume the same topic without interfering with each other.</em></p>
</li>
</ul>
<h3 id="heading-offset-in-kafka">Offset in Kafka</h3>
<p><em>Kafka tracks the offset, which is the position of a consumer in a partition — essentially a pointer to the message being read.</em></p>
<ul>
<li><p><em>Offsets are maintained per partition per consumer group.</em></p>
</li>
<li><p><em>By default, offsets are stored in an internal Kafka topic:</em> <code>__consumer_offsets</code><em>.</em></p>
</li>
</ul>
<p><em>You can configure how offsets are committed using auto-commit or manual commit modes.</em></p>
<h3 id="heading-auto-commit">Auto-Commit</h3>
<p><em>Kafka consumers can be configured to automatically commit offsets at regular intervals using:</em></p>
<p><em>This tells the consumer to commit the latest offset after every 5 seconds (by default). While convenient, it may lead to message loss if the consumer fails after receiving a message but before processing it.</em></p>
<h3 id="heading-manual-offset-commit">Manual Offset Commit</h3>
<p><em>More robust approach: manually committing the offset after processing a message. This ensures at-least-once delivery.</em></p>
<h3 id="heading-poll-interval">Poll Interval</h3>
<p><em>Kafka consumers use the</em> <code>poll()</code> <em>method to request messages from the broker. You must poll regularly — otherwise, Kafka considers the consumer as dead and triggers a rebalance.</em></p>
<p><em>If the consumer doesn't poll within this time, it's removed from the group, and partitions are reassigned.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745403816331/0f731783-8e3b-42d5-9f34-8ba3b1956f62.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-rebalancing-in-kafka-why-it-happens-and-how-it-affects-consumers">Rebalancing in Kafka: Why It Happens and How It Affects Consumers</h2>
<p><em>Rebalancing in Kafka is the process of redistributing partitions among consumers within a consumer group. It is triggered when there are changes in the group, such as a consumer joining or leaving, a topic being added or modified, or consumers failing to poll within a set interval.</em></p>
<p><em>This process temporarily pauses consumption as Kafka stops message delivery, reassigns partition ownership, and resumes once the new assignments are in place. While necessary for load balancing, rebalancing can cause latency or downtime, especially with stateful or slow-to-rejoin consumers.</em></p>
<p><em>Internally, Kafka handles rebalancing through:</em></p>
<ul>
<li><p><em>Coordinator election to manage the group,</em></p>
</li>
<li><p><em>Partition assignment using strategies like range, round-robin, or sticky,</em></p>
</li>
<li><p><em>Offset fetching to resume processing,</em></p>
</li>
<li><p><em>Consumer resumption from newly assigned partitions.</em></p>
</li>
</ul>
<p><em>Example: In a group with 4 partitions and 2 consumers, each consumer handles 2 partitions. If a third joins, Kafka rebalances to spread partitions across all three.</em></p>
<p><em>To minimize disruption from rebalancing:</em></p>
<ul>
<li><p><em>Use sticky assignment to reduce reshuffling.</em></p>
</li>
<li><p><em>Adjust session.timeout.ms and heartbeat.interval.ms for better tolerance.</em></p>
</li>
<li><p><em>Avoid frequent consumer churn.</em></p>
</li>
<li><p><em>Use cooperative rebalancing for smoother transitions (available in newer Kafka versions).</em></p>
</li>
</ul>
<h2 id="heading-leader-and-replica-in-kafka-high-availability-through-replication">Leader and Replica in Kafka: High Availability Through Replication</h2>
<p><em>In Kafka, every partition of a topic is replicated across multiple brokers to ensure fault tolerance and high availability.</em></p>
<ul>
<li><p><em>Leader Replica: Handles all read and write requests for the partition.</em></p>
</li>
<li><p><em>Follower Replicas: Passive replicas that copy data from the leader.</em></p>
</li>
</ul>
<p><em>Only one broker at a time is the leader for a given partition. The remaining replicas are known as followers.</em></p>
<p><em>Let’s say you have a topic</em> <code>SendEmailQueue</code> <em>with 3 partitions and a replication factor of 3:</em></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><em>Partition</em></td><td><em>Leader Broker</em></td><td><em>Follower Brokers</em></td></tr>
</thead>
<tbody>
<tr>
<td><em>P0</em></td><td><em>Broker 1</em></td><td><em>Broker 2, 3</em></td></tr>
<tr>
<td><em>P1</em></td><td><em>Broker 2</em></td><td><em>Broker 1, 3</em></td></tr>
<tr>
<td><em>P2</em></td><td><em>Broker 3</em></td><td><em>Broker 1, 2</em></td></tr>
</tbody>
</table>
</div><p><em>Each broker is leading one partition and following two others.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745489254157/1229d585-6109-4183-ac52-4e90dacf5ae5.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-what-happens-if-leader-fails">What Happens If Leader Fails?</h3>
<p><em>If a leader replica fails, Kafka elects a new leader from the ISR. If no replica is in sync, Kafka will wait until at least one follower catches up — unless</em> <code>unclean.leader.election</code> <em>is enabled (not recommended in production).</em></p>
<h2 id="heading-frequently-asked-questions-faqs-about-kafka">Frequently Asked Questions (FAQs) About Kafka</h2>
<p><em>Kafka can be a complex system to understand, especially when you are first diving into its various components and concepts. Here are some of the most frequently asked questions that can help clarify common doubts about Kafka.</em></p>
<h3 id="heading-what-is-the-difference-between-kafka-and-a-traditional-messaging-queue-like-rabbitmq"><strong>What is the difference between Kafka and a traditional messaging queue like RabbitMQ?</strong></h3>
<p><em>Kafka and RabbitMQ are both message brokers, but they have different use cases and design principles:</em></p>
<ul>
<li><p><em>Kafka is designed for high throughput and distributed data streaming. It stores messages in topics and partitions, and consumers can read messages at their own pace, replaying them if needed.</em></p>
</li>
<li><p><em>RabbitMQ is more focused on messaging between services with high reliability and flexible routing patterns. It uses queues for message delivery and is designed for scenarios requiring complex routing and guarantees like exactly-once or at-least-once delivery.</em></p>
</li>
</ul>
<p><em>Kafka is generally more suited for log aggregation, stream processing, and big data use cases, while RabbitMQ is preferred for traditional messaging with complex patterns like RPC or pub/sub.</em></p>
<h3 id="heading-what-happens-if-a-kafka-broker-goes-down"><strong>What happens if a Kafka broker goes down?</strong></h3>
<p><em>Kafka has built-in fault tolerance. When a broker goes down, the replicas of the partitions stored on that broker become available through other brokers. Kafka uses the concept of replicas and leader-follower architecture to ensure no data is lost:</em></p>
<ul>
<li><p><em>The leader replica for each partition will handle read and write operations.</em></p>
</li>
<li><p><em>The follower replicas replicate the leader’s data.</em></p>
</li>
</ul>
<p><em>If a leader replica is lost due to a broker failure, Kafka will automatically elect a new leader from the available followers. However, if there are no available replicas, the partition may become unavailable until the broker recovers.</em></p>
<h3 id="heading-what-is-a-kafka-consumer-group"><strong>What is a Kafka Consumer Group?</strong></h3>
<p><em>A Consumer Group is a group of consumers that work together to consume messages from one or more topics. Kafka ensures that each partition in a topic is consumed by only one consumer within a group. Consumer groups provide scalability and fault tolerance by distributing partition consumption across multiple consumers.</em></p>
<ul>
<li><p><em>If a consumer fails, other consumers in the group can pick up the partitions the failed consumer was consuming.</em></p>
</li>
<li><p><em>Consumer groups allow parallel processing of messages, and each message will only be processed once by a single consumer within the group.</em></p>
</li>
</ul>
<h3 id="heading-what-are-kafka-topics-and-partitions"><strong>What are Kafka Topics and Partitions?</strong></h3>
<ul>
<li><p><em>Topics are logical channels to which producers publish messages and from which consumers consume messages. Topics can be thought of as message categories.</em></p>
</li>
<li><p><em>Partitions are the physical storage units within a topic. A topic can have multiple partitions, and messages within a partition are ordered. Partitions enable Kafka to scale horizontally by allowing parallel reads and writes.</em></p>
</li>
</ul>
<p><em>Each partition can only be consumed by one consumer at a time in a consumer group, and messages in partitions are stored in offsets that consumers can track.</em></p>
<h3 id="heading-how-does-kafka-guarantee-message-order"><strong>How does Kafka guarantee message order?</strong></h3>
<p><em>Kafka guarantees message order at the partition level, not across the entire topic. Within a single partition, messages are ordered based on the order in which they were produced. The partition key determines how messages are distributed across partitions:</em></p>
<ul>
<li><em>If you want to preserve message order for a specific key, ensure that all messages with the same key are sent to the same partition.</em></li>
</ul>
<p><em>However, Kafka does not guarantee order across different partitions within a topic.</em></p>
<h3 id="heading-how-does-kafka-handle-message-retention"><strong>How does Kafka handle message retention?</strong></h3>
<p><em>Kafka has a retention policy that controls how long messages are stored in a topic. There are two main retention mechanisms:</em></p>
<ul>
<li><p><em>Time-based retention: Messages are retained for a specified period, after which they are deleted.</em></p>
</li>
<li><p><em>Size-based retention: Kafka deletes messages when a topic reaches a specified size limit.</em></p>
</li>
</ul>
<p><em>Once messages are deleted, they are no longer available for consumption, but they can be replayed as long as they are within the retention window.</em></p>
<h3 id="heading-what-is-kafka-consumer-lag"><strong>What is Kafka Consumer Lag?</strong></h3>
<p><em>Consumer lag refers to the difference between the latest offset (the last message produced) and the current offset (the last message consumed) for a consumer group in a partition. Lag occurs when consumers are behind in processing messages.</em></p>
<ul>
<li><p><em>High lag indicates that consumers are not keeping up with the rate of incoming messages.</em></p>
</li>
<li><p><em>Kafka provides monitoring tools to track lag, and it’s important to ensure that lag remains low for timely processing.</em></p>
</li>
</ul>
<h3 id="heading-how-do-kafka-producers-ensure-data-durability"><strong>How do Kafka Producers ensure data durability?</strong></h3>
<p><em>Kafka producers ensure durability through the acknowledgment mechanism:</em></p>
<ul>
<li><p><em>acks=0: The producer does not wait for any acknowledgment from the broker. This is faster but less reliable.</em></p>
</li>
<li><p><em>acks=1: The producer waits for acknowledgment from the leader broker. This ensures that the message is written to at least one broker.</em></p>
</li>
<li><p><em>acks=all: The producer waits for acknowledgment from all in-sync replicas. This provides the highest durability but may impact performance.</em></p>
</li>
</ul>
<h3 id="heading-what-is-kafkas-exactly-once-semantics-eos"><strong>What is Kafka's Exactly-Once Semantics (EOS)?</strong></h3>
<p><em>Kafka provides exactly-once semantics (EOS) to ensure that a message is neither lost nor duplicated during processing. EOS is achieved by:</em></p>
<ul>
<li><p><em>Idempotent Producers: Producers are idempotent, meaning that even if they send the same message multiple times, it will only be written once to the topic.</em></p>
</li>
<li><p><em>Transactional Producers and Consumers: Kafka supports transactions that allow producers to send messages as part of a single atomic operation. Consumers that process messages in a transaction can ensure that only one message is consumed, even in the case of retries.</em></p>
</li>
</ul>
<h3 id="heading-can-i-change-the-number-of-partitions-in-kafka"><strong>Can I change the number of partitions in Kafka?</strong></h3>
<p><em>Yes, you can increase the number of partitions in Kafka, but it is not possible to decrease the number of partitions. Increasing partitions allows Kafka to scale horizontally, distributing the load across more consumers.</em></p>
<p><em>However, adding partitions can disrupt consumer offset tracking because Kafka reassigns partitions to consumers. It’s important to handle rebalancing and consumer offsets carefully.</em></p>
<h3 id="heading-what-is-the-difference-between-kafka-console-consumer-and-kafka-console-producer"><strong>What is the difference between</strong> <code>kafka-console-consumer</code> and <code>kafka-console-producer</code>?</h3>
<ul>
<li><p><code>kafka-console-consumer</code> <em>is a command-line tool that allows you to consume messages from a Kafka topic.</em></p>
</li>
<li><p><code>kafka-console-producer</code> <em>is a command-line tool that allows you to produce messages to a Kafka topic.</em></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Spring Security in Spring Boot — A Complete Beginner's Guide]]></title><description><![CDATA[Introduction
In this guide, we’ll walk through how to set up Spring Security in a Spring Boot application using a custom user model, connect it to a PostgreSQL database, create a signup endpoint, encrypt passwords with BCrypt, and authenticate users ...]]></description><link>https://programminguides.hashnode.dev/spring-security-in-spring-boot-a-complete-beginners-guide</link><guid isPermaLink="true">https://programminguides.hashnode.dev/spring-security-in-spring-boot-a-complete-beginners-guide</guid><category><![CDATA[Java]]></category><category><![CDATA[Springboot]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Sun, 13 Apr 2025 16:20:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/4Tu-sIOXeA0/upload/7f243dffff08987fd553a9f214117d4f.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction</h3>
<p>In this guide, we’ll walk through how to set up Spring Security in a Spring Boot application using a custom user model, connect it to a PostgreSQL database, create a signup endpoint, encrypt passwords with BCrypt, and authenticate users securely.</p>
<p>We’ll cover:</p>
<ul>
<li><p>Required dependencies</p>
</li>
<li><p>PostgreSQL configuration in <code>application.properties</code></p>
</li>
<li><p>Creating user model and repository</p>
</li>
<li><p>Writing controller for signup</p>
</li>
<li><p>Creating a custom user detail service</p>
</li>
<li><p>Configuring Spring Security</p>
</li>
<li><p>Lifecycle of authentication in Spring Security</p>
</li>
</ul>
<h3 id="heading-required-dependencies-in-pomxml">Required Dependencies in <code>pom.xml</code></h3>
<p>Before we start writing code, let's first discuss the <strong>dependencies</strong> that need to be included in the project. These dependencies will provide us with essential tools to handle database interactions, web services, and Spring Security.</p>
<h4 id="heading-spring-boot-starter-data-jpa"><strong>Spring Boot Starter Data JPA</strong></h4>
<pre><code class="lang-java">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
<ul>
<li><p><strong>Purpose</strong>: This dependency is required to work with databases using <strong>JPA</strong> (Java Persistence API). It enables Spring Data JPA support and helps us interact with the PostgreSQL database seamlessly.</p>
</li>
<li><p><strong>Benefit</strong>: With Spring Data JPA, we don’t have to write SQL queries manually. It provides powerful query capabilities and automatic entity mapping.</p>
</li>
</ul>
<h4 id="heading-spring-boot-starter-web"><strong>Spring Boot Starter Web</strong></h4>
<pre><code class="lang-java">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
<ul>
<li><p><strong>Purpose</strong>: This dependency adds everything needed to build web applications, including RESTful web services. It integrates Spring MVC, Tomcat as the default container, and Jackson for JSON binding.</p>
</li>
<li><p><strong>Benefit</strong>: It allows us to create REST APIs and interact with users using HTTP requests (like GET, POST).</p>
</li>
</ul>
<h4 id="heading-spring-boot-devtools"><strong>Spring Boot DevTools</strong></h4>
<pre><code class="lang-java">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
    &lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
</code></pre>
<ul>
<li><p><strong>Purpose</strong>: DevTools enhances the development experience by providing features like <strong>auto-restart</strong> and <strong>live reload</strong>. It automatically restarts the application when you make changes to the code, which saves time.</p>
</li>
<li><p><strong>Benefit</strong>: It allows faster development cycles by reloading your application when you modify code, making it easier to see changes immediately.</p>
</li>
</ul>
<h4 id="heading-spring-boot-starter-security"><strong>Spring Boot Starter Security</strong></h4>
<pre><code class="lang-java">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
<ul>
<li><p><strong>Purpose</strong>: This dependency brings <strong>Spring Security</strong> into the project. Spring Security is a powerful and customizable authentication and access-control framework.</p>
</li>
<li><p><strong>Benefit</strong>: With Spring Security, we can easily configure authentication, authorization, and protect APIs from unauthorized access.</p>
</li>
</ul>
<h4 id="heading-postgresql-driver"><strong>PostgreSQL Driver</strong></h4>
<pre><code class="lang-java">xmlCopyEdit&lt;dependency&gt;
    &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
    &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
<ul>
<li><p><strong>Purpose</strong>: This dependency is required to <strong>connect Spring Boot to PostgreSQL</strong>. It provides the necessary JDBC driver to interact with PostgreSQL databases.</p>
</li>
<li><p><strong>Benefit</strong>: It allows the Spring Boot application to connect to a PostgreSQL database to store and retrieve data.</p>
</li>
</ul>
<h4 id="heading-lombok"><strong>Lombok</strong></h4>
<pre><code class="lang-java">&lt;dependency&gt;
    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    &lt;artifactId&gt;lombok&lt;/artifactId&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
</code></pre>
<ul>
<li><p><strong>Purpose</strong>: Lombok helps reduce boilerplate code in Java classes. It generates common methods like getters, setters, constructors, <code>toString()</code>, etc., at compile-time.</p>
</li>
<li><p><strong>Benefit</strong>: It makes your code cleaner, with less code to write and maintain.</p>
</li>
</ul>
<h3 id="heading-configuring-applicationproperties">Configuring <code>application.properties</code></h3>
<p>In Spring Boot applications, the <code>application.properties</code> (or <code>application.yml</code>) file is where you configure various properties that control the behavior of the application, such as database connections, server port, logging, and more.</p>
<h4 id="heading-spring-application-name-amp-server-port"><strong>Spring Application Name &amp; Server Port</strong></h4>
<pre><code class="lang-java">spring.application.name=demo
server.port=<span class="hljs-number">8080</span>
</code></pre>
<ul>
<li><p><code>spring.application.name=demo</code>: This sets the name of your Spring Boot application to <strong>"demo"</strong>. It's helpful for logging and monitoring purposes.</p>
</li>
<li><p><code>server.port=8080</code>: This specifies the port on which your application will run. The default is <code>8080</code>, but you can change it to any port number you prefer.</p>
</li>
</ul>
<h4 id="heading-database-configuration-postgresql"><strong>Database Configuration (PostgreSQL)</strong></h4>
<pre><code class="lang-java">spring.datasource.url=jdbc:postgresql:<span class="hljs-comment">//localhost:5432/customer</span>
spring.datasource.username=postgres
spring.datasource.password=<span class="hljs-number">12345</span>
</code></pre>
<ul>
<li><p><code>spring.datasource.url</code>: This is the JDBC URL for connecting to the PostgreSQL database. Here, <code>localhost</code> represents the database host, <code>5432</code> is the default PostgreSQL port, and <code>customer</code> is the database name.</p>
</li>
<li><p><code>spring.datasource.username</code>: The username to connect to the PostgreSQL database. In this case, it’s <strong>postgres</strong>.</p>
</li>
<li><p><code>spring.datasource.password</code>: The password for the <code>postgres</code> user in the database. Change this to match your database credentials.</p>
</li>
</ul>
<h4 id="heading-hibernate-jpa-configuration"><strong>Hibernate JPA Configuration</strong></h4>
<pre><code class="lang-java">spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-<span class="hljs-class"><span class="hljs-keyword">class</span>-<span class="hljs-title">name</span></span>=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
</code></pre>
<ul>
<li><p><code>spring.jpa.hibernate.ddl-auto=update</code>: This setting automatically updates the database schema based on the entities in your application. The options are:</p>
<ul>
<li><p><code>none</code>: No schema management.</p>
</li>
<li><p><code>update</code>: Updates the schema to match the entities.</p>
</li>
<li><p><code>create</code>: Drops and creates the schema every time the application starts.</p>
</li>
<li><p><code>validate</code>: Validates the schema but does not modify it.</p>
</li>
<li><p><strong>In production, avoid using</strong> <code>create</code> or <code>update</code>, as it may cause data loss.</p>
</li>
</ul>
</li>
<li><p><code>spring.datasource.driver-class-name</code>: Specifies the <strong>JDBC driver</strong> for PostgreSQL.</p>
</li>
<li><p><code>spring.jpa.database-platform</code>: This defines the Hibernate dialect for PostgreSQL. It helps Hibernate generate correct SQL queries for PostgreSQL.</p>
</li>
</ul>
<h4 id="heading-logging-configuration"><strong>Logging Configuration</strong></h4>
<pre><code class="lang-java">propertiesCopyEditlogging.file.name=application.log
logging.level.org.springframework.security=DEBUG
</code></pre>
<ul>
<li><p><code>logging.file.name=application.log</code>: This sets the file name for the log output. In this case, the logs will be saved to <strong>application.log</strong>.</p>
</li>
<li><p><code>logging.level.org.springframework.security=DEBUG</code>: This sets the logging level for Spring Security to <strong>DEBUG</strong>. It helps in logging detailed information about security-related actions, useful for debugging.</p>
</li>
</ul>
<h3 id="heading-controller-setup">Controller Setup</h3>
<p>In Spring Boot, controllers are responsible for handling HTTP requests, processing them, and returning responses. In this section, we’ll walk through the code for a simple <code>UserController</code> class that handles user registration and displays the user list.</p>
<h4 id="heading-usercontroller-code"><strong>UserController Code</strong></h4>
<p>Here’s the <code>UserController</code> class:</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.example.demo.controllers;

<span class="hljs-keyword">import</span> com.example.demo.models.UserModel;
<span class="hljs-keyword">import</span> com.example.demo.repositories.UserRepository;
<span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;
<span class="hljs-keyword">import</span> org.springframework.security.crypto.password.PasswordEncoder;
<span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.GetMapping;
<span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestParam;
<span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;

<span class="hljs-keyword">import</span> java.util.UUID;

<span class="hljs-meta">@RestController</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>{

    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> PasswordEncoder passwordEncoder;

    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> UserRepository userRepository;

    <span class="hljs-comment">// Endpoint to fetch all users</span>
    <span class="hljs-meta">@GetMapping("/users")</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">GetAllUsers</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello World"</span>;
    }

    <span class="hljs-comment">// Endpoint for user registration</span>
    <span class="hljs-meta">@GetMapping("/signup")</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">Signup</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam</span> String UserName, <span class="hljs-meta">@RequestParam</span> String Password)</span>
    </span>{
        <span class="hljs-keyword">var</span> user = UserModel.builder()
                .UserID(UUID.randomUUID().toString()) <span class="hljs-comment">// Generate a unique ID</span>
                .userName(UserName)
                .Password(passwordEncoder.encode(Password)) <span class="hljs-comment">// Encode the password using BCrypt</span>
                .build();
        userRepository.save(user); <span class="hljs-comment">// Save the user to the database</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Registered Successfully"</span>; <span class="hljs-comment">// Return a success message</span>
    }
}
</code></pre>
<h4 id="heading-explanation-of-code"><strong>Explanation of Code</strong></h4>
<ul>
<li><p><code>@RestController</code>: This annotation marks the class as a REST controller. It means that each method in this class will return data directly to the HTTP response (i.e., no need to render views like with traditional controllers).</p>
</li>
<li><p><code>@Autowired</code>: This annotation is used to automatically inject the dependencies into the class. In this case, we inject:</p>
<ul>
<li><p><code>PasswordEncoder</code>: To encrypt the user's password before saving it to the database.</p>
</li>
<li><p><code>UserRepository</code>: This handles interactions with the database to save and retrieve user data.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-repository-and-model-setup">Repository and Model Setup</h3>
<p>In this section, we will set up two crucial components in our Spring Boot application: <strong>UserModel</strong> and <strong>UserRepository</strong>.</p>
<h4 id="heading-usermodel-code"><strong>UserModel Code</strong></h4>
<p>The <code>UserModel</code> class represents the structure of the <code>User</code> entity, which is mapped to a table in the database. Here's the code:</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.example.demo.models;

<span class="hljs-keyword">import</span> jakarta.persistence.Column;
<span class="hljs-keyword">import</span> jakarta.persistence.Entity;
<span class="hljs-keyword">import</span> jakarta.persistence.Id;
<span class="hljs-keyword">import</span> jakarta.persistence.Table;
<span class="hljs-keyword">import</span> lombok.*;

<span class="hljs-meta">@Entity</span>
<span class="hljs-meta">@Table(name = "Users")</span>
<span class="hljs-meta">@AllArgsConstructor</span>
<span class="hljs-meta">@NoArgsConstructor</span>
<span class="hljs-meta">@Getter</span>
<span class="hljs-meta">@Setter</span>
<span class="hljs-meta">@Builder</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserModel</span> </span>{
    <span class="hljs-meta">@Id</span>
    <span class="hljs-keyword">private</span> String UserID; <span class="hljs-comment">// Primary Key for the User</span>

    <span class="hljs-keyword">private</span> String userName; <span class="hljs-comment">// Username for the user</span>

    <span class="hljs-keyword">private</span> String Password; <span class="hljs-comment">// Password for the user (encoded)</span>
}
</code></pre>
<h4 id="heading-explanation-of-usermodel-class"><strong>Explanation of</strong> <code>UserModel</code> Class</h4>
<ul>
<li><p><code>@Entity</code>: This annotation marks the class as a JPA entity. JPA (Java Persistence API) is used to interact with the database. The <code>UserModel</code> class will be mapped to a table in the PostgreSQL database.</p>
</li>
<li><p><code>@Table(name = "Users")</code>: This annotation specifies the name of the table in the database to which this entity will be mapped. In this case, the table is named <code>Users</code>.</p>
</li>
<li><p><code>@Id</code>: This annotation marks the <code>UserID</code> field as the primary key for the entity. Each user in the <code>Users</code> table will have a unique <code>UserID</code>.</p>
</li>
<li><p><strong>Lombok Annotations</strong>:</p>
<ul>
<li><p><code>@AllArgsConstructor</code>: Generates a constructor that accepts all fields as arguments.</p>
</li>
<li><p><code>@NoArgsConstructor</code>: Generates a no-argument constructor.</p>
</li>
<li><p><code>@Getter</code> &amp; <code>@Setter</code>: Automatically generates getter and setter methods for each field.</p>
</li>
<li><p><code>@Builder</code>: This generates a builder pattern for the <code>UserModel</code> class, which allows us to easily create instances of <code>UserModel</code> with method chaining.</p>
</li>
</ul>
</li>
</ul>
<h4 id="heading-userrepository-code"><strong>UserRepository Code</strong></h4>
<p>The <code>UserRepository</code> interface is responsible for interacting with the database. It provides CRUD operations for <code>UserModel</code> objects. Here’s the code:</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.example.demo.repositories;

<span class="hljs-keyword">import</span> com.example.demo.models.UserModel;
<span class="hljs-keyword">import</span> org.springframework.data.jpa.repository.JpaRepository;
<span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;

<span class="hljs-meta">@Repository</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserRepository</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">JpaRepository</span>&lt;<span class="hljs-title">UserModel</span>, <span class="hljs-title">String</span>&gt; </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> UserModel <span class="hljs-title">findByUserName</span><span class="hljs-params">(String UserName)</span></span>;
}
</code></pre>
<h4 id="heading-explanation-of-userrepository-interface"><strong>Explanation of</strong> <code>UserRepository</code> Interface</h4>
<ul>
<li><p><code>@Repository</code>: This annotation marks the interface as a Spring Data repository. It is used to perform CRUD operations on the <code>UserModel</code> entities in the database.</p>
</li>
<li><p><code>JpaRepository&lt;UserModel, String&gt;</code>: The <code>JpaRepository</code> interface provides several methods for working with the <code>UserModel</code> entity, such as saving, finding, deleting, and updating records in the database. The generic parameters specify:</p>
<ul>
<li><p><code>UserModel</code>: The type of the entity.</p>
</li>
<li><p><code>String</code>: The type of the primary key for the <code>UserModel</code> entity (<code>UserID</code>).</p>
</li>
</ul>
</li>
<li><p><code>findByUserName(String UserName)</code>: This is a custom query method. Spring Data JPA will automatically generate the query to find a <code>UserModel</code> by the <code>UserName</code> field. It returns the user associated with the provided username.</p>
</li>
</ul>
<h4 id="heading-database-mapping-in-postgresql"><strong>Database Mapping in PostgreSQL</strong></h4>
<p>When Spring Boot runs with this configuration, it will map the <code>UserModel</code> class to a <code>Users</code> table in the PostgreSQL database. This table will have columns for <code>UserID</code>, <code>userName</code>, and <code>Password</code>. The database will store each user's information, including their encrypted password.</p>
<h3 id="heading-custom-user-details-service">Custom User Details Service</h3>
<p>In this section, we will implement a <strong>CustomUserDetailService</strong> that integrates Spring Security with our <code>UserModel</code> and ensures proper authentication and authorization.</p>
<h4 id="heading-customuserdetailservice-code"><strong>CustomUserDetailService Code</strong></h4>
<p>The <code>CustomUserDetailService</code> class implements <code>UserDetailsService</code> and provides a custom way to load a user's details for Spring Security authentication. Here’s the code:</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.example.demo.services;

<span class="hljs-keyword">import</span> com.example.demo.repositories.UserRepository;
<span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;
<span class="hljs-keyword">import</span> org.springframework.security.core.userdetails.User;
<span class="hljs-keyword">import</span> org.springframework.security.core.userdetails.UserDetails;
<span class="hljs-keyword">import</span> org.springframework.security.core.userdetails.UsernameNotFoundException;
<span class="hljs-keyword">import</span> org.springframework.stereotype.Service;

<span class="hljs-meta">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CustomUserDetailService</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserDetailsService</span> </span>{
    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> UserRepository userRepository;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> UserDetails <span class="hljs-title">loadUserByUsername</span><span class="hljs-params">(String username)</span> <span class="hljs-keyword">throws</span> UsernameNotFoundException </span>{
        <span class="hljs-keyword">var</span> user = userRepository.findByUserName(username);  <span class="hljs-comment">// Fetch user from the database</span>

        <span class="hljs-keyword">if</span> (user == <span class="hljs-keyword">null</span>) {  <span class="hljs-comment">// If no user is found, throw UsernameNotFoundException</span>
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> UsernameNotFoundException(<span class="hljs-string">"User not found: "</span> + username);
        }

        <span class="hljs-comment">// Returning a UserDetails object with the user info</span>
        <span class="hljs-keyword">return</span> User.builder()
                .username(user.getUserName())  <span class="hljs-comment">// Set username</span>
                .password(user.getPassword())  <span class="hljs-comment">// Set password (already encoded)</span>
                .roles(<span class="hljs-string">"USER"</span>)  <span class="hljs-comment">// Set the role for this user</span>
                .build();
    }
}
</code></pre>
<h4 id="heading-explanation-of-customuserdetailservice-class"><strong>Explanation of</strong> <code>CustomUserDetailService</code> Class</h4>
<ul>
<li><p><code>@Service</code>: This annotation marks the class as a service that will be managed by Spring’s dependency injection container. It allows Spring to inject this service into other components like controllers and security configurations.</p>
</li>
<li><p><code>UserDetailsService</code> Interface: This is a Spring Security interface that contains a method <code>loadUserByUsername</code> which is used to fetch user details from a database based on the username provided. It is a core interface used by Spring Security for authentication.</p>
</li>
<li><p><code>loadUserByUsername</code> Method:</p>
<ul>
<li><p>This method takes a <code>username</code> as input and returns a <code>UserDetails</code> object. It is responsible for fetching the user information from the database.</p>
</li>
<li><p>The <code>userRepository.findByUserName(username)</code> fetches the <code>UserModel</code> from the database using the <code>UserName</code> field.</p>
</li>
<li><p>If no user is found, a <code>UsernameNotFoundException</code> is thrown.</p>
</li>
<li><p>The <code>User.builder()</code> creates a <code>User</code> object, which is a Spring Security class that implements <code>UserDetails</code>. This object contains the username, password (which is already encoded), and roles for the user. In this case, the user has a role of <code>"USER"</code>.</p>
</li>
</ul>
</li>
<li><p><strong>UserDetails</strong>: This is an interface in Spring Security that represents the user's information (like username, password, authorities, etc.) for authentication and authorization purposes.</p>
</li>
</ul>
<h4 id="heading-role-of-customuserdetailservice-in-spring-security"><strong>Role of</strong> <code>CustomUserDetailService</code> in Spring Security</h4>
<ul>
<li><p><strong>Authentication</strong>: When a user tries to log in, Spring Security will call the <code>loadUserByUsername</code> method to fetch the user's details from the database. The returned <code>UserDetails</code> object is then used to authenticate the user.</p>
</li>
<li><p><strong>Authorization</strong>: Based on the roles assigned to the user (in this case, <code>"USER"</code>), Spring Security can authorize or deny access to specific resources within the application.</p>
</li>
</ul>
<h3 id="heading-configuring-spring-security">Configuring Spring Security</h3>
<p>In this section, we will configure <strong>Spring Security</strong> to secure the application and control user access. We will do this by customizing the security filter chain and using Spring's authentication manager.</p>
<h4 id="heading-securityconfig-code"><strong>SecurityConfig Code</strong></h4>
<p>Here’s the <code>SecurityConfig</code> class where we configure Spring Security for the application:</p>
<pre><code class="lang-java">javaCopyEditpackage com.example.demo.configs;

<span class="hljs-keyword">import</span> com.example.demo.services.CustomUserDetailService;
<span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;
<span class="hljs-keyword">import</span> org.springframework.security.authentication.AuthenticationManager;
<span class="hljs-keyword">import</span> org.springframework.security.config.Customizer;
<span class="hljs-keyword">import</span> org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
<span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.builders.HttpSecurity;
<span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
<span class="hljs-keyword">import</span> org.springframework.security.core.userdetails.UserDetailsService;
<span class="hljs-keyword">import</span> org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
<span class="hljs-keyword">import</span> org.springframework.security.crypto.password.PasswordEncoder;
<span class="hljs-keyword">import</span> org.springframework.security.web.SecurityFilterChain;

<span class="hljs-meta">@Configuration</span>
<span class="hljs-meta">@EnableWebSecurity</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SecurityConfig</span> </span>{

    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> SecurityFilterChain <span class="hljs-title">securityFilterChain</span><span class="hljs-params">(HttpSecurity http)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        http
                .csrf(Customizer.withDefaults())  <span class="hljs-comment">// CSRF protection</span>
                .formLogin(Customizer.withDefaults())  <span class="hljs-comment">// Enable Form-based Authentication</span>
                .authorizeHttpRequests(authorize -&gt; authorize
                        .requestMatchers(<span class="hljs-string">"/signup"</span>).permitAll()  <span class="hljs-comment">// Allow access to /signup without authentication</span>
                        .anyRequest().authenticated()  <span class="hljs-comment">// Secure all other endpoints</span>
                );

        <span class="hljs-keyword">return</span> http.build();
    }

    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> AuthenticationManager <span class="hljs-title">authenticationManager</span><span class="hljs-params">(AuthenticationConfiguration configuration)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        <span class="hljs-keyword">return</span> configuration.getAuthenticationManager();
    }

    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> PasswordEncoder <span class="hljs-title">passwordEncoder</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Use BCryptPasswordEncoder for password encryption</span>
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> BCryptPasswordEncoder();
    }
}
</code></pre>
<h4 id="heading-72-explanation-of-securityconfig-class">7.2 <strong>Explanation of</strong> <code>SecurityConfig</code> Class</h4>
<ul>
<li><p><code>@Configuration</code>: This annotation marks the class as a source of bean definitions. It is used to configure Spring Beans in the context.</p>
</li>
<li><p><code>@EnableWebSecurity</code>: This annotation enables Spring Security in the application. It tells Spring to look for a <code>SecurityConfig</code> class for security-related configurations.</p>
</li>
<li><p><code>SecurityFilterChain</code> Bean:</p>
<ul>
<li><p>This bean is responsible for configuring the HTTP security of the application.</p>
</li>
<li><p><strong>CSRF Protection</strong>: <code>http.csrf(Customizer.withDefaults())</code> enables Cross-Site Request Forgery (CSRF) protection by default. This is a critical security feature for web applications.</p>
</li>
<li><p><strong>Form-Based Authentication</strong>: <code>http.formLogin(Customizer.withDefaults())</code> enables form-based authentication, where users will be required to provide their username and password to access secured endpoints.</p>
</li>
<li><p><strong>Authorization Rules</strong>: <code>authorizeHttpRequests(authorize -&gt; authorize...)</code> defines access control rules for HTTP requests:</p>
<ul>
<li><p><code>.requestMatchers("/signup").permitAll()</code>: This allows unrestricted access to the <code>/signup</code> endpoint.</p>
</li>
<li><p><code>.anyRequest().authenticated()</code>: This restricts access to all other endpoints and requires authentication.</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><code>AuthenticationManager</code> Bean:</p>
<ul>
<li>This bean is responsible for authenticating the user. Spring Security uses this to authenticate the user when they log in.</li>
</ul>
</li>
<li><p><code>PasswordEncoder</code> Bean:</p>
<ul>
<li><p><strong>BCryptPasswordEncoder</strong>: This bean is used to encrypt the password before storing it in the database and while comparing it during authentication.</p>
</li>
<li><p>We have used <code>BCryptPasswordEncoder</code> here because it is one of the most secure password encoding algorithms available in Spring Security.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-how-spring-security-filters-requests">How Spring Security Filters Requests</h3>
<ul>
<li><p><strong>Request Flow</strong>:</p>
<ul>
<li><p>When a request is made, Spring Security intercepts the request and checks the security configurations (e.g., authentication and authorization).</p>
</li>
<li><p>If the request is for a public endpoint like <code>/signup</code>, it is permitted without authentication.</p>
</li>
<li><p>If the request is for any other endpoint, it is secured and requires authentication. Spring Security checks if the user is logged in. If not, it will redirect the user to the login page.</p>
</li>
</ul>
</li>
<li><p><strong>Authentication</strong>:</p>
<ul>
<li><p>When a user submits their login credentials (username and password), Spring Security uses the <code>CustomUserDetailService</code> to fetch the user’s details from the database.</p>
</li>
<li><p>The password is compared using the <code>PasswordEncoder</code> (in our case, <code>BCryptPasswordEncoder</code>) to ensure the credentials are valid.</p>
</li>
<li><p>If the credentials are valid, the user is authenticated and allowed to access the requested resource.</p>
</li>
</ul>
</li>
<li><p><strong>Authorization</strong>:</p>
<ul>
<li><p>Once authenticated, Spring Security assigns the user roles and checks if they have the necessary permissions to access the requested resource.</p>
</li>
<li><p>In our case, we have a basic role <code>"USER"</code>. You can add more roles if needed, and Spring Security can be configured to allow or deny access to different parts of the application based on roles.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-next-steps">Next Steps</h3>
<p>Now that we have configured basic security for your Spring Boot application, here are some potential next steps you can take to enhance the security:</p>
<ol>
<li><p><strong>Add JWT Authentication</strong>: Implement JSON Web Tokens (JWT) for stateless authentication instead of relying on session-based authentication.</p>
</li>
<li><p><strong>Role-Based Access Control (RBAC)</strong>: Extend the roles and permissions structure, allowing fine-grained access control for various parts of your application (e.g., allowing only users with the <code>ADMIN</code> role to access certain pages).</p>
</li>
<li><p><strong>Two-Factor Authentication (2FA)</strong>: Implement an additional layer of security by requiring users to verify their identity via a second factor (such as an OTP).</p>
</li>
<li><p><strong>Rate Limiting</strong>: Protect your application against brute-force attacks by adding rate-limiting on endpoints such as login.</p>
</li>
<li><p><strong>Logging and Monitoring</strong>: Use Spring Security's logging capabilities to monitor login attempts and failed authentication events. You can also set up alerts to notify you of suspicious activities.</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Understanding EMR Architecture: Key Components, Configuration Options, and Scaling Strategies]]></title><description><![CDATA[Introduction to EMR Architecture
Amazon EMR (Elastic MapReduce) is a cloud-native, fully managed service provided by AWS (Amazon Web Services) for processing large volumes of data quickly and cost-effectively. It enables the distributed processing of...]]></description><link>https://programminguides.hashnode.dev/understanding-emr-architecture-key-components-configuration-options-and-scaling-strategies</link><guid isPermaLink="true">https://programminguides.hashnode.dev/understanding-emr-architecture-key-components-configuration-options-and-scaling-strategies</guid><category><![CDATA[AWS]]></category><category><![CDATA[emr]]></category><category><![CDATA[ec2]]></category><category><![CDATA[spark]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Thu, 20 Mar 2025 08:26:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1740590332773/db40b4aa-ceee-4a3a-bee6-a8c6d1b80dfa.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction-to-emr-architecture"><strong>Introduction to EMR Architecture</strong></h3>
<p><strong>Amazon EMR (Elastic MapReduce)</strong> is a cloud-native, fully managed service provided by AWS (Amazon Web Services) for processing large volumes of data quickly and cost-effectively. It enables the distributed processing of vast amounts of data across a scalable cluster of virtual machines (EC2 instances), making it an essential tool for big data processing, data analysis, and machine learning tasks.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1740590366217/164d604f-f742-4fd8-b8a9-b53c0b8fa63e.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-key-purposes-and-use-cases-of-amazon-emr"><strong>Key Purposes and Use Cases of Amazon EMR:</strong></h3>
<ol>
<li><p><strong>Big Data Processing:</strong></p>
<ul>
<li><p>EMR is designed to run distributed data processing frameworks such as Hadoop and Spark. These frameworks can process petabytes of data in parallel across many EC2 instances, ensuring fast and efficient computations.</p>
</li>
<li><p>Common use cases include batch processing, data transformation, and machine learning tasks at scale.</p>
</li>
</ul>
</li>
<li><p><strong>Data Storage and Analysis with S3 and HDFS:</strong></p>
<ul>
<li><p>EMR can leverage <strong>Amazon S3</strong> as a storage system for input and output data. This integration makes it easy to manage large datasets stored in S3 while processing them using distributed computing on EMR.</p>
</li>
<li><p>It also supports <strong>HDFS (Hadoop Distributed File System)</strong> if you want to store data locally within the EMR cluster.</p>
</li>
</ul>
</li>
<li><p><strong>Cost Optimization with On-Demand and Spot Instances:</strong></p>
<ul>
<li><p>EMR allows you to scale your clusters up or down based on demand. You can provision clusters with a mix of <strong>On-Demand</strong> and <strong>Spot Instances</strong>, ensuring you get the most cost-effective performance for your processing needs.</p>
</li>
<li><p>Spot Instances enable you to take advantage of unused EC2 capacity at a lower price, significantly reducing costs for data processing jobs.</p>
</li>
</ul>
</li>
<li><p><strong>Real-time Stream Processing:</strong></p>
<ul>
<li>With <strong>Apache Kafka</strong> and <strong>Spark Streaming</strong>, EMR can process data in real-time, making it ideal for use cases like log analysis, clickstream analysis, and IoT data processing, where timely insights are critical.</li>
</ul>
</li>
<li><p><strong>Machine Learning at Scale:</strong></p>
<ul>
<li><p>EMR supports frameworks like <strong>Apache Spark MLlib</strong>, <strong>TensorFlow</strong>, and other machine learning libraries to process large datasets and build machine learning models in a distributed environment.</p>
</li>
<li><p>Using EMR for machine learning allows businesses to handle large volumes of data and perform model training and inference at scale.</p>
</li>
</ul>
</li>
<li><p><strong>Easy Integration with AWS Services:</strong></p>
<ul>
<li><p>EMR is fully integrated with other AWS services, such as <strong>AWS Lambda</strong>, <strong>AWS Glue</strong>, <strong>Amazon RDS</strong>, <strong>Amazon Redshift</strong>, and more, making it easy to orchestrate end-to-end data processing pipelines.</p>
</li>
<li><p>It also integrates with <strong>AWS CloudWatch</strong> for monitoring and <strong>AWS IAM</strong> for access control and security.</p>
</li>
</ul>
</li>
<li><p><strong>Fault Tolerance and Scalability:</strong></p>
<ul>
<li><p>EMR provides high availability and fault tolerance. If a task or instance fails, EMR can automatically recover from failures by re-running tasks on other instances.</p>
</li>
<li><p>The service is also scalable, allowing users to increase or decrease the number of instances in the cluster as per their workload requirements.</p>
</li>
</ul>
</li>
<li><p><strong>Simplified Cluster Management:</strong></p>
<ul>
<li><p>EMR eliminates the need for managing infrastructure manually. AWS automatically takes care of cluster provisioning, configuration, and tuning, letting users focus on their data processing and analysis tasks.</p>
</li>
<li><p>It supports <strong>auto-scaling</strong>, so clusters can automatically expand or shrink based on workload demands.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-key-integration-options-that-emr-offers">Key integration options that EMR offers:</h3>
<ol>
<li><p>Amazon S3 (Simple Storage Service)</p>
</li>
<li><p>Amazon RDS (Relational Database Service)</p>
</li>
<li><p>Amazon Redshift</p>
</li>
<li><p>Amazon DynamoDB</p>
</li>
<li><p>AWS Lambda</p>
</li>
<li><p>Amazon CloudWatch</p>
</li>
<li><p>AWS Glue Amazon</p>
</li>
<li><p>Kinesis</p>
</li>
<li><p>Amazon ElasticSearch (Amazon OpenSearch Service)</p>
</li>
<li><p>Apache HBase (via Amazon EMR)</p>
</li>
<li><p>Amazon SageMaker</p>
</li>
<li><p>Apache Kafka</p>
</li>
<li><p>Apache Hive and Apache HCatalog</p>
</li>
<li><p>AWS IAM (Identity and Access Management)</p>
</li>
<li><p>AWS Data Pipeline</p>
</li>
<li><p>AWS Step Functions</p>
</li>
<li><p>Third-Party Tools (e.g., Jupyter Notebooks, Apache Zeppelin)</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1740590548943/7e582ec3-62e8-461a-934f-0e456b076641.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-the-building-blocks-primary-core-and-task-nodes"><strong>The Building Blocks: Primary, Core, and Task Nodes</strong></h3>
<p>The building blocks of Amazon EMR (Elastic MapReduce) represent the key components that make up an EMR cluster. These components work together to process large-scale data efficiently and cost-effectively.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1740590701855/d8f53df9-8049-474c-9b5d-33147eb2444a.png" alt class="image--center mx-auto" /></p>
<p>Let's dive deeper into each of these building blocks:</p>
<p>1. <strong>Cluster</strong></p>
<ul>
<li><p><strong>Definition:</strong> An <strong>EMR cluster</strong> is a collection of Amazon EC2 instances that work together to process and analyze large datasets. A cluster can consist of different types of nodes (primary, core, task nodes) that serve various purposes.</p>
</li>
<li><p><strong>Cluster Setup:</strong> When creating an EMR cluster, you can choose the number and type of EC2 instances for each node, configure software (like Hadoop or Spark), and select additional options like storage and scaling methods.</p>
</li>
<li><p><strong>Cluster Lifecycle:</strong> EMR clusters can be provisioned on-demand, and you have full control over the cluster lifecycle, including scaling, termination, and instance configurations.</p>
</li>
</ul>
<ol start="2">
<li><p><strong>Nodes</strong></p>
<p> The fundamental unit of an EMR cluster is the <strong>node</strong>. Each node in an EMR cluster runs a portion of the distributed data processing. There are three types of nodes in an EMR cluster:</p>
<ul>
<li><p><strong>Primary Node (Master Node):</strong></p>
<ul>
<li><p><strong>Role:</strong> The <strong>master node</strong> is responsible for managing the cluster's overall operation. It runs the <strong>resource manager</strong> (e.g., YARN for Hadoop or the Spark driver), which coordinates the distribution of tasks across the cluster.</p>
</li>
<li><p><strong>Responsibilities:</strong> It tracks the health of other nodes, schedules jobs, and manages job execution across the cluster. The master node also manages the cluster configuration and keeps track of the logs and results.</p>
</li>
</ul>
</li>
<li><p><strong>Core Nodes:</strong></p>
<ul>
<li><p><strong>Role:</strong> <strong>Core nodes</strong> perform the actual data processing. They run the <strong>worker</strong> tasks (such as mappers and reducers in Hadoop) and store the data in the cluster's HDFS (Hadoop Distributed File System).</p>
</li>
<li><p><strong>Responsibilities:</strong> These nodes handle the core tasks of computation and data storage, and they are typically required for the cluster to function. The loss of core nodes may impact the cluster's performance.</p>
</li>
</ul>
</li>
<li><p><strong>Task Nodes (Optional):</strong></p>
<ul>
<li><p><strong>Role:</strong> <strong>Task nodes</strong> are optional nodes that provide additional computational resources for performing tasks such as running map-reduce jobs. They don't store data but act as extra compute capacity for specific tasks.</p>
</li>
<li><p><strong>Responsibilities:</strong> Task nodes only run computations and are transient, meaning they can be added or removed dynamically from the cluster to handle fluctuations in workload or processing capacity.</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Executors</strong></p>
<p> When you run a <strong>Spark job</strong> on Amazon EMR, the <strong>Executor</strong> runs on the <strong>core nodes</strong> (or task nodes, if used). Here's how it fits into the EMR ecosystem:</p>
<ul>
<li><p>The <strong>Primary node (Master node)</strong> coordinates the cluster and assigns tasks to executors.</p>
</li>
<li><p>The <strong>Core nodes</strong> run <strong>executors</strong>, executing the actual Spark jobs.</p>
</li>
<li><p>If <strong>Task nodes</strong> are used, they also run executors to provide additional computational capacity when needed.</p>
</li>
</ul>
</li>
<li><p><strong>HDFS (Hadoop Distributed File System)</strong></p>
<ul>
<li><p><strong>Definition:</strong> <strong>HDFS</strong> is the distributed file system that is used by Hadoop (and Spark when running in a Hadoop-compatible mode) for storing large datasets across multiple nodes.</p>
</li>
<li><p><strong>Role in EMR:</strong> EMR leverages HDFS (or optionally Amazon S3 as storage) to distribute data across the cluster so that it can be processed in parallel by the nodes. HDFS is designed for high throughput and fault tolerance, enabling data to be replicated across nodes in the cluster.</p>
</li>
</ul>
</li>
<li><p><strong>Amazon S3 (Simple Storage Service)</strong></p>
<ul>
<li><p><strong>Definition:</strong> While HDFS is typically used for data storage within an EMR cluster, <strong>Amazon S3</strong> is commonly used for long-term storage of input/output data.</p>
</li>
<li><p><strong>Integration with EMR:</strong> You can use <strong>Amazon S3</strong> to store data that is used for batch processing or streaming, as well as for storing the results of data processing jobs. It's a highly scalable and durable storage system, and EMR clusters can be configured to read and write data directly to/from S3.</p>
</li>
<li><p><strong>Storage Flexibility:</strong> Unlike HDFS, S3 is more flexible and cost-efficient for storing large volumes of data without the overhead of managing local storage.</p>
</li>
</ul>
</li>
<li><p><strong>Resource Manager</strong></p>
<ul>
<li><p><strong>Definition:</strong> The <strong>Resource Manager</strong> is the component of the cluster responsible for managing resources across the nodes. It allocates resources to the various tasks that need to run in the cluster.</p>
</li>
<li><p><strong>Examples:</strong></p>
<ul>
<li><p><strong>YARN (Yet Another Resource Negotiator)</strong>: For managing resources in a Hadoop ecosystem, YARN is responsible for resource management and job scheduling.</p>
</li>
<li><p><strong>Spark’s Driver Program</strong>: For Spark applications, the Spark driver manages the resources and tasks.</p>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1740591325014/ed3d33dd-a879-4708-a1ad-7fc8c4d75263.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-use-cases-for-each-node-type">Use cases for each node type</h3>
<ol>
<li><p>Primary Node (Master Node): The <strong>Primary Node</strong> is the <strong>central coordination unit</strong> of the EMR cluster. It doesn't handle data storage but is responsible for managing the cluster's lifecycle, running resource management services, and coordinating the execution of tasks. The <strong>Primary Node</strong> manages job scheduling and distributes tasks across the other nodes.</p>
</li>
<li><p>Core Node: The <strong>Core Nodes</strong> are responsible for <strong>data storage</strong> and <strong>data processing</strong> in the cluster. These nodes are the heart of the EMR cluster because they handle both the computation and store the data in <strong>HDFS (Hadoop Distributed File System)</strong>. Core nodes store actual data that is being processed, utilizing <strong>HDFS</strong> for distributed storage. Each core node keeps part of the data and ensures redundancy and fault tolerance by replicating data blocks across other core nodes in the cluster.</p>
</li>
<li><p>Task Node: <strong>Task Nodes</strong> are optional nodes that can be added to an EMR cluster to provide <strong>additional computational resources</strong> for running tasks but without storing data. These nodes are typically used for <strong>scaling</strong> the cluster based on computational needs. Task nodes provide additional compute capacity when there is a need to process large datasets, and they are usually added when the workload increases beyond the capability of the core nodes.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1740591386001/5a1cf590-dd0a-4877-b958-17db878fe45b.png" alt class="image--center mx-auto" /></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Node Type</strong></td><td><strong>Role</strong></td><td><strong>Use Case</strong></td><td><strong>Storage</strong></td><td><strong>Compute</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Primary Node</strong></td><td>Master node responsible for coordination, management, and resource allocation</td><td>- Manages job scheduling and resource allocation  </td></tr>
</tbody>
</table>
</div><p>- Tracks cluster health<br />- Stores metadata | No | Manages resources |
| <strong>Core Nodes</strong> | Responsible for both data processing and storage | - Runs data processing tasks (MapReduce/Spark)<br />- Stores data in HDFS or S3 | Yes (HDFS/S3) | Yes (Data Processing) |
| <strong>Task Nodes</strong> | Provide additional compute capacity for processing tasks without storing data | - Adds more processing power when workload demands scaling<br />- Handles computation tasks only | No | Yes (Data Processing) |</p>
]]></content:encoded></item><item><title><![CDATA[Configuring AWS EventBridge with Lambda: A Step-by-Step Guide]]></title><description><![CDATA[Introduction
AWS EventBridge is a powerful event bus service that allows you to connect different AWS services and applications using events. By integrating EventBridge with AWS Lambda, you can create automated workflows that respond to changes in yo...]]></description><link>https://programminguides.hashnode.dev/configuring-aws-eventbridge-with-lambda-a-step-by-step-guide</link><guid isPermaLink="true">https://programminguides.hashnode.dev/configuring-aws-eventbridge-with-lambda-a-step-by-step-guide</guid><category><![CDATA[AWS]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Wed, 09 Oct 2024 15:18:01 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction</h3>
<p>AWS EventBridge is a powerful event bus service that allows you to connect different AWS services and applications using events. By integrating EventBridge with AWS Lambda, you can create automated workflows that respond to changes in your cloud environment. This article will guide you through the steps to configure EventBridge and a Lambda function to respond to a recurring schedule.</p>
<h3 id="heading-steps-to-configure-eventbridge-and-lambda-function">Steps to Configure EventBridge and Lambda Function</h3>
<h4 id="heading-step-1-create-a-rule-in-eventbridge">Step 1: Create a Rule in EventBridge</h4>
<ol>
<li><p><strong>Log in to the AWS Management Console</strong>: Navigate to the EventBridge service.</p>
</li>
<li><p><strong>Go to Rules</strong>: In the left-hand menu, click on "Rules."</p>
</li>
<li><p><strong>Create a New Rule</strong>:</p>
<ul>
<li><p>Click on the "Create rule" button.</p>
</li>
<li><p>Enter a name and description for your rule.</p>
</li>
</ul>
</li>
</ol>
<h4 id="heading-step-2-set-up-a-recurring-schedule">Step 2: Set Up a Recurring Schedule</h4>
<ol>
<li><p><strong>Select Schedule</strong>: Choose the option for "Schedule" under the rule type.</p>
</li>
<li><p><strong>Choose Cron-based Schedule</strong>:</p>
<ul>
<li><p>Select "Cron expression" for a more flexible scheduling option.</p>
</li>
<li><p>Enter your desired cron expression (e.g., <code>cron(0 12 * * ? *)</code> for every day at noon).</p>
</li>
</ul>
</li>
<li><p><strong>Flexible Time Window</strong>: If applicable, configure the flexible time window settings to specify when the rule should run.</p>
</li>
</ol>
<h4 id="heading-step-3-create-the-lambda-function">Step 3: Create the Lambda Function</h4>
<ol>
<li><p><strong>Navigate to the Lambda Service</strong>: In the AWS Management Console, go to the Lambda service.</p>
</li>
<li><p><strong>Create a New Function</strong>:</p>
<ul>
<li><p>Click on the "Create function" button.</p>
</li>
<li><p>Choose "Author from scratch."</p>
</li>
<li><p>Enter a name for your Lambda function and select the appropriate runtime (e.g., Python, Node.js).</p>
</li>
<li><p>Set the necessary execution role for the function.</p>
</li>
</ul>
</li>
<li><p><strong>Write Your Lambda Code</strong>: In the function editor, write the code that you want to execute when the event is triggered.</p>
</li>
<li><p><strong>Deploy the Lambda Function</strong>: Click on "Deploy" to save your changes.</p>
</li>
</ol>
<h4 id="heading-step-4-attach-lambda-function-to-eventbridge-rule">Step 4: Attach Lambda Function to EventBridge Rule</h4>
<ol>
<li><p><strong>Return to EventBridge</strong>: Go back to the EventBridge rule you created earlier.</p>
</li>
<li><p><strong>Configure Targets</strong>:</p>
<ul>
<li><p>In the "Targets" section, click on "Add target."</p>
</li>
<li><p>From the "Target type" dropdown, select "Lambda function."</p>
</li>
<li><p>Choose the Lambda function you created earlier from the dropdown menu.</p>
</li>
</ul>
</li>
<li><p><strong>Configure Permissions</strong>: Ensure that EventBridge has the necessary permissions to invoke your Lambda function. This is typically handled automatically when you attach the function, but you can check the IAM role permissions if needed.</p>
</li>
<li><p><strong>Create the Rule</strong>: Finally, click on "Create rule" to activate the event.</p>
</li>
</ol>
<h3 id="heading-conclusion">Conclusion</h3>
<p>By following these steps, you have successfully configured AWS EventBridge to trigger a Lambda function on a recurring schedule. This integration allows you to automate tasks and respond to events efficiently, making your cloud applications more dynamic and responsive. Experiment with different cron expressions and Lambda functionalities to further enhance your workflows!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728486966433/b225672f-321e-4554-958e-6639224b63b2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728486982386/b58c2038-db08-4e5c-9e1e-9c719f6adbed.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728486989564/6d9618c6-edc1-4644-b051-fa109f19fbc0.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728486996770/36b242a6-67a5-476c-8b27-6526a9eb7cdc.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728487003938/42907621-dcac-49bb-a034-24b6094145fc.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728487008301/5b12812c-d0fb-4940-9b7c-72b7c7e0d9c5.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[AWS Transfer Family 101: Establishing an SFTP Server in the Cloud]]></title><description><![CDATA[Overview







Comparing JSCAPE Server and AWS Transfer Family: Pros and Cons




FeatureJSCAPE ServerAWS Transfer Family



Setup ComplexityRequires manual installation and configuration.Fully managed service, easy to set up via AWS Console.

CostL...]]></description><link>https://programminguides.hashnode.dev/aws-transfer-family-101-establishing-an-sftp-server-in-the-cloud</link><guid isPermaLink="true">https://programminguides.hashnode.dev/aws-transfer-family-101-establishing-an-sftp-server-in-the-cloud</guid><category><![CDATA[AWS]]></category><category><![CDATA[SFTP]]></category><category><![CDATA[aws-transfer-family]]></category><category><![CDATA[mobaxterm]]></category><category><![CDATA[filezilla]]></category><category><![CDATA[S3]]></category><category><![CDATA[ssh]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Sun, 22 Sep 2024 09:43:30 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-overview">Overview</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986312882/5d590318-13d6-443c-a5ea-76ed19afedc3.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986848738/07aec71e-a02d-4898-b556-b4f8e896219f.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986382867/72ae0d45-4db7-49ab-9518-757ac420ed22.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986456838/2957e38c-5848-44bb-8e25-9cccc8716154.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986516135/fa43f15a-7724-48b9-89d3-4a982f3605a6.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986565600/683d14a9-4fad-47f8-b7c9-64b1dfeae899.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726986724578/cd27929d-ccd3-4b46-8b62-07a13af2b7f0.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-comparing-jscape-server-and-aws-transfer-family-pros-and-cons">Comparing JSCAPE Server and AWS Transfer Family: Pros and Cons</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>JSCAPE Server</td><td>AWS Transfer Family</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Setup Complexity</strong></td><td>Requires manual installation and configuration.</td><td>Fully managed service, easy to set up via AWS Console.</td></tr>
<tr>
<td><strong>Cost</strong></td><td>Licensing costs can be high; ongoing maintenance.</td><td>Pay-as-you-go pricing; no upfront costs.</td></tr>
<tr>
<td><strong>Scalability</strong></td><td>Limited by server resources; requires manual scaling.</td><td>Automatically scales with demand, no server management needed.</td></tr>
<tr>
<td><strong>Security</strong></td><td>Offers various security features but requires configuration.</td><td>Integrated with AWS security services (IAM, KMS, etc.) for robust security.</td></tr>
<tr>
<td><strong>Protocol Support</strong></td><td>Supports multiple protocols (SFTP, FTP, HTTP, etc.).</td><td>Primarily focused on SFTP, FTPS, and FTP.</td></tr>
<tr>
<td><strong>Monitoring and Reporting</strong></td><td>Basic monitoring tools included.</td><td>Integrated with AWS CloudWatch for detailed monitoring.</td></tr>
<tr>
<td><strong>Customizability</strong></td><td>Highly customizable; can be tailored for specific needs.</td><td>Limited customizability; focused on standard use cases.</td></tr>
<tr>
<td><strong>Maintenance</strong></td><td>Requires ongoing maintenance and updates.</td><td>No maintenance; AWS handles updates and availability.</td></tr>
<tr>
<td><strong>Integration</strong></td><td>Can integrate with various systems but may require more setup.</td><td>Seamlessly integrates with other AWS services (S3, Lambda, etc.).</td></tr>
<tr>
<td><strong>User Management</strong></td><td>Manual user management; can be cumbersome.</td><td>Managed via AWS IAM; easier and more secure user management.</td></tr>
</tbody>
</table>
</div><h3 id="heading-setting-up-your-s3-bucket">Setting Up Your S3 Bucket</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726989855336/71446e20-4074-4e2d-b7cc-c2cc6f1516bc.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726989868338/d4303f3a-f423-4043-82e7-8025b6f64cf0.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726989881709/03507e97-1f09-4d80-90e8-6ecb74c772d1.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726989892873/490db4ee-b305-496a-a614-2e63cf1088e0.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-steps-to-configure-aws-transfer-family-for-sftp">Steps to Configure AWS Transfer Family for SFTP</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990058677/5c52a542-31cf-4c76-88ec-072d99cc0afa.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990072335/8394a2d3-dc2e-4ead-bb19-9cca1dba1bf8.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990088264/71cf1ca1-fd4f-47cb-9799-e29c73f14e4a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990108084/dd763507-8947-4938-88e3-795b84acb288.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990122099/28f7282a-88cb-46c9-a82d-e1a31046ec7a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990141494/e8a02dce-6777-441b-b887-49a22943b5b0.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990154694/4d820609-fdaa-4170-8d03-173957340322.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990167640/ddea8280-4b2e-46c1-8c76-51c717653602.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990183610/44f25d89-d02f-4c9b-860e-b3d08373a167.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990197181/fe3849c9-0c63-4dcb-a0a8-6900a9c9c26b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990224107/aecdfc34-851c-42e9-a27a-8068aaa3cb37.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990244018/aa0f454c-3ebe-4d35-8b8f-d0fb72c971b2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990281354/c6beafaf-b0bc-419a-9b55-1628aed9fa7f.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-iam-policy-creation-steps">IAM Policy Creation Steps</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991591140/20eaaddd-a189-48d3-b4b2-c0e1643f2440.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991608466/17d1d77d-8972-4581-9e6f-0a632d31cffd.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726996783414/703f900d-215f-4784-bf3d-bef4c36681d8.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991682523/1e5343c8-8403-44f8-8b00-10706c049482.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991721655/e203131f-76e5-46b2-be19-98d3ffd9d392.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991737572/06ef0a96-e860-4820-aaaf-c286dfcf5749.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991750565/4cc52401-dc1b-4eb0-9e00-e5c52ca4ccce.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991761532/ff33b06d-3b9d-46a4-9959-bd744e5432da.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-generating-public-and-private-keys">Generating Public and Private Keys</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991941947/ae1cfe1f-2b5c-4369-96e1-c4a396dcec54.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726991978678/4e6e1fce-29d3-47aa-b2c3-e02ca8f5c341.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-steps-to-create-users-on-aws-transfer-family-server">Steps to Create Users on AWS Transfer Family Server</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726990812227/cf1c10e4-2f19-4874-b791-779cab120e68.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726992112074/4f0b90b6-a5f5-4290-a404-8f74c32d659f.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726992129344/01d5627a-2529-434d-9040-3c8eccbb3309.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726992220803/e852aedc-184d-4ed6-b68b-4838258be43e.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-connecting-mobaxterm-to-aws-transfer-family">Connecting MobaXterm to AWS Transfer Family</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726997588207/57892672-ab50-4081-9736-a9fdd1e3d9a6.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726997622212/6aa377a8-3b2c-47b7-997d-6c5c52ed70d6.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726997668139/637c1f14-7a27-4681-a6fa-c1b0aed5da4b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726997702684/4ed5ccec-d686-4ddd-81ea-ed1735d5ef6d.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Setting Up Your Android Phone as a Web Server: From SSH to Cloudflare Integration]]></title><description><![CDATA[Introduction:
Unlock the full potential of your Android phone by transforming it into a web server. This guide walks you through the essential steps to set up and manage your server directly from your mobile device. You’ll start by creating an SSH ke...]]></description><link>https://programminguides.hashnode.dev/setting-up-your-android-phone-as-a-web-server-from-ssh-to-cloudflare-integration</link><guid isPermaLink="true">https://programminguides.hashnode.dev/setting-up-your-android-phone-as-a-web-server-from-ssh-to-cloudflare-integration</guid><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Fri, 09 Aug 2024 18:07:53 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction:</strong></h3>
<p>Unlock the full potential of your Android phone by transforming it into a web server. This guide walks you through the essential steps to set up and manage your server directly from your mobile device. You’ll start by creating an SSH key pair, sending the public key to your Android device, and installing Termux, a powerful terminal emulator. After updating and linking your storage in Termux, you’ll configure SSH access by adding your public key to the <code>authorized_keys</code> file and starting the SSH daemon. Once connected to your Android phone via SSH, you’ll run your server with a simple command and install Cloudflare to secure and expose your server to the public. Follow these detailed instructions to get your web server up and running, with the added benefit of Cloudflare’s robust DNS and security features.</p>
<h3 id="heading-create-ssh-privatepublic-key-pair">Create SSH Private/Public Key Pair</h3>
<p>Generate an SSH key pair on your primary machine. This consists of a private key, which stays secure on your machine, and a public key, which you’ll share with your Android device. This key pair enables encrypted communication between your devices.</p>
<p><code>ssh-keygen -t ed25519 -f id_ed25519_android</code></p>
<p>After running the command you will get two keys generated. Send public key to your phone and store private key in your computer at <code>C:\Users\Account\.ssh</code> location.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723225507991/a5cdf198-3fe8-49a1-a68b-1f0e5decfe74.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-send-ssh-public-key-to-android-device">Send SSH Public Key to Android Device</h3>
<p>Transfer the public SSH key to your Android device. This step is crucial for establishing a secure connection and allows your machine to access the Android phone without needing to enter a password every time.</p>
<h3 id="heading-install-termux-on-android">Install Termux on Android</h3>
<p>Download and install Termux from the Google Play Store or F-Droid. Termux provides a powerful terminal emulator and Linux environment on your Android device, which is essential for running server software and executing commands.</p>
<h3 id="heading-updateupgrade-packages-in-termux">Update/Upgrade Packages in Termux</h3>
<p>Open Termux and update its package list and upgrade installed packages using the <code>pkg update</code> and <code>pkg upgrade</code> commands. Keeping your software up to date ensures you have the latest features and security patches.</p>
<pre><code class="lang-bash">pkg update
pkg upgrade
apt update
apt upgrade
pkg install git
pkg install nodejs-lts 
pkg install openssh
pkg install iproute2
pkg install nmap
</code></pre>
<h3 id="heading-link-android-storage-to-termux">Link Android Storage to Termux</h3>
<p>Grant Termux access to your Android device’s storage using the <code>termux-setup-storage</code> command. This step allows you to access files and directories on your device, which can be crucial for project files and data.</p>
<h3 id="heading-add-ssh-public-key-to-authorizedkeys">Add SSH Public Key to <code>authorized_keys</code></h3>
<p>Use below command to add transferred Public Key as authorized_keys</p>
<p><code>cat id_ed25519.pub &gt;&gt; ~/.ssh/authorized_keys</code></p>
<p><code>chmod 600 ~/.ssh/authorized_keys</code></p>
<h3 id="heading-start-ssh-daemon">Start SSH Daemon</h3>
<p>Launch the SSH daemon on your Android device by running the <code>sshd</code> command in Termux. This will enable your phone to accept SSH connections, allowing remote access and management.</p>
<h3 id="heading-get-ip-address-of-android-device">Get IP Address of Android Device</h3>
<p>Find the IP address of your Android device by executing <code>ip addr</code> or <code>ifconfig</code> in Termux. You’ll need this IP address to connect to your phone from your primary machine. Note: If you are unable to get IP Address via the above command you can navigate to Settings &gt; IP address &amp; Port to get your phone's IP Address. Make sure you have enabled Wireless Debugging (Developer Option &gt; Wireless Debugging)</p>
<h3 id="heading-connect-to-ssh-server-from-your-machine">Connect to SSH Server from Your Machine</h3>
<p>Use an SSH client on your main machine to connect to your Android phone using the IP address obtained earlier. This connection allows you to manage your Android-based web server remotely.</p>
<p><code>ssh -i ~/.ssh/id_ed25519_android 192.168.11.123 -p 8022</code></p>
<p>If at any point you get disconnected run <code>sshd</code> command to start SSH demon again.</p>
<p>For efficient use of SSH Client terminal on your laptop make sure to enable copy-paste functionality. Right Click on Terminal &gt; Properties &gt; Check the Copy Paste checkbox.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723226378117/25d35aad-fdc9-4eba-8116-8b956f2fe1b0.png" alt /></p>
<h3 id="heading-run-your-server">Run Your Server</h3>
<p>On your Android device, navigate to the directory containing your project and run <code>node index.js</code> (or the appropriate command for your server setup). This will start your server and make it accessible via your phone.</p>
<h3 id="heading-install-cloudflare-on-android">Install Cloudflare on Android</h3>
<p>Install Cloudflare’s tunneling service by running <code>pkg install cloudflared</code> in Termux. This tool will help you expose your local server to the internet securely.</p>
<h3 id="heading-create-cloudflare-tunnel">Create Cloudflare Tunnel</h3>
<p>Set up a Cloudflare tunnel to point to your local server. This configuration will make your Android-hosted server accessible via a publicly accessible domain, leveraging Cloudflare’s security and performance features.</p>
<p><code>cloudflared tunnel --url http://localhost:3000</code></p>
<h3 id="heading-references">References</h3>
<p><a target="_blank" href="https://developers.cloudflare.com/pages/how-to/preview-with-cloudflare-tunnel/">Preview local projects with Cloudflare Tunnel</a></p>
<p><a target="_blank" href="https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/">Install Cloudflare</a><br /><a target="_blank" href="https://github.com/rajbhx/cloudflared-termux">Installation of Cloudflared in Termux</a></p>
<p><a target="_blank" href="https://unix.stackexchange.com/questions/110684/copy-paste-into-sshd-vim-from-local-windows-clipboard">Copy/paste into SSH'd VIM from local (Windows) clipboard</a></p>
]]></content:encoded></item><item><title><![CDATA[Use Cases of SQL Server Integration Service]]></title><description><![CDATA[Importing Files to SQL Server

Import CSV files to SQL Server: SQL Server Integration Services (SSIS) provides a straightforward way to import CSV files into SQL Server databases. Using SSIS, you can create a package that includes a Flat File Source ...]]></description><link>https://programminguides.hashnode.dev/use-cases-of-sql-server-integration-service</link><guid isPermaLink="true">https://programminguides.hashnode.dev/use-cases-of-sql-server-integration-service</guid><category><![CDATA[SSIS]]></category><category><![CDATA[SQL]]></category><category><![CDATA[SQL Server]]></category><category><![CDATA[#SQLtutorial ]]></category><category><![CDATA[software development]]></category><category><![CDATA[Software Engineering]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Tue, 09 Jul 2024 13:53:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/LqKhnDzSF-8/upload/fc7a1cc2b0db615bbbbb39a84145a290.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-importing-files-to-sql-server">Importing Files to SQL Server</h3>
<ul>
<li><p><strong>Import CSV files to SQL Server:</strong> SQL Server Integration Services (SSIS) provides a straightforward way to import CSV files into SQL Server databases. Using SSIS, you can create a package that includes a Flat File Source component to read the CSV file and a SQL Server Destination component to write the data into a database table. SSIS handles the parsing of CSV files, ensuring that data types are correctly inferred and data is loaded efficiently.</p>
</li>
<li><p><strong>Load fixed width files to SQL Server:</strong> SSIS supports loading fixed width files into SQL Server databases by defining columns based on their positions within the file. You can use the Flat File Source component in SSIS to specify the column widths and data types, ensuring that each field is correctly extracted from the fixed width file. This process ensures accurate data loading and transformation into SQL Server tables.</p>
</li>
<li><p><strong>Load Excel files into SQL Server:</strong> SSIS simplifies the process of loading Excel files into SQL Server databases. With SSIS, you can configure an Excel Source component to read data from Excel spreadsheets, specifying the sheet name, range of cells, and data types. The data can then be transformed and loaded into SQL Server tables using a SQL Server Destination component. SSIS handles Excel data types and formats, ensuring compatibility and reliability in data integration tasks.</p>
</li>
<li><p><strong>Load XML files into SQL Server:</strong> SSIS facilitates the loading of XML files into SQL Server databases by providing XML Source and XML Destination components. You can configure the XML Source component to parse XML data, specifying XPath expressions to extract elements and attributes. SSIS can transform XML data into relational format and load it into SQL Server tables using the SQL Server Destination component. This process enables structured and efficient handling of XML data within SQL Server Integration Services.</p>
</li>
</ul>
<h3 id="heading-exporting-data-from-sql-server">Exporting Data from SQL Server</h3>
<ul>
<li><p><strong>Export data to CSV files:</strong> SSIS provides capabilities to export data from SQL Server databases to CSV files. You can use a Data Flow Task in SSIS, configuring a SQL Server Source component to retrieve data from the database and a Flat File Destination component to write the data to a CSV file. The Flat File Destination allows you to specify the delimiter (such as comma) and text qualifier, ensuring the exported CSV file is compatible with various applications and systems that consume CSV data.</p>
</li>
<li><p><strong>Export data to fixed width files:</strong> SSIS supports exporting data from SQL Server databases to fixed width files, where each column's width is predefined. Using a Data Flow Task, you can configure a SQL Server Source component to fetch data and a Flat File Destination component to write the data into a fixed width file. In SSIS, you define column mappings and specify the start and end positions for each column, ensuring that data is exported correctly formatted according to fixed width specifications.</p>
</li>
<li><p><strong>Export data to Excel files:</strong> SSIS facilitates exporting data from SQL Server databases to Excel files, leveraging Excel Destination components. With a Data Flow Task, you can configure a SQL Server Source component to retrieve data and an Excel Destination component to write the data into an Excel spreadsheet. SSIS allows you to specify the target sheet name, cell range, and data types, ensuring compatibility and proper formatting within Excel files. This approach provides flexibility in exporting SQL Server data to Excel for reporting and analysis purposes.</p>
</li>
</ul>
<h3 id="heading-data-transfer-operations">Data Transfer Operations</h3>
<ul>
<li><p><strong>Copy data between SQL Server instances using SSIS:</strong> SQL Server Integration Services (SSIS) facilitates the transfer of data between different SQL Server instances through its Data Flow Task. This task allows you to configure a SQL Server Source to extract data from a source instance and a SQL Server Destination to load it into a destination instance. SSIS supports mapping, transformations, error handling, and performance optimizations to ensure reliable and efficient data migration across SQL Server environments.</p>
</li>
<li><p><strong>Execute SQL tasks in SSIS:</strong> SSIS enables the execution of SQL tasks within its Control Flow, providing capabilities to perform various database operations such as executing SQL statements, running stored procedures, and managing transactions. The Execute SQL Task allows you to connect to SQL Server databases, execute commands, capture results, handle errors, and integrate these tasks seamlessly with other SSIS components. This feature enhances automation and flexibility in managing SQL operations as part of larger data integration workflows.</p>
</li>
</ul>
<h3 id="heading-file-system-operations">File System Operations</h3>
<ul>
<li><p><strong>File System Tasks in SSIS:</strong> SSIS File System Tasks allow integration workflows to interact with files and directories on the operating system level. These tasks encompass operations such as copying, moving, renaming, deleting files, creating directories, and setting attributes. They are crucial for automating file-related tasks within SSIS packages, offering robust configuration options to manage file operations seamlessly alongside data integration processes.</p>
</li>
<li><p><strong>Zip and Unzip Files in SSIS:</strong> SSIS provides capabilities to zip and unzip files, enhancing data management and storage efficiency. The Zip task compresses files into a zip archive format, reducing storage space and facilitating easier transmission. Conversely, the Unzip task extracts files from zip archives, enabling access to compressed data for further processing within SSIS workflows. These tasks streamline file handling operations, supporting comprehensive data integration and management tasks in SQL Server Integration Services.</p>
</li>
</ul>
<h3 id="heading-advanced-data-handling">Advanced Data Handling</h3>
<ul>
<li><strong>Load multiple Excel sheets using SSIS</strong>: SSIS allows you to load data from multiple Excel sheets in a straightforward manner. SSIS supports parallel processing of multiple sheets, optimizing performance during data extraction.</li>
</ul>
<h3 id="heading-ssis-transformations">SSIS Transformations</h3>
<ul>
<li><p><strong>Aggregate transformation:</strong> Computes aggregate values such as SUM, AVG, MIN, MAX on groups of rows.</p>
</li>
<li><p><strong>Row count transformation:</strong> Counts rows passing through it and stores the count in a variable.</p>
</li>
<li><p><strong>Data conversion transformation:</strong> Converts data from one data type to another.</p>
</li>
<li><p><strong>Character map transformation:</strong> Performs character-level operations like changing case or replacing characters based on defined mappings.</p>
</li>
<li><p><strong>Copy column transformation:</strong> Copies data from one column to another within the data flow.</p>
</li>
<li><p><strong>Derived column transformation:</strong> Creates new columns using expressions based on existing column values.</p>
</li>
<li><p><strong>Multicast transformation:</strong> Copies data to multiple outputs for parallel processing.</p>
</li>
<li><p><strong>Conditional split transformation:</strong> Routes rows to different outputs based on conditions.</p>
</li>
<li><p><strong>Union all transformation:</strong> Combines multiple data flows into a single output without any transformation.</p>
</li>
<li><p><strong>Merge transformation:</strong> Combines two sorted datasets into one dataset.</p>
</li>
<li><p><strong>Merge join transformation:</strong> Joins two datasets based on matching keys.</p>
</li>
<li><p><strong>Script component transformation:</strong> Allows custom transformations using C# or <a target="_blank" href="http://VB.NET">VB.NET</a> scripts.</p>
</li>
<li><p><strong>OLE DB command transformation:</strong> Executes SQL commands for each row in the data flow.</p>
</li>
<li><p><strong>Lookup transformation:</strong> Performs lookups to retrieve related data from a reference dataset.</p>
</li>
<li><p><strong>Fuzzy lookup transformation:</strong> Matches data based on similarity rather than exact matches.</p>
</li>
<li><p><strong>Fuzzy grouping transformation:</strong> Groups data based on similarity rather than exact matches for aggregation purposes.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Getting started with SQL Server Integration Services]]></title><description><![CDATA[Introduction to SSIS (SQL Server Integration Services)
SQL Server Integration Services (SSIS) is a powerful data integration and transformation tool provided by Microsoft as part of the SQL Server suite. It is used for building data integration and w...]]></description><link>https://programminguides.hashnode.dev/getting-started-with-sql-server-integration-services</link><guid isPermaLink="true">https://programminguides.hashnode.dev/getting-started-with-sql-server-integration-services</guid><category><![CDATA[Software Engineering]]></category><category><![CDATA[SQL]]></category><category><![CDATA[SSIS]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Tue, 09 Jul 2024 12:56:02 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/9HI8UJMSdZA/upload/a208307d8fa23efa491a3111ce0c6a8e.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction-to-ssis-sql-server-integration-services">Introduction to SSIS (SQL Server Integration Services)</h3>
<p>SQL Server Integration Services (SSIS) is a powerful data integration and transformation tool provided by Microsoft as part of the SQL Server suite. It is used for building data integration and workflow solutions.</p>
<p>Here are several reasons why SSIS is valuable and why we need it:</p>
<ol>
<li><p><strong>Data Integration:</strong> SSIS allows organizations to integrate data from various sources such as databases, flat files, Excel spreadsheets, and more. This is crucial for businesses that need to consolidate data from multiple systems into a central data warehouse or data lake.</p>
</li>
<li><p><strong>Data Transformation:</strong> SSIS provides a wide range of transformations that can be applied to data as it moves from source to destination. These transformations include cleaning, aggregating, merging, and validating data to ensure it meets the business requirements.</p>
</li>
<li><p><strong>Workflow Orchestration:</strong> SSIS enables the creation of complex workflows or data pipelines. These workflows can automate the execution of tasks such as data extraction, transformation, and loading (ETL), making data integration processes more efficient and reliable.</p>
</li>
<li><p><strong>Scalability:</strong> SSIS is designed to handle large volumes of data efficiently. It supports parallel processing, which improves performance when dealing with large datasets.</p>
</li>
<li><p><strong>Extensibility:</strong> SSIS provides a rich set of tools and APIs that allow developers to extend its capabilities. Custom components can be created to address specific business requirements or integrate with other systems.</p>
</li>
<li><p><strong>Maintenance and Monitoring:</strong> SSIS includes features for monitoring package execution, logging events, and handling errors. This helps administrators and developers identify issues quickly and ensure data integrity.</p>
</li>
<li><p><strong>Integration with SQL Server and Microsoft Ecosystem:</strong> SSIS integrates seamlessly with SQL Server databases and other Microsoft products such as Azure Data Services, Excel, SharePoint, and Dynamics. This makes it easier to leverage existing investments in Microsoft technologies.</p>
</li>
<li><p><strong>Compliance and Security:</strong> SSIS provides features for managing access control, encrypting sensitive data, and ensuring compliance with regulatory requirements such as GDPR or HIPAA.</p>
</li>
</ol>
<h3 id="heading-core-components-of-ssis">Core Components of SSIS</h3>
<p>In SQL Server Integration Services (SSIS), several key components and features play crucial roles in designing and executing data integration workflows. Let's break down each of these elements:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720528883274/8096225a-4432-44b5-a537-3921325fd60e.png" alt class="image--center mx-auto" /></p>
<p><strong>Control Flow Task</strong></p>
<p>The Control Flow in SSIS defines the workflow or logical structure of tasks that execute in a specified order. Control Flow tasks include operations such as executing SQL commands, running scripts, sending emails, or executing other packages.</p>
<p><strong>Examples of Control Flow tasks:</strong></p>
<ul>
<li><p><strong>Execute SQL Task:</strong> Executes SQL statements or stored procedures.</p>
</li>
<li><p><strong>Script Task:</strong> Runs custom code written in languages like C# or <a target="_blank" href="http://VB.NET">VB.NET</a>.</p>
</li>
<li><p><strong>Data Flow Task:</strong> Executes a Data Flow, which moves and transforms data between sources and destinations.</p>
</li>
<li><p><strong>Execute Package Task:</strong> Runs another SSIS package as part of the workflow.</p>
</li>
<li><p><strong>Send Mail Task:</strong> Sends email notifications during package execution.</p>
</li>
<li><p><strong>File System Task:</strong> Performs operations on files and directories, like copying, moving, or deleting.</p>
</li>
</ul>
<p><strong>Data Flow Task</strong></p>
<p>The Data Flow in SSIS is where data transformations occur. It enables the movement, manipulation, and transformation of data between sources and destinations. It consists of sources, transformations, and destinations.</p>
<p><strong>Components of a Data Flow task:</strong></p>
<ul>
<li><p><strong>Source:</strong> Retrieves data from a source system (e.g., database table, flat file).</p>
</li>
<li><p><strong>Transformations:</strong> Modify, clean, aggregate, or join data as it moves through the pipeline.</p>
</li>
<li><p><strong>Destination:</strong> Loads transformed data into a target system (e.g., database table, flat file).</p>
</li>
</ul>
<p><strong>Parameters</strong></p>
<p>Parameters in SSIS allow you to pass values at runtime to packages or tasks. They provide flexibility and make packages easier to configure and reuse.</p>
<p><strong>Types of Parameters:</strong></p>
<ul>
<li><p><strong>Package Parameters:</strong> Defined at the package level and can be used by all tasks within the package.</p>
</li>
<li><p><strong>Project Parameters:</strong> Defined at the project level and can be used across packages within the same project.</p>
</li>
<li><p><strong>Environment Parameters:</strong> Stored in SSISDB (SSIS catalog) and can be used to configure packages deployed to different environments (development, test, production).</p>
</li>
</ul>
<p><strong>Event Handlers</strong></p>
<p>Event Handlers in SSIS are workflows that respond to specific events raised during package execution. They allow you to handle errors, perform additional logging, or execute specific tasks based on the outcome of package events.</p>
<p><strong>Types of Event Handlers:</strong></p>
<ul>
<li><p><strong>OnError:</strong> Executes when an error occurs during package execution.</p>
</li>
<li><p><strong>OnTaskFailed:</strong> Executes when a specific task fails.</p>
</li>
<li><p><strong>OnWarning:</strong> Executes when a warning is generated during package execution.</p>
</li>
<li><p><strong>OnPreExecute:</strong> Executes just before a task begins execution.</p>
</li>
<li><p><strong>OnPostExecute:</strong> Executes immediately after a task completes successfully.</p>
</li>
<li><p><strong>OnProgress:</strong> Executes periodically during the execution of long-running tasks.</p>
</li>
</ul>
<h3 id="heading-installing-ssis-in-visual-studio">Installing SSIS in Visual Studio</h3>
<p>To install SSIS in Visual Studio, begin by downloading the Microsoft Data Tools - Integration Services extension from the Visual Studio Marketplace <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=SSIS.MicrosoftDataToolsIntegrationServices">here</a>.</p>
<ol>
<li><p><strong>Install SSIS Package in Visual Studio:</strong></p>
<ul>
<li>Visit <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=SSIS.MicrosoftDataToolsIntegrationServices">Microsoft Data Tools - Integration Services</a> and install the extension.</li>
</ul>
</li>
<li><p><strong>Create SSIS Project:</strong></p>
<ul>
<li><p>Open Visual Studio and select <strong>Create a new project</strong>.</p>
</li>
<li><p>Choose <strong>Integration Services Project</strong> from the available project templates.</p>
</li>
</ul>
</li>
<li><p><strong>Detailed Installation Guide:</strong></p>
<ul>
<li>For comprehensive installation instructions, refer to this instructional video: <a target="_blank" href="https://www.youtube.com/watch?v=XUIidSEsUCI">Installation Guide</a>.</li>
</ul>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720529131099/964319f2-720f-4a73-8c40-58d9a4098383.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-installing-ssis-in-sql-server">Installing SSIS in SQL Server</h3>
<p>To install SSIS in SQL Server, follow these steps:</p>
<ol>
<li><p><strong>Download SQL Server:</strong></p>
<ul>
<li><p>Download SQL Server from <a target="_blank" href="https://www.microsoft.com/en-ca/sql-server/sql-server-downloads">Microsoft's official website</a> and create an ISO file.</p>
<ul>
<li>To create ISO file click on setup and then select Download Media option.</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Installation Process:</strong></p>
<ul>
<li><p>Open the ISO file and run the setup.</p>
</li>
<li><p>Select "New SQL Server standalone installation" during setup.</p>
</li>
</ul>
</li>
<li><p><strong>Component Selection:</strong></p>
<ul>
<li><p>In the installation wizard, ensure to select the following checkboxes:</p>
<ul>
<li><p>Database Engine Services</p>
</li>
<li><p>Integration Services</p>
</li>
<li><p>Scale Out Master</p>
</li>
<li><p>Scale Out Worker</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Detailed Installation Guide:</strong></p>
<ul>
<li>For a detailed walkthrough, watch this instructional video: <a target="_blank" href="https://www.youtube.com/watch?v=3-xIc4ojrSA">Installation Guide</a>.</li>
</ul>
</li>
<li><p><strong>Post-Installation Tips:</strong></p>
<ul>
<li>After installation, use SQL Server Management Studio (SSMS) with Administrator permissions to avoid errors when accessing Integrated Service features.</li>
</ul>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720529477890/f062b96a-8c4d-4ff5-ab2e-1b139f07dc32.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720529508140/9b2b3b91-9468-4af5-b73f-1e094874a15f.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720529535235/8a1573be-96d6-4e44-a022-da1ef7d280d0.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720529588305/93c8b117-f673-4a5d-8ce8-8a70a0c7c107.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Efficient Strategies for Populating Large Datasets in SQL Databases]]></title><description><![CDATA[Introduction
Populating large datasets in SQL databases efficiently is a critical task for many applications, ranging from data warehousing to analytics platforms. However, inserting a large amount of data can be challenging and may impact database p...]]></description><link>https://programminguides.hashnode.dev/efficient-strategies-for-populating-large-datasets-in-sql-databases</link><guid isPermaLink="true">https://programminguides.hashnode.dev/efficient-strategies-for-populating-large-datasets-in-sql-databases</guid><category><![CDATA[SQL]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Mon, 15 Apr 2024 09:11:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/fkugMBhVf1c/upload/622ad1ffba38a04a7b0f857076e4f47c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction</h3>
<p>Populating large datasets in SQL databases efficiently is a critical task for many applications, ranging from data warehousing to analytics platforms. However, inserting a large amount of data can be challenging and may impact database performance if not done properly. In this article, we'll explore strategies for efficiently populating large datasets in SQL databases, focusing on best practices and optimizations. We'll also provide examples, including the use of SQL queries for bulk data insertion.</p>
<ol>
<li><p><strong>Data Preparation</strong>: Before populating large datasets, it's essential to prepare the data and ensure that it's in the right format. This includes cleaning the data, transforming it if necessary, and organizing it into batches for efficient insertion.</p>
</li>
<li><p><strong>Batch Insertion</strong>: One of the most efficient ways to insert large amounts of data into a SQL database is through batch insertion. Instead of inserting one row at a time, batch insertion allows multiple rows to be inserted in a single transaction, reducing overhead and improving performance.</p>
</li>
<li><p><strong>Using SQL Bulk Insert</strong>: SQL databases often provide mechanisms for bulk data insertion, such as the SQL Server's Bulk Insert statement or PostgreSQL's COPY command. These methods are optimized for inserting large volumes of data quickly and efficiently.</p>
</li>
</ol>
<p><strong>Example Query</strong>: Let's consider an example of populating a large dataset using a SQL query:</p>
<pre><code class="lang-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> YourTableName (
    <span class="hljs-keyword">id</span> <span class="hljs-built_in">INT</span> PRIMARY <span class="hljs-keyword">KEY</span>,
    <span class="hljs-keyword">name</span> <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-keyword">MAX</span>),
    description <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-keyword">MAX</span>),
    notes <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-keyword">MAX</span>)
);
</code></pre>
<pre><code class="lang-sql"><span class="hljs-keyword">DECLARE</span> @Counter <span class="hljs-built_in">INT</span> = <span class="hljs-number">0</span>;
<span class="hljs-keyword">DECLARE</span> @<span class="hljs-keyword">Name</span> <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-keyword">MAX</span>);
<span class="hljs-keyword">DECLARE</span> @Description <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-keyword">MAX</span>);
<span class="hljs-keyword">DECLARE</span> @Notes <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-keyword">MAX</span>);

<span class="hljs-comment">-- Begin transaction</span>
<span class="hljs-keyword">BEGIN</span> <span class="hljs-keyword">TRANSACTION</span>;

<span class="hljs-comment">-- Loop to insert data</span>
WHILE @Counter &lt; 1000000  <span class="hljs-comment">-- Inserting 1 million rows</span>
<span class="hljs-keyword">BEGIN</span>
    <span class="hljs-keyword">SET</span> @<span class="hljs-keyword">Name</span> = <span class="hljs-string">'Name_'</span> + <span class="hljs-keyword">CAST</span>(@Counter <span class="hljs-keyword">AS</span> <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-number">10</span>));
    <span class="hljs-keyword">SET</span> @Description = <span class="hljs-string">'Description_'</span> + <span class="hljs-keyword">CAST</span>(@Counter <span class="hljs-keyword">AS</span> <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-number">10</span>));
    <span class="hljs-keyword">SET</span> @Notes = <span class="hljs-string">'Notes_'</span> + <span class="hljs-keyword">CAST</span>(@Counter <span class="hljs-keyword">AS</span> <span class="hljs-built_in">VARCHAR</span>(<span class="hljs-number">10</span>));

    <span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> YourTableName (<span class="hljs-keyword">id</span>, <span class="hljs-keyword">name</span>, description, notes)
    <span class="hljs-keyword">VALUES</span> (@Counter, @<span class="hljs-keyword">Name</span>, @Description, @Notes);

    <span class="hljs-keyword">SET</span> @Counter = @Counter + <span class="hljs-number">1</span>;
<span class="hljs-keyword">END</span>;

<span class="hljs-comment">-- Commit transaction</span>
<span class="hljs-keyword">COMMIT</span> <span class="hljs-keyword">TRANSACTION</span>;
</code></pre>
<p>In this query:</p>
<ul>
<li><p>We declare variables for the columns to be inserted.</p>
</li>
<li><p>We start a transaction to ensure data consistency.</p>
</li>
<li><p>We use a loop to generate data and insert it into the table in batches.</p>
</li>
<li><p>Finally, we commit the transaction to make the changes permanent.</p>
</li>
</ul>
<h3 id="heading-conclusion">Conclusion</h3>
<p>Efficiently populating large datasets in SQL databases requires careful planning and optimization. By following best practices such as batch insertion and using database-specific bulk insertion methods, you can improve performance and minimize the impact on database resources. Additionally, leveraging the power of SQL queries for data population can streamline the process and make it easier to manage large-scale data operations.</p>
]]></content:encoded></item><item><title><![CDATA[Harnessing the Potential of AWS API Gateway for REST APIs]]></title><description><![CDATA[In the dynamic realm of modern software development, AWS API Gateway emerges as a pivotal service, offering a comprehensive solution for building, managing, and securing REST APIs on the Amazon Web Services (AWS) platform. With its rich set of featur...]]></description><link>https://programminguides.hashnode.dev/harnessing-the-potential-of-aws-api-gateway-for-rest-apis</link><guid isPermaLink="true">https://programminguides.hashnode.dev/harnessing-the-potential-of-aws-api-gateway-for-rest-apis</guid><category><![CDATA[AWS]]></category><category><![CDATA[APIs]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Thu, 04 Apr 2024 04:54:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/DSn1k2kCriA/upload/8f0234aeb8ded7335eaa4e8cb20d57ef.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the dynamic realm of modern software development, AWS API Gateway emerges as a pivotal service, offering a comprehensive solution for building, managing, and securing REST APIs on the Amazon Web Services (AWS) platform. With its rich set of features and seamless integration with other AWS services, API Gateway empowers developers to unleash the full potential of their REST APIs. Let's delve into three key functionalities that AWS API Gateway provides for REST APIs:</p>
<ol>
<li><p><strong>Query String Validation</strong>: AWS API Gateway simplifies the process of validating query strings, enabling developers to define rules for required or optional query parameters with ease. Through the intuitive API Gateway console or the AWS Management Console, developers can configure parameter constraints, data types, and validation rules effortlessly. This feature ensures that incoming requests conform to the expected structure, enhancing the reliability and security of the API endpoints.</p>
</li>
<li><p><strong>Request Body Validation with Custom Models</strong>: Building upon the foundation of query string validation, AWS API Gateway offers robust support for validating request bodies using custom models. By leveraging JSON Schema or AWS-specific models such as AWS API Gateway Models, developers can define intricate data structures and enforce strict validation rules for incoming payloads. Whether it's validating complex nested objects or ensuring data integrity, API Gateway provides a flexible and scalable solution to validate request bodies effectively.</p>
</li>
<li><p><strong>API Key and Usage Plan Management</strong>: Security and scalability are paramount concerns in API management, and AWS API Gateway addresses these challenges adeptly with its built-in support for API key and usage plan management. With API Gateway, developers can effortlessly generate API keys, associate them with usage plans, and enforce fine-grained access control policies. By configuring usage quotas, rate limits, and API throttling, developers can safeguard their APIs against abuse while optimizing resource utilization. Moreover, API Gateway seamlessly integrates with AWS Identity and Access Management (IAM), enabling developers to manage access permissions and authentication mechanisms seamlessly.</p>
</li>
</ol>
<p>In conclusion, AWS API Gateway serves as a cornerstone for building resilient, scalable, and secure REST APIs on the AWS cloud. From query string validation to request body validation with custom models, and API key and usage plan management, API Gateway offers a plethora of features to streamline API development and management workflows. By harnessing the power of AWS API Gateway, developers can accelerate their journey towards building resilient and scalable applications while ensuring the highest standards of security and reliability.</p>
]]></content:encoded></item><item><title><![CDATA[Complete Guide: Triggering AWS Lambda Functions via S3 Bucket Events]]></title><description><![CDATA[Setup Lambda Function

Click on the "Create Function" button.
  

Give the desired name and click on "Create Function" button.


  After creation, you will get a dashboard like this.
  


Setup S3 Bucket

Click on the "Create Bucket" button.


  Now ...]]></description><link>https://programminguides.hashnode.dev/complete-guide-triggering-aws-lambda-functions-via-s3-bucket-events</link><guid isPermaLink="true">https://programminguides.hashnode.dev/complete-guide-triggering-aws-lambda-functions-via-s3-bucket-events</guid><category><![CDATA[AWS]]></category><category><![CDATA[S3]]></category><category><![CDATA[lambda]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Sun, 24 Mar 2024 17:40:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/LVJRzXqbJ1s/upload/6ef901ca8876c654b097dea849095d47.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-setup-lambda-function"><strong>Setup Lambda Function</strong></h3>
<ul>
<li><p>Click on the "Create Function" button.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711298608220/d9e23419-9303-44e1-a390-11aba580dd54.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Give the desired name and click on "Create Function" button.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711298677841/969ca24c-a3a2-4c17-888e-dffabcbbb709.png" alt class="image--center mx-auto" /></p>
<p>  After creation, you will get a dashboard like this.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711298747981/0e0823e1-dcdf-464e-b745-4694e34d9bda.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h3 id="heading-setup-s3-bucket"><strong>Setup S3 Bucket</strong></h3>
<ul>
<li><p>Click on the "Create Bucket" button.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711298822219/092e75e9-78ef-4275-9530-e5849d4deadb.png" alt class="image--center mx-auto" /></p>
<p>  Now give the desired name and click on the create button.</p>
</li>
</ul>
<h3 id="heading-create-iam-to-have-access-to-s3"><strong>Create IAM to Have Access to S3</strong></h3>
<ul>
<li><p>Click on the "Create Role" button to create a new IAM role.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299020594/f5e80ddf-75f2-4451-a661-65ef9832d0e7.png" alt class="image--center mx-auto" /></p>
<p>  Select the use case as Lambda.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299054301/18a0fdd8-ae41-4c5e-a022-c6f89cc4cf18.png" alt class="image--center mx-auto" /></p>
<p>  Now select S3 full access IAM role.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299101136/74f4d706-4fa0-4f54-a08d-78bec8d4527e.png" alt class="image--center mx-auto" /></p>
<p>  Finally, give a name to your IAM role and click on the create button.</p>
</li>
</ul>
<h3 id="heading-attach-iam-to-your-lambda"><strong>Attach IAM to Your Lambda</strong></h3>
<ul>
<li><p>Navigate to your Lambda and select the "Permissions" tab from the configuration and click on the "Edit" button.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299288246/eaaf17a8-3c0b-4335-ac72-3d28222f51ae.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Select the name of IAM role you have created and click on the save button.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299380970/49f838a3-9677-4489-b79e-630fc0597749.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h3 id="heading-add-the-trigger"><strong>Add the Trigger</strong></h3>
<ul>
<li><p>Now click on "Add Trigger" button to add the trigger.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299453884/d5c28bac-b44e-419e-963f-9b62a1d81e08.png" alt class="image--center mx-auto" /></p>
<p>  Select S3 and your bucket name from the dropdown and click on "Add" button.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299496782/465d2533-3670-446c-9f0b-9ec554666446.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h3 id="heading-write-below-code-and-push-to-aws-lambda"><strong>Write Below Code and Push to AWS Lambda</strong></h3>
<ul>
<li><p>Now write the below code in <code>index.js</code>, install <code>aws-sdk</code> package via npm</p>
</li>
<li><p>Create the zip file.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299634072/05c58cde-b844-4198-8efb-1071a4ae27a2.png" alt class="image--center mx-auto" /></p>
<p>  Click on "Upload From" button to upload this .zip file.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299722976/d86d53f1-3936-44b6-ac61-99ea4bc1cf22.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> AWS = <span class="hljs-built_in">require</span>(<span class="hljs-string">'aws-sdk'</span>);
  <span class="hljs-keyword">const</span> s3 = <span class="hljs-keyword">new</span> AWS.S3();

  <span class="hljs-built_in">exports</span>.handler = <span class="hljs-keyword">async</span> (event) =&gt; {
      <span class="hljs-keyword">try</span> {
          <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> record <span class="hljs-keyword">of</span> event.Records) {
              <span class="hljs-keyword">const</span> bucketName = record.s3.bucket.name;
              <span class="hljs-keyword">const</span> objectKey = <span class="hljs-built_in">decodeURIComponent</span>(record.s3.object.key.replace(<span class="hljs-regexp">/\+/g</span>, <span class="hljs-string">' '</span>));
              <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Bucket Name:'</span>, bucketName);
              <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Object Key:'</span>, objectKey);

              <span class="hljs-keyword">const</span> getObjectParams = {
                  <span class="hljs-attr">Bucket</span>: bucketName,
                  <span class="hljs-attr">Key</span>: objectKey
              };
              <span class="hljs-keyword">const</span> fileContent = <span class="hljs-keyword">await</span> s3.getObject(getObjectParams).promise();
              <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'File Content:'</span>, fileContent.Body.toString(<span class="hljs-string">'utf-8'</span>));
          }

          <span class="hljs-keyword">return</span> {
              <span class="hljs-attr">statusCode</span>: <span class="hljs-number">200</span>,
              <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
                  <span class="hljs-attr">message</span>: <span class="hljs-string">'Successfully processed all S3 file uploads'</span>
              })
          };
      } <span class="hljs-keyword">catch</span> (error) {
          <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Error:'</span>, error);
          <span class="hljs-keyword">return</span> {
              <span class="hljs-attr">statusCode</span>: <span class="hljs-number">500</span>,
              <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
                  <span class="hljs-attr">message</span>: <span class="hljs-string">'Error processing S3 file uploads'</span>,
                  <span class="hljs-attr">error</span>: error.message
              })
          };
      }
  };
</code></pre>
</li>
</ul>
<h3 id="heading-whenever-you-upload-anything-on-s3-lambda-will-get-triggered"><strong>Whenever You Upload Anything on S3 Lambda Will Get Triggered</strong></h3>
<ul>
<li><p>Inside the bucket add the file by clicking on "Upload" button.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711299963354/fc1ec95d-1b20-4a8a-8258-336d1585834a.png" alt class="image--center mx-auto" /></p>
<p>  Now navigate to your Lambda function and click on cloud watch log group to check the logs.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711300010958/92f15efa-2117-4a07-96a8-17aa3708bf3b.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h3 id="heading-conclusion">Conclusion</h3>
<p>From the Logs We Can See That Lambda Got Triggered when file uploaded to S3.</p>
<p>It Was Able to Read Bucket Name, File Name and Its Content.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711300456940/bfde7293-7c44-448c-b9d3-b154cf86e9c7.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[How to Set Up and Configure API Gateway on AWS: A Comprehensive Guide]]></title><description><![CDATA[Navigate to API Gateway:

Open your web browser and go to the API Gateway service provided by your cloud provider.
  


Click on Create API button:

Look for the "Create API" button on the API Gateway dashboard and click on it.

In the API creation w...]]></description><link>https://programminguides.hashnode.dev/how-to-set-up-and-configure-api-gateway-on-aws-a-comprehensive-guide</link><guid isPermaLink="true">https://programminguides.hashnode.dev/how-to-set-up-and-configure-api-gateway-on-aws-a-comprehensive-guide</guid><category><![CDATA[AWS]]></category><category><![CDATA[serverless]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Thu, 21 Mar 2024 13:26:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/VV2XHRdJE18/upload/a530504e0ee431c77988e8ced34481ae.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Navigate to API Gateway</strong>:</p>
<ul>
<li><p>Open your web browser and go to the API Gateway service provided by your cloud provider.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025158104/869f02ef-117d-4e69-a359-6407366b07c5.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p><strong>Click on Create API button</strong>:</p>
<ul>
<li><p>Look for the "Create API" button on the API Gateway dashboard and click on it.</p>
</li>
<li><p>In the API creation wizard, select the type of API you want to create. For this example, select "HTTP API". Then click on the "Build" button to proceed.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025196102/968ab0b4-0164-4687-9975-61445ded92ea.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p><strong>Add Integration</strong>:</p>
<p>Now, you need to point this route to an already configured backend. Click on the "Add Integration" button.</p>
<ul>
<li><p>In the integration settings, add the server URL where your backend service is hosted. Also, give your API a name to identify it later.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025398490/4252c63a-2874-473f-94ef-02f65725b18b.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025256876/d1b3f78f-a4ad-4989-bdca-9b47af7e7708.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p><strong>Define Resource Path</strong>:</p>
<ul>
<li><p>After setting up the integration, define the resource path. This is the URL path where your API will be accessible.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025436245/ce44302d-d5ac-4a15-a834-c75bb7f8a6f0.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p><strong>Add Stage Name</strong>:</p>
<ul>
<li><p>Next, add a stage name for your API. The stage represents a specific deployment of your API (e.g., "development", "production", etc.).</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025687819/f5947aa6-d365-43e6-bbbe-237d101cc7e1.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p><strong>Click on Create Button</strong>:</p>
<ul>
<li><p>Once you've configured all the necessary settings, click on the "Create" button. This will create your API with the specified configurations.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025755380/520dc9b0-eb69-4ac8-ac7f-eac36de76d3b.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p><strong>Deploy your API</strong>:</p>
<ul>
<li><p>After creating the API, it needs to be deployed to make it accessible. Click on the "Deploy" button.</p>
</li>
<li><p>Select the stage that you've created earlier (e.g., "development", "production", etc.) from the dropdown menu.</p>
</li>
<li><p>Confirm the deployment by clicking on the "Deploy" button. After deployment you will get dashboard like this</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025843737/3490df9f-55bd-4480-9795-c63b0c57146f.png" alt class="image--center mx-auto" /></p>
<p>  <strong>Access the Dashboard</strong>:</p>
<ul>
<li><p>After deployment, you will be directed to a dashboard where you can manage your API.</p>
</li>
<li><p>Here, you can monitor usage, set up custom domains, configure authorizers, etc.</p>
</li>
</ul>
</li>
</ul>
<p>    <strong>Use the API</strong>:</p>
<ul>
<li><p>To use your API, simply copy the provided endpoint URL (usually displayed on the dashboard or deployment confirmation page) along with your route.</p>
</li>
<li><p>Use this URL to make requests to your API. You should receive responses from your backend service accordingly.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711025955263/8a74009a-b960-46f9-8358-5033020482bb.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Infinite Scrolling: Intersection Observer]]></title><description><![CDATA[The Intersection Observer is an API used to detect when a specific element enters the viewport. With this functionality, we can create logic that depends on the visibility of that particular element. For example, fetching data when a specific element...]]></description><link>https://programminguides.hashnode.dev/infinite-scrolling-intersection-observer</link><guid isPermaLink="true">https://programminguides.hashnode.dev/infinite-scrolling-intersection-observer</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[interview]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Sun, 10 Dec 2023 09:41:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702200856410/e1f1a5b8-dbb8-45f1-ab3e-ecd06da6b30e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The Intersection Observer is an API used to detect when a specific element enters the viewport. With this functionality, we can create logic that depends on the visibility of that particular element. For example, fetching data when a specific element is reached, implementing infinite scroll, collecting user interactions with a particular section, and more.</p>
<p>In the code below, there is a 'colors' array and a 'moreColors' array. When we reach the end of the page at "this is a div with a black color," more colors are fetched, thereby implementing infinite scroll.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useEffect, useRef, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./App.css"</span>

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> moreColors = [<span class="hljs-string">'#FF00FF'</span>, <span class="hljs-string">'#00FFFF'</span>, <span class="hljs-string">'#FFD700'</span>, <span class="hljs-string">'#8A2BE2'</span>, 
  <span class="hljs-string">'#00FF00'</span>, <span class="hljs-string">'#FF4500'</span>, <span class="hljs-string">'#7FFF00'</span>, <span class="hljs-string">'#FF1493'</span>, <span class="hljs-string">'#32CD32'</span>, <span class="hljs-string">'#FF8C00'</span>];
  <span class="hljs-keyword">const</span> lastElement = useRef();

  <span class="hljs-keyword">const</span> [colors, setColors] = useState([<span class="hljs-string">'#FF5733'</span>, <span class="hljs-string">'#33FF57'</span>, 
  <span class="hljs-string">'#5733FF'</span>, <span class="hljs-string">'#FF33A6'</span>, <span class="hljs-string">'#33A6FF'</span>, <span class="hljs-string">'#A6FF33'</span>, <span class="hljs-string">'#FF3366'</span>, 
  <span class="hljs-string">'#3366FF'</span>, <span class="hljs-string">'#66FF33'</span>, <span class="hljs-string">'#FF6633'</span>]);
  <span class="hljs-keyword">const</span> [isVisible, setIsVisible] = useState(<span class="hljs-literal">false</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> observer = <span class="hljs-keyword">new</span> IntersectionObserver(<span class="hljs-function">(<span class="hljs-params">entries</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> entry = entries[<span class="hljs-number">0</span>];
      setIsVisible(entry.isIntersecting);
    })
    observer.observe(lastElement.current);
  }, [])

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">if</span> (isVisible === <span class="hljs-literal">true</span>) {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Fetch more colors..."</span>);
      setColors(<span class="hljs-function">(<span class="hljs-params">preColors</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> [...preColors, ...moreColors];
      });
      <span class="hljs-built_in">console</span>.log(colors);
    }
  }, [isVisible])

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {colors &amp;&amp; colors.map((color, index) =&gt; {
        return (
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">backgroundColor:</span> <span class="hljs-attr">color</span> }} 
           <span class="hljs-attr">className</span>=<span class="hljs-string">"divClass"</span>&gt;</span>
            this is div with {color} color.
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        )
      })}
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">backgroundColor:</span> "<span class="hljs-attr">black</span>" }} 
       <span class="hljs-attr">className</span>=<span class="hljs-string">"divClass"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{lastElement}</span>&gt;</span>
        this is div with black color.
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.divClass</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">5rem</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">1rem</span>;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">700</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span>;
}
</code></pre>
]]></content:encoded></item><item><title><![CDATA[How to Use CDC Tables to Capture Change Data]]></title><description><![CDATA[Introduction to CDC tables
Change Data Capture (CDC) tables are a type of database table that is used to capture changes that are made to other tables. CDC tables are typically used to track changes that are made to data in a real-time or near-real-t...]]></description><link>https://programminguides.hashnode.dev/how-to-use-cdc-tables-to-capture-change-data</link><guid isPermaLink="true">https://programminguides.hashnode.dev/how-to-use-cdc-tables-to-capture-change-data</guid><category><![CDATA[SQL]]></category><category><![CDATA[Databases]]></category><category><![CDATA[SQL Server]]></category><category><![CDATA[change data capture]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Tue, 11 Jul 2023 14:06:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1689084297218/e217f829-e312-4b10-a045-c2bc1327167a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction-to-cdc-tables"><strong>Introduction to CDC tables</strong></h3>
<p>Change Data Capture (CDC) tables are a type of database table that is used to capture changes that are made to other tables. CDC tables are typically used to track changes that are made to data in a real-time or near-real-time manner. CDC tables can be scaled to handle large volumes of data. This makes them a good choice for applications that need to track changes to large datasets. CDC tables can capture changes that are made to data in real-time or near-real-time. This allows you to track changes as they happen and take action as needed.</p>
<h3 id="heading-comparison-of-cdc-vs-triggers"><strong>Comparison of CDC vs Triggers</strong></h3>
<p>Now the question arises why do we need a CDC table when we can do the same thing using triggers? There are a few reasons why we might want to use a CDC table instead of triggers.</p>
<ul>
<li><p><strong>Performance:</strong> CDC tables are typically less performance-intensive than triggers because they do not have to fire for every individual DML operation. This can be especially important for high-volume databases.</p>
</li>
<li><p><strong>Scalability:</strong> CDC tables are more scalable than triggers because they can be used to capture changes from multiple tables at the same time. This can be helpful for large databases with a lot of data.</p>
</li>
<li><p><strong>Flexibility:</strong> CDC tables are more flexible than triggers because they can be used to capture changes in a variety of ways. This can be helpful for applications that need to capture specific types of changes or changes in a specific order.</p>
</li>
</ul>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Feature</strong></td><td><strong>CDC Tables</strong></td><td><strong>Triggers</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Performance</td><td>Less performance-intensive</td><td>More performance-intensive</td></tr>
<tr>
<td>Scalability</td><td>More scalable</td><td>Less scalable</td></tr>
<tr>
<td>Flexibility</td><td>More flexible</td><td>Less flexible</td></tr>
<tr>
<td>Complexity</td><td>More complex to set up and manage</td><td>Less complex to set up and manage</td></tr>
<tr>
<td>Capabilities</td><td>Can capture changes from multiple tables</td><td>Can only capture changes from the source table</td></tr>
</tbody>
</table>
</div><h3 id="heading-setting-up-cdc-tables"><strong>Setting up CDC tables</strong></h3>
<ol>
<li>Run the below script in the Student database to enable CDC</li>
</ol>
<pre><code class="lang-sql"><span class="hljs-keyword">USE</span> Student;
GO
<span class="hljs-keyword">EXECUTE</span> sys.sp_cdc_enable_db;
GO
</code></pre>
<ol>
<li><p>Enable SQL Server Agent</p>
<ol>
<li><p>Open SQL Server Management Studio.</p>
</li>
<li><p>In the Object Explorer, expand <strong>Management</strong> &gt; <strong>Services</strong>.</p>
</li>
<li><p>Right-click <strong>SQL Server Agent</strong> and select <strong>Start</strong>.</p>
</li>
</ol>
</li>
<li><p>Run the below command in the details table to enable CDC for the Details table</p>
</li>
</ol>
<pre><code class="lang-sql">EXEC sys.sp_cdc_enable_table
     @source_schema = N'dbo',
     @source_name = N'Details',
     @role_name = NULL
GO
</code></pre>
<h3 id="heading-terminologies-of-cdc-table"><strong>Terminologies of CDC Table</strong></h3>
<p>Data captured in the CDC table from the source table will look something like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689083845800/3ff0370f-69ce-4a79-99b0-fa678da73744.png" alt class="image--center mx-auto" /></p>
<p>Source table for corresponding CDC table:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689083910215/ea163655-e073-4c5b-a8f4-638638f6a3cf.png" alt /></p>
<p><strong>__$operation column represents what exactly the changes were</strong>:-</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operation</td><td>Value</td></tr>
</thead>
<tbody>
<tr>
<td>Delete</td><td>1</td></tr>
<tr>
<td>Insert</td><td>2</td></tr>
<tr>
<td>Update (Old Value)</td><td>3</td></tr>
<tr>
<td>Update (New Value)</td><td>4</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item><item><title><![CDATA["Unlocking the Power of Multithreading: Enhancing Performance and Concurrency in Your Applications"]]></title><description><![CDATA[Boost performance using Multi-threading
Print count of even and odd numbers from 1 to 1e9. You will see a trivial approach of doing with its run time taken, multi-threading way involving two threads and then involving all the threads currently presen...]]></description><link>https://programminguides.hashnode.dev/unlocking-the-power-of-multithreading-enhancing-performance-and-concurrency-in-your-applications</link><guid isPermaLink="true">https://programminguides.hashnode.dev/unlocking-the-power-of-multithreading-enhancing-performance-and-concurrency-in-your-applications</guid><category><![CDATA[multithreading]]></category><category><![CDATA[C++]]></category><category><![CDATA[asynchronous]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Sun, 02 Jul 2023 11:12:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1688296239079/5e2894ef-42ed-43fb-9914-966da5809890.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-boost-performance-using-multi-threading">Boost performance using Multi-threading</h3>
<p>Print count of even and odd numbers from 1 to 1e9. You will see a trivial approach of doing with its run time taken, multi-threading way involving two threads and then involving all the threads currently present in your PC. Run time might differ depending on your PC's performance but you will see a drastic change in run time.</p>
<p><strong>Normal Way:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">findEven</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> num)</span> </span>{
   <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> cnt=<span class="hljs-number">0</span>;
   <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;=num; i++)
   {
      <span class="hljs-keyword">if</span>(i%<span class="hljs-number">2</span>==<span class="hljs-number">0</span>) cnt++;
   }
   <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Count: "</span> &lt;&lt; cnt &lt;&lt; <span class="hljs-built_in">endl</span>;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">findOdd</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> num)</span> </span>{
   <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> cnt=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;=num; i++)
    {
        <span class="hljs-keyword">if</span>(i%<span class="hljs-number">2</span>!=<span class="hljs-number">0</span>) cnt++;
    }
   <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Count: "</span> &lt;&lt; cnt &lt;&lt; <span class="hljs-built_in">endl</span>;
}

<span class="hljs-function"><span class="hljs-keyword">signed</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    freopen(<span class="hljs-string">"output.txt"</span>, <span class="hljs-string">"w"</span>, <span class="hljs-built_in">stdout</span>);
    <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> num=<span class="hljs-number">1e9</span>;
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span>&gt; even, odd;

    <span class="hljs-keyword">auto</span> start = <span class="hljs-built_in">std</span>::chrono::high_resolution_clock::now();

    findEven(num);
    findOdd(num);

    <span class="hljs-keyword">auto</span> end = <span class="hljs-built_in">std</span>::chrono::high_resolution_clock::now();
    <span class="hljs-keyword">double</span> duration = <span class="hljs-built_in">std</span>::chrono::duration_cast&lt;<span class="hljs-built_in">std</span>::chrono::milliseconds&gt;(end - start).count();

    duration = (duration/<span class="hljs-number">1000</span>);
    <span class="hljs-built_in">cout</span> &lt;&lt; duration &lt;&lt; <span class="hljs-string">" seconds"</span>;
}
</code></pre>
<p><strong>Involving two threads:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">findEven</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> num)</span> </span>{
   <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> cnt=<span class="hljs-number">0</span>;
   <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;=num; i++)
   {
      <span class="hljs-keyword">if</span>(i%<span class="hljs-number">2</span>==<span class="hljs-number">0</span>) cnt++;
   }
   <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Count: "</span> &lt;&lt; cnt &lt;&lt; <span class="hljs-built_in">endl</span>;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">findOdd</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> num)</span> </span>{
   <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> cnt=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;=num; i++)
    {
        <span class="hljs-keyword">if</span>(i%<span class="hljs-number">2</span>!=<span class="hljs-number">0</span>) cnt++;
    }
   <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Count: "</span> &lt;&lt; cnt &lt;&lt; <span class="hljs-built_in">endl</span>;
}

<span class="hljs-function"><span class="hljs-keyword">signed</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    freopen(<span class="hljs-string">"output.txt"</span>, <span class="hljs-string">"w"</span>, <span class="hljs-built_in">stdout</span>);
    <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> num=<span class="hljs-number">1e9</span>;
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span>&gt; even, odd;

    <span class="hljs-keyword">auto</span> start = <span class="hljs-built_in">std</span>::chrono::high_resolution_clock::now();

   <span class="hljs-function">thread <span class="hljs-title">t1</span><span class="hljs-params">(findEven, num)</span></span>;
   <span class="hljs-function">thread <span class="hljs-title">t2</span><span class="hljs-params">(findOdd, num)</span></span>;

   t1.join(); 
   t2.join();

    <span class="hljs-keyword">auto</span> end = <span class="hljs-built_in">std</span>::chrono::high_resolution_clock::now();
    <span class="hljs-keyword">double</span> duration = <span class="hljs-built_in">std</span>::chrono::duration_cast&lt;<span class="hljs-built_in">std</span>::chrono::milliseconds&gt;(end - start).count();

    duration = (duration/<span class="hljs-number">1000</span>);
    <span class="hljs-built_in">cout</span> &lt;&lt; duration &lt;&lt; <span class="hljs-string">" seconds"</span>;
}
</code></pre>
<p><strong>Involving multiple threads:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">countEvenNumbers</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span>
</span>{
    <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = start; i &lt;= end; ++i)
    {
        <span class="hljs-keyword">if</span> (i % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>)
        {
            count++;
        }
    }
    <span class="hljs-keyword">return</span> count;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">counOddNumbers</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span>
</span>{
    <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = start; i &lt;= end; ++i)
    {
        <span class="hljs-keyword">if</span> (i % <span class="hljs-number">2</span> != <span class="hljs-number">0</span>)
        {
            count++;
        }
    }
    <span class="hljs-keyword">return</span> count;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">calculateCount</span><span class="hljs-params">(<span class="hljs-keyword">int</span> chunkStart, <span class="hljs-keyword">int</span> chunkEnd)</span> 
</span>{
    <span class="hljs-keyword">int</span> count = countEvenNumbers(chunkStart, chunkEnd);
    <span class="hljs-keyword">int</span> count1 = counOddNumbers(chunkStart, chunkEnd);
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Thread "</span> &lt;&lt; this_thread::get_id() &lt;&lt; <span class="hljs-string">": "</span> &lt;&lt; count &lt;&lt; <span class="hljs-string">" even numbers."</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    freopen(<span class="hljs-string">"output.txt"</span>, <span class="hljs-string">"w"</span>, <span class="hljs-built_in">stdout</span>);

    <span class="hljs-keyword">int</span> numThreads = thread::hardware_concurrency();
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Number of threads available: "</span> &lt;&lt; numThreads &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">int</span> startNumber = <span class="hljs-number">1</span>;
    <span class="hljs-keyword">int</span> endNumber = <span class="hljs-number">1e9</span>;

    <span class="hljs-keyword">int</span> chunkSize = (endNumber - startNumber + <span class="hljs-number">1</span>) / numThreads;
    <span class="hljs-built_in">vector</span>&lt;thread&gt; threads;

    <span class="hljs-keyword">auto</span> start = <span class="hljs-built_in">std</span>::chrono::high_resolution_clock::now();

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; numThreads; ++i)
    {
        <span class="hljs-keyword">int</span> chunkStart = startNumber + i * chunkSize;
        <span class="hljs-keyword">int</span> chunkEnd = chunkStart + chunkSize - <span class="hljs-number">1</span>;

        <span class="hljs-keyword">if</span> (i == numThreads - <span class="hljs-number">1</span>)
        {
            chunkEnd = endNumber;
        }

        threads.push_back(thread(calculateCount, chunkStart, chunkEnd));
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span>&amp; thread : threads)
    {
        thread.join();
    }

    <span class="hljs-keyword">auto</span> end = <span class="hljs-built_in">std</span>::chrono::high_resolution_clock::now();
    <span class="hljs-keyword">double</span> duration = <span class="hljs-built_in">std</span>::chrono::duration_cast&lt;<span class="hljs-built_in">std</span>::chrono::milliseconds&gt;(end - start).count();

    duration = (duration/<span class="hljs-number">1000</span>);
    <span class="hljs-built_in">cout</span> &lt;&lt; duration &lt;&lt; <span class="hljs-string">" seconds"</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><strong>Run time Comparision:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Normal Way</td><td>Involving two Threads</td><td>Involving multiple Threads</td></tr>
</thead>
<tbody>
<tr>
<td><code>7.066 seconds</code></td><td><code>3.483 seconds (50.70%)</code></td><td><code>0.945 seconds (86.62%)</code></td></tr>
</tbody>
</table>
</div><p>From the above table, you can see that just by using two threads we improved run time by <code>50.70%</code> and by using multiple threads we improved run time by <code>86.62%</code>.</p>
<h3 id="heading-terminologies-in-multi-threading">Terminologies in Multi-threading</h3>
<ol>
<li><p><strong>join():</strong> The <code>join()</code> function is a member function of the <code>std::thread</code> class in C++. It is used to wait for a thread to complete its execution. When you call <code>join()</code> on a thread object, the calling thread will block until the thread being joined finishes its execution.</p>
</li>
<li><p><strong>joinable():</strong> The <code>joinable()</code> function is a member function of the <code>std::thread</code> class. It is used to check if a thread is joinable. A thread is joinable if it represents a thread of execution that has not been joined or detached. If a thread is joinable, it means that it can be joined by calling the <code>join()</code> function.</p>
</li>
<li><p><strong>detach():</strong> The <code>detach()</code> function is a member function of the <code>std::thread</code> class. It allows you to detach a thread from the calling thread. When a thread is detached, it continues its execution independently, and the calling thread does not wait for its completion. The detached thread will clean up itself when it finishes execution. The <code>detach()</code> function is useful when you have a thread that performs a background task or provides a service independently from the main thread. You may not need to wait for its completion or retrieve any return value from it. Detaching the thread allows it to run in the background while the main thread continues its execution or terminates.</p>
</li>
<li><p><strong>mutex:</strong> A mutex (short for mutual exclusion) is a synchronization primitive used to protect shared resources from simultaneous access by multiple threads. It ensures that only one thread can access a critical section of code (protected by the mutex) at a time. In C++, the <code>std::mutex</code> class is provided as a standard mutex implementation.</p>
</li>
<li><p><strong>race condition:</strong> A race condition occurs when two or more threads access shared data concurrently, and the outcome of the program depends on the relative timing of their execution. Race conditions can lead to unpredictable and erroneous behaviour in a multi-threaded program. They typically occur when at least one thread is modifying shared data, and there is no synchronization mechanism (such as a mutex) in place to enforce exclusive access.</p>
</li>
<li><p><strong>timed_mutex:</strong> A timed mutex is an extension of the <code>std::mutex</code> class in C++ that allows for timed locking and unlocking. It provides additional capabilities to acquire a lock on a mutex for a specified duration. The <code>std::timed_mutex</code> class provides member functions like <code>try_lock_for()</code> and <code>try_lock_until()</code> that attempt to acquire a lock for a specified duration or until a specific point in time, respectively. These functions return immediately with a success or failure indication, allowing the caller to handle the case when a lock cannot be acquired within the given time frame.</p>
</li>
</ol>
<p>Timed mutexes are useful in scenarios where you want to acquire a lock on a mutex for a specific duration or wait until a certain time, and then proceed with the execution accordingly.</p>
<p>It's important to note that using mutexes and synchronization mechanisms correctly is crucial to prevent race conditions and ensure thread safety in multi-threaded programs.</p>
<h3 id="heading-difference-between-uniquelock-and-lockguard">Difference between unique_lock and lock_guard</h3>
<ul>
<li><p><code>std::unique_lock</code> is a more versatile class that provides additional features and flexibility compared to <code>std::lock_guard</code>.</p>
</li>
<li><p>Unlike <code>std::lock_guard</code>, <code>std::unique_lock</code> allows manual control over the locking and unlocking of the associated mutex. It can be locked and unlocked multiple times within its lifetime.</p>
</li>
<li><p><code>std::unique_lock</code> is movable, which means it can be transferred between scopes or threads by moving its ownership.</p>
</li>
<li><p>It supports more advanced synchronization scenarios, such as condition variable waiting (<code>cv.wait()</code>) and timed locking.</p>
</li>
<li><p><code>std::unique_lock</code> is typically used when you need more fine-grained control over locking and unlocking, or when you want to use additional synchronization features like condition variables.</p>
</li>
</ul>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;mutex&gt;</span></span>

<span class="hljs-built_in">std</span>::mutex mtx;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">someFunction</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock</span><span class="hljs-params">(mtx)</span></span>;
    <span class="hljs-comment">// Critical section: Mutex is locked here</span>
    <span class="hljs-comment">// Perform operations on shared resources</span>
    lock.unlock(); <span class="hljs-comment">// Manually unlock the mutex</span>
    <span class="hljs-comment">// Perform some non-critical operations without holding the lock</span>
    lock.lock(); <span class="hljs-comment">// Manually lock the mutex again</span>
    <span class="hljs-comment">// Continue with critical section</span>
    <span class="hljs-comment">// Mutex is automatically released when 'lock' goes out of scope</span>
}
</code></pre>
<h3 id="heading-condition-variable">Condition Variable</h3>
<p>In this example, we have three functions: <code>withdrawMoney()</code>, <code>addMoney()</code>, and the <code>main()</code> function.</p>
<ol>
<li><p>The <code>withdrawMoney()</code> function represents a thread that tries to withdraw a certain amount of money from the account. It first acquires the lock on the mutex using <code>std::unique_lock</code>. Then, it waits on the condition variable <code>cv</code> until there are sufficient funds in the account. The lambda function <code>[amount] { return accountBalance &gt;= amount; }</code> specifies the condition for the waiting thread to continue. Once the condition is satisfied, the withdrawal is performed, and the account balance is updated.</p>
</li>
<li><p>The <code>addMoney()</code> function represents a thread that adds a certain amount of money to the account. It also acquires the lock on the mutex using <code>std::unique_lock</code> and adds the money to the account balance. After updating the balance, it calls <code>cv.notify_one()</code> to notify one waiting thread that funds are available.</p>
</li>
<li><p>In the <code>main()</code> function, three threads are created: one for withdrawal and two for adding money. The threads run concurrently, and the withdrawals and additions happen in an interleaved manner.</p>
</li>
<li><p>This makes sure that withdraw function is only executed when you have sufficient funds. Let's say instead of adding 350 you added only 300 then one thread would be waiting till infinity.</p>
</li>
<li><p>Condition variable makes sure that one thread is executed when a particular condition is met.</p>
</li>
</ol>
<p>By using <code>std::mutex</code>, <code>std::condition_variable</code>, and <code>std::unique_lock</code>, we ensure that withdrawals are blocked until sufficient funds are available in the account. The condition variable allows threads to wait efficiently without busy waiting, and the unique lock ensures exclusive access to shared resources.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;thread&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;mutex&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;condition_variable&gt;</span></span>

<span class="hljs-built_in">std</span>::mutex mtx;
<span class="hljs-built_in">std</span>::condition_variable cv;
<span class="hljs-keyword">int</span> accountBalance = <span class="hljs-number">0</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">withdrawMoney</span><span class="hljs-params">(<span class="hljs-keyword">int</span> amount)</span>
</span>{
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock</span><span class="hljs-params">(mtx)</span></span>;
    <span class="hljs-comment">// Wait until there are sufficient funds in the account</span>
    cv.wait(lock, [amount] { <span class="hljs-keyword">return</span> accountBalance &gt;= amount; });

    <span class="hljs-comment">// Perform the withdrawal</span>
    accountBalance -= amount;
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Withdrawn "</span> &lt;&lt; amount &lt;&lt; <span class="hljs-string">" units. Account balance: "</span> &lt;&lt; accountBalance &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addMoney</span><span class="hljs-params">(<span class="hljs-keyword">int</span> amount)</span>
</span>{
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock</span><span class="hljs-params">(mtx)</span></span>;

    <span class="hljs-comment">// Add money to the account</span>
    accountBalance += amount;
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Added "</span> &lt;&lt; amount &lt;&lt; <span class="hljs-string">" units. Account balance: "</span> &lt;&lt; accountBalance &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;

    <span class="hljs-comment">// Notify one waiting thread that funds are available</span>
    cv.notify_one();
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    freopen(<span class="hljs-string">"output.txt"</span>, <span class="hljs-string">"w"</span>, <span class="hljs-built_in">stdout</span>);
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">t1</span><span class="hljs-params">(withdrawMoney, <span class="hljs-number">100</span>)</span></span>;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">t2</span><span class="hljs-params">(addMoney, <span class="hljs-number">350</span>)</span></span>;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">t3</span><span class="hljs-params">(withdrawMoney, <span class="hljs-number">150</span>)</span></span>;

    t1.join();
    t2.join();
    t3.join();

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<h3 id="heading-deadlock">Deadlock</h3>
<p>A deadlock occurs in a multi-threaded program when two or more threads are blocked indefinitely, waiting for each other to release resources. This situation leads to a state where none of the threads can make progress, resulting in a deadlock. Deadlocks can occur when there is a circular dependency among threads and resources they are trying to acquire.</p>
<p>Let's consider an example scenario with two threads (<code>Thread A</code> and <code>Thread B</code>) and two resources (<code>Resource 1</code> and <code>Resource 2</code>). The threads need to acquire both resources to perform their tasks. The following code snippet demonstrates a potential deadlock situation:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;mutex&gt;</span></span>

<span class="hljs-built_in">std</span>::mutex mutex1;
<span class="hljs-built_in">std</span>::mutex mutex2;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">threadA</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Acquire lock on mutex1</span>
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock1</span><span class="hljs-params">(mutex1)</span></span>;

    <span class="hljs-comment">// Sleep to simulate some processing</span>
    <span class="hljs-built_in">std</span>::this_thread::sleep_for(<span class="hljs-built_in">std</span>::chrono::seconds(<span class="hljs-number">1</span>));

    <span class="hljs-comment">// Attempt to acquire lock on mutex2</span>
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock2</span><span class="hljs-params">(mutex2)</span></span>;

    <span class="hljs-comment">// Perform Thread A's task using Resource 1 and Resource 2</span>
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">threadB</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Acquire lock on mutex2</span>
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock2</span><span class="hljs-params">(mutex2)</span></span>;

    <span class="hljs-comment">// Sleep to simulate some processing</span>
    <span class="hljs-built_in">std</span>::this_thread::sleep_for(<span class="hljs-built_in">std</span>::chrono::seconds(<span class="hljs-number">1</span>));

    <span class="hljs-comment">// Attempt to acquire lock on mutex1</span>
    <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock1</span><span class="hljs-params">(mutex1)</span></span>;

    <span class="hljs-comment">// Perform Thread B's task using Resource 1 and Resource 2</span>
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">t1</span><span class="hljs-params">(threadA)</span></span>;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">t2</span><span class="hljs-params">(threadB)</span></span>;

    t1.join();
    t2.join();

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, <code>Thread A</code> and <code>Thread B</code> both acquire a lock on one mutex and then attempt to acquire a lock on the other mutex. However, if the timing is unfavourable, a deadlock can occur. Here's how the deadlock scenario unfolds:</p>
<ol>
<li><p><code>Thread A</code> acquires <code>mutex1</code> and <code>Thread B</code> acquires <code>mutex2</code>.</p>
</li>
<li><p><code>Thread A</code> tries to acquire <code>mutex2</code> but gets blocked because <code>Thread B</code> holds the lock on <code>mutex2</code>.</p>
</li>
<li><p><code>Thread B</code> tries to acquire <code>mutex1</code> but gets blocked because <code>Thread A</code> holds the lock on <code>mutex1</code>.</p>
</li>
<li><p>Both threads are now waiting for the other thread to release the lock, resulting in a deadlock. None of them can proceed.</p>
</li>
</ol>
<p>Here's a diagram illustrating the deadlock scenario:</p>
<pre><code class="lang-cpp">Thread A                        Thread B
----------------------------------------------
Acquire lock on mutex1
                                    Acquire lock on mutex2
Attempt to acquire lock on mutex2
                                    Attempt to acquire lock on mutex1
      [Deadlock occurs]
</code></pre>
<p>To avoid deadlocks, it's important to carefully analyze and synchronize access to shared resources, ensuring that potential circular dependencies are avoided. Techniques such as using a fixed ordering of locks, resource allocation hierarchies, or employing deadlock detection and recovery algorithms can help prevent and handle deadlocks in multi-threaded programs.</p>
<h3 id="heading-producer-consumer-problem">Producer Consumer Problem</h3>
<p>The Producer-Consumer problem is a classic synchronization problem in concurrent programming. It involves two entities, the producer and the consumer, which share a common buffer or queue. The producer generates data items and adds them to the buffer, while the consumer consumes the data items by removing them from the buffer.</p>
<p>Here's an example implementation in C++ using threads and a shared queue:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;queue&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;thread&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;mutex&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;condition_variable&gt;</span></span>

<span class="hljs-built_in">std</span>::<span class="hljs-built_in">queue</span>&lt;<span class="hljs-keyword">int</span>&gt; buffer;
<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> bufferSize = <span class="hljs-number">10</span>;
<span class="hljs-built_in">std</span>::mutex mtx;
<span class="hljs-built_in">std</span>::condition_variable cv;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">producer</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">20</span>; ++i) {
        <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock</span><span class="hljs-params">(mtx)</span></span>;
        cv.wait(lock, [] { <span class="hljs-keyword">return</span> buffer.size() &lt; bufferSize; });

        buffer.push(i);
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Produced: "</span> &lt;&lt; i &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;

        lock.unlock();
        cv.notify_one();
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">consumer</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">20</span>; ++i) {
        <span class="hljs-function"><span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; <span class="hljs-title">lock</span><span class="hljs-params">(mtx)</span></span>;
        cv.wait(lock, [] { <span class="hljs-keyword">return</span> !buffer.empty(); });

        <span class="hljs-keyword">int</span> item = buffer.front();
        buffer.pop();
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Consumed: "</span> &lt;&lt; item &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;

        lock.unlock();
        cv.notify_one();
    }
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">producerThread</span><span class="hljs-params">(producer)</span></span>;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::thread <span class="hljs-title">consumerThread</span><span class="hljs-params">(consumer)</span></span>;

    producerThread.join();
    consumerThread.join();

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, the <code>producer</code> function produces values from 1 to 20 and adds them to the shared <code>buffer</code> until it reaches the <code>bufferSize</code>. If the buffer is full, the producer waits for the consumer to consume items before adding more.</p>
<p>The <code>consumer</code> function consumes items from the buffer. If the buffer is empty, the consumer waits for the producer to produce items before consuming them. Once an item is consumed, it is removed from the buffer.</p>
<p>The <code>std::mutex</code> (mtx) is used to protect the shared buffer and ensure that only one thread can access it at a time. The <code>std::condition_variable</code> (cv) is used for signalling and synchronization between the producer and consumer threads. The <code>cv.wait</code> function is used to wait until a certain condition is met, and <code>cv.notify_one</code> is used to notify the waiting threads.</p>
<p>By coordinating the producer and consumer using mutexes and condition variables, we ensure that the producer doesn't produce items when the buffer is full, and the consumer doesn't consume items when the buffer is empty, thus avoiding synchronization issues.</p>
<h3 id="heading-sleep-vs-wait">Sleep vs Wait</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1688285606852/2f5c6584-cf5b-4bed-be67-0737506e4fd1.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-multi-threading-vs-async-await">Multi-threading vs async-await</h3>
<p>Multi-threading is utilizing multiple threads for parallel execution whereas async-await utilizes threads efficiently. For example, if you made an API call then instead of waiting for the response of the API call thread can perform other tasks and this is asynchronous i.e. not blocking the execution of the program. And when the response comes it handles that.</p>
]]></content:encoded></item><item><title><![CDATA[(18) Array: Number of Sub-arrays With Odd Sum]]></title><description><![CDATA[Question Link  and  Solution Link 
Difficulty: Medium
Problem Statement: Given an array, we need to return the number of subarrays with an odd sum.
Approach: Calculate prefix sum of the array and then traverse over that array and whenever you encount...]]></description><link>https://programminguides.hashnode.dev/18-array-number-of-sub-arrays-with-odd-sum</link><guid isPermaLink="true">https://programminguides.hashnode.dev/18-array-number-of-sub-arrays-with-odd-sum</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Thu, 20 Jan 2022 17:49:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/mRMQwK513hY/upload/v1642700961188/RBoY_Sk1D.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a target="_blank" href="https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/">Question Link</a>  and  <a target="_blank" href="https://leetcode.com/submissions/detail/624038848/">Solution Link</a> </p>
<p><strong>Difficulty</strong>: Medium</p>
<p><strong>Problem Statement</strong>: Given an array, we need to return the number of subarrays with an odd sum.</p>
<p><strong>Approach</strong>: Calculate prefix sum of the array and then traverse over that array and whenever you encounter even number increment even else increment odd and finally return <code>((even*odd)%mod + odd%mod)%mod</code>.</p>
<pre><code><span class="hljs-keyword">int</span> numOfSubarrays(vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span> <span class="hljs-operator">&amp;</span>arr)
{
    long long odd <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, even <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, n <span class="hljs-operator">=</span> arr.size(), mod <span class="hljs-operator">=</span> <span class="hljs-number">1e9</span> <span class="hljs-operator">+</span> <span class="hljs-number">7</span>;
    <span class="hljs-keyword">int</span> prex[n];
    prex[<span class="hljs-number">0</span>] <span class="hljs-operator">=</span> arr[<span class="hljs-number">0</span>];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i <span class="hljs-operator">&lt;</span> n; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) prex[i] <span class="hljs-operator">=</span> arr[i] <span class="hljs-operator">+</span> prex[i <span class="hljs-operator">-</span> <span class="hljs-number">1</span>];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> n; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>)
    {
        <span class="hljs-keyword">if</span> (prex[i] <span class="hljs-operator">%</span> <span class="hljs-number">2</span> <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) even<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
        <span class="hljs-keyword">else</span> odd<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }
    <span class="hljs-keyword">return</span> (<span class="hljs-keyword">int</span>)((even <span class="hljs-operator">*</span> odd) <span class="hljs-operator">%</span> mod <span class="hljs-operator">+</span> odd <span class="hljs-operator">%</span> mod) <span class="hljs-operator">%</span> mod;
}
</code></pre><p><strong>Subscribe to the newsletter so that you never miss any post or update just like this one.</strong></p>
<p>You can follow me on Hashnode for:</p>
<ul>
<li>Daily Data Structure and Algorithm based questions</li>
<li><p>Getting knowledge of various development-related tools, concepts, and practices</p>
<p><a target="_blank" href="https://twitter.com/_Arsalaan_">Twitter</a> ,  <a target="_blank" href="https://github.com/arsalanhub">GitHub</a> ,  <a target="_blank" href="https://www.linkedin.com/in/mohammadarsalan/">LinkedIn</a>  and  <a target="_blank" href="https://hashnode.com/@mohdarsalan">Hashnode</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(17) Array: Special Reverse]]></title><description><![CDATA[Difficulty: Easy
Problem Statement: Given a string and you need to reverse it such that after the reversal position of a special character do not change.
Input: intell#ect, Output: tcelle#tni
Input: h@ello, Output: o@lleh
Input: a#b@c, Output: c#b@a
...]]></description><link>https://programminguides.hashnode.dev/17-array-special-reverse</link><guid isPermaLink="true">https://programminguides.hashnode.dev/17-array-special-reverse</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Wed, 19 Jan 2022 13:23:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/qSR0KbeNbtk/upload/v1642598544549/o6xysmjHf.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Difficulty</strong>: Easy</p>
<p><strong>Problem Statement</strong>: Given a string and you need to reverse it such that after the reversal position of a special character do not change.</p>
<p><strong>Input</strong>: intell#ect, <strong>Output</strong>: tcelle#tni</p>
<p><strong>Input</strong>: h@ello, <strong>Output</strong>: o@lleh</p>
<p><strong>Input</strong>: a#b@c, <strong>Output</strong>: c#b@a</p>
<p><strong>Approach</strong>: We store special characters in <code>res</code> and the character which is not special we store space instead of that.</p>
<p>In <code>newS</code> we store the reverse of string except for a special character. Now we traverse both the list and at any point if there is a special character we store it in the answer else we store the alphabet. At last print the answer.</p>
<pre><code>s <span class="hljs-operator">=</span> input()
res <span class="hljs-operator">=</span> []
<span class="hljs-keyword">for</span> i in s:
    <span class="hljs-keyword">if</span> i <span class="hljs-operator">&gt;</span><span class="hljs-operator">=</span> <span class="hljs-string">'a'</span> and i <span class="hljs-operator">&lt;</span><span class="hljs-operator">=</span> <span class="hljs-string">'z'</span>: res.append(<span class="hljs-string">' '</span>)
    <span class="hljs-keyword">else</span>: res.append(i)
s <span class="hljs-operator">=</span> s[::<span class="hljs-number">-1</span>]
pos <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
newS <span class="hljs-operator">=</span> <span class="hljs-string">''</span>
<span class="hljs-keyword">for</span> i in s:
    <span class="hljs-keyword">if</span> i <span class="hljs-operator">&gt;</span><span class="hljs-operator">=</span> <span class="hljs-string">'a'</span> and i <span class="hljs-operator">&lt;</span><span class="hljs-operator">=</span> <span class="hljs-string">'z'</span>:
        newS <span class="hljs-operator">+</span><span class="hljs-operator">=</span> i

(x, y) <span class="hljs-operator">=</span> (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
(n, m) <span class="hljs-operator">=</span> (len(newS), len(res))
ans <span class="hljs-operator">=</span> <span class="hljs-string">''</span>
<span class="hljs-keyword">while</span> x <span class="hljs-operator">&lt;</span> n and y <span class="hljs-operator">&lt;</span> m:
    <span class="hljs-keyword">if</span> res[y] <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-string">' '</span>:
        ans <span class="hljs-operator">+</span><span class="hljs-operator">=</span> res[y]
        y <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">else</span>:
        ans <span class="hljs-operator">+</span><span class="hljs-operator">=</span> newS[x]
        x <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
        y <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> x <span class="hljs-operator">&lt;</span> n:
    ans <span class="hljs-operator">+</span><span class="hljs-operator">=</span> newS[x]
    x <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> y <span class="hljs-operator">&lt;</span> m:
    <span class="hljs-keyword">if</span> res[y] <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">' '</span>:
        y <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
        <span class="hljs-keyword">continue</span>
    ans <span class="hljs-operator">+</span><span class="hljs-operator">=</span> res[y]
    y <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
print ans
</code></pre><p><strong>Subscribe to the newsletter so that you never miss any post or update just like this one.</strong></p>
<p>You can follow me on Hashnode for:</p>
<ul>
<li>Daily Data Structure and Algorithm based questions</li>
<li><p>Getting knowledge of various development-related tools, concepts, and practices</p>
<p><a target="_blank" href="https://twitter.com/_Arsalaan_">Twitter</a> ,  <a target="_blank" href="https://github.com/arsalanhub">GitHub</a> ,  <a target="_blank" href="https://www.linkedin.com/in/mohammadarsalan/">LinkedIn</a>  and  <a target="_blank" href="https://hashnode.com/@mohdarsalan">Hashnode</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(16) Array: Minimum swaps and K together]]></title><description><![CDATA[Question Link  
Difficulty: Medium
Problem Statement: Given an array and a number k. You need to group all numbers less than equal to number k together.
Approach: Here we will use the window to solve this problem. Basically here we calculate all numb...]]></description><link>https://programminguides.hashnode.dev/16-array-minimum-swaps-and-k-together</link><guid isPermaLink="true">https://programminguides.hashnode.dev/16-array-minimum-swaps-and-k-together</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Tue, 18 Jan 2022 17:24:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/TN8inGqMH7k/upload/v1642526633652/hrJZkmMnE.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a target="_blank" href="https://practice.geeksforgeeks.org/problems/minimum-swaps-required-to-bring-all-elements-less-than-or-equal-to-k-together4847/1#">Question Link</a>  </p>
<p><strong>Difficulty</strong>: Medium</p>
<p><strong>Problem Statement</strong>: Given an array and a number k. You need to group all numbers less than equal to number k together.</p>
<p><strong>Approach</strong>: Here we will use the window to solve this problem. Basically here we calculate all numbers such that they are less than equal to k and store them in <code>count</code>. Also, we calculate <code>bad</code> which we define as all number that is greater than the number <code>k</code>.</p>
<p>Now every time we will shift the window. By shifting window we mean that excluding <code>i</code> and including <code>j</code>. We exclude <code>i</code> when it is greater than <code>k</code> and similarly we include <code>j</code> in bad if it's greater than <code>k</code>.</p>
<pre><code><span class="hljs-keyword">int</span> minSwap(<span class="hljs-keyword">int</span> arr[], <span class="hljs-keyword">int</span> n, <span class="hljs-keyword">int</span> k)
{
    <span class="hljs-keyword">int</span> bad <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, count <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> n; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>)
    {
        <span class="hljs-keyword">if</span> (arr[i] <span class="hljs-operator">&lt;</span><span class="hljs-operator">=</span> k) count<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> count; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>)
    {
        <span class="hljs-keyword">if</span> (arr[i] <span class="hljs-operator">&gt;</span> k) bad<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }
    <span class="hljs-keyword">int</span> ans <span class="hljs-operator">=</span> bad;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, j <span class="hljs-operator">=</span> count; j <span class="hljs-operator">&lt;</span> n; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>, j<span class="hljs-operator">+</span><span class="hljs-operator">+</span>)
    {
        <span class="hljs-keyword">if</span> (arr[i] <span class="hljs-operator">&gt;</span> k) bad<span class="hljs-operator">-</span><span class="hljs-operator">-</span>;
        <span class="hljs-keyword">if</span> (arr[j] <span class="hljs-operator">&gt;</span> k) bad<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
        ans <span class="hljs-operator">=</span> min(ans, bad);
    }
    <span class="hljs-keyword">return</span> ans;
}
</code></pre><p><strong>Subscribe to the newsletter so that you never miss any post or update just like this one.</strong></p>
<p>You can follow me on Hashnode for:</p>
<ul>
<li>Daily Data Structure and Algorithm based questions</li>
<li><p>Getting knowledge of various development-related tools, concepts, and practices</p>
<p><a target="_blank" href="https://twitter.com/_Arsalaan_">Twitter</a> ,  <a target="_blank" href="https://github.com/arsalanhub">GitHub</a> ,  <a target="_blank" href="https://www.linkedin.com/in/mohammadarsalan/">LinkedIn</a>  and  <a target="_blank" href="https://hashnode.com/@mohdarsalan">Hashnode</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(15) Recursion: Combination Sum]]></title><description><![CDATA[Question Link  and  Solution Link 
Difficulty: Medium
Problem Statement: Given an array of elements and a target sum you need to select elements from that array such that the sum of that elements are equal to the target element. Also, you can pick an...]]></description><link>https://programminguides.hashnode.dev/15-recursion-combination-sum</link><guid isPermaLink="true">https://programminguides.hashnode.dev/15-recursion-combination-sum</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Mohammad Arsalan]]></dc:creator><pubDate>Mon, 17 Jan 2022 09:56:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/UgA3Xvi3SkA/upload/v1642413384212/uRVoOkH0y.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a target="_blank" href="https://leetcode.com/problems/combination-sum/submissions/">Question Link</a>  and  <a target="_blank" href="https://leetcode.com/submissions/detail/621491742/">Solution Link</a> </p>
<p><strong>Difficulty</strong>: Medium</p>
<p><strong>Problem Statement</strong>: Given an array of elements and a target sum you need to select elements from that array such that the sum of that elements are equal to the target element. Also, you can pick an element any number of times.</p>
<p><strong>Approach</strong>: Every time we check whether the current element is less than the target sum or not. If it's less than the target sum then we will include that in a temporary vector and not change the index because in the future we might need to include that element and decrease the target sum value by that number.</p>
<p>Else what we can do is that to not include the number and in this case, we increment the index. The most important part is to pop_bcak() from the temporary vector. Also when our current index is equal to the size of the array and tar=0 then this means we achieve our sum and hence we will include it in our resultant vector.</p>
<pre><code>void solve(<span class="hljs-keyword">int</span> idx, vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span> nums, <span class="hljs-keyword">int</span> tar, vector<span class="hljs-operator">&lt;</span>vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&gt;</span> <span class="hljs-operator">&amp;</span>res, vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span> tmp) {
    <span class="hljs-keyword">if</span>(idx<span class="hljs-operator">=</span><span class="hljs-operator">=</span>nums.size())
    {
        <span class="hljs-keyword">if</span>(tar<span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-number">0</span>) res.push_back(tmp);
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-keyword">if</span>(nums[idx] <span class="hljs-operator">&lt;</span><span class="hljs-operator">=</span> tar)
    {
        tmp.push_back(nums[idx]);
        solve(idx, nums, tar<span class="hljs-operator">-</span>nums[idx], res, tmp);
        tmp.pop_back();
    }
    solve(idx<span class="hljs-operator">+</span><span class="hljs-number">1</span>, nums, tar, res, tmp);
}
vector<span class="hljs-operator">&lt;</span>vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&gt;</span> combinationSum(vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&amp;</span> candidates, <span class="hljs-keyword">int</span> target) {
    vector<span class="hljs-operator">&lt;</span>vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&gt;</span> res;
    vector<span class="hljs-operator">&lt;</span><span class="hljs-keyword">int</span><span class="hljs-operator">&gt;</span> tmp;
    solve(<span class="hljs-number">0</span>, candidates, target, res, tmp);
    <span class="hljs-keyword">return</span> res;
}
</code></pre><p><strong>Subscribe to the newsletter so that you never miss any post or update just like this one.</strong></p>
<p>You can follow me on Hashnode for:</p>
<ul>
<li>Daily Data Structure and Algorithm based questions</li>
<li><p>Getting knowledge of various development-related tools, concepts, and practices</p>
<p><a target="_blank" href="https://twitter.com/_Arsalaan_">Twitter</a> ,  <a target="_blank" href="https://github.com/arsalanhub">GitHub</a> ,  <a target="_blank" href="https://www.linkedin.com/in/mohammadarsalan/">LinkedIn</a>  and  <a target="_blank" href="https://hashnode.com/@mohdarsalan">Hashnode</a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>