Next: chapter-2-back-of-the-envelope-estimation
First, you might try to put everything on a single server.
Breaking that down, first each client makes a DNS request to the DNS server to get the IP address connected to www.mysite.com. Then requests are made to our IP address, which returns HTML pages/JSON for clients.
To handle more users, we’ll split out the db so it can scale independently.
Relational databases (RDBMS) represent and store data in tables and rows. You can join across tables using foreign keys.
Non-Relational Databases (NoSQL) are grouped into four categories:
Vertical Scaling means adding more power to your servers. Horizontal Scaling means adding more servers to your pool of servers.
Pros of Vertical Scaling:
Cons of Vertical Scaling:
With a load balancing setup, clients now query the load balancer, which routes requests to N number of servers in a private IP. The load balancer communicates with web servers through private IPs and returns the requests. Now we have graceful failover for requests made to the web.
To achieve failover for the database tier, one way is master slave partitioning.
Advantages:
A cache tier is a fast in-memory storage that stores the result of expensive results or frequently accessed data.
Caches make reads faster, since the web server can query the cache first, and if there’s a cache hit, it can serve that data instead.
you would cache normally like so (using memcached):
const Memcached = require(;
const cache = new Memcached();
.set("myKey", "hi there", 3600); // 1 hour
cache.get(; // should return 'hi there' cache
Caching improves performance, but is complicated when synchronizing the webserver with the cache.
You can use a CDN to cache data, which serves static assets as long as the TTL has not expired.
Stateful architectures allow the web server to keep state in between requests – but that requires that sessions are always routed to the same web server, which can be challenging and makes adding or removing servers problematic, since if a server goes down, some state will disappear for our users.
Stateless architectures do not keep the user’s state, instead, they store state in a data layer that has its own mechanisms for reliability. This both has higher consistency and availability.
If one data center is down, the load balancer will load balance to another region.
This adds some complication:
To decouple the server from other servers/workers, you can use a message queue. This message queue keeps a buffer of tasks to do, completes the task, then notifies consumers who are subscribed.
When working with a large system, you must have logging and metrics.
Pros:
Cons:
A hash function can be used to hash users, for example, so users go to the shard of their user_id % 4.
Cons:
Databases can also be sharded by table, but if a particular shard goes down, you will lose data to that table.
Concluding Tips: