Cloud Architecture That Actually Scales

Technical Note: This article covers cloud infrastructure patterns with code examples for caching, connection pooling, and CDN configuration. Concepts apply across major cloud providers.
The Myth of Infinite Scale
"Just throw it in the cloud" is the modern equivalent of "just add more servers." Cloud infrastructure scales;until it doesn't. And when it doesn't, you're looking at a 3 AM incident, a stressed engineering team, and customers who just want their app to work.
We've seen startups burn through six-figure cloud bills because their architecture couldn't handle success. The irony: they optimized for scale they didn't need while ignoring bottlenecks that would bite them at 10,000 users.
Start With the Constraints
Before designing any cloud architecture, we ask three questions:
- What's the read/write ratio? A blog is 99% reads. A real-time chat app is 50/50. This changes everything.
- Where does latency matter? Payment processing needs milliseconds. Analytics dashboards can wait seconds.
- What's the blast radius? If this service fails, does the whole app go down, or just one feature?
These constraints shape every architectural decision.
The Three-Tier Reality
Most applications fit into a simple pattern:
[CDN] → [Edge Functions] → [Database]
↓
[Background Jobs]
Layer 1: CDN for Static Assets
Every image, CSS file, and JavaScript bundle should hit a CDN, not your origin server. This alone handles 80% of traffic for most applications.
// Proper cache headers for static assets
app.use('/static', express.static('public', {
maxAge: '1y',
etag: false,
setHeaders: (res, path) => {
if (path.endsWith('.html')) {
res.setHeader('Cache-Control', 'no-cache');
}
}
}));
Layer 2: Edge Functions for Dynamic Content
Edge functions run close to users, reducing latency for dynamic requests. But they have constraints;cold starts, execution limits, and no persistent connections.
Use them for:
- Authentication
- API routing
- Light data transformations
Not for:
- Complex queries
- Long-running processes
- Anything requiring database transactions
Layer 3: Database With Connection Pooling
The database is always the bottleneck. Connection pooling is non-negotiable:
// Connection pooler config
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 20, // Maximum connections
idleTimeoutMillis: 30000, // Close idle connections after 30s
connectionTimeoutMillis: 2000, // Fail fast if can't connect
});
Horizontal vs. Vertical: Pick Your Battles
Vertical scaling (bigger machines) is simpler and cheaper;until it isn't. Horizontal scaling (more machines) is complex but unlimited.
Our rule: Scale vertically until you can't, then scale horizontally where it hurts.
For most applications under 1M users, a single powerful database server with read replicas handles everything. The complexity of sharding isn't worth it until you're processing millions of writes per day.
Caching Strategy: The 80/20 Rule
Cache the 20% of data that serves 80% of requests:
const getCachedUser = async (userId: string) => {
const cacheKey = `user:${userId}`;
// Check cache first
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);
// Fetch from database
const user = await db.from('users').select().eq('id', userId).single();
// Cache for 5 minutes
await redis.setex(cacheKey, 300, JSON.stringify(user.data));
return user.data;
};
But caching brings cache invalidation;the second hardest problem in computer science. Keep it simple: short TTLs and explicit invalidation on writes.
The Real Scaling Playbook
- Measure before optimizing. Add observability from day one.
- Optimize queries first. A missing index is cheaper to fix than a caching layer.
- Queue heavy operations. Anything over 100ms belongs in a background job.
- Plan for failure. Every external service will fail. Handle it gracefully.
Cloud architecture isn't about preparing for billions of users. It's about building systems that grow predictably, fail gracefully, and don't wake you up at 3 AM.