What Is Sharding in System Design?

As applications grow, data grows.
And when data grows too much, a single database can’t handle it efficiently.

This is where sharding comes in.


The Problem: One Database, Too Much Load

Imagine you have:

  • Millions of users
  • Billions of records
  • Thousands of requests per second

If all data lives in one database:

  • Queries become slow
  • CPU and memory max out
  • Scaling becomes expensive
  • One failure can bring everything down

πŸ‘‰ Vertical scaling (adding more RAM/CPU) has limits.


The Solution: Sharding

Sharding means:

Splitting a large database into smaller, independent pieces called shards and storing them across multiple servers.

Each shard holds only a portion of the data.


Real-World Analogy: Supermarket Billing Counters

Imagine a supermarket with:

  • One billing counter
  • Hundreds of customers

❌ Chaos.

Now introduce multiple counters:

  • Counter 1 β†’ Customers A–D
  • Counter 2 β†’ Customers E–J
  • Counter 3 β†’ Customers K–Z

βœ” Faster checkout
βœ” Less load per counter
βœ” Easy to add new counters

πŸ‘‰ That’s sharding.


How Sharding Works (Conceptually)

Instead of:

Single Database
└── All Users Data

We have:

Shard 1 β†’ User IDs 1–1,000,000
Shard 2 β†’ User IDs 1,000,001–2,000,000
Shard 3 β†’ User IDs 2,000,001–3,000,000

Each shard is:

  • Independent
  • Handles only its own data
  • Can scale separately

Shard Key (Most Important Concept)

A shard key decides:

Which data goes to which shard

Common shard keys:

  • User ID
  • Customer ID
  • Region
  • Date

Example:

userId % 3
userIdShard
1Shard 1
2Shard 2
3Shard 3
4Shard 1

Types of Sharding

1️⃣ Horizontal Sharding (Most Common)

Split rows across shards.

Users 1–1000 β†’ Shard A
Users 1001–2000 β†’ Shard B

βœ” Used in most real systems


2️⃣ Vertical Sharding

Split columns.

User Profile β†’ Shard A
User Orders β†’ Shard B

βœ” Useful when some data is accessed more frequently


3️⃣ Directory-Based Sharding

A lookup table tells:

β€œWhich shard has this data?”

βœ” Flexible
❌ Extra lookup cost


Real-World Use Cases

CompanyHow They Use Sharding
FacebookUser-based sharding
InstagramMedia & user shards
AmazonCustomer & order shards
NetflixRegional sharding
Banking AppsAccount number shards

Challenges in Sharding

Sharding is powerful, but not free.

❌ Cross-Shard Queries

  • Joining data across shards is expensive

❌ Re-sharding

  • Changing shard strategy later is painful

❌ Uneven Load

  • Bad shard key = hotspot shard

Sharding vs Replication (Very Important)

ShardingReplication
Splits dataCopies data
Improves write scalabilityImproves read availability
Each shard has different dataAll replicas have same data

πŸ‘‰ Large systems often use both together.


When Should You Use Sharding?

Use sharding when:

  • Data size is huge
  • Write traffic is high
  • Single DB can’t scale vertically
  • Latency is increasing

❌ Don’t shard too early β€” it adds complexity.


Simple Rule to Remember

Replication is for availability.
Sharding is for scalability.


Final Thoughts

Sharding is not a database feature β€”
it’s a system design decision.

Understanding sharding means:

  • You think about scale
  • You think about failure
  • You think like a backend engineer

Webpack Module Federation

Modern web applications are getting bigger, faster, and more complex.
Teams want to work independently, deploy features separately, and still make everything feel like one app.

That’s exactly where Webpack Module Federation comes in.

Let’s break it down step by step β€” no heavy jargon, I promise 😊


What Is Module Federation?

Module Federation is a Webpack feature that allows one JavaScript application to use code from another application at runtime.

πŸ‘‰ In simple words:

One app can load and use components, utilities, or modules from another app β€” without rebuilding the whole project.


Why Was Module Federation Introduced?

Before Module Federation, we had two main problems:

❌ Problem 1: Huge Bundles

  • All code was bundled together
  • Any small change required rebuilding the entire app

❌ Problem 2: Team Dependency

  • Multiple teams working in one repo caused conflicts
  • Deployment had to be coordinated

The Solution: Micro Frontends

Micro Frontends mean:

  • Break a big frontend into small independent apps
  • Each app can be built and deployed separately

But… how do these apps share code?

➑️ That’s where Module Federation shines.


Real-World Analogy

Think of Module Federation like a food court πŸ”πŸ•πŸœ

  • Each stall (app) cooks its own food
  • You don’t cook everything in one kitchen
  • Customers (users) can order from multiple stalls in one place

Here:

  • Food stall = Independent app
  • Food = Components / logic
  • Food court = Host application

Key Concepts (Very Important)

1️⃣ Host Application

  • The main app
  • Loads modules from other apps

2️⃣ Remote Application

  • The app that exposes components
  • Other apps can consume them

3️⃣ Shared Dependencies

  • Common libraries like react, react-dom
  • Prevents loading the same library multiple times

Basic Example (Simple & Clear)

Scenario

  • App1 (Host) wants to use a Button from App2 (Remote)

πŸ”Ή App2 (Remote) – Expose a Component

// webpack.config.js (App2)
new ModuleFederationPlugin({
name: "app2",
filename: "remoteEntry.js",
exposes: {
"./Button": "./src/Button",
},
});

πŸ‘‰ This means:

  • App2 is saying:
    β€œHey! I’m sharing my Button component.”

πŸ”Ή App1 (Host) – Consume the Remote Component

// webpack.config.js (App1)
new ModuleFederationPlugin({
name: "app1",
remotes: {
app2: "app2@http://localhost:3002/remoteEntry.js",
},
});

Now in your React code:

const RemoteButton = React.lazy(() => import("app2/Button"));

πŸŽ‰ Boom! App1 is using App2’s component at runtime.


How Is This Different from NPM Packages?

NPM PackagesModule Federation
Installed at build timeLoaded at runtime
Needs rebuild for updatesNo rebuild needed
Version conflicts possibleShared dependencies
StaticDynamic

Why Module Federation Is Powerful

βœ… Independent deployments
βœ… Faster builds
βœ… Better team scalability
βœ… True micro frontend architecture
βœ… Runtime code sharing


Things to Be Careful About

  • Version mismatch of shared libraries
  • Network dependency (remote app must be available)
  • Slightly complex setup initially

πŸ’‘ Tip: Always share React as a singleton.


When Should You Use Module Federation?

Use it when:

  • Your app is large
  • Multiple teams work independently
  • You want micro frontends
  • You want faster deployments

Avoid it if:

  • Small app
  • Single developer
  • Simple requirements

Final Thoughts

Webpack Module Federation is not magic β€” it’s a smart way to share code between apps without tight coupling.

Think of it as:

β€œImporting code from another app… but at runtime.”

Once you understand the concept, it feels natural and extremely powerful πŸ’ͺ

ReactDOM vs createRoot: Why Did React Change the Way We Render Apps?

If you’ve worked with React for a while, you probably remember this familiar line of code:

ReactDOM.render(<App />, document.getElementById("root"));

But starting from React 18, you’ll see a new approach:

import { createRoot } from "react-dom/client";
const root = createRoot(document.getElementById("root"));
root.render(<App />);

So the obvious question is:

πŸ‘‰ Why did React introduce createRoot and move away from ReactDOM.render?
Let’s break it down simply.


What is ReactDOM.render()?

ReactDOM.render() was the traditional way to render a React application into the DOM.

Example (Before React 18)

import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));

How it worked

  • React rendered the entire UI synchronously
  • Updates happened immediately
  • Simple and easy, but limited for performance optimizations

What is createRoot()?

createRoot() is the new rendering API introduced in React 18.

Example (React 18 and above)

import { createRoot } from "react-dom/client";
import App from "./App";
const root = createRoot(document.getElementById("root"));
root.render(<App />);

How it works

  • Enables Concurrent Rendering
  • React can pause, resume, or abandon renders
  • Makes apps smoother and more responsive

Why Did React Introduce createRoot()?

The change was made to support modern React features.

Key reasons:

1. Concurrent Rendering

React can:

  • Work on multiple UI updates at the same time
  • Prioritize important updates (like user input)
  • Delay less important renders

This was not possible with ReactDOM.render().


2. Better Performance

With createRoot:

  • Large UI updates don’t block the main thread
  • The app feels more responsive
  • Smooth animations and transitions

3. Foundation for New Features

Many React 18 features only work with createRoot:

  • startTransition
  • Automatic batching
  • Streaming server-side rendering
  • Suspense improvements

What Happens If You Still Use ReactDOM.render()?

  • It still works, but:
  • Runs React in legacy mode
  • You won’t get React 18 performance benefits
  • You may see warnings in future versions

πŸ‘‰ React recommends migrating to createRoot.


Key Differences at a Glance

FeatureReactDOM.rendercreateRoot
Introduced inReact 16React 18
Rendering typeSynchronousConcurrent
PerformanceGoodBetter
Supports new features❌ Noβœ… Yes
Future-proofβŒβœ…

Should You Migrate?

βœ… Yes, if:

  • You’re using React 18
  • You want better performance
  • You plan to use new React features

Migration is simple and usually requires changing just a few lines.


Final Thoughts

The shift from ReactDOM.render() to createRoot() is not just a syntax change β€”
it’s a major architectural upgrade in how React handles rendering.

Even though the old way still works, createRoot is the future of React.

Small change in code, big improvement in performance.


IndentationError: unexpected indent – Python

If you are learning Python, you may have seen this error:

IndentationError: unexpected indent

Unlike many other languages, indentation is part of Python syntax.
This error appears when Python finds spaces or tabs where it does not expect them.

Let’s understand this in the simplest way.


What Does This Error Mean?

Python found extra spaces or tabs at the beginning of a line where they are not allowed.

In Python, indentation must follow strict rules.
You cannot add random spaces like in Java or JavaScript.


❌ Example 1 – Extra Space at the Start

 print("Hello World")

Output

IndentationError: unexpected indent

βœ… Fix

Remove the extra space:

print("Hello World")

❌ Example 2 – Indent Without a Block

name = "Alice"
print(name)

Python does not expect indentation after a normal statement.

βœ… Fix

name = "Alice"
print(name)

❌ Example 3 – Mixing Tabs and Spaces

def show():
print("Hello") # tab
print("World") # spaces

Mixing tabs + spaces confuses Python.

βœ… Fix

Use only spaces (recommended):

def show():
print("Hello")
print("World")

❌ Example 4 – Wrong Indentation Level

if True:
print("Hi")

Code inside if must be indented.

βœ… Fix

if True:
print("Hi")

🧠 Why Python Is Strict About Indentation

In Python, indentation:

  • Defines code blocks
  • Replaces { } used in other languages
  • Makes code more readable

So Python treats wrong indentation as a syntax error.


βœ… Simple Rules to Avoid This Error

  1. Do not add spaces at the beginning randomly
  2. Indent only after:
    • if, for, while, def, class, try
  3. Use either spaces OR tabs β€” never both
  4. Recommended: use 4 spaces per indent

πŸ›  Best Practice in VS Code / Editors

  • Enable: Convert tabs to spaces
  • Set indentation to 4 spaces
  • Turn on: Show whitespace

🎯 Interview One-Liner

IndentationError: unexpected indent occurs when Python finds indentation at a place where it is not syntactically allowed.


✨ Final Thought

Python is basically saying:

β€œI didn’t ask for indentation here!”

Fix the spaces β†’ error gone πŸš€

SyntaxError: unterminated string literal – Python

If you are learning Python, one of the first errors you may see is:

SyntaxError: unterminated string literal

This error looks scary, but the reason is very simple:

Python found the start of a string but could not find where it ends.

Let’s understand this with easy examples.


❌ Why This Error Happens

In Python, strings must start and end with the same quote:

  • 'single quotes'
  • "double quotes"

If Python cannot find the closing quote, it throws:

πŸ‘‰ SyntaxError: unterminated string literal


πŸ§ͺ Example 1 – Missing Closing Quote

message = "Hello World

Output

SyntaxError: unterminated string literal

βœ… Fix

message = "Hello World"

πŸ§ͺ Example 2 – Mixed Quotes

text = "Python is fun'

Started with double quote "
Ended with single quote '

Python gets confused β†’ error.

βœ… Fix

Use the same quotes:

text = "Python is fun"

or

text = 'Python is fun'

πŸ§ͺ Example 3 – Multiline String Mistake

info = "This is line one
This is line two"

Normal quotes cannot span multiple lines.

βœ… Fix 1 – Use Triple Quotes

info = """This is line one
This is line two"""

βœ… Fix 2 – Use newline character

info = "This is line one\nThis is line two"

πŸ§ͺ Example 4 – Quote Inside String

msg = "It's a nice day"

This works because outer quotes are double quotes.

But this fails:

msg = 'It's a nice day'

βœ… Fix

Use escape character:

msg = 'It\'s a nice day'

or switch quotes:

msg = "It's a nice day"

🧠 Simple Rules to Avoid This Error

  1. Always close strings with the same quote you opened.
  2. For multiline text β†’ use triple quotes.
  3. If your text contains quotes β†’ use:
    • opposite quotes, or
    • escape character \.

βœ… Quick Checklist

MistakeSolution
Missing quoteAdd closing quote
Mixed quotesUse same quote type
Multiline stringUse """ """
Quote inside textEscape or change quote

🎯 Interview One-Liner

β€œSyntaxError: unterminated string literal occurs when Python cannot find the closing quote of a string.”


✨ Final Thought

This error is not about logic β€”
it’s just Python saying:

β€œHey, you started a string… but never told me where it ends!”

Fix the quotes β†’ error gone πŸš€