Number 22
April 24, 2024

Calculate your needs

Estimating the resources needed for many software projects is essential. An otherwise excellently written program will be useless if frequent operations take minutes to complete due to a lack of resources. On the other hand, if we spend too many resources on the application, users won’t see any benefit, and we will be wasting our money.

A monkey throws coins into the sea as a man looks on in horror.

Unfortunately, it is very easy to overestimate the required resources for an application and end up buying too many machines, CPUs, memory and other resources we don’t need. Fortunately, it is straightforward to learn how to estimate them correctly. All you need to know fits in one email and is timeless knowledge you’ll learn today and still serve you in forty years.

The first thing you need to do is calculate your application’s utilization. With some luck, you’ll discover that this utilization is so low that you don’t even need to estimate resources. That happened to a small company that wanted to install a program for ten users, each performing 300 daily operations, adding up to 3000 daily operations that, during working hours, worked out to one operation every 10 seconds. Ultimately, they installed it on a server they already had lying around.

For the second step, you need to perform load tests to determine the resources you need for each utilization unit. As we live in the age of web applications, I’ll use the “request” as my utilization unit. For this example, suppose that each request takes 100 milliseconds to complete and consumes 100 MB of RAM, 40 milliseconds of CPU time, and 1 MB of network traffic.

After this, you need to compute the number of simultaneous requests your system will serve. To do this, multiply the number of requests per second by the number of seconds each request takes.

For example, 10 requests per second and 100 milliseconds per request yield 1: that is, you will serve, on average, 1 request at a time. On the other hand, if you had 50 requests per second, that would work out to 5 simultaneous requests.

That is all the information you need to compute the capacity you require: the number of requests per second, the number of simultaneous requests, and the resources consumed by each request. Let’s use the previous examples to calculate how much RAM, CPUs, and network capacity we need to serve 50 requests per second.

A request only consumes RAM while it runs, so we must multiply each request’s RAM requirement by the number of simultaneous requests, giving us 500 MB of RAM.

For the number of CPUs, we can multiply each request’s CPU time by the number of requests per second. That yields 2 CPU seconds per real-time second, which tells us we need 2 CPUs.

Finally, for the network capacity, we multiply the number of bytes per request by the number of requests per second, which gives us 50 MB per second, or 500 megabits per second.

Those are our minimum requirements. Since we usually need some extra for the operating system, for growth, and for spikes in usage, we can double them and say that, to serve 50 requests per second, we need a server with 1 GB of RAM, 4 CPUs, and 1Gbps of network.

Those are the specifications of a cheap Raspberry Pi.

Calculate your resource needs or waste your money.

The illustration for this Coding Sheet comes from an engraving by Tony Johannot for an edition of “La Fontaine’s Fables.”