---
title: "Code Snippets"
description: "A curated collection of useful code snippets, examples, and ready-to-use solutions for various programming languages, frameworks, and common development scenarios. Copy, paste, and adapt for your projects."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/codesnippets
---

# Code Snippets

16 entries.

- [Python Async HTTP Requests with aiohttp: Fetch Multiple URLs Concurrently](https://mkabumattar.com/codesnippets/post/python-async-http-aiohttp-concurrent-requests) — A Python snippet demonstrating concurrent HTTP requests using aiohttp and asyncio. Covers creating sessions, fetching multiple URLs in parallel, handling errors gracefully, and controlling concurrency with semaphores.
- [Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](https://mkabumattar.com/codesnippets/post/bash-retry-function-exponential-backoff) — A reusable Bash function that retries any failing command with configurable attempts and exponential backoff. Ideal for wrapping flaky network calls, AWS CLI commands, or deployment scripts in CI/CD pipelines.
- [Node.js Environment Variable Validation with Zod at Startup](https://mkabumattar.com/codesnippets/post/nodejs-env-validation-zod-startup) — Stop trusting process.env blindly. This Node.js and TypeScript snippet validates every environment variable at startup with a Zod schema, coerces strings into real types, and refuses to boot on bad config so you catch mistakes before a single request is served.
- [AWS EC2 Instance Management with Boto3: Start, Stop, and Query Instances](https://mkabumattar.com/codesnippets/post/aws-ec2-instance-management-boto3-python) — Learn how to automate AWS EC2 instance management using Python and Boto3. This guide covers authentication with IAM roles, starting and stopping instances, using waiters, filtering by tags, running bulk operations, and handling API errors. Practical code examples included for DevOps engineers and cloud developers
- [Redis Caching Patterns: Cache-Aside, Write-Through & Cache Invalidation](https://mkabumattar.com/codesnippets/post/redis-caching-patterns-architecture) — Master production-ready Redis caching patterns with practical examples. Learn cache-aside (lazy loading), write-through, consistency patterns, TTL strategies, and cache invalidation techniques to reduce database load and improve application performance.
- [PostgreSQL Query Optimization: Indexes, EXPLAIN ANALYZE & Execution Plans](https://mkabumattar.com/codesnippets/post/postgresql-query-optimization-indexes-explain) — Master PostgreSQL query optimization with practical examples. Learn EXPLAIN ANALYZE interpretation, effective index strategies, query rewriting, and connection pooling to identify and fix slow queries in production microservices.
- [Multi-Environment Secret Management with HashiCorp Vault](https://mkabumattar.com/codesnippets/post/hashicorp-vault-multi-environment-secrets) — Manage secrets securely across dev, staging, and production with HashiCorp Vault. This snippet demonstrates dynamic secret generation, rotation, and cross-environment secret syncing patterns.
- [Top 7 Open Source OCR Models for Document Processing](https://mkabumattar.com/codesnippets/post/top-7-open-source-ocr-models) — Explore the best open source OCR models for converting documents, images, and PDFs to text. Compare olmOCR, PaddleOCR, OCRFlux, and more with performance benchmarks and implementation guides.
- [Why printf Beats echo in Linux Scripts](https://mkabumattar.com/codesnippets/post/printf-beats-echo-linux-scripts) — Learn why printf is superior to echo for output in Linux scripts. Discover the reliability issues with echo, the advantages of printf, and when to use each command for better scripting practices.
- [Essential Bash Variables for Every Script](https://mkabumattar.com/codesnippets/post/essential-bash-variables) — Master the most useful Bash special parameters and environment variables. Learn how to use $0, $?, $@, $UID, $EUID, and XDG variables to write more robust and portable scripts.
- [Per-App Shell History for Zsh](https://mkabumattar.com/codesnippets/post/zsh-per-app-history) — Keep your Zsh history organized across different terminal applications. This snippet automatically creates separate history files for each terminal emulator you use.
- [Per-App Shell History for Bash](https://mkabumattar.com/codesnippets/post/bash-per-app-history) — Keep your Bash history organized across different terminal applications. This snippet automatically creates separate history files for each terminal emulator you use.
- [Optimizing your python code with __slots__?](https://mkabumattar.com/codesnippets/post/python-slots-optimization) — Discover how Python `__slots__` can reduce memory usage by up to 40% in data-heavy applications. Perfect for MLOps pipelines and big data processing where millions of objects consume precious memory resources.
- [List S3 Buckets](https://mkabumattar.com/codesnippets/post/python-list-s3-buckets) — Automate AWS S3 interactions. This Python snippet uses Boto3 to easily list all S3 buckets in your account.
- [AWS Secrets Manager](https://mkabumattar.com/codesnippets/post/nodejs-aws-secrets-manager) — Access secrets securely in your Node.js apps. This snippet demonstrates fetching sensitive data from AWS Secrets Manager.
- [Check S3 Bucket Existence](https://mkabumattar.com/codesnippets/post/bash-s3-bucket-exists) — Validate AWS S3 bucket presence in your scripts. This Bash snippet efficiently checks if a bucket exists before proceeding with operations.
