EngineeringSeries · Part 3 of 6

Keeping the cloud bill small without cutting corners

One database, one deployment and an event bus you pay for by the message, and the signal that would make us change each one

In the last post we drew GrowMagik platform on one page. This one is about the bill behind that page, and the rules we set for it before we wrote any code.

The problem: a big-company setup on a small-shop budget

Early on, we sat down with the list of business areas we planned to build. It had about two dozen services on it. Then we priced the setup most guides recommend for a system like that.

Every service gets its own database. Every service also gets its own deployment, the microservices way, with at least two replicas (running copies) so one crash doesn't take it down. A streaming platform sits in the middle to carry events between them. That adds up to about fifty replicas, two dozen databases, and a messaging cluster that charges a fixed amount every month before it carries a single message. All of that before one shop had signed up.

Our customers are neighbourhood jewellers, travel companies to start with. Their own customers save small amounts every month. A platform built for them cannot start with a cloud bill sized for a bank.

But we also hold people's savings, so cheap could not mean careless. We needed a setup that was small on day one, safe from day one, and able to grow without being rebuilt.

The options we weighed

For each big piece, there was a "do it like a large company" option and a smaller one. Each smaller option saves money and comes with a risk.

Piece The usual way The small way The risk we take on
Database One database for each service One shared PostgreSQL A busy service can slow the others
Shops' data A database per shop, or all shops in one set of tables A schema for each shop inside the one database Anything that visits every shop gets slower as shops are added
Deploying the services A separate deployment for each service from day one (microservices) All services in one deployment first (a modular monolith), split later A fault in one area can reach the rest
Events A streaming platform with a fixed monthly cost Queues you pay for by the message No long history to replay in order

None of these risks is a reason to say no. Each one is a reason to watch something. That idea became the rule behind all the others: we don't make a cost choice unless we have also written down the signal that would make us change it.

What we chose, and the way back from each

Four cost rules in a table, each with what it saves, what it replaced, and the signal that would make us change it: one database for every service, a schema for each shop, one deployment first (a modular monolith), and an event bus you pay for by the message

1. One database for every service

All the services share one PostgreSQL database on Amazon RDS. At our size, that costs about a fifth of what a database per service would, and it means one thing to back up, watch and keep safe.

The way back stays open because of one firm rule: each service owns its own tables and its own migrations, and no service reads another service's tables. If it needs something, it asks that service. This is the "private tables per service" way of giving each service its own data: one database server, but every table belongs to exactly one service. So moving a busy service out to a database of its own later is a matter of moving its tables, not untangling its code.

We change it when one service's load starts to slow the others, or reports start to fight with day-to-day work. The first step is already prepared and switched off: a read copy of the database for reports.

One thing we learned before splitting anything: every running replica brings its own pool of database connections. Split one deployment into several, each with its own replicas, and the connections to that one database multiply. So the pool sizes, or the database size, have to be worked out before the split, not after.

2. A schema for each shop

Inside that one database, every shop gets its own schema, a separate set of tables with the same layout. A new shop is a new schema, created on its own when the shop is set up, and it adds almost nothing to the bill.

It also gives each shop a firm wall at the database level. One shop's data is not sitting in the same table as another's, kept apart only by a filter that one forgotten line of code could break.

The catch is anything that has to visit every shop. Nightly jobs and database changes both get slower as shops are added, even when the shops themselves are quiet. We met the first sign of this while planning for thousands of shops: most of the database's work at night would have been jobs checking shops where nothing was due. So we changed our background jobs to keep a list of shops that have work due, and to visit only those.

We change it when a shop needs a database of its own, because of its size or a contract, or when the work of visiting every shop grows faster than the work the shops bring in. Our posts on multi-tenancy, later this month, go into this in detail.

3. One deployment first

As the last post explained, our services call each other through interfaces that work inside one process or over the network. So the same code can be deployed in three shapes.

Same code, three ways to deploy it: one deployment (a modular monolith) with two replicas, then three deployments, one for each group of services (service-based), with six, then the busiest services split out (microservices) with twelve. The planned bill grows from 1× to about 1.3× to about 2×, while the code, database, gateways and event bus stay the same

We start with every service in one deployment: one application, running as one process, with two replicas for safety. The industry name for this is a modular monolith. Inside it, the services are still separate modules, each with its own tables and its own interface. They are simply built, shipped and run together. That is two replicas instead of about fifty.

Next come three deployments, one for each group of services: core, supporting and generic. This middle shape is often called a service-based architecture: a handful of larger services, each deployed on its own, sharing one database. Later, only the busiest services get a deployment of their own, which is the microservices shape, and real load decides which ones those are, not a guess made today. The rest stay together.

Look at the planned bill under each shape. Going from one deployment to the busiest-split-out shape roughly doubles the bill. That is fine when the load needs it, and wasteful when it doesn't.

We have already run the split shapes in our development environment, so we know that moving between them is a change of deployment settings, not a rewrite.

We change it when one area needs to scale, or ship, at its own pace, or when a fault in one area must not be able to reach the rest.

4. An event bus you pay for by the message

Services tell each other what happened through events. We send them through Amazon EventBridge into SQS queues, which we pay for by the message, with nothing to pay while things are quiet. A streaming platform such as Kafka needs servers running all the time, and has a monthly minimum before it carries anything. At our volume, our bus costs less than a twentieth of that minimum.

What we give up is a long history of events that can be replayed in order, and the very high volumes streaming platforms are built for. We don't need either today.

We change it when the per-message charges pass what a cluster would cost, or when we need to replay a long history of events in order.

Smaller rules that follow the same idea

  • Pay per request at the edge. The web apps are static files on S3 behind CloudFront, and the API gateways charge per request. When nobody is using the app at night, there is almost nothing to pay.
  • Test environments sleep. A non-production environment can be parked at night, with its machines off and its database stopped. That leaves about half the bill, because a few parts can only be deleted, never paused. Our first version of the park script missed some of the running parts, so "parked" cost far more than we thought. For longer breaks we take the environment down completely and rebuild it from Terraform when needed, which leaves only a few percent of the bill.
  • Discounts come last. Longer-term commitments that bring the price down are bought only once usage has settled, not before.

Two things we did not cut

A platform that holds people's savings has to be boringly available. So the database keeps a standby copy in a second data centre, and everything customers use runs with at least two replicas. Together they add a small slice to the bill. We think of them as the cheapest insurance we have.

The take-away

Make every cost choice one you can undo, and write down the signal that tells you when to undo it, before you write the code.

We keep a short record for each cost choice, next to the code, and read it again whenever the bill or the load jumps. A measure has to be something you can read today. A signal that nobody watches is not a signal.

Coming up next

On Thursday we look at how we keep every service, app and website in one repository, and how we build them in the right order.

How do you keep your own cloud bill in check? We'd love to hear what has worked for you, and what hasn't.

Discuss this post on LinkedIn →

Written by

Linjith Kunnon

Co-Founder and Chief Architect, QuickBee

Follow along

New posts appear here first. Follow QuickBee to see the rest of what we are building.