Developers don't always see the connection. It's easy to write code on your own machine, where the RAM and extra disk space were paid for when you bought the machine. If you have two terabytes of disk space, you might not notice how much of it your code uses. If a new algorithm takes twice as long to run, your desktop might not even blink—and besides, who's going to notice a few extra milliseconds? But doubling the computation will almost certainly increase your cloud bill.

Modern cloud computing excels at turning resource usage into an itemized bill. Savvy cloud developers know they can make smarter choices when writing their code. It can be as simple as running a profiler to find slow sections or avoiding unnecessary data storage to reduce memory usage.

Here are 12 ways to optimize your code so it's leaner, faster, and cheaper to use.

Write faster code

Most developers don't spend much time optimizing their code. If it runs in a fraction of a second on their laptop, they don't notice that over time it runs 20%, 30%, or even 300% slower. The program still responds in a fraction of a second. But those differences add up when they happen millions of times on a server. Careful profiling can flag slow parts. Rewriting them can reduce the number of instances your app needs.

Reduce RAM usage

The amount of RAM used is an important factor in cloud instance pricing. In many cases, doubling the RAM also doubles the cost. Programmers can reduce RAM usage by avoiding storing data in memory. Some streaming algorithms, such as Java's Stream classes, are designed to work with large data files without loading them all into memory. The Apache DataSketches project generates approximate answers for complex big-data statistics without using up all your memory.

As an added benefit, using RAM sparingly can also speed up your algorithms. Sometimes the operating system starts paging data to disk using virtual memory. This prevents a crash, but can slow your programs down considerably.

Use lower-resolution images and video

Using lower-resolution images and video can pay off in several ways. First, they'll be cheaper to store. Second, any data egress charges will be lower. Third, the app will feel faster to users.

All static images should be shrunk from the start. Unfortunately, the right degree of minimization isn't straightforward, because at some point the visual quality degrades enough for users to notice. Finding the right tradeoff is a design decision some programmers aren't ready to make.

Some apps that use uploaded images can also create smaller thumbnails and lower-resolution versions after receiving an image. Toolkits such as ImageMagik and formats such as WebP were developed for this purpose.

Drop unnecessary data

Many developers are digital pack rats who keep information just in case they might need it someday. They fill tables with endless columns and then never delete rows. Extra data costs nothing if you have the hardware and enough disk space. But the cloud charges for everything. Will you really need all those values in the future? Does the user need that much detail? Deleting some of that old data will save you money on storage and data egress.

Limit disk space

Using local disk on cloud instances is not only dangerous, it can also be expensive. Local disk space is often designed to be fast enough to keep the operating system running efficiently. Many developers write their code on a personal computer with one or more terabytes of storage. Cloud storage is rarely that cheap or readily available. Clouds often bill directly for storage based on its size, so the best approach is to use as little storage as possible. Consider ways to minimize not only the temporary files your app creates, but also the system libraries it needs.

Clean up your logs

Log files are great for finding problems and debugging software during development. But once the code is in production, you don't need to keep them all. All that extra information clutters up either the local disk or object storage. When developing a logging system, set it up to delete logs frequently. Many logging packages, such as Log4j, can be configured to keep only a minimal number of logs and delete them as new ones come in.

Go serverless

Serverless plans bill only when your code runs, which can save you a lot when workloads are intermittent. Even apps with a steady stream of users have more downtime than you might expect.

Many serverless plans reward careful coding and very high performance with minimal RAM usage. The billing formula calculates response time in milliseconds and charges only for the time the processor is busy. As a developer, you get immediate feedback because you can track response time directly and see how your code changes affect it.

The serverless approach is ideal for small or more experimental projects, and the bill can often be just a few cents a month. If your app runs some functions only occasionally, it may make sense to go serverless.

Archive old data

The older the data, the less often it's accessed. You can plan for this by configuring your app to move old data to a cheaper location. Some clouds charge much less for what's called “cold storage,” where retrieving bits can take minutes or even hours. Other clouds, such as Wasabi or Backblaze, specialize in Amazon S3 object archiving and charge significantly less than the major clouds. In some cases, they don't even charge for data egress. Offloading data once it's no longer in high demand can be extremely cost-effective.

Simplify CSS layouts

If you've seen the HTML tags generated by some frameworks, you know just how ridiculous layouts can get. It's just DIV tags completely nested inside DIV tags, which costs money to create and deliver. A web designer I know brags that they cut their bandwidth bills by 30% just by creating a simpler layout with more sensible use of CSS.

Build static sites

Some frameworks, such as React, require quite a lot of computing power, especially if they use features like server-side rendering. All that code adds to the monthly cloud bill. The opposite philosophy is to build a static site made from immutable blocks of HTML, CSS, and jаvascript, served verbatim from a cache. Using a content delivery network can speed up delivery even further by moving caches closer to the user.

Various frameworks support this static philosophy. Jekyll, Hugo, Gridsome, and Pelican are just a few tools that will package all your content into a set of compact, immutable files. You can still embed personalization on pages with AJAX calls, but most of the site puts very little load on servers.

Externalize computation and storage

As browsers get more powerful, some frameworks make it easier to move more computation directly to the client. Good jаvascript or WebAssembly code can shift most of the load to the user's computer instead of your cloud servers. Some developers cut their cloud tier down to just a database with a little business logic for authentication. One friend runs everything with static HTML and a server-side version of PostgreSQL with built-in procedures that output JSON.

Browsers also have more sophisticated options for local data storage, such as the HTML web storage standard and the W3C Indexed Database API. It's no longer just short strings and cookies. This data is available faster because it isn't sent over the internet, and users like knowing their data isn't stored in a centralized database that could be hacked. Why pay for data storage and exfiltration when it can live on the user's computer for free?

Assign a cost engineer

Some developers specialize in maintaining databases. Some like creating a beautiful first impression with a well-designed interface. Now that cloud computing costs are so flexible, some teams formally assign “cost engineers” to manage code costs and efficiency. A cost engineer's top priority is making sure the application code runs cleaner, faster, lighter, and therefore cheaper. Making this part of someone's job signals that managing code costs is important and part of the development team's role and responsibilities.