Search code examples
apacheperformancearchitecturelampsystem-design

Designing a service for scale. Number of servers needed


Suppose that I need to design a web service. To keep it simple, assume that I use LAMP (Linux-Apache-MySQL-PHP).

I know that I will serve exactly N user requests per second. The requests are basically simple CRUD operations to the database, no file uploads or complex calculations.

Suppose that each request executes M ms and takes K Mb of memory on my server, having G Gb of RAM.

How many such servers do I need? Is it just N * K / G?

The reasonable value for M is 200ms. What is the reasonable value for K?

Do we need to take CPU power into account in this question?

Any additional considerations?


Solution

  • What you're doing is a good back of the envelope approximation but by no means should you use your thought exercise as a definitive guide for scaling your service.

    That is because no service will exhibit that type of constant behavior as you describe (blame if on unpredictable peripheral i/o, garbage collection, external factors, user input, etc)

    The correct approach is to perform scale and load testing. After you've written your service, start to load test your service and note the performance characteristics of your service. If you do things right you should reach a point where your configuration maxes out: either the CPU, the network throughput, the memory, or disk I/O. If neither are maxed out and you hit a limit then it's one of your upstream dependencies (your database etc.)

    Once you've reached your peak it will tell you how many requests per second you can handle at peak.

    You will also notice that in most cases peak performance is not sustainable: your setup may be able to burst for short periods of time handling many more requests per second than under sustained load.

    After you get the numbers for a single server, you can start to vary in two ways:

    1. test with different hardware configurations (add more RAM if you're memory bound, add a better CPU if you're CPU bound, etc)

    2. test with multiple servers; start adding servers and see how your service scales horizontally

    Ideally your service should scale linearly as you add servers but you will likely find that the performance curve is not linear.

    Get your numbers, tweak your design. Rinse. Repeat.

    There is no substitute, magic formula.