API Maker

The framework for AI era

Level 4 · ResilientArchitecture 11 of 13

Multi-cloud

The same API Maker on VPS of several providers, joined by encrypted tunnels. No provider can take you down.

Run API Maker on plain VPS of two or three providers at once. DNS spreads the users over the sites, WireGuard joins their private networks, and the databases keep a copy on every site. Lose a provider, or leave one, without downtime.

See it live

Every request is drawn as it travels. Slow it down, pause, go step by step, or open the full canvas and zoom in.

api-maker/architectures/multi-cloudLive
  • Request
  • Response
  • Cache hit
  • Write
  • Replication
  • DNS
Sites
2/2serving
Providers
3in use
Primary
mongo-1of rs0
Capacity
~11,036req/s

One API, served by two providers. The same API Maker runs on VPS of Provider A and Provider B. DNS answers with both load balancers.

How it works

The same steps as the diagram, in more detail.

  1. One API, served by two providers.

    API Maker runs on plain VPS, so nothing ties it to one provider. Here sites 1 and 2 each run a load balancer and two API servers with the same .env, and DNS answers with the address of both load balancers. Every server serves every API, whichever provider it runs on.

  2. Providers talk through encrypted tunnels.

    Each site has a WireGuard gateway, and the gateways join the private networks of the providers into one. MongoDB replication, Redis traffic and writes to a primary on another site travel only inside these encrypted tunnels: no database port is ever open to the internet.

  3. Every site holds a copy of the data.

    The replica set has a member on each of the three sites, and the Redis Cluster a master on each, with the replica of another site's master. Site 3 runs no API server: it only holds the vote that keeps a majority when a whole provider is lost. Servers read from the nearest member and write to the primary.

  4. A whole provider fails, the others carry on.

    When site 1 is lost, its address is removed from DNS by the health checks of your DNS provider, or by a script. mongo-2 and mongo-3 are still a majority and elect mongo-2, and the replica of redis-1 kept on site 2 is promoted. Users on site 2 carry on; the others move over when their DNS answer expires.

  5. Changing provider is a rebuild, not a migration.

    Create VPS at the new provider, install API Maker with the same .env, add the new MongoDB member and Redis node to their clusters, and let them copy the data from the other sites. Then add the new load balancer to DNS. No export, no import, no downtime: leaving a provider works the same way.

Why choose this architecture

No lock-in

Plain VPS and open source software on every site: any provider that rents a Linux server can host one.

Survives a provider

A whole provider can go dark: the other sites keep the majority, elect a primary and carry the traffic.

Private by design

Sites talk only through WireGuard tunnels. No database port is ever open to the internet.

Move at your pace

Add a site at a new provider, let it copy the data, then retire the old one. No export, no import.

Hybrid too

A site can be your own data center: keep databases on premises and serve them through the same APIs.

Each provider for its strength

Cheap compute from one, fast disks from another. Mix them by what they do best and what they cost.

Server configuration

Plain VPS from any provider, or your own servers, with Ubuntu 22.04 LTS. Sizes are a starting point: measure and adjust.

Load balancers

×1 per serving site
CPU
2 vCPU
Memory
2 GB
Storage
20 GB SSD

Runs

  • HAProxy, for the API servers of its site

Ports

  • 443: from the internet

API servers

×2+ per serving site
CPU
4 – 8 vCPU
Memory
8 – 16 GB
Storage
40 GB SSD

Runs

  • API Maker, the same .env on every site
  • Reads from the nearest MongoDB member

Ports

  • 38246 and 38245: from the load balancer of its site

Data servers

×1 per site, 3 sites
CPU
4 vCPU
Memory
16 GB
Storage
200 GB NVMe

Runs

  • A MongoDB member of rs0
  • A Redis master, and the replica of another site's master

Ports

  • 27017, 6379 – 6380, 16379 – 16380: private network only

Three sites, not two: with two, losing either one also loses the majority.

WireGuard gateways

×1 per site
CPU
1 vCPU
Memory
1 GB
Storage
10 GB SSD

Runs

  • WireGuard, routing its site to the others
  • Can share a VPS with the load balancer

Ports

  • 51820/udp: from the other gateways only
Every API server, on every site.env
am__serverName="site1-api-1"   # site and server, to find it in the dashboards# Private addresses of the three sites, reached through the tunnelsam__mongo_db_connection="mongodb://api_maker:***@10.10.1.20:27017,10.10.2.20:27017,10.10.3.20:27017/api_maker_db?authSource=admin&replicaSet=rs0"am__redisInternal='{"nodes": [{host: "10.10.1.21", port: 6379, pass: "***"}, {host: "10.10.2.21", port: 6379, pass: "***"}, {host: "10.10.3.21", port: 6379, pass: "***"}]}'am__redisExternal='{"nodes": [{host: "10.10.1.21", port: 6379, pass: "***"}, {host: "10.10.2.21", port: 6379, pass: "***"}, {host: "10.10.3.21", port: 6379, pass: "***"}]}'
Gateway of site 1/etc/wireguard/wg0.conf
[Interface]Address = 10.10.1.1/24ListenPort = 51820PrivateKey = ***[Peer]                                   # gateway of site 2, the way to site 3 tooPublicKey = ***Endpoint = 192.0.2.30:51820AllowedIPs = 10.10.2.0/24, 10.10.3.0/24PersistentKeepalive = 25

The machines of each site route 10.10.0.0/16 through their gateway.

Instance of your data: reads from the nearest memberconnection string
mongodb://app:***@10.10.1.20:27017,10.10.2.20:27017,10.10.3.20:27017/shop?replicaSet=rs0&readPreference=nearest

Nearest reads can be a moment behind the primary. Leave readPreference out where a read must see the latest write.

Good to know

  • Writes cross providers to reach the primary: keep the sites a few milliseconds apart.
  • Most providers bill the traffic leaving their network, and replication adds to it.
  • Three sites are needed for a majority: two sites alone cannot tell a lost provider from a lost tunnel.
  • More moving parts: tunnels, DNS health checks and several providers to keep patched and paid.
When you outgrow itYour users are spread over continents, and each of them should reach a site near them. Geo-routing: the nearest region

Questions

Can one of the sites be my own data center?

Yes. A site is any set of Linux machines: VPS of a provider or servers of your own. Join it with the same WireGuard tunnels and it works like any other site.

How far apart can the sites be?

MongoDB and Redis work best with a few milliseconds between sites, such as neighbouring cities or countries. Farther apart, every write waits longer: for users on several continents, see the Geo-routing architecture.

Do I need a DNS provider with health checks?

It removes a lost site from the answers by itself. Without it, a script on a surviving site can update the record through the API of your DNS provider, or you switch it by hand.