Part 8 of 9
Scalability
- A system that works reliably today may not tomorrow, most often because load grows: from 10,000 to 100,000 users, from 1 million to 10 million, or much more data.
- Scalability is a system's ability to cope with more load.
- People say "you are not Google, just use a relational database". Whether that is right depends on your app.
- A new product with few users should aim for simplicity and the freedom to change features as you learn. Designing for imagined scale is at best wasted effort and early optimization, and at worst locks you into a stiff design.
- "X is scalable" means nothing by itself. Ask: if we grow in this specific way, what are our options, how do we add resources, and when do we hit the limits of the current design?
- Once you are popular, you learn your real bottlenecks and which kinds of growth matter. That is the time to work on scale.
Understanding load
- Measure today's load first, then ask growth questions like "what if it doubles?".
- Load is often a throughput number: requests per second, gigabytes of new data per day, orders per hour. Sometimes it is the peak of something that varies, like users online at the same time.
- Other numbers shape the load too: read/write ratio, cache hit rate, items per user (like followers). Sometimes the average matters, and sometimes a few extreme cases are what cause the bottleneck.
- Two ways to study growth:
- Keep resources fixed and see how performance changes. - Keep performance fixed and see how many more resources you need.
- The usual goal is to stay within the SLA at the lowest cost. More resources cost more, and which hardware gives the best value changes over time.
- Linear scalability: twice the resources handle twice the load at the same speed. That is good.
- Sometimes you do better than linear, from economies of scale or spreading peaks over more users. More often cost grows faster, because with more stored data, one write can mean more work even if the request is the same size.
Shared-memory, shared-disk, and shared-nothing
| Architecture | What it is | Limits |
|---|---|---|
| Shared-memory | One machine; processes and threads share its RAM | Twice the hardware costs much more than twice, and bottlenecks mean it rarely handles twice the load |
| Shared-disk | Several machines with their own CPU and RAM share one disk array over a fast network (NAS or SAN) | Contention and locking overhead limit growth; mostly used for on-site data warehouses |
| Shared-nothing | Many nodes, each with its own CPU, RAM, and disk, coordinating in software over a normal network | Needs explicit sharding and brings all the hard parts of distributed systems |
- Vertical scaling (scaling up) means moving to a bigger machine. Single CPU cores are not getting much faster, but you can buy more cores, RAM, and disk.
- Horizontal scaling (scaling out) is the shared-nothing approach.
- Shared-nothing pros:
- it can scale close to linearly, - it can use whatever hardware gives the best value, especially in the cloud, - it can grow and shrink resources more easily, - it tolerates faults better, even across datacenters and regions.
- Shared-nothing cons: sharding (covered later) and the full complexity of distributed systems (also covered later).
- Some cloud-native databases split storage and transaction processing into separate services, with many compute nodes using one storage service. This looks like shared-disk, but the storage offers an interface built for the database, not a plain file system or block device, so it avoids the old scaling limits.
Principles for scalability
- Large-scale designs are very specific to each app. There is no general, one-size-fits-all scalable design.
- Same data rate, different problems: 50,000 small messages a second and one huge file upload every few seconds can both be about 100 MB a second, but need very different designs.
- A design that fits one level of load will likely break at 10 times that load. Fast-growing services should expect to rethink their design at each order of magnitude, and it is rarely worth planning more than one step ahead.
- Split the system into smaller parts that run mostly on their own. This idea is behind microservices, sharding, stream processing, and shared-nothing. The hard part is choosing where to draw the lines.
- Do not make it more complex than needed. One database on one machine beats a complex cluster if it does the job. Autoscaling is appealing, but manual scaling may cause fewer surprises when load is predictable. Five services are simpler than fifty. Good designs mix approaches in a practical way.