Skip to content

Instantly share code, notes, and snippets.

@yogain123
Created March 14, 2025 07:09
Show Gist options
  • Save yogain123/26f607dea81716f3a04299523cc8c841 to your computer and use it in GitHub Desktop.
Save yogain123/26f607dea81716f3a04299523cc8c841 to your computer and use it in GitHub Desktop.
Backend System Design Concepts
@yogain123
Copy link
Author

yogain123 commented Mar 14, 2025

Elastic Search

Think of it as Google for your data—it helps you quickly search, analyze, and retrieve data, whether it's text, numbers, or structured data.

Key Features of Elasticsearch

  1. Full-Text Search: Finds relevant results from large text data, similar to how Google searches web pages.
  2. Real-Time Indexing & Searching: Data is searchable almost instantly after being added.
  3. Schema-less (Flexible Data Model): It stores data in JSON format and does not require a predefined schema.

Example to Understand the Difference

Imagine you have a library with 1 million books, and you want to find books with the title containing "Harry Potter."

  • Database (SQL Example):

    SELECT * FROM books WHERE title LIKE '%Harry Potter%';
    • This is slow because the database must scan every record to check for the word "Harry Potter."
  • Elasticsearch Example (Query DSL):

    {
      "query": {
        "match": {
          "title": "Harry Potter"
        }
      }
    }
    • This is much faster because Elasticsearch indexes words separately and retrieves results instantly.

How Elasticsearch Works (Step-by-Step)

Indexing Data

  • Instead of storing rows and columns, Elasticsearch stores data as JSON documents inside Indexes (similar to databases).
  • Example: Adding a document to an index named books:
    POST books/_doc/1
    {
      "title": "Harry Potter and the Sorcerer's Stone",
      "author": "J.K. Rowling",
      "year": 1997
    }

Conclusion

  • Use Elasticsearch when you need fast, real-time searching and analytics.
  • Use a traditional database for structured data and transactions like banking or inventory systems.

@yogain123
Copy link
Author

Redis and Memcached

What is an In-Memory Database?

An in-memory database (IMDB) is a database that stores data in RAM (Random Access Memory) instead of disk storage. This makes it extremely fast because RAM is much faster than traditional disk-based storage.

💡 Why use an in-memory database?

  • Speed: Reading/writing from RAM is much faster than a disk.
  • Scalability: They can handle a large number of requests per second.
  • Caching: Used to store frequently accessed data to reduce load on databases.

🚀 1. Redis (Remote Dictionary Server)

Redis is an open-source, key-value store database that supports multiple data structures like strings, lists, sets, and hashes.

3️⃣ Code Example:

const redis = require('redis');
const client = redis.createClient();

// Connect to Redis
client.on('connect', () => {
    console.log('Connected to Redis!');
});

// Store a key-value pair
client.set('user:1', 'John Doe', (err, reply) => {
    if (err) console.error(err);
    console.log('Set:', reply);
});

// Get the value of the key
client.get('user:1', (err, reply) => {
    if (err) console.error(err);
    console.log('Get:', reply); // Output: John Doe
});

// Close the connection
client.quit();

🚀 2. Memcached

Memcached is also an open-source, key-value store but is designed only for caching. Unlike Redis, it does not support persistence or advanced data types.

const Memcached = require('memcached');
const memcached = new Memcached('localhost:11211');

// Store a key-value pair
memcached.set('user:1', 'John Doe', 10, (err) => {
    if (err) console.error(err);
    console.log('Stored in Memcached');
});

// Get the value of the key
memcached.get('user:1', (err, data) => {
    if (err) console.error(err);
    console.log('Get:', data); // Output: John Doe
});

// Close connection
memcached.end();

@yogain123
Copy link
Author

Scalability

  1. Vertical Scaling (Scaling Up)
  2. Horizontal Scaling (Scaling Out)
  3. Hybrid Scaling (Combination of Both) - Some instance is more powerfull, some less

1. Vertical Scaling (Scaling Up)

📌 What is it?

Vertical scaling means adding more power (CPU, RAM, storage) to a single machine to handle more load.

📌 How does it work?

  • Instead of adding more machines, you upgrade the existing one.
  • Think of it as upgrading your laptop from 8GB RAM to 16GB RAM for better performance.
  • Your application still runs on a single powerful machine.

📌 Example

Imagine you run an e-commerce website on one server. If traffic increases, you:

  • Increase CPU cores
  • Add more RAM
  • Use faster SSDs instead of HDDs

2. Horizontal Scaling (Scaling Out)

📌 What is it?

Horizontal scaling means adding more machines (servers) to distribute the load.

📌 How does it work?

  • Instead of upgrading a single server, you add more servers to handle the load.
  • These servers work together to manage requests efficiently.
  • A Load Balancer distributes traffic across multiple servers.

📌 Example

Imagine Netflix’s streaming service.

  • If 1 million users watch movies at the same time, one server cannot handle it.
  • Instead, Netflix adds more servers and distributes the traffic among them.

💡 Load Balancer ensures that no single server is overloaded.

@yogain123
Copy link
Author

yogain123 commented Mar 14, 2025

Rate Limiting & DDoS

1. Rate Limiting

Rate Limiting controls how many requests a user or system can send within a specific time to prevent abuse and server overload.

Example: A login system allows only 5 attempts per minute to prevent brute-force attacks. If a user exceeds this, further requests are blocked temporarily.

2. DDoS (Distributed Denial of Service) Attack

A DDoS attack happens when multiple devices flood a server with requests, overwhelming it and making it unavailable to real users.

Example: A hacker infects thousands of computers (botnet) and makes them send millions of requests to a website, causing it to crash.

@yogain123
Copy link
Author

Write-Through vs. Write-Behind Cache

1. Write-Through Cache

How it Works?

  • Data is written to both the cache and the database at the same time.
  • Ensures data consistency, but slower writes since it waits for the DB update.

Example:

  • Updating a product price in an e-commerce app → the new price is stored instantly in both cache & DB.

2. Write-Behind Cache

How it Works?

  • Data is written to the cache first, and the DB update is delayed (batched or async).
  • Faster writes, but there’s a risk of data loss if the system crashes before syncing with the DB.

Example:

  • A shopping cart system → items are first stored in cache and synced with DB after some time.

@yogain123
Copy link
Author

What is Serverless

Serverless is a cloud computing model where you don’t manage servers directly. Instead, you write code, and the cloud provider (AWS, Google Cloud, Azure) automatically handles the server infrastructure, scaling, and maintenance for you.


How it Works (Simple Explanation)

  1. You write a function (e.g., a Node.js or Python function).
  2. Deploy it to a cloud provider like AWS Lambda.
  3. The function runs only when triggered (e.g., by an HTTP request or a database update).
  4. You pay only for the execution time (no cost when idle).

Example (AWS Lambda with API Gateway)

You want an API that returns a greeting message.

  • Step 1: Write a simple function in AWS Lambda.
  • Step 2: Connect it to API Gateway (to expose it as an API).
  • Step 3: Call the API, and the function runs only when requested.

Example Code (AWS Lambda - Node.js)

exports.handler = async (event) => {
    return {
        statusCode: 200,
        body: JSON.stringify({ message: "Hello, Serverless World!" }),
    };
};

This function runs only when triggered and does not need a dedicated server running 24/7.

Advantages & Drawbacks of Serverless Computing

Advantages

  1. No Server Management – You don’t need to set up, maintain, or scale servers; the cloud provider does it.
  2. Auto-Scaling – Functions automatically scale up or down based on demand.
  3. Cost-Efficient – You pay only for the execution time of your function, not for idle servers.
  4. Faster Development – Focus on writing code instead of managing infrastructure.
  5. High Availability – Built-in redundancy and fault tolerance in cloud providers ensure high uptime.

Drawbacks

  1. Cold Start Latency – The first request may be slow if the function has been idle for a while.

@yogain123
Copy link
Author

File System (S3) and CDNs

1. File System (S3)

Think of Amazon S3 (Simple Storage Service) as a giant cloud-based hard drive where you can store and retrieve files (images, videos, documents, etc.). It’s similar to Google Drive but designed for apps and websites.

💡 How it works?

  • You upload files to buckets (like folders).
  • Files are stored securely and can be accessed via a URL or API.
  • S3 is highly durable and scalable, meaning it won't lose your data and can handle large traffic.

💻 Example:

  • A social media app stores profile pictures in S3.
  • When a user uploads an image, it gets saved in S3 and can be retrieved when needed.

2. CDN (Content Delivery Network)

A CDN is like a network of super-fast servers spread across the world that stores copies of files (images, CSS, JavaScript, videos) to speed up website loading and reduce load on the main server.

💡 How it works?

  • Your website’s files are stored in multiple locations (called edge servers).
  • When a user visits your site, the nearest server delivers the file faster instead of fetching it from the main server.

S3 + CDN Together?

  • You store files in S3.
  • You use CDN (e.g., CloudFront) to cache those files globally for fast delivery.

Real-World Example:
Netflix stores movies in S3 and delivers them via CDNs, ensuring smooth playback worldwide.

@yogain123
Copy link
Author

Consistent Hashing

Consistent hashing is a distributed hashing technique that minimizes data movement when nodes are added or removed from a system. It helps distribute data across a dynamic set of servers efficiently.

How It Works:

  1. Hashing the Servers & Data:

    • A circular hash space (0 to 2³²-1) is used.
    • Each server (node) is assigned a position on this ring using a hash function.
    • Each data item is also assigned a position using the same hash function.
  2. Placing Data on the Ring:

    • A data item is mapped to the next available node in a clockwise direction.
    • If a node is removed, its data is reassigned to the next node in the ring without affecting other nodes.

Example:

  • Assume we have 3 servers: S1, S2, S3.

  • Hash positions:

    • S1 → 10
    • S2 → 50
    • S3 → 90
  • Data items are hashed:

    • D1 → 12 (goes to S2)
    • D2 → 55 (goes to S3)
    • D3 → 92 (goes to S1)
  • If S2 is removed, D1 will be reassigned to S3, while D3 remains unaffected.

Advantages:

  • Minimizes reassignments: Only a fraction of data moves when a node joins/leaves.
  • Scalability: Works well in dynamic distributed systems like caching (e.g., Memcached, CDNs).

@yogain123
Copy link
Author

yogain123 commented Mar 14, 2025

Database Sharding vs Partitioning

1. Database Sharding
Sharding is a horizontal partitioning technique where data is split across multiple databases (shards). Each shard operates independently, reducing the load on a single database.
Use Case: A social media platform (e.g., Twitter) shards users based on user IDs—users with IDs 1-10M in one database, 10M-20M in another.

2. Database Partitioning
Partitioning is dividing a single database table into smaller, more manageable chunks. It can be:

  • Horizontal Partitioning (same as sharding but within a single database)
  • Vertical Partitioning (splitting columns into different tables)

Use Case: A large Orders table partitioned by order_date, so queries for recent orders are faster.

Key Difference:

  • Sharding = Multiple databases, distributed system
  • Partitioning = Dividing data within a single database

@yogain123
Copy link
Author

yogain123 commented Mar 22, 2025

The only AWS services we need in AWS

  • EC2/AWS Elastic Beanstalk
  • S3 -> Countfront(CDN)
  • DB/DynomoDB/Mongo
  • Serverless function(lymbda)
  • Kafka
  • Redis

@yogain123
Copy link
Author

yogain123 commented Mar 25, 2025

Leetcode Patterns

1. Two Pointers

  • Kya Hai?: Isme do pointers (do alag positions) use karte hain array ya list mein. Ek pointer ek taraf se start hota hai, doosra doosri taraf se, ya dono ek jagah se bhi chal sakte hain.
  • Kab Use Karein?: Jab sorted array mein kuch find karna ho ya do elements ka sum/check karna ho.
  • Example: "Find two numbers in array jinka sum 10 banta hai." Ek pointer start se, ek end se, aur adjust karte jao.
  • Simple Samajh: Ek bhai left se chalta hai, ek right se, dono milke kaam nipta dete hain.

2. Sliding Window

  • Kya Hai?: Ek "window" ya chhota sa part array ka lete hain, aur usko slide karte hain (aage badhate hain) problem solve karne ke liye.
  • Kab Use Karein?: Jab continuous subarray ya substring mein kuch dhundna ho, jaise max sum ya min length.
  • Example: "Ek subarray dhundo jiska sum 15 ho." Window chhoti-badi karke check karo.
  • Simple Samajh: Jaise train ki khidki se scenery dekhte ho, thodi si khisko aur dekho.

3. Fast and Slow Pointers (Tortoise and Hare)

  • Kya Hai?: Do pointers hain, ek slow (dheere chalta hai), ek fast (tez chalta hai). Linked list ya cycle detection mein kaam aata hai.
  • Kab Use Karein?: Jab cycle check karna ho ya middle element dhundna ho.
  • Example: "Linked list mein loop hai ya nahi?" Fast wala 2 steps, slow wala 1 step, agar mil gaye toh loop hai.
  • Simple Samajh: Ek bhai tez bhaagta hai, ek aaram se, agar dono mil jayein toh kuch gadbad hai.

4. Binary Search

  • Kya Hai?: Sorted array mein tezi se element dhundne ka tareeka. Har baar aadha hissa eliminate karte hain.
  • Kab Use Karein?: Jab array sorted ho aur kuch search karna ho.
  • Example: "Ek number dhundo sorted array mein." Middle check karo, chhota hai toh left, bada hai toh right.
  • Simple Samajh: Jaise phonebook mein naam dhundte ho, beech ka page kholo, phir aadha chhodo.

5. Depth-First Search (DFS)

  • Kya Hai?: Graph ya tree mein ek rasta lekar end tak jao, phir wapas aao aur doosra rasta try karo.
  • Kab Use Karein?: Jab saare possible paths check karne hon ya connected components dhundne hon.
  • Example: "Maze mein rasta dhundo." Ek taraf jao, band ho toh wapas aao.
  • Simple Samajh: Ek gali mein ghuso, agar band ho toh wapas aao, doosri gali try karo.

6. Breadth-First Search (BFS)

  • Kya Hai?: Graph ya tree mein level by level explore karo, pehle close nodes, phir door wale.
  • Kab Use Karein?: Jab shortest path dhundna ho ya level-wise kaam karna ho.
  • Example: "Tree mein level order print karo." Queue use karke ek ek level dekho.
  • Simple Samajh: Jaise line mein ek ek banda aage badhta hai, sabko baari baari dekho.

7. Recursion

  • Kya Hai?: Function khud ko call karta hai chhoti problem solve karne ke liye.
  • Kab Use Karein?: Jab problem ko chhote chhote parts mein tod sakte ho, jaise tree traversal.
  • Example: "Factorial nikalna." 5! = 5 * 4!, phir 4! ko todte jao.
  • Simple Samajh: Ek bhai se help mango, woh doosre se, phir sab milkar kaam pura karo.

8. Dynamic Programming (DP)

  • Kya Hai?: Badi problem ko chhoti subproblems mein todkar solve karo, aur results save karo taaki repeat na karna pade.
  • Kab Use Karein?: Jab overlapping subproblems hon, jaise Fibonacci ya knapsack.
  • Example: "Min steps to reach 10." Har step ke liye best option save karo.
  • Simple Samajh: Pehle chhoti chhoti cheezein seekho, phir unko jodkar bada kaam niptao.

9. Greedy

  • Kya Hai?: Har step pe best option chuno, bina future ke baare mein zyada soch ke.
  • Kab Use Karein?: Jab local best choice overall best ban jaye, jaise coin change.
  • Example: "Min coins se amount banao." Sabse bada coin pehle lo.
  • Simple Samajh: Jo abhi best lage, woh karo, baad ki baad mein dekhi jayegi.

10. Backtracking

  • Kya Hai?: Saare possible solutions try karo, agar galat lage toh wapas jao aur doosra try karo.
  • Kab Use Karein?: Jab combinations ya permutations banane hon, jaise N-Queens.
  • Example: "8 queens ko board pe set karo." Ek jagah rakho, na chale toh wapas hatao.
  • Simple Samajh: Trial and error, galat hua toh wapas shuru se try karo.

11. Hashing

  • Kya Hai?: Data ko key-value pair mein store karo taaki tezi se dhund sako.
  • Kab Use Karein?: Jab frequency count karni ho ya duplicates check karne hon.
  • Example: "Array mein duplicates dhundo." HashMap mein daal do, repeat check karo.
  • Simple Samajh: Jaise almirah mein har cheez ka number hota hai, turant mil jati hai.

12. Stack

  • Kya Hai?: Last In, First Out (LIFO) tareeke se kaam karta hai.
  • Kab Use Karein?: Jab reverse karna ho ya nesting check karni ho, jaise valid parentheses.
  • Example: "Brackets valid hain ya nahi?" Stack mein push/pop karke match karo.
  • Simple Samajh: Jaise plate stack karte ho, upar wali pehle uthegi.

13. Queue

  • Kya Hai?: First In, First Out (FIFO) tareeke se kaam karta hai.
  • Kab Use Karein?: Jab order maintain karna ho, jaise BFS.
  • Example: "Level order traversal." Queue mein daalo, ek ek nikalo.
  • Simple Samajh: Jaise line mein pehle aaya, pehle gaya.

14. Heap (Priority Queue)

  • Kya Hai?: Min ya max element tezi se nikalne ke liye use hota hai.
  • Kab Use Karein?: Jab top k elements chahiye, jaise "top 5 scores".
  • Example: "K largest elements dhundo." Max heap banao, top k nikalo.
  • Simple Samajh: Jaise sabse bada ya chhota banda tezi se chuno.

Types of DP problem

  • Top-Down (Memoization)
    • Recursion + caching
    • You break the problem into subproblems recursively, and store (memoize) their results to avoid recomputation.
  • Bottom-Up (Tabulation)
    • Iterative + table building
    • You solve all smaller subproblems first, then build up the solution.

@yogain123
Copy link
Author

yogain123 commented May 26, 2025

Cloud Providers ways

Hybrid: On-premises + Public cloud
Multi-cloud: Multiple public cloud providers

Example 1 - Hybrid:
On-premises: Oracle database running in your own data center
Public cloud: EC2 instance on AWS connecting to your on-premises Oracle DB

Example 2 - Hybrid:
On-premises: Your application servers in your data center
Public cloud: Oracle Autonomous Database in OCI, accessed by your on-premises app

Artifacts vs Sourcecode

source-code
Source code is the original code written by developers using a programming language (e.g., JavaScript, Python, Java, C++) that defines the behavior and functionality of the application.

Artifacts
Artifact refers to the output generated after the source code has been processed through a build or compilation process. It’s what is actually deployed to servers or delivered to clients.

@yogain123
Copy link
Author

Why Some Programming Languages Are Faster Than Others

  • Go compiles directly to machine code (Ahead-of-Time), so the CPU runs it immediately — fast and efficient.

  • Java compiles to bytecode, which runs on the JVM. JVM uses Just-In-Time (JIT) compilation at runtime, which is fast but not as fast as Go's native binary.

  • JavaScript is highly dynamic. It can’t be fully compiled ahead of time. It runs in an engine (like V8) that interprets or JITs the code while running — slower and less predictable.

@yogain123
Copy link
Author

credentials - "includes" in cookies

Same-Origin Requests (Frontend and Backend on same domain)

  • Cookies are sent automatically — no config needed.

  • Example:
    Frontend → https://myapp.com
    API → https://myapp.com/api

fetch('/api/user'); // ✅ cookies included by default

Cross-Origin Requests (Different domain or subdomain)

By default, cookies are NOT sent, for security.

To enable cookies in cross-origin:

✅ Frontend (fetch):

fetch('https://api.backend.com/user', {
  credentials: 'include' // 🔥 explicitly ask browser to send cookies
});

✅ Backend (Express + CORS):

app.use(cors({
  origin: 'https://frontend.com',       // Exact domain
  credentials: true                     // 🔥 allow credentials
}));

✅ Cookies (when setting cookies from server):

res.cookie('token', token, {
  httpOnly: true,
  secure: true,
  sameSite: 'None' // 🔥 critical for cross-origin
});

@yogain123
Copy link
Author

yogain123 commented Jun 17, 2025

How Semantic actions works in AI Apps

🧠 OpenAI as the Brain: Understanding the Semantic Action Flow

Think of OpenAI as a smart brain that understands user intent through natural language. But it needs our help to know what actions it can perform — that’s where Semantic Actions and the OpenAPI YAML file come in.


🔁 The End-to-End Flow

  1. Define Actions in openapi.json
    We start by defining all our semantic actions in a JSON file (openapi.json).
    This includes:

    • Action names (like viewPatientReport)
    • Expected parameters (like reportType, patientName)
    • Their descriptions and structure
  2. Convert json to Functions Schema
    This JSON is converted into a JavaScript function array.. operationid is actually semantic action(function name)
    OpenAI API understands and uses to reason about your app.

  3. User Inputs a Natural Language Query
    For example:

    "Show me the ECG report for patient John Doe"
    
  4. Send Query + function array
    We send this query to OpenAI along with the function array.
    OpenAI processes the message and tries to match the intent with the closest semantic action.

  5. OpenAI Returns a Structured Response
    OpenAI returns a response like:

    {
      "name": "viewPatientReport",
      "arguments": {
        "reportType": "ECG",
        "patientName": "John Doe"
      }
    }
  6. Shell App Executes the Action
    This structured response is then passed to the Shell app, which acts as the executor. The Shell might:

    • Trigger an event on an event bus
    • Navigate to a microfrontend (MFE)
    • Call a function you defined for that semantic action
  7. Result is Shown to the User
    The appropriate MFE or module handles the action and shows the correct report (e.g., ECG report of John Doe) on the screen.


🧩 In Short

  • OpenAI understands what the user means.
  • openai.json tells OpenAI what your app can do.
  • You write the actual logic to handle the response — either in functions, events, or routing inside the Shell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment