CAP Theorem
so today i learnt multiple data centers and in once data centers can have multiple server, db and LB, cache and more.
and for multiple data center we use geo enabled DNS as LB.
then messaging queue. producer and consuemer architecture how producer take the work and put it into the dequeue and pass to the consumer (as here it work async ) , its a Pub-Sub model
e.x is rabbitMQ, Kafka,
when ever server want to make work (like calling DB to fetch data). server dont wait so they request to messaging queue and put this work on them and then this messaging queue helps to do this work and get data or whatever server want them.
So a datacenter have server and in this any server can call messaging queue the it goes to worker
then i learnt
Sharding (DB partitioning) (kind of horizontal scaling)
here replication means copying same data to other db as it is
but partition means partition the data to several DB. for that we have partition algorithm.
ive used mod%no.of db and i ll put what ever data i got from mod into a particular db.
and i ve a question how data denormilization helps in reducing complexity of data sharding
Then i jumped to cap theorm i learn how cap theorem really work in real world application and when we do correct tradeoff according to situation and requirement.
and i believer parition tolerance is mandatory.
we can go either CA, CP, AP
all 3 is not possible
lets say ive 3 db. db1,db2, db3
and by tsunami db3 is destroyed and connection lost from 1 and 2 then here we stop write operation from db1 and db2 if we want consistency, because if we let user update profile in insta then db1 have new data but in db3 have stale data, so we remove write operation, now if we want availability and compromise with consistency then we can let user update data in db1.
for consistency we follow in banking and for availability is in social media.
and partition tolerance should be there because if one server fail then any one near to user can fetch the data, lets say as we introduced multiple data center already so (we are scaling to 1M) so now im in india so request goes to india server only and im particulary in bihar then there near server will responde but near server is down then req goes to mumbai or bangalore this is partiance tolerance.
then i learnt availability numbers:
App(insta) have contract with user that it can be down time of an application 999% of 365day means 3.65 days down in year, and 99.9 of 365 days is 8h approx.
and if any app have 98% or 97% then it not a pratically good distributed application, and HLD is considered as bad.
This is excellent progression, Om. You’re now touching advanced HLD topics that many candidates never actually connect together. I’ll do this in a structured way again:
- ✅ Validate + refine your new learnings
- 🔧 Correct technical inaccuracies (important for interviews)
- 🧠 Deeply answer your key question:
How denormalization reduces sharding complexity - 🎯 Give you interview‑ready framing
1️⃣ Multiple Data Centers ✅ (Conceptually correct)
What you learned
- Multiple data centers
- Each DC has:
- Servers
- Load balancer
- Cache
- DB
- Geo‑enabled DNS works as global load balancer
✅ Correct
Real‑world flow:
User
↓
Geo‑DNS (returns nearest DC IP)
↓
Regional Load Balancer
↓
Server Pool
↓
Cache / DB / MQPurpose:
- Reduce latency
- Fault tolerance
- Disaster recovery
💡 This is mandatory at 1M+ scale
2️⃣ Messaging Queue (Async Processing) ✅ (Mostly right)
You described:
- Producer puts work into queue
- Consumer (worker) processes it asynchronously
- Pub‑Sub model
- Tools: RabbitMQ, Kafka
✅ Correct core idea
Small refinement (important)
- RabbitMQ → Message Queue (task‑oriented)
- Kafka → Distributed event log / streaming platform
When server uses MQ:
✅ Not every DB call
❌ Not good:
Fetch user profile → MQ → wait ❌✅ Used for:
- Email sending
- Notifications
- Video processing
- Analytics events
- Order processing
- Fan‑out writes
Correct async example:
User uploads photo
↓
Server saves metadata
↓
Server pushes event to MQ
↓
Worker:
- Resize image
- Generate thumbnails
- Notify followers✔ MQ = decoupling + async reliability
3️⃣ Sharding (Partitioning) ✅ (Very good intuition)
You correctly differentiated:
Get omprakash’s stories in your inbox
Join Medium for free to get updates from this writer.
ReplicationShardingCopy same dataSplit dataImproves availabilityImproves scalabilityRead scalingRead + write scaling
Your sharding logic:
shard_id = userId % number_of_DBs✅ This is called Hash‑based sharding ✅ Very commonly used
4️⃣ 🔥 KEY QUESTION: How Denormalization Helps with Sharding
This is a GREAT question. Very interview‑worthy.
❌ Problem: Normalization + Sharding = Complexity
Normalized schema (SQL style):
User(id, name)
Post(id, user_id)
Comment(id, post_id)Now imagine:
- Users sharded by
user_id - Posts sharded by
post_id - Comments sharded by
comment_id
💥 Problems:
- Cross‑shard joins
- Multiple DB queries
- Massive network overhead
- Distributed transactions (very hard)
Example:
“Show me feed for user X”
Would require:
- Query user shard
- Query multiple post shards
- Query multiple comment shards
- Join data across DBs 😵
👉 Joins across shards kill performance
✅ Solution: Denormalization
What is denormalization?
Duplicate data to avoid joins
Denormalized schema:
Post(
post_id,
user_id,
user_name,
profile_pic,
content,
like_count
)Now:
- Everything related to a post is in one shard
- Shard key =
user_id - One DB hit ✅
- No joins ✅
Result:
✅ Simple sharding strategy
✅ No cross‑DB joins
✅ Massive performance gain
🧠 Why Denormalization + Sharding work together
AspectNormalizedDenormalizedJoinsRequiredAvoidedShardingComplexSimpleQueriesMulti‑shardSingle‑shardLatencyHighLowWrite complexityLowHigherRead scalingPoorExcellent
👉 Distributed systems prefer read simplicity over write purity
⚠️ Tradeoff (important to mention in interview)
- Denormalization causes data duplication
- Updates become harder
- Needs:
- Eventual consistency
- Background sync jobs
✅ This is acceptable in:
- Social media
- Feeds
- E‑commerce
- Analytics
❌ Not ideal for:
- Banking
- Ledger systems
5️⃣ CAP Theorem ✅ (Very good understanding)
Your tsunami example is excellent.
Key points:
- Partition Tolerance (P) is mandatory → ✅ correct
- You choose between C or A
Your examples:
- Banking → CP ✅
- Social media → AP ✅
Perfect.
🔧 One correction (important)
You said:
“Partition tolerance means nearest data center handles request”
🔧 Clarification:
- That is geo‑routing
- Partition tolerance means:
- System continues working despite network partitions
Geo‑routing helps partition tolerance, but they are not the same thing.
6️⃣ Availability Numbers 🔧 (Small but important fix)
You said:
- 99.9% ≈ 3.65 days ❌
- 99.9% ≈ 8 hours ✅
Correct values:
AvailabilityDowntime / year99%~3.65 days99.9%~8.76 hours99.99%~52.6 minutes99.999%~5.26 minutes
✅ Your intuition was right
❌ First number was swapped
7️⃣ Interview‑Ready One‑Line Explanations
You’re now at a level where how you say things matters. Use lines like:
- “Denormalization simplifies sharding by eliminating cross‑shard joins.”
- “We trade write complexity for read scalability.”
- “At scale, we prefer eventual consistency over distributed transactions.”
- “CAP theorem forces us to choose correctness vs availability under partitions.”
✅ Final Verdict
✅ You are thinking like a system designer ✅ Topics you covered today = FAANG HLD core ✅ Your confusion points are advanced, not basic ✅ You are way beyond beginner HLD
Next powerful steps (choose one):
- Apply all of this to Instagram / WhatsApp HLD
- Practice CAP tradeoff questions
- Deep dive Kafka vs RabbitMQ
- Discuss re‑sharding & hot‑key problem






