$0.10 Per-User Per-Year: NightTroop

Nightscout is an open-source server that lets people with diabetes, and their caregivers, view, share and analyse diabetic data in real time. My son has his very own Nightscout server that his phone communicates with so we can monitor his health, alert if something is wrong, and adjust his insulin delivery if needed. Nightscout is an integral part of my son’s diabetes management.

Nightscout is a Node.js server with a MongoDB backend, so self-hosting Nightscout requires a lot of technical knowledge that you don’t magically get when you’re diagnosed with diabetes. Also, Nightscout can only host a single person’s data. So if you are technically inclined and wanted to help by hosting other peoples Nightscout instances (like many awesome people do!), that means you have to run and maintain a server, a database, routing and configuration for each Nightscout instance. The more instances you host, the more work you have, it doesn’t scale. This has led to many third parties offering to host Nightscout by charging as much as $144 per year. It costs that much because it is hard to host Nightscout.

My goal is to enable everyone managing diabetes to have access to a Nightscout site for free, regardless of technical ability.

Towards that goal I created “NightTroop” (a “troop” being a group of scouts). The requirements for NightTroop are:

  1. Nightscout compatible API working with all major clients
  2. support at least 10k users on a single server
  3. make it low cost enough to pay for everything myself
  4. One-click (or one API call) to get a Nightscout server
  5. Open-source and self-hostable for anyone else who wants to use it

Let’s set a price target of $1000 per year, or $0.10 per-user per-year to support 10,000 users. Is it possible to build NightTroop within these constraints? I don’t know.


The Plan: Good, Fast, and Cheap

NightTroop is a multi-tenant Nightscout-compatible system, where each “tenant” is called a site. For NightTroop to be free, sustainable and maintainable by me, it must be

  1. low complexity because I am not that smart
  2. low cost because I don’t want to go bankrupt with an AWS bill

To me, that sounds like a Golang single binary executable using SQLite. SQLite makes things simpler and cheaper since it is built into NightTroop and I don’t have to pay to run another process. But, SQLite does come with some big tradeoffs around backups, deployment and disaster recovery. All solvable using tools like Litestream, but this does add some complexity.

NightTroop has two kinds of data:

  1. Site Data: Data mastered on the Person With Diabetes’ (PWD) phone, in an app like AAPS or Loop, e.g. BGL readings and treatments
  2. Control Data: Data that is unique to NightTroop, e.g. control plane users, authentication tokens, connector information…

With SQLite, these two types of data can be completely separated into their own databases. Even more, each site can have its own database to isolate it further. This separation has some big benefits, like partial back ups, limiting the blast radius of a noisy neighbor, and decreasing the risk of leaking data between sites. It has one giant downside, though: NightTroop has to manage 10,001 databases.

What Resources 10K Users on NightTroop Need

With the above system I can start estimating the costs of system resources, which break down into:

  1. Compute, based on the estimated requests per second
  2. RAM, probably mostly used by database connections
  3. Local Storage, based on SQLite DB size
  4. Remote Storage for backups, based on SQLite compressed size and frequency of backups
  5. Network Bandwidth, how much throughput in GB per day 10k users need

Let’s put in some assumptions based on my usage to estimate these values:

  1. A user writes 1 value per minute (the current rate AAPS writes at), and has 4 polling followers each reading once per minute.
  2. The average returned API body size is 1KB. This seems about correct since most reads are a poll.

To get some real numbers I made a small Golang+SQLite HTTP app to read and write data to. This showed that:

  1. The SQLite DB is 2MB after loading 1 month’s worth of data, and its compressed backup was 1MB, so each user needs 3MB per month.
  2. The Golang app on a fly.io server with a single shared core and 256MB of RAM gets 4000rps, easily enough.
  3. Under load, a single SQLite DB connection uses about 2MB of RAM (with the SQLite DB cache_size set to 10KB).

With 10K users NightTroop would need:

  1. 20GB of RAM (at least)
  2. 360GB of local storage for a year’s worth of data, 120GB of which is backed up to S3
  3. support over 1000rps, which at 1KB a message is 2.5TB of bandwidth a month. With 120GB of daily backups, that makes it 6TB of bandwidth per month
  4. negligible compute: NightTroop is not CPU bound as its load is stable, so let’s say 4 dedicated vCPUs

That is a pretty chunky computer, but doable.

What Is the Price of Everything?

With daily backups the remote storage costs are pretty negligible at around $0.01 per site per year. The major cost of the system will be hosting, here are some vendor prices in USD per month:

  1. Fly.io: 4vCPU + 20GB RAM + 360GB Volume is $300 per month. Bandwidth is $120 per month. Total: $420 per month, or $0.50 per site per year
  2. Hostinger: 8vCPU + 32GB RAM + 400GB Volume + 32TB bandwidth is $50 per month. $0.06 per site per year
  3. Hetzner: 4 Xeon cores + 64GB RAM + 6TB storage + 10TB of traffic is $70 per month. $0.08 per site per year

Fly.io is where I usually host projects, as it is cheap and easy to deploy and manage small projects. Since a VPS is mostly a flat cost, Fly.io would be cheaper until it hosts more than 1000 sites. Also, a VPS like Hostinger and Hetzner do not come with some of the Fly.io features, like in built deploy system, backups, metrics server… So if a deploy has less than 1000 sites, I would recommend Fly.io, above that it would depend on how much you will pay for Fly.io’s extra features.


Implementation. It Took Me a Year.

Not wanting to leave this as a thought experiment, I spent most of 2026 working on NightTroop to see if it would work. I am still don’t know.

You can check out the code at grahamjenson/nighttroop or use NightTroop in alpha at https://staging.nighttroop.nz/. This staging server is wiped every so often, but it is a pretty good place to test it out. NightTroop still doesn’t have a production site.

The Stack

NightTroop uses the GoTHS stack, which generates a lot of code:

  1. Golang
  2. Templ: For simple HTML templating
  3. HTMX: Removes a front-end JS framework, pushing rendering to the backend
  4. SQLc: Generated SQL, no ORM, only prepared queries

NightTroop also uses these:

  1. Tailwind for CSS
  2. go-migrate for migrations
  3. tink encryption, hashing, JWT
  4. liteq SQLite queueing (it is vendored)

Optional External Dependencies include:

  1. S3: backups are optional, highly recommended
  2. Sentry For Error tracking
  3. Prometheus for metrics collection endpoint
  4. Resend for email integration, as a site can optionally register an email for account recovery and notifications
  5. PagerDuty For alerting Admins of server issues

Onboarding + Rate Limiting

To create a NightTroop site the user just needs to click a button. This anonymity adds a lot of extra work to make sure users are behaving as one noisy neighbor could take down performance for everyone. One malicious user could do even more harm.

To keep users well behaved, NightTroop has to:

  1. API:
    • Rate limit reads, writes, site creation, socket connections…
    • Rate limit by site, identity and IP.
    • Have separate rate limits for authenticated and unauthenticated requests, so an unauthenticated person can’t block logging in.
    • Put strict limits on the API, like limiting upload size.
    • Limit the length of data sent and returned, e.g. not returning a million records.
    • Shed load by refusing requests rather than queueing them
  2. Database:
    • Limit Constrain all fields lengths, to stop someone uploading gigabytes of garbage to flood the DB.
    • Have a limit on DB size, and enforce it by stopping writes when 4x above that limit.
    • Automatically truncating old records on an interval (described below).
    • Limit the number of records for things like identities.
    • When file system is nearly full start refusing writes and creating new sites
  3. Archiving and deleting inactive users: use it or lose it.

A really fun part of the system is the “Nightscout Connector” where a user inputs their Nightscout URL and token, then NightTroop will continuously sync its records over. This is an fun security problem if you think maliciously. A user could:

  1. Try to access the local network by inputting 127.0.0.1 or a clouds metadata address like 169.254.169.254, or creating a domain that is mapped to those addresses. Depending on the returned error, using NightTroop a proxy for scanning ips and ports on an internal network.
  2. Set up loops of sites, each syncing data to one another, to exponentially spam data and fill the disk.
  3. Create hundreds of NightTroop sites all pointing at a URL, trying to DoS it.
  4. Create their own URL that works, but returns GBs of garbage data on requests or returns data incredibly slowly, to block job queues and use resources.

These are all individually dealt with, but it takes a lot of code to defend a seemingly simple feature.

Prepared Queries: CPU or RAM

I made a decision early that I didn’t want an ORM, as they make code messy and performance harder to improve. SQLc makes it very easy to have prepared queries, which in NightTroop are up to 40% faster. Prepared queries, however, come at a cost to RAM usage on each DB connection. Per connection, NightTroop queries cost about 800KB of RAM, and each site has 2 connections, meaning that 10k users will be using 16GB of RAM just for those queries.

It is very easy to switch from using prepared queries, which means I can delay this decision until it is actually required.

Data and Truncation

Everything gets slower, more expensive and worse the larger a site database gets. Slower queries, longer backups, longer deploys, more bandwidth, more CPU, more RAM… Keeping these site databases small is the only way to achieve NightTroop’s goals.

Most NightTroop data is only important for a short time. The largest database table in Nightscout is the minute-by-minute update of the state of the insulin delivery algorithm, device_status. This is useful for maybe an hour, and has very little long-term use. Keeping the resolution of device_status updates per minute is a waste, so at the moment NightTroop thins them out over days and weeks, and completely deletes old records.

Other information, like glucose readings and treatments, are useful for longer but not at such a high resolution. NightTroop builds summaries of this important information over days, weeks, fortnights and months, where each summary is a contains all useful information in a compressed record. Once a summary has been frozen, the records it was built from can be deleted, and only the necessary information is needed.

This is a tradeoff compared with other systems, where a user has more control over their data. With some local testing, the size of a users database is dropped by up to 90%, making everything faster and cheaper.

Clients, Clients, With So Many Different Issues

Nightscout has guidance but no constraints on what a client can send. It just takes a JSON document and stores it in a collection until a client requests it. There is no type checking, so a client may send a number where another client expects a string. Even some very important strings are not standardised, like the units mg/dL and mmol/L break on some clients unless they are the less correct mg/dl or mmol.

Testing how these clients interact and making sure they all work with one another has been a large compromise with NightTroops simplicity.

Backup, Deploy and Disaster Recovery

NightTroop will experience downtime on every deploy.

A deploy’s steps are:

  1. Shut down the HTTP server and close all connections
  2. Wait for all jobs to stop
  3. Back up every site to S3
  4. Back up the control database to S3
  5. Close

Then:

  1. Start
  2. Fetch the control DB from S3 if needed, and open it
  3. Migrate the control DB
  4. Fetch all site DBs from S3 if needed, then open them
  5. Migrate the site DBs

Here you can see a few things:

  1. With no migrations, deploying back onto the same server with the DBs available, downtime will be very low: only a few seconds.
  2. If there are migrations, they will add at least a few more seconds to the deploy.
  3. If we move to a new server, or are recovering from a disaster, then the new server will have to download all sites and bring them back online, which might take a while.

Next Steps

The next steps for NightTroop are:

  1. Code review NightTroop: I want to go over the important aspects and make sure I understand them
  2. Make a production environment
  3. Sync data from my son’s existing Nightscout to it, and move my follower apps over to it
  4. Move off the old Nightscout to be fully on NightTroop
  5. Once I am satisfied with the results, invite others who want to try it out

Other Projects

There are other projects solving the same problems as NightTroop:

  1. Nocturne
  2. Nightscout for Cloudflare