System Design Series - Day 12/30
Read-Write Splitting: The Missing Half of Database Replication
Database replication is only half the battle.
You also need smart read-write splitting in your application so that writes always go to the primary and reads go to replicas.
Here’s exactly how big companies do it in production.
The Challenge
You now have:
- 1 Primary database (handles all writes)
- Multiple Replicas (handle all reads)
Your app must intelligently route every query.
Pattern 1: Manual Router (Most Flexible)
class DatabaseRouter:
def __init__(self):
self.primary = connect('postgresql://primary:5432/db')
self.replicas = [connect('replica1:5432/db'),
connect('replica2:5432/db'),
connect('replica3:5432/db')]
self.index = 0
def execute_write(self, query):
return self.primary.execute(query)
def execute_read(self, query):
replica = self.replicas[self.index]
self.index = (self.index + 1) % len(self.replicas)
return replica.execute(query)
Simple and gives you full control.
Pattern 2: Framework-Level Routing (Django, Rails)
Use custom routers so the framework automatically sends writes to primary and reads to replicas.
Clean for full-stack frameworks.
Pattern 3: Database Proxy (Zero Code Change)
Use ProxySQL or PgBouncer in front of your databases.
Application talks to one endpoint.
The proxy intelligently routes SELECT queries to replicas and everything else to primary.
Best for teams using multiple languages.
The Replication Lag Problem
User writes data → goes to primary.
User immediately refreshes → read goes to replica.
Data is missing because of lag → bad user experience.
Solution: Read-Your-Writes Consistency
Track recent writes per user and temporarily route their reads to the primary for a few seconds.
Real-World Usage
- Instagram uses ProxySQL for MySQL routing
- Shopify uses custom routers with 50+ replicas
- GitHub uses application-level routing
Results After Implementation
- 90% of queries now hit replicas
- Read latency dropped from 300ms → 45ms
- Primary only handles 10% of traffic
- Overall capacity increased dramatically
Key Takeaways
1. Choose the pattern based on your stack and team (manual for control, framework for simplicity, proxy for flexibility)
2. Always handle replication lag for user-facing reads
3. Monitor lag constantly and alert if it exceeds 1 second
4. Read-your-writes consistency is essential for good UX
What read-write splitting approach are you using (or planning to use) ?
Drop it below 👇
MONOLITHIC ARCHITECTURE EXPLAINED
WHAT IS MONOLITHIC ARCHITECTURE
→ A software architecture where the entire application is built as a single, unified codebase
→ All components (UI, business logic, database access) are tightly coupled and deployed together
→ Any change requires rebuilding and redeploying the entire system
CORE COMPONENTS OF A MONOLITH
→ USER INTERFACE (UI) → Handles user interactions (web/mobile views)
→ BUSINESS LOGIC → Processes application rules and workflows
→ DATA ACCESS LAYER → Communicates with the database
→ DATABASE → Stores application data
All these components exist in one codebase and run as one service
HOW IT WORKS
→ Client sends request
→ Request hits the application server
→ Business logic processes the request
→ Data is fetched/updated from the database
→ Response is returned to the client
Everything happens inside a single deployable unit
KEY CHARACTERISTICS
→ SINGLE CODEBASE → All features in one project
→ SINGLE DEPLOYMENT → One build, one deployment
→ TIGHT COUPLING → Components depend heavily on each other
→ SHARED DATABASE → One database for the entire system
ADVANTAGES OF MONOLITHIC ARCHITECTURE
→ SIMPLE TO BUILD → Easy for beginners and small teams
→ EASY TO DEBUG → All code in one place
→ STRAIGHTFORWARD DEPLOYMENT → No distributed system complexity
→ PERFORMANCE → Faster internal communication (no network latency)
→ CONSISTENT DEVELOPMENT → Unified structure and standards
DISADVANTAGES OF MONOLITHIC ARCHITECTURE
→ HARD TO SCALE → Must scale the entire application, not parts
→ SLOW DEVELOPMENT OVER TIME → Large codebase becomes complex
→ RISKY DEPLOYMENTS → Small change can break the whole system
→ TECHNOLOGY LOCK-IN → Hard to adopt new technologies
→ TEAM COLLABORATION ISSUES → Multiple developers working on same codebase
WHEN TO USE MONOLITHIC ARCHITECTURE
→ STARTUPS → Fast development and iteration
→ SMALL APPLICATIONS → Limited complexity
→ MVP DEVELOPMENT → Validate ideas quickly
→ SMALL TEAMS → Easier coordination
WHEN IT BECOMES A PROBLEM
→ APPLICATION GROWS LARGE → Codebase becomes hard to manage
→ HIGH TRAFFIC → Scaling becomes inefficient
→ MULTIPLE TEAMS → Coordination becomes difficult
→ FREQUENT DEPLOYMENTS → Increased risk of failures
MONOLITH VS MODERN ARCHITECTURES
→ MONOLITH → Simple, fast to start, hard to scale
→ MICROSERVICES → Complex, scalable, flexible
→ MODULAR MONOLITH → Middle ground (structured but still one app)
REAL-WORLD EXAMPLE FLOW
→ USER LOGS IN
→ AUTHENTICATION LOGIC RUNS INSIDE SAME APP
→ USER DATA FETCHED FROM SAME DATABASE
→ DASHBOARD RENDERED BY SAME CODEBASE
Everything happens within a single system
KEY TAKEAWAY
→ Monolithic architecture is the fastest way to build and launch
→ But as systems grow, it can become a bottleneck
→ Understanding it is essential before moving to distributed systems
LEARN MORE ABOUT SOFTWARE ARCHITECTURES
→ Grab the Software Architectures Ebook: https://t.co/rQ5uqOjgfE
System Design Series - Day 8/30
API Gateway Patterns – The Front Door of Your Microservices
API Gateway is the single entry point for all your clients.
Without it:
- Mobile/web clients call 10+ different services directly
- Authentication is duplicated everywhere
- Rate limiting, CORS, logging → repeated in every service
- Services are fully exposed to the internet
With it:
- One clean URL for clients
- Centralized auth, rate limiting, routing, aggregation
- Backend services stay hidden and secure
Here’s everything you need to know about API Gateway patterns.
What is an API Gateway?
Think of it as the hotel front desk
Without a front desk:
- Guests wander around looking for rooms
- No security check
- Housekeeping and room service have no coordination
With a front desk:
- Single check-in point
- Routes guests to correct room
- Handles security, coordination, and requests
API Gateway does exactly that for your microservices.
The Problem It Solves
Before API Gateway:
Mobile app needs user profile + orders:
→ Calls User Service directly
→ Calls Order Service directly
→ Calls Payment Service directly
Problems:
- Client knows internal service URLs
- Multiple network calls (slow on mobile)
- Auth tokens sent to every service
- No centralized rate limiting or logging
- Services exposed to the internet
After API Gateway:
Mobile app calls one URL:
https://api.example. com/profile
Gateway handles everything internally:
- Authenticates once
- Routes and aggregates calls
- Returns combined response
Benefits:
- 1 network call from client
- Services completely hidden (security win)
- Centralized cross-cutting concerns
- Much better client experience
Core Responsibilities:
1. Routing
Maps external URLs to internal services
GET /api/users → User Service
GET /api/orders → Order Service
2. Authentication & Authorization
Validates JWT/OAuth once at the gateway.
Services trust the gateway.
3. Rate Limiting
Prevents abuse (e.g., 100 requests/min per user).
4. Request Aggregation
Combines multiple backend calls into one response for the client.
5. Protocol Translation
Client uses REST → Service uses gRPC (handled at gateway).
Advanced Patterns
- Circuit Breaker → Prevents cascading failures when a service is down
- Request/Response Transformation → Convert old → new API formats
- Caching → Cache frequent responses at the gateway level
- Logging & Monitoring → Centralized observability
When to Use API Gateway
Use it when:
- You have multiple microservices
- External clients (mobile, web, third-party)
- You need centralized auth, rate limiting, or aggregation
Don’t use it when:
- Simple monolith (overkill)
- Only internal service-to-service communication
- Ultra-low latency is critical (extra hop)
Popular Solutions
- Kong (open-source, powerful plugins)
- AWS API Gateway (managed, serverless)
- NGINX + Lua (DIY, lightweight)
- Traefik, Envoy, KrakenD
Summary
API Gateway is not just a proxy.
It is the security layer, traffic manager, and aggregator for your entire backend.
It simplifies client code, hides internal complexity, and centralizes cross-cutting concerns.
Trade-offs:
- Extra network hop (adds latency)
- Becomes a critical component (make it highly available)
Used correctly, it’s one of the most valuable pieces in any microservices architecture.
Tomorrow (Day 9): Inter-Service Communication Patterns
Questions about API Gateway?
Drop them below 👇
#SystemDesign #APIGateway #Microservices #Backend
@EmbaMexInd Not able to access my account. Says it's blocked for more than a week now. Reached out to your embassy but not got any response. Please refer what is the procedure to fix this.
Here are some of my recent favorite whitepapers:
1. Amazon Physalia - Millions of tiny databases: https://t.co/ukjIDLxums
2. Improving CDN path latencies at Google: https://t.co/YeUYCUz3vq
3. SIEVE is Simpler than LRU (Just brilliant): https://t.co/iOexxjmrrB
4. Caching strategies at Twitter: https://t.co/oDOVG0N6b2
5. Improving Multi-CDN Delivery on Netflix: https://t.co/UzruFYi5rD
6. Finding n-hop connections at LinkedIn: https://t.co/Hzd1sIoBiZ
7. Catching Fake Profiles on LinkedIn: https://t.co/573YdIZn1Z
8. LinkedIn Magnet Large-scale Data Processing: https://t.co/glSt360JW2
9. Amazon Aurora: https://t.co/GxiH7w1vfY
10. Collaborative Filtering for Amazon Retail: https://t.co/BWOpVLkt6n
You can find the full list here: https://t.co/36agVu0KkW
Cheers!
#systemdesign #tech #whitepapers
Hello X Algorithm, I would like to connect with these people in the fields:
- Software Development
- Backend Development
- Frontend Development
- Software Architect
- Web Development
- DevOps
- AI/ML
If that’s you, connect and drop a 👋
I'll #connect back with you 😊