What Hall 3B Is

Hall 3B is a college social simulation in the style of a 2005 MySpace page. It lives at hall3b.com. The people in it are characters that a local AI drives: roommates, the guy down the hall, whoever is gossiping in the lounge. They talk to visitors. They also keep living their lives when nobody is looking.

That shape sets the requirements. The site must answer web requests quickly. It must run a simulation in the background and call an AI model that is sometimes slow. And it must sit on the public internet without giving out the address of the machine behind it. This post shows how I built each of those parts.

How a Request Reaches the App

There are four hops, and each hop has one job:

Visitor
   │
   ▼
[1] CDN edge ........ DDoS filtering, WAF rules, TLS, cached static files
   │
   ▼
[2] Ingress proxy ... reverse proxy on Naydra, cleans up headers, forwards
   │   (private WireGuard mesh, no public path)
   ▼
[3] Mesh transit .... encrypted tunnel to the app host on Dinraal
   │
   ▼
[4] App ............. Node.js + Express in a container, SQLite on disk

Hop 1: the CDN edge. Visitors connect to a public CDN first. The CDN absorbs junk traffic and runs firewall rules. It also terminates TLS and serves static files from cache. As a side effect, port scans of the site only see the CDN, not my hardware.

Hop 2: the ingress proxy. Requests that pass the edge go to a reverse proxy on Naydra, one of my cloud nodes. The proxy terminates the TLS connection from the CDN. It drops malformed headers and forwards clean requests inward. It also proxies the analytics script through the site’s own paths, so ad blockers do not drop the page-view stats. Before anything reaches the analytics store, the proxy strips sensitive headers.

Hop 3: the private mesh. From the proxy, traffic crosses my WireGuard-based mesh (Tailscale) to the app host on Dinraal. The app host has no public web ports open. Its firewall drops unsolicited connections.

One lesson here gave me a scare. Docker’s published ports bypass the ordinary firewall rules of the host, because Docker puts its own rules in front of them. Docker publishes my app’s port. So the real protection is a drop rule in Docker’s own DOCKER-USER chain that lets only mesh traffic through.

After one reboot, the rule did not come back. It lived in a rules file that nothing loaded, and the port was open to the internet for a few minutes. Now a small guard service applies the rule again at every boot and every time Docker restarts. I also check from an outside node that the port is closed.

One more mesh detail bit me. When a machine uses a mesh exit node, LAN traffic can go through the exit node and come back on a different path. I turned on the “allow LAN access” option for the exit node. Now local packets and mesh packets stay on the paths I expect.

Hop 4: the app. This is a Node.js/Express server in a container, with an embedded SQLite database. I have no database server to run, patch or expose.

Making SQLite Handle Two Writers

The web handlers and the background simulation both use the same database file. So I had to design around lock contention. On every connection I set:

PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;
PRAGMA temp_store = MEMORY;

WAL mode lets readers keep reading while a writer appends to the log. synchronous = NORMAL is the usual pair for WAL. It does a little less fsync work, with a small durability trade-off. For a simulation, I accept that. busy_timeout makes a blocked query wait up to five seconds before it fails.

I added three more things:

  • Driver: better-sqlite3, which is synchronous. Queries run directly on the event loop, with no callbacks to juggle.
  • Writers: background workers use IMMEDIATE transactions (db.transaction(...).immediate()). They take the write lock at the start. This prevents a deadlock: two transactions start as readers and both try to become writers.
  • Readers: web handlers use normal deferred transactions. They read from the WAL while the worker commits.

When the AI Is Slow: Three Tiers

The dialogue of the characters comes from a local language model on Farosh, the GPU node of the lab. If a request waits on a slow model, it can time out at the proxy. So every AI call has a fallback chain:

Prompt
  │
  ▼
[Tier 1] Local model over the mesh ── answers in time ──► response
  │ (timeout or overloaded)
  ▼
[Tier 2] Cloud AI service ────────── answers ───────────► response
  │ (error, rate limit, or no WAN)
  ▼
[Tier 3] Built-in lore engine ────── always answers ────► response

Tier 1 goes over the private mesh to the local model. Conversations stay inside my network, and each request costs nothing.

Tier 2 is a circuit breaker. If the local model does not answer before a short deadline, the wrapper aborts the request and sends it to a cloud AI service. The wrapper is small:

async function askAgent(prompt, systemContext) {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), LOCAL_TIMEOUT_MS);
  try {
    const res = await fetch(LOCAL_MODEL_URL, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      signal: controller.signal,
      body: JSON.stringify({
        model: LOCAL_MODEL,
        messages: [
          { role: "system", content: systemContext },
          { role: "user", content: prompt },
        ],
        stream: false,
      }),
    });
    clearTimeout(timer);
    return (await res.json()).message.content;
  } catch (err) {
    clearTimeout(timer);
    return cloudFallback(prompt, systemContext);
  }
}

Tier 3 is for when the cloud service is also down, rate-limits me or is out of reach. It is a built-in lore engine with no AI in it. It uses templates, lookup tables and state-machine rules to put together in-character lines in memory. The dialogue is simpler. But it answers almost at once, and it never needs the network.

In practice, a visitor gets a reply even if the GPU node is busy and the internet is down. I do not say that it can never fail, because the app itself can still break. But the AI no longer takes the page down.

The World Keeps Moving Without Visitors

The hallway chatter, dorm events and character interactions do not need anyone to be online. A separate background worker runs on a cron schedule:

Cron tick ─► Heartbeat worker
                ├─ read each character's current state
                ├─ run a batch of AI calls (same three tiers)
                └─ write the results to the database

I do three things so that the worker does not slow down the site:

  • The worker batches updates, movements and chatter in memory and commits them together.
  • Commits use IMMEDIATE transactions in contiguous blocks, so the worker holds the write lock only for a short time.
  • The web app and the worker share the database through the shared-memory file of WAL. A page load right after a commit sees the new state.

What I Learned

  • Hide the origin, not only the ports. The CDN, the ingress proxy and the firewall each remove a different way to find or reach the app host. The firewall closes the public web ports.
  • Put a time limit on the AI and keep a dumb fallback. The last tier must work with no network at all.
  • SQLite goes further than I expected. WAL mode, a few pragmas and explicit write transactions were enough. They handle a web app plus a busy background worker. I need no database cluster.
  • Keep the simulation apart from the web traffic. With a cron-driven heartbeat, the world does not need visitors. And visitors do not wait on the world.

Takeaways:

  • Layer your ingress: a CDN, a proxy, then a private mesh to an origin without public web ports.
  • Bind the app to the private interface. Then a firewall mistake is not the only barrier.
  • Give every AI call a deadline and a chain of fallbacks. End the chain with something local and simple.
  • WAL mode, busy_timeout and IMMEDIATE write transactions let two writers share one SQLite file.
  • Test the odd cases on purpose: a busy GPU, a rate-limited cloud service, a dead WAN link.