<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ankit malik</title>
    <description>The latest articles on Forem by Ankit malik (@ankitmalikg).</description>
    <link>https://forem.com/ankitmalikg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F697325%2Fb7604217-a4e4-4b07-9ae5-cac52846f252.png</url>
      <title>Forem: Ankit malik</title>
      <link>https://forem.com/ankitmalikg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ankitmalikg"/>
    <language>en</language>
    <item>
      <title>Scaling Amazon Kinesis and AWS Lambda</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Sat, 31 Jan 2026 09:27:30 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/handling-production-issue-scaling-amazon-kinesis-and-aws-lambda-3f3n</link>
      <guid>https://forem.com/ankitmalikg/handling-production-issue-scaling-amazon-kinesis-and-aws-lambda-3f3n</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Amazon Kinesis Data Streams (provisioned mode) and AWS Lambda are commonly paired to build event-driven pipelines. Getting them to scale predictably requires understanding how capacity is allocated (shards), how Lambda consumes shards, and which operational signals reveal problems. This note corrects common terminology and gives concrete actions you can take when streams backlog or show increased latency.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Kinesis fundamentals
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Shards are the unit of scale.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Per-shard limits (provisioned mode):

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Write:&lt;/strong&gt; 1 MB/sec or 1,000 records/sec&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read:&lt;/strong&gt; 2 MB/sec (shared across consumers)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If input rate approaches shard limits you must increase shard count; if you double shards you roughly double throughput and shard-hour cost.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Partition-key distribution matters.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Low-cardinality keys → hot shards → throttling regardless of total shard count.&lt;/li&gt;
&lt;li&gt;Best practice: high-cardinality keys or composite/hash keys to spread load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more about kinesis in my previous article - &lt;a href="https://dev.to/ankitmalikg/what-is-amazon-kinesis-data-streams-43m9"&gt;link&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Lambda consumption model
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Shard-driven parallelism
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each shard is the unit Lambda polls. Parallelism is driven by shard count and the “&lt;em&gt;concurrent batches per shard&lt;/em&gt;” setting. This is similar to increasing the number of consumers for other message broker like kafka, rabbitmq etc. We increase consumer in this way.&lt;/li&gt;
&lt;li&gt;Rough mapping:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Max concurrent Lambdas = shard_count × concurrent_batches_per_shard&lt;/code&gt; (subject to Lambda account-level concurrency limits).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Ordering: preserved within a batch, not across concurrently processed batches from the same shard.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Batch size and windowing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;BatchSize&lt;/code&gt; controls how many records Lambda receives per invocation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MaximumBatchingWindowInSeconds&lt;/code&gt; controls how long Kinesis will wait to accumulate a batch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trade-offs:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Larger batch → fewer invocations, higher per-invocation work, higher end-to-end latency.&lt;/li&gt;
&lt;li&gt;Smaller batch → more invocations, lower latency, higher overhead/cost.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. The parameter: “Concurrent batches per shard”
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AWS console &amp;amp; Lambda event source mapping calls it &lt;strong&gt;“Concurrent batches per shard”&lt;/strong&gt; (sometimes displayed as a UI field), not &lt;code&gt;ParallelizationFactor&lt;/code&gt; in that terminology.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavior:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Default is 1 (one batch processed at a time per shard).&lt;/li&gt;
&lt;li&gt;Increasing this lets Lambda process multiple batches concurrently from the same shard.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Use-case:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;When a single batch takes long (CPU-heavy work, slow downstream calls), increasing concurrent batches lets you drain the backlog faster &lt;strong&gt;without&lt;/strong&gt; increasing shard count immediately.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Caution:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Ordering guarantees weaken: ordering is guaranteed only within a batch. If strict ordering across all records is required, increasing concurrent batches may violate your ordering assumptions.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5dlufwhrcncx5mn4oww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5dlufwhrcncx5mn4oww.png" alt="lambda kinesis trigger settings" width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4. How to recover a stuck/lagging stream
&lt;/h2&gt;

&lt;p&gt;If you see many messages backlogged and &lt;code&gt;GetRecords.IteratorAgeMilliseconds&lt;/code&gt; &lt;strong&gt;rising&lt;/strong&gt;, you can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Confirm whether the consumer is the bottleneck (high Lambda duration, throttles, or low concurrency).&lt;/li&gt;
&lt;li&gt;If consumer CPU or downstream latency is the cause, increase &lt;strong&gt;Concurrent batches per shard&lt;/strong&gt; to add concurrency per shard and clear backlog faster.

&lt;ul&gt;
&lt;li&gt;This increases concurrent Lambda invocations and potentially cost — check account concurrency limits and downstream capacity.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If shard-level throughput is saturated (Write/Read throttles, high &lt;code&gt;WriteProvisionedThroughputExceeded&lt;/code&gt;), add shards — do not rely on increasing concurrent batches alone.&lt;/li&gt;
&lt;li&gt;If multiple consumers exist and read contention is present, evaluate &lt;strong&gt;Enhanced Fan-Out (EFO)&lt;/strong&gt; to allocate dedicated read throughput per consumer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Increasing concurrent batches is a fast operational lever to reduce iterator age when consumer-side parallelism is the limiting factor. If Kinesis itself is slow to deliver records, increasing concurrency will not fix it (see Section 6).&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Provisioned streams — right-size shards for input/output
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For provisioned mode, ensure shard count can handle both &lt;strong&gt;incoming writes&lt;/strong&gt; and &lt;strong&gt;outgoing reads&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If incoming rate &amp;gt; shard write capacity → producer-side throttles.&lt;/li&gt;
&lt;li&gt;If consumers require more read throughput than shard-level read capacity → consider more shards or EFO.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Strategy:

&lt;ul&gt;
&lt;li&gt;Measure incoming rate (IncomingRecords / IncomingBytes).&lt;/li&gt;
&lt;li&gt;Map required consumer parallelism to shards × concurrent batches per shard, while observing downstream limits.&lt;/li&gt;
&lt;li&gt;Resize shards only when shard-level saturation is the root cause.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Key metrics to monitor (CloudWatch), and what they mean
&lt;/h2&gt;

&lt;p&gt;Create dashboards and alerts for these metrics. The ones below are essential:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kinesis-level&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IncomingRecords&lt;/code&gt; / &lt;code&gt;IncomingBytes&lt;/code&gt; — production or input rates.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GetRecords.Records&lt;/code&gt; — records returned per &lt;code&gt;GetRecords&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GetRecords.IteratorAgeMilliseconds&lt;/code&gt; — consumer lag (older values = backlog).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GetRecords.Latency - Average (Milliseconds)&lt;/code&gt; (a.k.a. &lt;em&gt;GetRecords latency - average&lt;/em&gt; in console) — how long Kinesis is taking to respond to &lt;code&gt;GetRecords&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If this metric rises significantly, it indicates Kinesis is slow to deliver records — this can be an AWS-side or stream-level issue.&lt;/strong&gt; Increasing consumer concurrency will not help if Kinesis itself is the bottleneck.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;WriteProvisionedThroughputExceeded&lt;/code&gt; / &lt;code&gt;ReadProvisionedThroughputExceeded&lt;/code&gt; — throttling on shard capacity.&lt;/li&gt;

&lt;li&gt;You can create this type of cloudwatch metrics dashboard to to check if there is any issues similar to this screenshot where read latency is increased which caused fewer reads.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmf75zrrzrw5nltzxo6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmf75zrrzrw5nltzxo6v.png" alt="kinesis cloudwatch metrics" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lambda-level&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ConcurrentExecutions&lt;/code&gt; — how many Lambdas are active; should correlate with &lt;code&gt;shards × concurrent_batches_per_shard&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IteratorAge&lt;/code&gt; (Lambda) — similar to Kinesis iterator age; useful cross-check.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Duration&lt;/code&gt; and &lt;code&gt;Throttles&lt;/code&gt; — consumer-side slowness and throttling.&lt;/li&gt;
&lt;li&gt;Invocation count and error/ DLQ metrics.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. GetRecords latency — special attention
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GetRecords latency - average (Milliseconds)&lt;/code&gt; measures how long Kinesis takes to return records for a &lt;code&gt;GetRecords&lt;/code&gt; call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operational interpretation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A rising trend can mean Kinesis is under pressure, experiencing internal delays, or there are &lt;strong&gt;transient AWS-side issues&lt;/strong&gt;. When this increases:

&lt;ul&gt;
&lt;li&gt;Check AWS service health &amp;amp; region notifications.&lt;/li&gt;
&lt;li&gt;Check whether many consumers are contending for read throughput.&lt;/li&gt;
&lt;li&gt;Investigate shard-level throttling.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If &lt;code&gt;GetRecords latency&lt;/code&gt; is high &lt;em&gt;and&lt;/em&gt; &lt;code&gt;IteratorAgeMilliseconds&lt;/code&gt; continues to increase despite raising consumer concurrency, this points to Kinesis/service-side delivery problems rather than your consumer.&lt;/li&gt;

&lt;li&gt;To resolve this quickly you can increase &lt;strong&gt;“Concurrent batches per shard”&lt;/strong&gt; which can help process the records faster.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  8. Cost considerations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Increasing shards increases Kinesis shard-hour costs linearly.&lt;/li&gt;
&lt;li&gt;Increasing concurrent batches per shard increases Lambda concurrency and invocation count — this increases Lambda costs (GB-seconds × invocations).&lt;/li&gt;
&lt;li&gt;EFO brings additional Kinesis costs per consumer.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Concurrent batches per shard&lt;/strong&gt; (not the misnamed &lt;code&gt;ParallelizationFactor&lt;/code&gt;) to increase parallelism per shard when consumer-side slowness is the cause of backlog.&lt;/li&gt;
&lt;li&gt;Right-size shards for your &lt;strong&gt;input and output&lt;/strong&gt; workloads — don’t rely exclusively on increasing Lambda concurrency.&lt;/li&gt;
&lt;li&gt;Monitor &lt;strong&gt;GetRecords.IteratorAgeMilliseconds&lt;/strong&gt; and &lt;strong&gt;GetRecords.Latency - Average (Milliseconds)&lt;/strong&gt; closely: iterator age shows backlog; GetRecords latency indicates whether Kinesis itself is slow to deliver messages.&lt;/li&gt;
&lt;li&gt;Build a focused CloudWatch dashboard with the panels and alerts above so you can confidently distinguish consumer-side vs Kinesis-side issues and act accordingly.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>kinesis</category>
      <category>lambda</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>What is Amazon Kinesis Data Streams</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Sun, 25 Jan 2026 08:25:47 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/what-is-amazon-kinesis-data-streams-43m9</link>
      <guid>https://forem.com/ankitmalikg/what-is-amazon-kinesis-data-streams-43m9</guid>
      <description>&lt;p&gt;Amazon Kinesis Data Streams (KDS) is a managed AWS service for collecting, processing and storing &lt;strong&gt;real-time&lt;/strong&gt; streaming data at scale. If your application needs to ingest continuous streams of events (logs, clicks, telemetries, sensor data, audit events) and process them with low latency, Kinesis Data Streams is one of the standard options on AWS.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why use a streaming data store?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You have data arriving continuously (millions of small messages per minute).&lt;/li&gt;
&lt;li&gt;You need near real-time processing (analytics, alerts, enrichment, ETL).&lt;/li&gt;
&lt;li&gt;You want to decouple producers and consumers (producers write events; many different consumers can read and process them).&lt;/li&gt;
&lt;li&gt;You want to replay or reprocess historical data when needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kinesis Data Streams gives you a durable, ordered buffer that sits between producers and consumers so you can build streaming pipelines reliably.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basic concepts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A stream is the top-level object. Producers put records into a stream. Consumers read records from it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shard&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A stream is made of one or more &lt;em&gt;shards&lt;/em&gt;. A shard is a unit of capacity — it controls how much data you can write to and read from the stream. You scale a stream by adding or removing shards (resharding).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Record&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A record is a single unit of data that producers write. It contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a data blob (binary or text),&lt;/li&gt;
&lt;li&gt;a partition key,&lt;/li&gt;
&lt;li&gt;a sequence number (assigned by Kinesis when the record is accepted).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Partition key&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Producers supply a partition key with each record. Kinesis uses the partition key to determine which shard will store the record. Records with the same partition key go to the same shard and preserve ordering relative to each other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sequence number&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each record in a shard receives a unique sequence number. Consumers use sequence numbers for reading and for checkpoints (to resume processing).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retention&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Kinesis stores data for a configurable retention period. That means consumers can re-read and replay data within that window.&lt;/p&gt;




&lt;h2&gt;
  
  
  How data flows (high level)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Producers&lt;/strong&gt; write records to the stream using the AWS SDK or PutRecord/PutRecords API.&lt;/li&gt;
&lt;li&gt;Kinesis routes each record to a shard using the partition key.&lt;/li&gt;
&lt;li&gt;Within a shard, records are ordered and get sequence numbers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consumers&lt;/strong&gt; read shards using GetRecords (polling), Enhanced Fan-Out (push to registered consumers), or via managed integrations such as AWS Lambda.&lt;/li&gt;
&lt;li&gt;Consumers process the records and checkpoint progress (so they can resume from where they left off).&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Consumers and consumption models
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Standard (shared) consumers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consumers poll the shard endpoints for records. Multiple consumers share the shard read throughput.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enhanced Fan-Out (EFO)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With EFO, each registered consumer gets a dedicated HTTP/2 connection and receives records with very low latency and without sharing read throughput. This is useful when several independent applications need the same stream data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kinesis Client Library (KCL)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;KCL is a client-side library (Java, also wrappers for other languages) that helps with shard leasing, checkpointing, rebalancing and fault tolerance. Use it when you need an auto-managed consumer group behavior similar to Kafka consumers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS Lambda integration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can configure Lambda as a consumer. Lambda will poll the stream and invoke your function with batches of records. This provides a serverless way to process stream data but you need to manage batch sizes and function concurrency.&lt;/p&gt;




&lt;h2&gt;
  
  
  Scaling and resharding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To increase capacity, add shards. To decrease, merge shards.&lt;/li&gt;
&lt;li&gt;Resharding is done by splitting a shard into two or merging two shards into one.&lt;/li&gt;
&lt;li&gt;Partition key design is important: if keys are skewed, traffic concentrates on a small set of shards and causes hot-shard problems. Prefer even hashing and avoid a single hot key.&lt;/li&gt;
&lt;li&gt;For sudden spikes, consider provisioning more shards proactively or design producers/consumers to handle transient throttling.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Ordering and replay
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ordering is preserved &lt;strong&gt;per partition key&lt;/strong&gt; (i.e., within a shard). There is no global ordering across all shards.&lt;/li&gt;
&lt;li&gt;Because Kinesis retains data for the configured retention period, consumers can reprocess old data by reading from an older sequence number or timestamp. This is useful for replays, bug fixes, and reprocessing pipelines.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Reliability, security, and durability
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Data is replicated across multiple availability zones (managed by AWS) to provide durability.&lt;/li&gt;
&lt;li&gt;Access is controlled via IAM policies (who can put, get, or manage streams).&lt;/li&gt;
&lt;li&gt;You can enable encryption at rest (KMS) and data in transit is TLS-protected.&lt;/li&gt;
&lt;li&gt;Use proper IAM roles for producers and consumers; give least privilege.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Cost (overview)
&lt;/h2&gt;

&lt;p&gt;Kinesis Data Streams cost is primarily driven by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;shard hours (how many shards and for how long),&lt;/li&gt;
&lt;li&gt;data ingestion (PUT operations / payload),&lt;/li&gt;
&lt;li&gt;optionally enhanced fan-out consumers,&lt;/li&gt;
&lt;li&gt;data retrieval or extended retention if you keep data longer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For cost-sensitive workloads, tune shard count, batch records with PutRecords, and consider Firehose or MSK if they are a better fit.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Best practices (short list)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Design partition keys to spread load evenly across shards.&lt;/li&gt;
&lt;li&gt;Use PutRecords (batch API) instead of many single PutRecord calls.&lt;/li&gt;
&lt;li&gt;Monitor consumer lag and scale shards before lag becomes critical.&lt;/li&gt;
&lt;li&gt;Use KCL or a managed consumer pattern to handle shard rebalancing and checkpointing.&lt;/li&gt;
&lt;li&gt;Keep messages small and carry only necessary data; offload large payloads to S3 if needed.&lt;/li&gt;
&lt;li&gt;Use IAM, encryption, VPC endpoints (if needed), and CloudWatch for observability.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  11. When to choose Kinesis Data Streams
&lt;/h2&gt;

&lt;p&gt;Choose Kinesis Data Streams when you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ordered, durable streaming with replay capability,&lt;/li&gt;
&lt;li&gt;multiple independent consumers reading the same stream,&lt;/li&gt;
&lt;li&gt;tight control over shard-level capacity and scaling,&lt;/li&gt;
&lt;li&gt;integration with AWS tools (Lambda, Kinesis Data Analytics, Firehose).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Amazon Kinesis Data Streams is a robust, AWS-managed building block for real-time streaming pipelines. It gives you ordered, durable streams, multiple consumption options, and the ability to replay data. For technical teams building event-driven or streaming analytics systems, Kinesis Data Streams is a practical and well-integrated choice on AWS — provided you design partition keys, shards and consumers carefully.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>dataengineering</category>
      <category>devops</category>
    </item>
    <item>
      <title>Python: How to Encrypt and Decrypt with AES</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Tue, 23 Sep 2025 00:57:52 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/python-how-to-encrypt-and-decrypt-with-aes-4h1k</link>
      <guid>https://forem.com/ankitmalikg/python-how-to-encrypt-and-decrypt-with-aes-4h1k</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Sometimes we need to keep data secret—like passwords, personal details, or private messages. &lt;strong&gt;AES (Advanced Encryption Standard)&lt;/strong&gt; is a very popular way to do this.&lt;/p&gt;

&lt;p&gt;AES is a method of turning normal text into unreadable text (encryption) and then back to normal (decryption) using the same secret key (symmetric algorithm).&lt;/p&gt;

&lt;p&gt;When you need to protect sensitive information—such as passwords, financial data, or confidential messages—encryption is essential.&lt;/p&gt;




&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;Before you start:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;strong&gt;Python 3.7 or newer&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install the Python package &lt;code&gt;cryptography&lt;/code&gt; by running:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;cryptography
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Understanding AES Encryption
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;AES&lt;/strong&gt; is a &lt;strong&gt;symmetric key algorithm&lt;/strong&gt;, which means the same secret key is used for both encryption and decryption.&lt;/p&gt;

&lt;p&gt;Key sizes supported by AES:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;128 bits&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;192 bits&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;256 bits&lt;/strong&gt; (the strongest and what we use in this example)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modes of operation&lt;/strong&gt; decide how the algorithm works internally. Common modes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ECB (Electronic Codebook):&lt;/strong&gt; Simple but not very secure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CBC (Cipher Block Chaining):&lt;/strong&gt; Better than ECB but needs extra care for integrity.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;GCM (Galois/Counter Mode):&lt;/strong&gt; Modern and secure. It gives both encryption and a built-in check to ensure the data hasn’t been changed.&lt;/p&gt;

&lt;p&gt;In our code, we will use &lt;strong&gt;GCM mode&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Python Code Example
&lt;/h3&gt;

&lt;p&gt;Here is a complete example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives.ciphers.aead&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AESGCM&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;encrypt_with_aes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enc_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enc_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;plaintext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;aesgcm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AESGCM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aesgcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Change the encrypted bytes to a readable Base64 string
&lt;/span&gt;    &lt;span class="n"&gt;ciphertext_str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ciphertext_str&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decrypt_with_aes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enc_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enc_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;aesgcm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AESGCM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aesgcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_iv_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Create a random string for the nonce
&lt;/span&gt;    &lt;span class="n"&gt;chars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ascii_letters&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;digits&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;#$()*+,-.:;&amp;lt;=&amp;gt;?@[]_&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;enc_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1Xt5YfM4ZNuFdwp3OfVkwkhhQLagWKtt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# 32-character secret key
&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_iv_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# make a random nonce/iv
&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This is a top secret message&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_with_aes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enc_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# base64 encoded data
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ciphertext:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decrypt_with_aes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enc_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Decrypted:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How the code works:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;encrypt_with_aes:&lt;/strong&gt; Takes the message and makes it unreadable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;decrypt_with_aes:&lt;/strong&gt; Turns the unreadable message back to normal text.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;generate_iv_string:&lt;/strong&gt; Creates a new random nonce each time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you run it, you will see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ciphertext: I1M8nE7HxHlmv7uKZPM/FsorN4hIiNhAm8fg2TavM75Dxp00zFrgRQem67E=
Decrypted: This is a top secret message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Tips for Security
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep the key safe:&lt;/strong&gt; Don’t write the key directly in your real code. Store it in environment variables or a secure vault.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use a different nonce every time:&lt;/strong&gt; Never reuse the same nonce with the same key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Change keys over time:&lt;/strong&gt; For long-term projects, rotate (change) your keys regularly.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;AES-GCM is a strong and trusted way to keep data safe.&lt;/p&gt;

&lt;p&gt;With a few lines of Python, you can hide a message and later get it back using the same key.&lt;/p&gt;

&lt;p&gt;To learn more, check the &lt;a href="https://cryptography.io/en/latest/" rel="noopener noreferrer"&gt;cryptography library documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to use reduce function in Python</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Thu, 18 Sep 2025 20:55:15 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/how-to-use-reduce-function-in-python-56mk</link>
      <guid>https://forem.com/ankitmalikg/how-to-use-reduce-function-in-python-56mk</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Python, the &lt;strong&gt;&lt;code&gt;reduce&lt;/code&gt;&lt;/strong&gt; function is a powerful tool for performing cumulative operations on iterable data, such as lists or tuples. Instead of writing a loop to repeatedly apply a function to elements, &lt;code&gt;reduce&lt;/code&gt; lets you &lt;strong&gt;“reduce”&lt;/strong&gt; the iterable into a single value by successively combining elements. This makes your code more concise and functional in style.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax and Parameters
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;reduce&lt;/code&gt; function is available in the &lt;strong&gt;&lt;code&gt;functools&lt;/code&gt;&lt;/strong&gt; module, so you must import it first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt;
&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="n"&gt;initializer&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameters:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;function&lt;/code&gt;&lt;/strong&gt;: A function that takes two arguments and returns a single value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;iterable&lt;/code&gt;&lt;/strong&gt;: The sequence (list, tuple, etc.) to process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;initializer&lt;/code&gt;&lt;/strong&gt; &lt;em&gt;(optional)&lt;/em&gt;: A starting value. If provided, the reduction starts with this value; otherwise, the first item of the iterable is used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Return value:&lt;/strong&gt; A single value that is the cumulative result after applying the function across all elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coding Examples
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;reduce&lt;/code&gt; is often used with &lt;strong&gt;lambda functions&lt;/strong&gt; but for learning purpose I am not going to user lambda function here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Summing Numbers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;add&lt;/code&gt; function is applied pairwise: &lt;code&gt;((2 + 4) + 6) + 8&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Finding the Product of a List
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 105
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 3: Concatenating Strings
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;join_strings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reduce&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tutorial&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;sentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;join_strings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: "Python reduce function tutorial"
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 4: Using an Initializer
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Starts with 100
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 160
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Real-World Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Processing&lt;/strong&gt;: Summing sales figures, combining CSV rows, or aggregating sensor readings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mathematical Computations&lt;/strong&gt;: Calculating factorials or cumulative products.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;String Manipulation&lt;/strong&gt;: Joining words or formatting complex strings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Aggregations&lt;/strong&gt;: Reducing logs, merging dictionaries, or performing multi-step computations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefer built-in functions&lt;/strong&gt; like &lt;code&gt;sum()&lt;/code&gt; or &lt;code&gt;max()&lt;/code&gt; when possible—they are more readable and optimized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use named functions instead of lambdas&lt;/strong&gt; for clarity, especially in complex operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always import from &lt;code&gt;functools&lt;/code&gt;&lt;/strong&gt;, as &lt;code&gt;reduce&lt;/code&gt; is not a built-in function in Python 3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Provide an initializer&lt;/strong&gt; to avoid errors when the iterable is empty.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep functions pure&lt;/strong&gt;: Avoid side effects to ensure predictable results.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;reduce&lt;/code&gt; function&lt;/strong&gt; in Python simplifies operations that require the combination of iterable elements into a single value. By understanding its syntax and applying it with clearly defined functions, you can write cleaner, more functional-style code. Whether you’re aggregating data, performing mathematical calculations, or manipulating strings, &lt;code&gt;reduce&lt;/code&gt; is a valuable tool for elegant and concise Python programming.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Use Partial Function in Python</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Tue, 16 Sep 2025 21:16:44 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/how-to-use-partial-function-in-python-1d9a</link>
      <guid>https://forem.com/ankitmalikg/how-to-use-partial-function-in-python-1d9a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Python, functions are first-class objects, which means they can be passed around, modified, or even treated as variables. The &lt;code&gt;functools&lt;/code&gt; module provides a tool called &lt;strong&gt;partial function&lt;/strong&gt;, which allows you to fix a certain number of arguments of a function and generate a new function with fewer parameters. This is especially useful when working with repetitive function calls where many arguments remain the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;p&gt;Partial functions are commonly used in scenarios where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pre-filling function parameters:&lt;/strong&gt; You want to reuse a function with some arguments fixed, avoiding repeated input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplifying callbacks:&lt;/strong&gt; Passing simplified functions to APIs, GUIs, or event-driven frameworks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improving code readability:&lt;/strong&gt; Defining specialized versions of general-purpose functions, such as formatting or mathematical calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data processing pipelines:&lt;/strong&gt; When mapping or filtering with functions that require multiple arguments, a partial function can simplify the structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code Example with Explanation
&lt;/h3&gt;

&lt;p&gt;Let’s explore an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;partial&lt;/span&gt;

&lt;span class="c1"&gt;# A normal function that multiplies two numbers
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="c1"&gt;# Creating a partial function to always multiply with 2
&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;partial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Creating another partial function to always multiply with 5
&lt;/span&gt;&lt;span class="n"&gt;five_times&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;partial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Using the partial functions
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;# Output: 20 (2 * 10)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;five_times&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;    &lt;span class="c1"&gt;# Output: 20 (5 * 4)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;The function &lt;code&gt;multiply(x, y)&lt;/code&gt; takes two arguments.
&lt;/li&gt;
&lt;li&gt;By applying &lt;code&gt;partial(multiply, 2)&lt;/code&gt;, we fix &lt;code&gt;x = 2&lt;/code&gt;. The new function &lt;code&gt;double(y)&lt;/code&gt; only needs one argument.
&lt;/li&gt;
&lt;li&gt;Similarly, &lt;code&gt;five_times(y)&lt;/code&gt; will always multiply numbers by 5.
&lt;/li&gt;
&lt;li&gt;This approach eliminates rewriting separate functions for these common operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another real-world example with formatting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;partial&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;format_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="si"&gt;}{&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}{&lt;/span&gt;&lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# Create specific formatters using partial functions
&lt;/span&gt;&lt;span class="n"&gt;add_brackets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;partial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;format_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;add_quotes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;partial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;format_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add_brackets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;   &lt;span class="c1"&gt;# Output: [Python]
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add_quotes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;      &lt;span class="c1"&gt;# Output: "Hello"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This technique is particularly handy when creating specialized behaviors without redefining functions.&lt;/p&gt;

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

&lt;p&gt;Partial functions in Python, provided by the &lt;code&gt;functools&lt;/code&gt; module, are a clean and efficient way to reduce code duplication. By fixing certain arguments of a function, they let you create customized versions of existing functions for specific use cases. Whether for callbacks, formatting, or data processing, partial functions help write cleaner and more reusable code.  &lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Kubernetes Cheatsheet</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Fri, 23 May 2025 07:40:24 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/kubernetes-cheatsheet-1162</link>
      <guid>https://forem.com/ankitmalikg/kubernetes-cheatsheet-1162</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I often forget these kubernetes or K8s commands. So I have created this sheet to quickly look any command.&lt;br&gt;
It might be helpful for you to quickly look for any command. You can bookmark this blog for quick search.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 &lt;strong&gt;Kubectl Basics&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl version                             &lt;span class="c"&gt;# Show kubectl &amp;amp; cluster version&lt;/span&gt;
kubectl config view                         &lt;span class="c"&gt;# View kubeconfig details&lt;/span&gt;
kubectl get nodes                           &lt;span class="c"&gt;# List all cluster nodes&lt;/span&gt;
kubectl get all                             &lt;span class="c"&gt;# List all resources in the current namespace&lt;/span&gt;
kubectl get pods &lt;span class="nt"&gt;-A&lt;/span&gt;                         &lt;span class="c"&gt;# List all pods in all namespaces&lt;/span&gt;
kubectl get services                        &lt;span class="c"&gt;# List all services&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🚀 &lt;strong&gt;Pods&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl run nginx &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nginx             &lt;span class="c"&gt;# Create a new pod&lt;/span&gt;
kubectl get pods                            &lt;span class="c"&gt;# List all pods&lt;/span&gt;
kubectl describe pod &amp;lt;pod-name&amp;gt;             &lt;span class="c"&gt;# Detailed info about a pod&lt;/span&gt;
kubectl logs &amp;lt;pod-name&amp;gt;                     &lt;span class="c"&gt;# View logs&lt;/span&gt;
kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;--&lt;/span&gt; /bin/bash    &lt;span class="c"&gt;# Shell into a pod&lt;/span&gt;
kubectl delete pod &amp;lt;pod-name&amp;gt;               &lt;span class="c"&gt;# Delete a pod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🏗️ &lt;strong&gt;Deployments&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl create deployment myapp &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;myimage    &lt;span class="c"&gt;# Create a deployment&lt;/span&gt;
kubectl get deployments                            &lt;span class="c"&gt;# List deployments&lt;/span&gt;
kubectl scale deployment myapp &lt;span class="nt"&gt;--replicas&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3        &lt;span class="c"&gt;# Scale deployment&lt;/span&gt;
kubectl edit deployment myapp                      &lt;span class="c"&gt;# Edit deployment config&lt;/span&gt;
kubectl rollout status deployment/myapp            &lt;span class="c"&gt;# Check rollout status&lt;/span&gt;
kubectl rollout undo deployment/myapp              &lt;span class="c"&gt;# Rollback deployment&lt;/span&gt;
kubectl delete deployment myapp                    &lt;span class="c"&gt;# Delete deployment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🌐 &lt;strong&gt;Services&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl expose deployment myapp &lt;span class="nt"&gt;--type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ClusterIP &lt;span class="nt"&gt;--port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;80     &lt;span class="c"&gt;# Create service&lt;/span&gt;
kubectl get svc                                                 &lt;span class="c"&gt;# List services&lt;/span&gt;
kubectl describe svc myapp                                      &lt;span class="c"&gt;# Service details&lt;/span&gt;
kubectl delete svc myapp                                        &lt;span class="c"&gt;# Delete service&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Service Types&lt;/strong&gt;: &lt;code&gt;ClusterIP&lt;/code&gt; (default), &lt;code&gt;NodePort&lt;/code&gt;, &lt;code&gt;LoadBalancer&lt;/code&gt;, &lt;code&gt;ExternalName&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🛠️ &lt;strong&gt;ConfigMaps &amp;amp; Secrets&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl create configmap myconfig &lt;span class="nt"&gt;--from-literal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;value
kubectl get configmaps
kubectl describe configmap myconfig

kubectl create secret generic mysecret &lt;span class="nt"&gt;--from-literal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;mypassword
kubectl get secrets
kubectl describe secret mysecret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📂 &lt;strong&gt;Namespaces&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl get namespaces
kubectl create namespace dev
kubectl config set-context &lt;span class="nt"&gt;--current&lt;/span&gt; &lt;span class="nt"&gt;--namespace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📄 &lt;strong&gt;YAML Manifest Commands&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; config.yaml           &lt;span class="c"&gt;# Apply from file&lt;/span&gt;
kubectl delete &lt;span class="nt"&gt;-f&lt;/span&gt; config.yaml          &lt;span class="c"&gt;# Delete resources from file&lt;/span&gt;
kubectl create &lt;span class="nt"&gt;-f&lt;/span&gt; config.yaml          &lt;span class="c"&gt;# Create resources from file&lt;/span&gt;
kubectl explain pod                    &lt;span class="c"&gt;# Explain resource structure&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📊 &lt;strong&gt;Monitoring &amp;amp; Debugging&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl top pod                        &lt;span class="c"&gt;# Show resource usage&lt;/span&gt;
kubectl describe node &amp;lt;node-name&amp;gt;     &lt;span class="c"&gt;# Node info&lt;/span&gt;
kubectl get events                    &lt;span class="c"&gt;# Cluster event&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📚 Bonus: Useful Shortcuts
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
kubectl get po                         &lt;span class="c"&gt;# po = pods&lt;/span&gt;
kubectl get deploy                     &lt;span class="c"&gt;# deploy = deployments&lt;/span&gt;
kubectl get svc                        &lt;span class="c"&gt;# svc = services&lt;/span&gt;
kubectl get ns                         &lt;span class="c"&gt;# ns = namespaces&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






</description>
      <category>kubernetes</category>
      <category>cheatsheet</category>
    </item>
    <item>
      <title>API Gateway vs Service Mesh</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Thu, 22 May 2025 15:56:04 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/api-gateway-vs-service-mesh-2fl9</link>
      <guid>https://forem.com/ankitmalikg/api-gateway-vs-service-mesh-2fl9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As organizations adopt microservices architectures to improve scalability, agility, and team autonomy, managing service communication becomes a central concern. Two popular infrastructure components that help tackle these challenges are &lt;strong&gt;API Gateways&lt;/strong&gt; and &lt;strong&gt;Service Meshes&lt;/strong&gt;. While both deal with communication and traffic management in distributed systems, they serve &lt;strong&gt;distinct roles&lt;/strong&gt; and operate at different layers of the application stack.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore the key differences between API Gateways and Service Meshes, their purposes, features, and when you might use one—or both—in your architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚪 What is an API Gateway?
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;API Gateway&lt;/strong&gt; is the single entry point for external clients (such as mobile apps, frontend applications, or third-party systems) to access backend services in a microservices architecture. It acts as a reverse proxy that accepts API calls, applies policies, and routes requests to the appropriate services.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔑 Core Responsibilities:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Routing and forwarding external requests&lt;/li&gt;
&lt;li&gt;Authentication and authorization (e.g., OAuth, JWT)&lt;/li&gt;
&lt;li&gt;Rate limiting and throttling&lt;/li&gt;
&lt;li&gt;Request transformation (e.g., path rewriting, protocol translation)&lt;/li&gt;
&lt;li&gt;Aggregating responses from multiple services&lt;/li&gt;
&lt;li&gt;Caching and load shedding&lt;/li&gt;
&lt;li&gt;Logging and monitoring API usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🧰 Common API Gateway Solutions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS API Gateway&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kong&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Apigee&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;NGINX&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Traefik&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📌 Example Use Case:
&lt;/h3&gt;

&lt;p&gt;A mobile app sends a request to &lt;code&gt;/user/profile&lt;/code&gt;. The API Gateway:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Authenticates the request using a JWT token.&lt;/li&gt;
&lt;li&gt;Applies rate limiting rules.&lt;/li&gt;
&lt;li&gt;Routes the request to the User Service.&lt;/li&gt;
&lt;li&gt;Aggregates additional data from a Preferences Service (if needed).&lt;/li&gt;
&lt;li&gt;Returns the response to the client.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🔄 What is a Service Mesh?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Service Mesh&lt;/strong&gt; is an infrastructure layer designed to handle &lt;strong&gt;internal service-to-service&lt;/strong&gt; communication. It decouples networking logic from application code and provides fine-grained control over how services discover and interact with each other.&lt;/p&gt;

&lt;p&gt;Service Meshes typically use &lt;strong&gt;sidecar proxies&lt;/strong&gt; (like Envoy) that run alongside each service instance to manage network traffic.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔑 Core Responsibilities:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Secure service-to-service communication with &lt;strong&gt;mutual TLS (mTLS)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Fine-grained traffic routing and load balancing&lt;/li&gt;
&lt;li&gt;Automatic retries, timeouts, and circuit breakers&lt;/li&gt;
&lt;li&gt;Distributed tracing and telemetry&lt;/li&gt;
&lt;li&gt;Policy enforcement and access control&lt;/li&gt;
&lt;li&gt;Fault injection and chaos testing&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🧰 Popular Service Mesh Solutions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Istio&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linkerd&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Consul Connect&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kuma&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AWS App Mesh&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📌 Example Use Case:
&lt;/h3&gt;

&lt;p&gt;Service A wants to call Service B. The service mesh:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Intercepts the request via a sidecar proxy.&lt;/li&gt;
&lt;li&gt;Applies mTLS for encrypted communication.&lt;/li&gt;
&lt;li&gt;Enforces retry logic and timeout settings.&lt;/li&gt;
&lt;li&gt;Logs the request metadata for distributed tracing.&lt;/li&gt;
&lt;li&gt;Forwards the request to Service B’s sidecar, which passes it to the app.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🆚 API Gateway vs. Service Mesh: Key Differences
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;API Gateway&lt;/th&gt;
&lt;th&gt;Service Mesh&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Primary Role&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Manage traffic between clients and services&lt;/td&gt;
&lt;td&gt;Manage internal service-to-service traffic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Traffic Direction&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;North-South (external ↔ internal)&lt;/td&gt;
&lt;td&gt;East-West (internal ↔ internal)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Deployment&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;At the edge of the network&lt;/td&gt;
&lt;td&gt;As sidecars alongside services&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Authentication&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;OAuth, API keys, JWT&lt;/td&gt;
&lt;td&gt;mTLS, service identity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Traffic Policies&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Rate limiting, throttling&lt;/td&gt;
&lt;td&gt;Retries, timeouts, circuit breaking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Observability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;API usage metrics&lt;/td&gt;
&lt;td&gt;Service-level telemetry and tracing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Complexity&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Moderate&lt;/td&gt;
&lt;td&gt;Higher (due to mesh orchestration)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🧩 Can You Use Both Together?
&lt;/h2&gt;

&lt;p&gt;Yes—and in fact, many production-grade microservice architectures &lt;strong&gt;combine both&lt;/strong&gt; to handle different types of traffic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;API Gateway&lt;/strong&gt; sits at the edge of your system, managing communication from external clients.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Service Mesh&lt;/strong&gt; governs communication within the system, between internal services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This layered approach provides full control over &lt;strong&gt;all&lt;/strong&gt; traffic flows, end-to-end security, and improved observability.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ When to Use What?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Use API Gateway&lt;/th&gt;
&lt;th&gt;Use Service Mesh&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;You need to expose services to the public&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You want mTLS between internal services&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You want retry logic and circuit breaking&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You need OAuth2 authentication for APIs&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You want telemetry for service-to-service calls&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You need to aggregate multiple services for a client&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




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

&lt;p&gt;Both API Gateways and Service Meshes are essential tools in a microservices toolkit—but they serve &lt;strong&gt;very different purposes&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use an &lt;strong&gt;API Gateway&lt;/strong&gt; to &lt;strong&gt;secure, route, and manage&lt;/strong&gt; client access to your services.&lt;/li&gt;
&lt;li&gt;Use a &lt;strong&gt;Service Mesh&lt;/strong&gt; to &lt;strong&gt;secure, control, and observe&lt;/strong&gt; internal service-to-service communication.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By understanding their roles, you can build more secure, resilient, and observable distributed systems.&lt;/p&gt;

</description>
      <category>apigateway</category>
      <category>servicemesh</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Golang - How to do Benchmarking</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Thu, 15 May 2025 12:26:58 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/golang-how-to-do-benchmarking-4h20</link>
      <guid>https://forem.com/ankitmalikg/golang-how-to-do-benchmarking-4h20</guid>
      <description>&lt;p&gt;Performance matters — especially when dealing with large-scale systems or performance-critical code. Go (Golang) provides first-class support for benchmarking via the &lt;code&gt;testing&lt;/code&gt; package. This article walks you through the fundamentals of benchmarking in Go: what it is, why it's important, and how to run benchmarks to compare multiple functions.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 What is Benchmarking?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Benchmarking&lt;/strong&gt; is the process of measuring how efficiently a piece of code executes. In Go, this typically involves determining how long a function takes to run, how many resources it uses, or how well it scales under load.&lt;/p&gt;

&lt;p&gt;Benchmarking is different from testing — it doesn’t check if your code is correct but it checks how well it performs.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ Why We Should Do Benchmarking
&lt;/h2&gt;

&lt;p&gt;Benchmarking is useful when you want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compare different implementations&lt;/strong&gt; of the same logic (e.g., using &lt;code&gt;+&lt;/code&gt; vs &lt;code&gt;strings.Builder&lt;/code&gt; for string concatenation).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find performance bottlenecks&lt;/strong&gt; in your application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Track regressions&lt;/strong&gt; over time, especially in performance-sensitive systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make data-driven decisions&lt;/strong&gt; when optimizing code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, benchmarking gives you &lt;strong&gt;quantitative insights&lt;/strong&gt; into your code’s behavior under stress.&lt;/p&gt;




&lt;h2&gt;
  
  
  💻 Example
&lt;/h2&gt;

&lt;p&gt;Let’s take an easy and real world example which we often overlook&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;benchmark&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"strings"&lt;/span&gt;
    &lt;span class="s"&gt;"testing"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;concatWithPlus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;concatWithBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;
    &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;BenchmarkConcatWithPlus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;testing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;concatWithPlus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;BenchmarkConcatWithBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;testing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;concatWithBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ▶️ Running the Benchmark
&lt;/h2&gt;

&lt;p&gt;Save the file as &lt;code&gt;concat_test.go&lt;/code&gt; and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;-bench&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📤 Output
&lt;/h2&gt;

&lt;p&gt;Example output might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;goos: linux
goarch: amd64
BenchmarkConcatWithPlus-8        10000000               150 ns/op
BenchmarkConcatWithBuilder-8     20000000                90 ns/op
PASS
ok      example.com/benchmark   2.345s

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  📖 How to Read the Output
&lt;/h2&gt;

&lt;p&gt;Each line shows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;BenchmarkConcatWithPlus-8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Benchmark function run on 8 logical CPUs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;10000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Number of iterations Go used for benchmarking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;150 ns/op&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Average time taken for one operation (in nanoseconds)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In this case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;concatWithBuilder&lt;/code&gt; is faster (90 ns/op) than &lt;code&gt;concatWithPlus&lt;/code&gt; (150 ns/op).&lt;/li&gt;
&lt;li&gt;Therefore, &lt;code&gt;strings.Builder&lt;/code&gt; is a more efficient way to concatenate strings in this context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optional Memory Metrics
&lt;/h3&gt;

&lt;p&gt;To view memory allocations and usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;-bench&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-benchmem&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
BenchmarkConcatWithPlus-8        10000000               150 ns/op            16 B/op          1 allocs/op
BenchmarkConcatWithBuilder-8     20000000                90 ns/op             0 B/op          0 allocs/op
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;150 ns/op&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;How many bytes of memory were allocated per iteration.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1 allocs/op&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;How many memory allocation events occurred per iteration.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This shows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;concatWithPlus&lt;/code&gt; allocates memory (16 bytes per operation).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;concatWithBuilder&lt;/code&gt; avoids allocations — a big win for performance.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🏁 Conclusion
&lt;/h2&gt;

&lt;p&gt;Benchmarking in Go is easy to set up and incredibly valuable. It helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand the performance characteristics of your code&lt;/li&gt;
&lt;li&gt;Choose the most efficient implementations&lt;/li&gt;
&lt;li&gt;Catch regressions before they ship to production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By comparing two simple functions, we saw how small differences in implementation can lead to measurable performance improvements.&lt;/p&gt;

</description>
      <category>go</category>
      <category>benchmarking</category>
    </item>
    <item>
      <title>What is redis pipeline</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Sun, 11 May 2025 10:53:01 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/what-is-redis-pipeline-11ab</link>
      <guid>https://forem.com/ankitmalikg/what-is-redis-pipeline-11ab</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Redis is a fast, in-memory data store widely used for caching, message brokering, and real-time analytics. While Redis is incredibly efficient, network overhead can become a bottleneck when a large number of commands are sent sequentially. This is where &lt;strong&gt;Redis pipelining&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;Redis pipelining allows a client to send multiple commands to the server without waiting for individual responses. The server processes the batch of commands and returns all responses in a single reply, significantly reducing the latency caused by round-trip communication.&lt;/p&gt;

&lt;p&gt;If you are struggling to remember redis commands then this &lt;a href="https://dev.to/ankitmalikg/redis-cheatsheet-2im8"&gt;cheatsheet&lt;/a&gt; might help you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;p&gt;Redis pipelining offers several advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reduced Latency&lt;/strong&gt;: By sending multiple commands in a single request, you cut down on network round-trips.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Throughput&lt;/strong&gt;: It enables higher command throughput, especially for bulk operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient Network Utilization&lt;/strong&gt;: Reduces packet overhead and increases the efficiency of TCP connections.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Atomicity&lt;/strong&gt; (optional with MULTI/EXEC): When used inside transactions, pipelining can contribute to atomic command execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoids Race condition:&lt;/strong&gt; If we are in some situation like we want to read some counter value and then increment it. Pipeline would help there by reducing the time and run both commands at same time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, pipelining doesn’t provide isolation like transactions—it simply groups commands for performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Example - Golang
&lt;/h2&gt;

&lt;p&gt;Perform various type of operations with pipeline -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"context"&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/redis/go-redis/v9"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Background&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;rdb&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Options&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Addr&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"localhost:6379"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="n"&gt;pipe&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;rdb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c"&gt;// Queue up multiple commands&lt;/span&gt;
    &lt;span class="n"&gt;incr1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Incr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"counter1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;incr2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Incr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"counter2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"key1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"value1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Execute all commands&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Access responses&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"counter1:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;incr1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Val&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"counter2:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;incr2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Val&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;counter1: 1
counter2: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rdb.Pipeline()&lt;/code&gt; creates a pipeline.&lt;/li&gt;
&lt;li&gt;Commands are queued (&lt;code&gt;Incr&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Exec()&lt;/code&gt; sends all commands and receives the results.&lt;/li&gt;
&lt;li&gt;Each response is accessed via the queued command result objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code Example - Python
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Redis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create a pipeline
&lt;/span&gt;&lt;span class="n"&gt;pipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Queue multiple commands
&lt;/span&gt;&lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;incr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;counter1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;incr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;counter2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;key1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;value1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Execute all commands
&lt;/span&gt;&lt;span class="n"&gt;responses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Responses:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;responses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Responses: &lt;span class="o"&gt;[&lt;/span&gt;2, 2, True]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pipeline()&lt;/code&gt; creates a pipeline object.&lt;/li&gt;
&lt;li&gt;Commands are queued with &lt;code&gt;incr&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;execute()&lt;/code&gt; sends all at once and returns results in order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Redis pipelining is a powerful feature that optimizes the communication between client and server by batching multiple commands. This leads to faster operations and improved efficiency, especially in use cases involving bulk writes or reads. While pipelining does not ensure atomic execution like transactions, it significantly boosts performance for high-volume interactions with Redis.&lt;/p&gt;

</description>
      <category>redis</category>
      <category>database</category>
      <category>go</category>
      <category>python</category>
    </item>
    <item>
      <title>VsCode Debugger for Ruby on Rails</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Sat, 10 May 2025 10:22:59 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/vscode-debugger-for-ruby-on-rails-5en</link>
      <guid>https://forem.com/ankitmalikg/vscode-debugger-for-ruby-on-rails-5en</guid>
      <description>&lt;h2&gt;
  
  
  Steps to Add debugger
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;code&gt;rdbg&lt;/code&gt; extention &lt;a href="https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;launch.json&lt;/code&gt; file in vscode.&lt;/li&gt;
&lt;li&gt;Add the given configuration to run the code&lt;/li&gt;
&lt;li&gt;[Optional] You can try this command also if this is working then it should run &lt;code&gt;rdbg --open -n -c -- bundle exec rails s&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;launch.json&lt;/code&gt; file for &lt;code&gt;vscode&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"version"&lt;/span&gt;: &lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;,
        &lt;span class="s2"&gt;"configurations"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
                 &lt;span class="o"&gt;{&lt;/span&gt;
                        &lt;span class="s2"&gt;"type"&lt;/span&gt;: &lt;span class="s2"&gt;"rdbg"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Run rake test"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"request"&lt;/span&gt;: &lt;span class="s2"&gt;"launch"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"command"&lt;/span&gt;: &lt;span class="s2"&gt;"rake"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"script"&lt;/span&gt;: &lt;span class="s2"&gt;"test"&lt;/span&gt;, 
                        &lt;span class="s2"&gt;"args"&lt;/span&gt;: &lt;span class="o"&gt;[]&lt;/span&gt;,
                        &lt;span class="s2"&gt;"askParameters"&lt;/span&gt;: &lt;span class="nb"&gt;false&lt;/span&gt; 
                &lt;span class="o"&gt;}&lt;/span&gt;,
                &lt;span class="o"&gt;{&lt;/span&gt;
                        &lt;span class="s2"&gt;"type"&lt;/span&gt;: &lt;span class="s2"&gt;"rdbg"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Debug Rails server"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"request"&lt;/span&gt;: &lt;span class="s2"&gt;"launch"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"command"&lt;/span&gt;: &lt;span class="s2"&gt;"bin/rails"&lt;/span&gt;,
                        &lt;span class="s2"&gt;"script"&lt;/span&gt;: &lt;span class="s2"&gt;"server"&lt;/span&gt;, // Launch Rails server with debugger
                        &lt;span class="s2"&gt;"args"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"-p"&lt;/span&gt;, &lt;span class="s2"&gt;"3000"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;, // Optional: specify port
                        &lt;span class="s2"&gt;"askParameters"&lt;/span&gt;: &lt;span class="nb"&gt;false&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Details Reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=e_RKkgiimXE" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=e_RKkgiimXE&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Redis cheatsheet</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Sat, 10 May 2025 07:40:39 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/redis-cheatsheet-2im8</link>
      <guid>https://forem.com/ankitmalikg/redis-cheatsheet-2im8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Redis is a powerful in-memory data structure store, commonly used as a database, cache, and message broker. &lt;/p&gt;

&lt;p&gt;This is a quick reference guide containing the most commonly used Redis commands. If you are like me who often forget about commands when not using redis actively then it would be helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cheatsheet Commands
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. String&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use:&lt;/strong&gt; Store simple values (text, numbers, JSON, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commands:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SET key value
GET key
INCR key     &lt;span class="c"&gt;# increment number&lt;/span&gt;
DECR key     &lt;span class="c"&gt;# decrement number&lt;/span&gt;
APPEND key value
DEL key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;2. List&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use:&lt;/strong&gt; Ordered list of strings (like a queue or stack)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commands:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;LPUSH key value   &lt;span class="c"&gt;# add to head&lt;/span&gt;
RPUSH key value   &lt;span class="c"&gt;# add to tail&lt;/span&gt;
LPOP key          &lt;span class="c"&gt;# remove from head&lt;/span&gt;
RPOP key          &lt;span class="c"&gt;# remove from tail&lt;/span&gt;
LRANGE key 0 &lt;span class="nt"&gt;-1&lt;/span&gt;   &lt;span class="c"&gt;# get entire list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;3. Set&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use:&lt;/strong&gt; Unordered collection of unique strings&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commands:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SADD key value
SREM key value
SMEMBERS key
SISMEMBER key value
SUNION key1 key2
SINTER key1 key2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;4. Sorted Set (ZSet)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use:&lt;/strong&gt; Like a set, but each value has a score for sorting&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commands:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ZADD key score member
ZRANGE key 0 &lt;span class="nt"&gt;-1&lt;/span&gt;        &lt;span class="c"&gt;# get sorted elements&lt;/span&gt;
ZREM key member
ZSCORE key member
ZRANK key member
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;5. Hash&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use:&lt;/strong&gt; Store field–value pairs (like a mini JSON or dict)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commands:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;HSET key field value
HGET key field
HGETALL key
HDEL key field
HMSET key field1 val1 field2 val2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;6. Fetch All keys&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;KEYS &lt;span class="k"&gt;*&lt;/span&gt;           &lt;span class="c"&gt;# all keys&lt;/span&gt;
KEYS user:&lt;span class="k"&gt;*&lt;/span&gt;      &lt;span class="c"&gt;# keys that start with "user:"&lt;/span&gt;
KEYS &lt;span class="k"&gt;*&lt;/span&gt;:profile   &lt;span class="c"&gt;# keys that end with ":profile"&lt;/span&gt;
KEYS order:?     &lt;span class="c"&gt;# keys like order:1, order:2 (one character wildcard)&lt;/span&gt;
KEYS session:[a-z]&lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="c"&gt;# regex-style pattern (for lowercase a-z)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Scan is production safe&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SCAN 0 MATCH user:&lt;span class="k"&gt;*&lt;/span&gt; COUNT 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🔹 &lt;strong&gt;7. Hyperloglog Commands&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Uses hyperloglog data structure under the hood. This helps in cardinality of any key and their members.&lt;br&gt;
For example - You want to count the unique number likes for social media post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Add a like - 
&lt;/span&gt;&lt;span class="n"&gt;PFADD&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;PFADD&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;hll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;post_123&lt;/span&gt; &lt;span class="n"&gt;user_456&lt;/span&gt;

&lt;span class="c1"&gt;# Get unique like count - 
&lt;/span&gt;&lt;span class="n"&gt;PFCOUNT&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;PFCOUNT&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;hll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;post_123&lt;/span&gt;

&lt;span class="c1"&gt;# Merge HLLs (e.g. across datacenters)
&lt;/span&gt;&lt;span class="n"&gt;PFMERGE&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;hll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;post_123&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;hll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;post_123&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;dc1&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;hll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;post_123&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;dc2&lt;/span&gt;

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

&lt;/div&gt;



</description>
      <category>redis</category>
      <category>cheatsheet</category>
      <category>database</category>
    </item>
    <item>
      <title>What is Pydantic in python</title>
      <dc:creator>Ankit malik</dc:creator>
      <pubDate>Thu, 08 May 2025 11:45:31 +0000</pubDate>
      <link>https://forem.com/ankitmalikg/what-is-pydantic-in-python-nok</link>
      <guid>https://forem.com/ankitmalikg/what-is-pydantic-in-python-nok</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Python, data validation and settings management are common challenges, especially when working with APIs, configuration files, or user inputs. While Python is a dynamically typed language, developers often need ways to ensure that data provided should be of expected types. This is where Pydantic comes into picture.&lt;/p&gt;

&lt;p&gt;Pydantic is a data validation and parsing library that uses Python type annotations to validate structured data. Built on top of Python 3.6+ type hints, Pydantic provides automatic data validation and conversion, making it especially popular in FastAPI and other modern Python frameworks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Pydantic
&lt;/h2&gt;

&lt;p&gt;Here are some reasons why Pydantic is a go-to tool for developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Validation:&lt;/strong&gt; Automatically validate input data using standard Python types.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Reporting:&lt;/strong&gt; Provides clear, structured error messages when validation fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Pydantic is built using dataclasses and is extremely fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type Coercion:&lt;/strong&gt; Automatically converts compatible types (e.g., string to integer).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration:&lt;/strong&gt; Plays well with modern Python frameworks like FastAPI, which uses it extensively for request and response validation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ease of Use:&lt;/strong&gt; Leverages Python type hints, reducing the need for verbose validation logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Here’s a simple example to demonstrate Pydantic in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ValidationError&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;is_active&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Valid input
&lt;/span&gt;&lt;span class="n"&gt;user_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;123&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Will be coerced to int
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;tags&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;python&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;user_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_dump&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# Invalid input
&lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;invalid_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;invalid_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;ValidationError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;



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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="n"&gt;is_active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;python&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;is_active&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;tags&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;python&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;validation&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;
&lt;span class="nb"&gt;id&lt;/span&gt;
  &lt;span class="n"&gt;Input&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt; &lt;span class="n"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unable&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;parse&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;integer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;int_parsing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;further&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt; &lt;span class="n"&gt;visit&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pydantic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;2.11&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;int_parsing&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt;
  &lt;span class="n"&gt;Input&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;string_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;further&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt; &lt;span class="n"&gt;visit&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pydantic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;2.11&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;string_type&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While both Pydantic and TypedDict from the typing module allow type annotations for dictionaries, they serve different purposes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Pydantic&lt;/th&gt;
&lt;th&gt;TypedDict&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Validation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Default Values&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes (in Python 3.9+)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Type Coercion&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes (e.g., str → int)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Runtime Behavior&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Full-featured at runtime&lt;/td&gt;
&lt;td&gt;Mainly for static analysis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Error Reporting&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Detailed errors via exceptions&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Slightly slower due to checks&lt;/td&gt;
&lt;td&gt;Faster (no runtime overhead)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Summary: Use TypedDict when you only need type hints for static checking (e.g., with mypy). Use Pydantic when you need runtime validation, coercion, and structured error handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Pydantic is a powerful and user-friendly library that brings the type-safe validation into dynamic Python code. It helps developers write cleaner, more maintainable code by enforcing data structure constraints and providing detailed feedback on invalid inputs. Whether you're building a REST API, working with external data sources, or managing configurations, Pydantic can be an essential part of your Python toolkit.&lt;/p&gt;

</description>
      <category>python</category>
      <category>pydantic</category>
    </item>
  </channel>
</rss>
