Rujula retweeted
🚀 Learn System Design with me
Day 41 [Part 1/2]: Backpressure
Imagine a producer sending 10,000 messages per second while the consumer can process only 1,000.
What happens to the remaining messages ?
They pile up in the queue, consume memory, and can eventually overwhelm the system.
That's where Backpressure comes in.
Backpressure is a mechanism that allows a slower component to signal a faster component to slow down when it can't keep up.
There are several ways to handle this.
1. Bounded Queues
Set a maximum queue capacity. When the queue is full, new messages are rejected or the producer must wait until space becomes available.
2. Rate Limiting
Limit how many requests or messages a producer can send within a given period. This prevents excessive traffic from overwhelming downstream services.
3. Pull-Based Consumption
Instead of pushing messages to consumers continuously, let consumers fetch messages when they're ready to process them.
4. Batching
Process multiple messages together instead of handling each one individually. This can improve throughput by reducing per-message overhead.
5. Load Shedding
When the system is overloaded, deliberately reject or drop lower-priority work to protect critical operations and keep the service responsive.
6. Reactive Streams
Use demand-based flow control, where consumers explicitly request a certain number of items when they're ready to handle them.
Each approach solves the problem differently. Some slow down incoming traffic, some control how much work is accepted, and others improve processing efficiency.
The key takeaway ?
Backpressure isn't one specific algorithm. It's the broader idea of controlling data flow so that a fast producer doesn't overwhelm a slower consumer.
[Part 2/2]: Circuit Breakers - how to stop a failing downstream service from bringing your entire system down with it.
🚀 Learn System Design with me
Day 40: Dead Letter Queues (DLQ)
First, let's understand how a message queue works.
A Message Queue allows one service to send messages to another without waiting for the receiving service to process them immediately. The consumer picks up messages from the queue and processes them when it's ready.
But what happens when a message repeatedly fails to process?
Imagine a message contains invalid data or triggers a bug in the consumer. The system retries it, but it keeps failing.
If the system retries forever, it wastes resources and might block other messages.
That's where a Dead Letter Queue (DLQ) comes in.
The system sets a retry limit. After a message fails a certain number of times, it's moved to a separate queue called the DLQ instead of being retried endlessly.
The main queue can continue processing other messages, while engineers investigate the failed ones and fix the underlying issue.
Once fixed, those messages can often be reprocessed.
The DLQ helps identify problems like malformed data, application bugs, and unexpected errors without repeatedly disrupting normal processing.
The key takeaway?
A message queue helps services communicate asynchronously. A DLQ handles messages that repeatedly fail, keeping the main queue healthy.
Tomorrow: Circuit Breaker vs Bulkhead - how to prevent one struggling service from bringing down your entire system.
Rujula retweeted
🚀 Learn System Design with me
Day 40: Dead Letter Queues (DLQ)
First, let's understand how a message queue works.
A Message Queue allows one service to send messages to another without waiting for the receiving service to process them immediately. The consumer picks up messages from the queue and processes them when it's ready.
But what happens when a message repeatedly fails to process?
Imagine a message contains invalid data or triggers a bug in the consumer. The system retries it, but it keeps failing.
If the system retries forever, it wastes resources and might block other messages.
That's where a Dead Letter Queue (DLQ) comes in.
The system sets a retry limit. After a message fails a certain number of times, it's moved to a separate queue called the DLQ instead of being retried endlessly.
The main queue can continue processing other messages, while engineers investigate the failed ones and fix the underlying issue.
Once fixed, those messages can often be reprocessed.
The DLQ helps identify problems like malformed data, application bugs, and unexpected errors without repeatedly disrupting normal processing.
The key takeaway?
A message queue helps services communicate asynchronously. A DLQ handles messages that repeatedly fail, keeping the main queue healthy.
Tomorrow: Circuit Breaker vs Bulkhead - how to prevent one struggling service from bringing down your entire system.
🚀 Learn System Design with me
Day 39: Message Queues 101
When one service needs to communicate with another, calling it directly creates a dependency. If the receiving service is down or overloaded, the sender may fail or have to wait.
That's where Message Queues come in.
Instead of calling the receiving service directly, the sender puts a message into a queue and continues working. The consumer processes that message whenever it's ready.
This makes both services independent of each other's timing.
But what if the consumer crashes ?
The messages remain in the queue until the consumer can process them, depending on the queue's durability and configuration. Once the consumer recovers, it can continue processing the pending messages.
Message queues also help handle sudden traffic spikes. If 10,000 requests arrive at once, messages can wait in the queue while consumers process them at a manageable rate.
Popular tools include RabbitMQ, Amazon SQS, and Apache Kafka, each with different approaches to message delivery, ordering, and throughput.
The key takeaway ?
Message queues help services communicate asynchronously, handle traffic spikes, and recover from temporary failures without requiring the sender and receiver to be available at the same time.
Tomorrow: Dead Letter Queues (DLQs) - what happens when a message repeatedly fails to process.
Bhaii kuch tweet krne hi nahi h bas ek project bana rhi hu aaur tho kn chal raha 🤔
Tum batyo kuch aacha project chalu h?
4
12
620
Me, my playlist, and this stupid amount of happiness against the world.
Ahahaa it makes me so happyyyyyyyy😭😭
2
11
366
Rujula retweeted
Day 26 [Part 2/2]: GraphQL - The N+1 Problem
GraphQL's flexibility has a hidden cost.
A single query can trigger many database calls if related data isn't resolved carefully.
This is known as the N+1 problem.
Imagine requesting 50 blog posts and the author's name for each post.
A naive resolver might do:
1 query → fetch 50 posts
+ 50 queries → fetch each author
That's 51 database queries for one API request.
Hence: N + 1
The scary part?
It can look completely fine during development with a few records.
But when a list grows to hundreds or thousands of items, one GraphQL request can suddenly generate hundreds or thousands of database calls.
The common solution is batching.
Instead of fetching each author separately:
Post 1 → Author 1
Post 2 → Author 2
Post 3 → Author 3
the server collects all required author IDs and fetches them together:
[Author 1, Author 2, Author 3, ...] → ONE query
Tools like DataLoader make this pattern easier by batching and caching requests during query execution.
GraphQL gives clients flexibility - but the server still needs to control how that flexibility translates into database work.
Tomorrow: a completely different API approach, built around speed and efficiency rather than flexibility.
Day 26 [Part 1/2]: GraphQL Basics
REST has a common limitation - endpoints usually return a fixed response shape.
But what if the client only needs 2 fields from that response?
GraphQL was designed to solve this problem.
Instead of having many endpoints with predefined responses, GraphQL typically exposes a single endpoint.
The client sends a query describing exactly what it needs.
Need only a user's name and email?
Ask for:
name
email
And that's what comes back.
This helps solve two common REST problems:
Over-fetching → receiving more data than you need.
Under-fetching → needing multiple API requests to collect the data required for one screen.
GraphQL lets the client request the required data in a single query.
This flexibility comes from a schema.
The schema defines the types, objects, fields, and relationships available through the API.
Because the schema is strongly typed, tools can provide features like validation, autocomplete, and automatically generated documentation.
But this flexibility comes with its own problems.
A single GraphQL query can potentially trigger hundreds of database calls if the server isn't designed carefully.
[Part 2/2] : the N+1 query problem — one of the most important performance issues to understand when working with GraphQL.
Rujula retweeted
Day 24 [Part 2/2]: Event Sourcing
1. Most databases store the current state of your data.
If a user's balance is $50, you know the balance is $50.
But you may not know how it became $50.
Event Sourcing takes a different approach.
2. Instead of storing only the current state, we store every change as an event.
For example:
Deposited $100
Withdrew $30
Withdrew $20
Current balance = $50
The current state is built by replaying these events in order.
3. Why is this useful?
You get a complete history of how the system reached its current state.
This is valuable in systems like banking, payments, and inventory, where knowing what happened can be just as important as knowing what the current state is.
4. Event Sourcing also fits naturally with CQRS.
The event log becomes the source for changes, while one or more Read Models can be built from those events.
Need a different Read Model later ?
You can rebuild it by replaying the events.
5. But there's a problem.
What if there are millions of events?
Replaying all of them every time would be slow.
So systems create snapshots of the current state periodically.
Then they only replay the events that happened after the latest snapshot.
6. So remember:
Traditional DB: Store current state
Event Sourcing: Store changes that created the state
You gain history and rebuildability, but add complexity.
Tomorrow: We move from databases to APIs - how systems communicate with each other.
Day 24 [Part 1/2]: CQRS
1. Until now, we've mostly used the same database model for both writing data and reading data.
But here's the problem:
Writing data and reading data often have very different needs.
So why force them to use the same model?
That's the idea behind CQRS.
2. CQRS stands for Command Query Responsibility Segregation.
It separates:
Commands: Change data
Queries: Read data
So we can have:
Write Model: Designed for safely updating data.
Read Model: Designed for quickly retrieving data.
3. Let's take an e-commerce app.
When placing an order, the write model might store:
Order → Customer → Products → Payment
in separate, structured tables to maintain data integrity.
But the order page might need:
Order + Customer Name + Product Names + Total + Payment Status
Instead of joining all this data every time, the read model can store it in a format already optimized for that screen.
4. When an order is created or updated, the write model changes first.
That change can then be sent to the read model, often using CDC, which we learned earlier.
So for a short time:
Write Model: Latest data
Read Model: Slightly older data
This is called eventual consistency.
You're accepting this small delay in exchange for faster and more optimized reads.
5. Why would we do this?
Imagine:
1,000 writes/sec
100,000 reads/sec
The system can optimize the write side for handling changes and the read side for handling huge numbers of queries.
The two sides can even scale independently.
6. So the simple idea is:
Normal approach: One model handles reads + writes.
CQRS: One model handles writes.
Another model handles reads.
You don't need CQRS for every application.
It becomes useful when read and write workloads or data requirements are very different.
[Part - 2/2]: What if instead of storing only the current state, we stored every change that ever happened?
TEACH ABOUT CONSENT AND BOUNDARIES!!!!! EDUCATE!!!!!
Don’t just restrict girls educate boys too
3
39
1,367
Rujula retweeted
Day 20 [Part 2/3]: Consistent Hashing - How It Works
1. In Part 1, we saw why naive hashing causes problems.
Adding one shard can remap a huge number of keys, redirecting traffic and causing cache misses.
So how can we add a shard without disturbing most of the existing data?
Consistent Hashing solves this using a hash ring.
2. Instead of calculating: hash(key) % number_of_shards
we create a fixed circular space, like a clock.
Both shards and keys are placed on this circle using a hash function.
Each key is assigned to the first shard clockwise from its position.
3. Imagine we start with 5 shards:
Shard A
Shard B
Shard C
Shard D
Shard E
Thousands of keys are also spread around the ring.
Each key looks clockwise until it finds its responsible shard.
For example:
Key X → clockwise → Shard C
Key Y → clockwise → Shard E
4. Now we add Shard F.
We don't recalculate every key.
Shard F is placed at a position on the ring.
It takes responsibility for only the small section immediately before it.
For example:
Before: Keys in that section → Shard A
After: Keys in that section → Shard F
The other keys stay with their existing shards.
5. This is the big difference:
Naive Hashing: Add 1 shard → many keys can change shards.
Consistent Hashing: Add 1 shard → only keys in one affected section move.
So we get: Less data movement
Fewer cache misses
Less database load
Less traffic disruption
6. The same idea works when removing a shard.
If Shard C is removed, only the keys that belonged to Shard C need to move to the next shard.
The rest of the ring remains unchanged.
That's why Consistent Hashing makes adding and removing shards much cheaper.
Part 3: But there's still a problem — what if our 5 shards aren't evenly distributed around the ring?
Day 20 [Part 1/3] : Consistent Hashing - The Problem
Why naive hashing hurts during scaling -
Yesterday, we saw that with naive hashing:
shard = hash(key) % number_of_shards
If we add or remove a shard, the calculation changes, so a huge number of keys can suddenly map to different shards.
That means a large amount of data may need to move, and a huge amount of traffic may suddenly be redirected to different servers.
There's another hidden problem: caches.
Suppose a request for user_123 was previously handled by Server 1, and its data was already in Server 1's cache.
After resharding, user_123 may now go to Server 3.
Server 3 doesn't have that cached data, so the request becomes a cache miss and the database has to be accessed again.
A large number of remapped keys can therefore cause a sudden drop in cache hit rate, increasing database load and latency.
That's one more reason we want to minimize data movement when adding or removing shards.
Rujula retweeted
Day 19: Sharding - Resharding & the Hot Shard Problem
1. Yesterday, we split our database across multiple shards.
But data doesn't grow evenly.
Imagine:
Shard 1: 90 GB
Shard 2: 95 GB
Shard 3: 20 GB
Soon, some shards may become full or overloaded while others still have plenty of capacity.
We need to move some data between shards.
This process is called resharding.
2. Now imagine we're using: shard = hash(user_id) % 3
We have 3 shards:
Shard 0
Shard 1
Shard 2
For a user: hash(user_id) % 3 = 1
So the user goes to Shard 1.
Everything works.
3. But now Shard 1 is getting full.
We add Shard 3.
So we change: hash(user_id) % 3
to:
hash(user_id) % 4
Here's the problem.
The result for many users changes.
A user that was: hash(user_id) % 3 = 1
might now become: hash(user_id) % 4 = 3
So the database thinks that user belongs on Shard 3 instead of Shard 1.
And this can happen to a huge number of keys.
4. That means adding just one shard could require moving a large amount of data.
And while that data is moving, the system still needs to:
serve users
keep reads working
accept new writes
keep old and new locations consistent
That's why resharding is difficult.
We need a better way to distribute keys so adding a shard doesn't cause massive data movement.
That's the problem Consistent Hashing is designed to solve.
Tomorrow: Consistent Hashing.
5. There's another sharding problem: 🔥 Hot Shards
Imagine we have 10 shards and millions of users.
Normally, traffic is spread across them.
But then a celebrity joins the platform.
Millions of users start viewing that person's profile.
If that user's data lives on Shard 4, suddenly:
Shard 4: 🔥🔥🔥🔥🔥
Other shards: 😐
The database isn't overloaded overall.
One shard is overloaded.
That's a hot shard.
6. One common solution is to split the traffic for that hot key.
Instead of storing everything under: celebrity:123
we can create multiple keys:
celebrity:123:1
celebrity:123:2
celebrity:123:3
These can be distributed across different shards.
Now the traffic is spread out.
The trade-off?
When reading the data, we may need to read from multiple shards and combine the results.
7. So today we found two different problems:
Resharding: Adding shards can cause lots of data to move.
Hot Shard: One extremely popular key can overload a single shard.
Tomorrow, we'll solve the first problem:
Consistent Hashing - how to add or remove shards while minimizing data movement.
Day 18: Sharding - Hash vs Range Partitioning
1. Yesterday, we saw how replication copies the same data across multiple machines.
But there's a problem: Every copy still needs to receive every write.
So what if the data itself is too large for one machine?
That's where Sharding comes in.
2. Sharding means splitting data across multiple machines, so each machine stores only a portion of the data.
For example:
Shard 1: Users 1–1M
Shard 2: Users 1M–2M
Shard 3: Users 2M–3M
Now one machine doesn't have to store everything.
But how do we decide which shard gets which data?
Two common approaches are Hash Partitioning and Range Partitioning.
3. Hash Partitioning
Take a key, such as user_id, and pass it through a hash function.
The result decides which shard stores the data.
For example: user_id → hash(user_id) → Shard
Because a good hash function spreads keys across shards, the data is usually distributed fairly evenly.
This helps prevent one shard from getting much more traffic than the others.
4. But there's a catch.
Imagine you ask: “Give me all users created this week.”
With hash partitioning, those users could be spread across every shard.
The database may need to ask multiple shards and combine the results.
So hash partitioning is great for even distribution, but not ideal for range queries.
5. Range Partitioning
Instead of hashing the key, we divide it into ranges.
For example:
Shard 1: User IDs 1–1M
Shard 2: User IDs 1M–2M
Shard 3: User IDs 2M–3M
Now a query for: User ID 1.2M–1.5M
can go directly to Shard 2.
Range queries become much easier.
6. But range partitioning has its own problem: Hotspots.
Imagine we're storing data by timestamp.
Most new writes are happening right now.
So almost every new write goes to the shard containing the latest timestamps.
That shard becomes overloaded while others sit mostly idle.
7. So the basic trade-off is:
Hash Partitioning
Better distribution
Harder range queries
Range Partitioning
Better range queries
Can create hotspots
The key question is: How does your data get accessed?
Tomorrow: What happens when one shard becomes too large or too busy?
Rujula retweeted
System Design feels overwhelming when you don't know where to start.
So I wrote a beginner-friendly guide based on what I wish I knew when I started — focusing on understanding, not memorizing.
Build the right mental model before the complex stuff.
Rujula retweeted
Day 17 [Extra]: Quorum — How Replicas Agree
1. In today's post, we saw Leaderless Replication.
But if there is no Leader, a new question appears
How many replicas need to respond before we consider a request successful?
That's where Quorum comes in.
2. Quorum is the minimum number of replicas that must respond to an operation for the system to consider that operation successful.
Imagine we have 3 replicas:
Replica A
Replica B
Replica C
We decide that 2 replicas must respond.
So our quorum is 2.
3. Suppose we write: Balance = ₹5,000
A confirms
B confirms
C is down
The write can still succeed because 2 out of 3 replicas responded.
4. The same idea can be used for reads.
If a read requires responses from 2 replicas, the system can compare those responses and determine the appropriate version according to its consistency mechanism.
5. For N replicas:
W = replicas required for a write
R = replicas required for a read
A common quorum setup uses: R + W > N
For 3 replicas: R = 2 & W = 2
2 + 2 > 3
This means the read and write sets overlap.
In simple words:
A write reaches at least 2 replicas.
A read checks at least 2 replicas.
With only 3 replicas, at least one replica must be part of both groups.
6. Why not wait for every replica?
Because replicas can fail.
Quorum gives us a middle ground:
All replicas: More waiting
Quorum: Enough replicas to proceed
Too few: Greater risk of stale or conflicting results
Quorum: You don't need everyone. You need enough replicas.
Tomorrow: We stop copying the same data and start splitting the data itself — Sharding.
Day 17: Replication - Multi-Leader & Leaderless
1. Yesterday, we had a simple setup:
One Leader handles writes.
Followers keep copies and can handle reads.
But now imagine our app has users all over the world.
India, Europe, US...
Should every write still travel to one Leader?
That can become a bottleneck.
2. So what if we simply add more Leaders?
That's Multi-Leader Replication.
India has a Leader.
US has another Leader.
Users can write to the Leader closer to them, and the Leaders sync their changes with each other.
But now we've created a new problem.
3. What if two Leaders change the same data at almost the same time?
India Leader: Balance = ₹5,000
US Leader: Balance = ₹7,000
Which one is correct?
Now the system needs conflict resolution.
It might use rules like Last Write Wins, merge the changes, or ask the application to resolve the conflict.
4. But what if we remove Leaders completely?
That's Leaderless Replication.
There is no special server responsible for writes.
A request can go to any replica.
The system can use a quorum - a minimum number of replicas that must respond for a read or write to be considered successful.
For example, with 3 replicas, a system might require 2 replicas to confirm a write.
One replica goes down?
The other replicas can still handle the request.
But now there's another question:
What if different replicas have different versions of the data?
The system needs mechanisms to detect and resolve those differences.
5. So we moved from:
One Leader : Simple, but writes depend on one node.
Multiple Leaders : Writes can happen closer to users, but conflicts become a problem.
No Leader : No single write bottleneck, but keeping replicas consistent becomes more complex.
And this leads to the next question:
What if copying the same data isn't enough?
What if we split the data itself across different servers?
Tomorrow : Sharding - answer to all questions
















