Two apps, one PostHog install, and 16 GiB of memory

In my post about making money from apps, I shared a chart from my workout app. It showed 34 people had reached the payment screen and three had bought. I got those numbers from PostHog. The app sends it an event when someone sees that screen and another when they buy. That’s enough to draw the chart. The post with the workout app chart

I wanted to use one PostHog installation for our apps at DevsNorte and still be able to pull up those numbers for each app individually. We had two things to sort out. Our open-source edition wouldn’t let us create a second project, and at Fly.io’s list price the main machine alone, with 48 GiB of memory, came to about $737 every 30 days.

What are we paying to run PostHog?

We moved the main machine from 8 performance CPUs and 48 GiB of memory to 4 of the same CPUs and 16 GiB. Fly charges for the capacity we reserve while the machine is running. That’s where the dollar savings come from. By the time we measured the code changes, we were already sharing those 16 GiB.

I used the public São Paulo prices on November 25, 2026, and worked out what 720 hours would cost. The three smaller machines and the disk are the same in both columns. Fly.io prices by region

Estimated cost in US dollars per 30 days Before After
Main PostHog machine $736.79 $290.82
Three supporting machines (web gateway, cache, and background jobs) $33.57 $33.57
64 GB disk that holds our data $9.60 $9.60
Total for four machines and disk $779.96 $333.99

That works out to $445.97 less every 30 days if everything stays on. For the month we made the switch, the hours are split between the two machine sizes. The estimate leaves out outbound traffic, backups, and on-demand services, including disk storage for an optional machine while it’s stopped. It’s before tax and discounts.

Why was our logs service loading the session recorder?

Seven Node.js services started from the same file. The import statements for all the server implementations were at the top. Further down, a switch read the configuration and picked the server to run. By the time the logs process found out it would be handling logs, it had already loaded the code for session recordings.

We moved each import into the case that uses it. Here are the two lines in the recordings branch.

const { IngestionSessionReplayServer } = await import('./servers/ingestion-session-replay-server.js')
return new IngestionSessionReplayServer()

By the time Node opens that module, it knows its job is to receive recordings. The logs process takes a different branch. We checked that all 18 implementations still loaded and that the seven services we use at DevsNorte came up ready. Between them those seven were using 1.94 GiB in the earlier measurement and 1.05 GiB after the test. The recordings branch · Production measurements

Starting Django three times

Before the web server takes requests, PostHog checks that its databases have the structure the code expects. The startup script launched Django to check PostgreSQL, exited, then launched Django again for ClickHouse, the database that handles analytics. It still had to start a third copy for changes that run in the background.

We put the three calls inside one command, python manage.py check_all_migrations. Django starts once and runs the checks in their original order. We deliberately left one migration pending to see what the next person doing a deploy would get. Startup stopped and the message named the missing migration. With the databases up to date, the web server was ready in 2 minutes 52 seconds on that rollout. The previous one took 3 minutes 49 seconds. Startup command, test, and timings

The script that applies migrations had a similar quirk. It would start Django just to report how long the work took, even when we hadn’t given it an address to send the measurement to. With the default settings the script now checks for an address first. Installations that supply their own Django settings can still set the destination there. The metrics change

Where did those 384 MiB come from?

In a separate test we looked at the function that groups similar AI model runs. Each run is represented by a list of numbers. The value we want to keep is the distance from each list to the center of each group.

With 1,024 runs and 16 centers, that’s 16,384 distances. At eight bytes each, the result fits in 128 KiB. The expensive part came midway through the calculation. Each list had 3,072 numbers, and the old expression kept the individual differences for every run and center before summing them. Multiply 1,024 by 16 by 3,072 and you’re holding over 50 million temporary values.

We used SciPy’s cdist(embeddings, centroids, metric="euclidean") to compute the distance table directly. We ran both versions with the same synthetic data already in memory. Peak allocation traced during the calculation went from 384.126 MiB to 0.126 MiB. The distances agreed to a relative tolerance of 1e-12. Code and memory test

Redis was back. What was PostHog waiting for?

A feature flag lets us roll out a screen to a few users. The app asks whether it should show the screen, and PostHog uses Redis to help answer. If the service can’t connect to Redis at startup, it waits and tries again.

The comment in the code said the delays would be 100, 200, and 400 milliseconds. The call underneath was from_millis(100). This library takes that number as the base of a power. By the third retry the calculation was 100 × 100 × 100 milliseconds. The service could spend 16 minutes and 40 seconds waiting even after Redis had recovered.

We used a base of 2 and a multiplier of 50. Both Redis connection paths now use the same function, and the test checks for 100, 200, and 400 ms before the random variation is applied. We also kept Redis offline for 34 seconds in a local test. The service restarted automatically and picked up requests again. Retry fix and recovery test

Getting that second app its own project

In our copy of PostHog, owners and admins can now create additional ordinary projects. There’s still a limit of 2,000 per organization. Creating a project means counting how many already exist. Renaming one leaves the count exactly where it was. We put view.action == "create" ahead of .count() and checked the rule in production in a read-only test. For an update, it ran zero database queries. Project creation rule

We brought up the new copy alongside the one already taking requests and asked it for a flag decision using the DevsNorte project before switching traffic. The public address answered all 51 health checks during the web rollout.

We tried creating a project as an ordinary member and got turned down. An owner from another organization couldn’t even see the project. As for the numbers I’d see in the dashboard, we could check those with a single event. Each project gets its own key. We sent the event using the second project’s key and ran the same search in both. One result in the new project, zero in the first. Project creation and isolation tests

Comments (0)

No comments yet

Be the first to share your thoughts!