Skip to content

Writing · The Archive

Why my portfolio runs on a $2 server

Serverless WebSockets would have cost over $150 a month. One small Graviton box costs about $2. Here's the cost model behind a real-time 3D portfolio, and what I gave up for it.

· 4 min read

Every visitor on this site is a live avatar. Each one sends its position about ten times a second, and every update has to reach everyone standing nearby. Before writing any code, I priced that.

The serverless bill

My first instinct was serverless: API Gateway WebSockets in front of Lambda. No servers, pay per use. Then I did the maths.

API Gateway charges about $1 per million messages, and every inbound message also invokes a Lambda. Take just five visitors who keep moving all month:

5 visitors × 10 updates/s × 86,400 s × 30 days ≈ 130 million messages

That's over $150 a month, before a single update is fanned out to anyone else. "Pay per use" is great when use is a request now and then. A game loop is the opposite: constant, tiny, and forever.

My budget was $5 a month, with 1,000 concurrent visitors as the design target.

The $2 alternative

A t4g.nano (2 Graviton vCPUs, 512 MB) costs about $2.04 a month in Mumbai. It runs one Node.js process with uWebSockets.js that keeps the whole world in memory. Everything that is plain request and response (contact form, garden, stats) stays on Lambda, where serverless really is the cheapest.

  1. Browser

    • Classic pages
    • 3D world (Three.js)
    • One WebSocket at /rt
  2. CloudFront

    • /* → S3 (static export)
    • /api/* → Lambda URLs
    • /rt → VPC origin
  3. Real-time

    built by me

    • EC2 t4g.nano, private subnet
    • Node.js + uWebSockets.js
    • World state in memory
  4. Durable state

    • DynamoDB, one table
    • S3 snapshots
One origin per kind of traffic. The browser only ever talks to its own domain.

How one small box keeps up

Cheap hardware only works if every message is cheap too. Three decisions do most of the work.

  • Binary, not JSON

    • About 8 bytes per avatar per update
    • Position in 16 bits per axis, facing in 8
    • JSON only for rare messages (chat, games)
  • Only what you can see

    • The island is a grid of 16 m cells
    • Each cell is a pub/sub topic
    • You subscribe to the 3×3 cells around you
  • Encode once, send many

    • Each tick, one delta per changed cell
    • Published once to every subscriber
    • Cost grows with busy cells, not players²
// One encoded snapshot per changed cell, published once to everyone who can see it.
function publishSnapshot(topic: string, states: EntityState[]): void {
  const recipients = app.numSubscribers(topic);
  if (recipients === 0 || states.length === 0) return;
  const bytes = encodeSnapshot(worldTick, states);
  app.publish(topic, bytes, true);
}

In a local load test with 1,000 bots in the 3D world, 70% of them walking at 10 updates a second, all of them connected with zero errors. The world tick stayed at about 10 ms at p99, each player cost about 1.9 KB/s, and the server peaked at 71 MB of memory. The real test, on the real nano, comes at launch.

The costs nobody puts on the pricing page

The instance was never the expensive part. Two "standard" pieces of AWS networking would each have blown the budget on their own:

  • a public IPv4 address: $3.65 a month;
  • a NAT gateway, so a private server can reach AWS: $33 or more a month.

So the server has neither. CloudFront reaches it through a VPC origin (which now carries WebSockets), so it sits in a private subnet with no public IP and no open ports. Outbound traffic to AWS goes over IPv6 and free gateway endpoints. Networking cost: $0, and it's more secure too.

The last trap is CPU credits. In "unlimited" mode, a runaway loop on a T-instance can quietly bill you around $55 a month. I set it to standard: if the server runs hot, it slows down instead of costing more. At portfolio traffic, the whole bill comes to about $3.40 a month.

The cheapest architecture isn't the one with the cheapest parts. It's the one where you know what every message costs.

What I gave up

One server is a single point of failure, so I designed around that from day one:

  • The server is disposable. Nothing it holds in memory is the only copy. Durable state lives in DynamoDB and S3, and EC2 brings a dead instance back by itself.
  • Clients come back on their own. They reconnect with their identity and pick up where they were. I tested exactly that with a chaos test: a whole post of its own.
  • Lambda doesn't care. If the real-time server is down, the pages, contact form and garden still work.

Would I build a bank this way? No. For a portfolio that has to feel alive for a few dollars a month, one honest server beats a pile of services billed by the message.

aws · architecture · websockets · cost

Enjoyed this? Leave a reaction.

All writing