strangerly
Start chatting →

How We Cut strangerly's Running Costs as Traffic Grew 20×

Strangerly Team · · 6 min read

engineering
Illustration: a chart with usage rising while cost falls

In the middle of November our traffic went from about 600 people a day to about 13,000 in ten days. That is good news for a chat site, and bad news for a bill that was designed for 600. This post walks through what we changed so that costs grow slower than users do. It builds on our earlier post, How strangerly Is Built.

First, find out what you actually pay for

Our realtime backend is Firebase Realtime Database. It charges mainly for data downloaded by clients, not for storage or for how often you write. That one fact explained almost every surprise on the bill:

  • A small value that thousands of phones listen to is expensive.
  • A list that grows with the number of users, read by every user, is very expensive.
  • Many tiny messages cost more than you'd think, because each one carries protocol overhead.

So every change below is really one question: who downloads this, and how many times?

1. Signaling was 80% of the bill

When we profiled usage, about four fifths of all downloads were WebRTC signaling: the messages two devices exchange to set up a call. By default every network path a device discovers (an "ICE candidate") is sent as its own message, and a phone on mobile data can find a dozen of them.

What we changed:

  • Each side waits until it has gathered its candidates, or one second, and sends them all inside a single offer or answer. Only late stragglers go separately.
  • We send two relay addresses instead of five (one UDP, one TLS on port 443). The other three reached the same relay and only added messages.
  • We drop candidate types that never connect on mobile networks.

Older app versions still accept the new format, so we could ship it without waiting for everyone to update.

2. Matching cost grew with the square of the queue

With matching done on the phones, every person searching downloads the waiting queue. When one person joins or leaves, that change is sent to everyone else who is searching. Ten times more searchers meant roughly a hundred times more downloads.

We cut it in stages:

  • A searcher only listens to the part of the queue it is allowed to claim: starting from its own id, because only the lower id ever starts a match. That halves the queue each phone sees.
  • The one-off read before a match reuses that listener's data instead of downloading the queue again.
  • Extra lookups (blocks, reputation) happen only when there is someone to match with, and only for the best few candidates, not for everyone waiting.

Then we fixed it properly. A server matchmaker now runs on a single Cloudflare Durable Object. It holds one streaming connection to the queue, so the queue is downloaded once instead of once per searcher, and it pairs people every second using the same rule files the apps use. Phones now only watch their own entry and wait to be told where to go.

Some details that made it safe to switch on:

  • It uses the exact steps a phone would to claim two people, so older app versions are matched by it without knowing it exists.
  • It writes a heartbeat every 10 seconds. If the heartbeat goes stale, phones fall back to matching among themselves within 15 seconds.
  • It slows down when idle: once a second while people are waiting, once every five seconds when nobody is, once a minute when switched off.

What it costs: one always-on object fits inside the Cloudflare Workers Paid plan's included time, so the matchmaker adds about $0.30 a month on top of the $5 plan. And that cost doesn't grow with users, because there is only ever one.

3. Never let a client read a list that grows with users

Some things looked harmless at 600 users:

  • Counts are prepared, not counted live. Numbers like "people nearby" are computed once a minute by a scheduled job and stored as one small value that apps read, instead of every app counting everyone.
  • Big lists are admin-only. The database rules stop any app from reading whole lists of presence, bans or rooms. Apps look up a single person when they need to.
  • Presence goes stale on its own. A phone that is killed or loses signal never says goodbye, so instead of clean-up writes, every reader simply ignores presence older than five minutes.
  • Dead rooms are swept hourly, so the database doesn't fill up with abandoned chats.

4. Fewer background calls

Every app also tells our server a little about itself when it goes online (for bot detection and safety). That used to happen on every match. A person who taps Next fifty times sent fifty of them.

Now the app sends it when it comes online, and again on a match only if the mode changed or ten minutes have passed. Same information for moderators, a fraction of the requests.

5. Hosting that fails over for free

The website runs from one build on two hosts:

  • Cloudflare Workers serve it normally. On the Paid plan, 10 million requests a month are included; we use about 600,000 a day.
  • Vercel stays behind it. The Worker route is set to fail open, so if Workers ever has a problem, requests fall through to Vercel automatically.

The AI model files (a few megabytes each) are served from a separate static host on Cloudflare, so they never count against a bandwidth bill. Static files on Workers are free.

6. Relays: measure before you worry

Video relays (TURN) are the line item people warn you about. We measured instead of guessing: across all our calls, relay traffic is about half a megabyte per user per day, because most calls connect directly and only some mobile networks need the relay. At today's traffic that is around a fifth of Cloudflare's free monthly relay allowance.

We still added two levers in case that changes: relay credentials now last two hours instead of 24, so a configuration change reaches everyone within the hour, and an admin switch can turn relaying off entirely (calls then connect directly only).

What we learned

  • Profile the bill like you profile code. Our biggest cost (signaling) was nowhere near where we expected.
  • Watch for anything that is N × N. "Everyone listens to everyone" is fine at 100 users and ruinous at 10,000.
  • Keep fallbacks. Every change above can be switched off or falls back to the old behaviour on its own, which is what let us ship them in the middle of a traffic spike.
  • Ship rule changes to both clients together. The website and the app share the same rule files and the same test cases, which is the only reason we could move matching to a server without breaking either.

Frequently asked questions

Did cutting costs make strangerly slower or less private?

No. Most changes removed downloads nobody needed. Matching is as fast as before, video still goes directly between the two devices, and the nudity check still runs on your own device.

Why not just pay for a bigger server?

Our biggest cost grew with the square of the number of people searching. Paying more would not have fixed that; changing how matching works did.

Related reading