Skip to content
All articles

Three replicas, zero availability: why I replaced Pomerium with a 15 MiB OAuth gateway

How an outage of my authentication layer led me to build a small, self-hosted OAuth gateway for web applications and AI assistants.

One morning I started getting alerts from everywhere.

One of my self-hosted applications was down, then another, then another: dashboards, my task manager, a few private APIs, all running on my Kubernetes cluster in the cloud.

My first thought was the cluster. But Kubernetes was fine, the nodes were fine, the applications were running. Another API on the same cluster, one that handles its own authentication, was answering in 70 ms through the same ingress.

Every application that relied on my shared sign-in, instead, was returning HTTP 503.

What did they have in common? Pomerium, the authentication gateway I had put in front of them a couple of weeks before to keep them private.

Why Pomerium in the first place

I’ve been self-hosting applications for years, both in my homelab and in the cloud. Some of them are public. Others, like dashboards, internal tools and admin panels, need to be reachable from anywhere, but only by the right people.

For browser access, Google sign-in was enough. Then MCP came into the picture. I wanted ChatGPT and Claude to use some of those private services too, and an AI assistant can’t click a Google sign-in button every time its token expires. It needs a proper OAuth flow.

That narrowed the options a lot. Pomerium was the only self-hosted gateway I found that could handle both browser sign-in and OAuth for remote MCP clients, as an identity-aware reverse proxy, without changes to the applications behind it.

But a single gateway in front of everything means a pod restart takes everything down. More replicas are easy; the problem is where they share sessions and OAuth tokens. A single Databroker is a single point of failure, and PostgreSQL only moves it, unless I ran an entire HA PostgreSQL cluster just to let a few users sign in.

The alternative was the experimental clustered Databroker, introduced in v0.31: Raft leader election, state replicated on each node’s local storage, automatic failover, no external database.

Experimental, yes. But it was exactly what I needed, and it worked well enough that I almost forgot about it.

Until that morning, when every protected application started returning 503.

Ready, but not alive

So, back to the outage. The applications were fine, which left Pomerium itself. I opened its three pods, and the picture was clear:

  • all three were at 490–498 MiB, on a 512 MiB limit;
  • none of them had been OOM-killed or restarted, in almost three days;
  • CPU was at 0.6–0.8 cores, against 0.01–0.03 at rest;
  • the logs were full of CLUSTER_HAS_NO_LEADER and failed leader elections.

And Kubernetes still reported all three pods as ready.

Why it happened

Pomerium ships a large binary and, at every start, extracts a 118 MB Envoy into /tmp. The pages of those files count against the container memory. On freshly restarted pods, with no relevant traffic, the container was already using 320–415 MiB, while the processes themselves used less than 90 MiB.

On top of that, the process memory grows over time. This is a known issue, with the same version, the same 512 MiB limit and the same lifetime of about two and a half days. The memory leak has since been addressed, but that doesn’t change the underlying resource requirements. In the same thread, a maintainer advises against 512 MiB limits in production and suggests at least 12 GB.

The interesting part is what happens at the limit. Instead of being killed, the container keeps evicting and re-reading the executable pages from disk. The process stays alive, but it is too slow to respect its own timeouts: Raft elections fail, DNS lookups time out, and no leader means no authentication.

Since the three pods had started together and grew at the same rate, they hit the limit together. Three replicas don’t protect you from a failure that every replica shares.

And why didn’t Kubernetes notice? Because I was using TCP readiness probes. As long as the port accepted connections, the pod was ready. Whether Pomerium could actually authenticate a request was another story.

I raised the limit to 1 GiB, set GOMEMLIMIT=900MiB, and restarted the pods. Everything came back online.

Problem solved. Except that I now had three pods with a combined memory limit of 3 GiB, just to sign in to a handful of applications.

The workaround that came back to bite me

There was a reason for those TCP probes.

A few days earlier I had been testing Pomerium during rolling updates and Raft leader failures. With some tuning, I had brought browser errors during rolling updates down to zero. The TCP probes were part of that tuning.

MCP was a different matter. During the rolling-update and recovery windows, a significant share of MCP calls failed, and fast token refreshes ended in invalid_grant, which means reconnecting the AI assistant by hand.

A few failed MCP requests during an update, I could live with. Losing access to every application at once was a different story.

I could give Pomerium more memory, add PostgreSQL, or look elsewhere. But before adding yet another component to the cluster, I took a step back.

Back to the requirements

Looking at my routes, I was using only a small subset of what Pomerium offers:

  • web applications protected by Google sign-in and an email allowlist;
  • a few MCP endpoints, which need OAuth for remote clients;
  • some applications that need to know who the authenticated user is;
  • control over which MCP tools an assistant can call.

No admin UI, no control plane. Just a file where I can say which hostname and path to protect, who is allowed in, and where to forward the request.

With that list in hand, I looked at the alternatives again:

  • Cloudflare Access could cover everything on paper, but its managed OAuth was still in beta, its MCP portal changes the server URL and caps calls at 60 seconds, and I preferred to keep authentication on my own infrastructure;
  • oauth2-proxy plus a separate MCP auth proxy covered sign-in and OAuth, but meant two components, no tool blocking, and Redis anyway to stop authorization codes and refresh tokens from being reused;
  • authentik, Keycloak and Authelia either needed PostgreSQL or didn’t support the dynamic client registration MCP clients rely on.

Nothing covered all of it without introducing a relational database, relying on a managed service, or giving something up. The list itself, though, was short enough to build.

So I decided to write my own gateway. The harder part was deciding what not to build. I didn’t want a gateway with its own distributed consensus, and I didn’t want a shared filesystem between nodes.

I also set myself a budget: 200 MiB per gateway pod. After what I had just seen, even that would have been a good result.

That’s how Panda OAuth Gateway started.

A stateless gateway

I wanted a small executable that runs on Kubernetes or locally with Docker Compose. I went with Go and kept the configuration deliberately boring: environment variables and a JSON route file.

{
  "routes": [
    {
      "host": "dashboard.example.com",
      "path_prefix": "/",
      "upstream": "http://dashboard:8080",
      "mode": "browser",
      "allowed_emails": ["[email protected]"]
    },
    {
      "host": "mcp.example.com",
      "path_prefix": "/mcp/",
      "upstream": "http://mcp-server:8080",
      "mode": "mcp",
      "allowed_emails": ["[email protected]"],
      "mcp_denied_tools": ["delete_project", "purge_data"]
    }
  ]
}

In browser mode, Google verifies who you are and the gateway keeps a session. In mcp mode, the gateway itself acts as the OAuth authorization server for the AI client. Both go through the same routing and the same access policies.

The reverse proxy is the easy part to replicate. The hard part is everything that happens before the request reaches the upstream: login sessions, authorization codes, grants, refresh tokens and rate limits. If I sign in through one replica, the other one must recognize me, otherwise every rolling update is another login.

Instead of teaching the gateway to replicate that state, I moved it to Valkey. The gateway replicas own no durable state: no persistent volumes, no sticky sessions, nothing to elect.

A browser and an MCP client reach their upstreams through Panda OAuth Gateway. Google handles browser sign-in and Valkey stores sessions and tokens.
Two ways in, one gateway, state in Valkey.

Moving the single point of failure, on purpose

Of course, a single Valkey instance behind two gateway replicas would put me right back where I started. In the cloud cluster I run one Valkey primary, two replicas and three Sentinels:

Ingress routes to two gateways, which query Sentinel and read and write directly to the Valkey primary. Three Sentinels watch the Valkey nodes, and the primary replicates asynchronously to two replicas.
Gateways talk to the primary. Sentinel tells them which node it is.

On the gateway side it’s a few environment variables:

VALKEY_MODE=sentinel
VALKEY_SENTINELS=sentinel-1:26379,sentinel-2:26379,sentinel-3:26379

The gateway asks Sentinel which node is the current primary and follows it after a failover. All replicas share the same routes and derive their signing keys from the same secret, so I can replace one without logging anyone out.

Valkey replication is asynchronous, though, and that matters for rotating refresh tokens. If a client receives a new token and the primary dies before replicating it, the new primary has never seen that token.

To reduce this risk, every refresh-token rotation waits for at least one replica to acknowledge the write (VALKEY_REFRESH_MIN_REPLICA_ACKS, default 1, within 500 ms). If Sentinel loses its majority, rotations are refused until quorum returns, instead of pretending everything is fine.

It’s not a guarantee. A failover can still cause a short interruption, and a write acknowledged just before the crash can still be lost. In the worst case a client has to authorize again. But these failure modes are documented and tested, rather than discovered through another wave of alerts.

In my homelab, by the way, I run a single Valkey instance and accept the trade-off. Not every environment needs the same topology.

MCP: authorizing the client is only half the problem

Think about a task manager connected to ChatGPT. I want the assistant to create tasks and update them. That doesn’t mean I want it to delete an entire project.

An OAuth token says who is authorized to reach a resource. It doesn’t say which tools they can call.

That’s what mcp_denied_tools in the route above is for. A denied tool disappears from the tools/list response, and, more importantly, a direct tools/call is rejected too. Hiding a tool from a list is not access control.

A route also can’t forget about it: an MCP route without a tool policy answers every request with 503 mcp_tool_policy_pending, and nothing reaches the upstream. Exposing everything requires an explicit "mcp_tool_policy": "allow_all".

This gives me a layer of control without forking every MCP server I use. The application still has to enforce its own permissions, of course.

On the OAuth side, clients discover the authorization server, identify themselves, use the authorization code flow with PKCE, and refresh their tokens without sending me back to a login page. Tokens are bound to a specific MCP resource, and refresh tokens rotate with replay detection.

Passing my own tests was one thing. Talking to real clients was another. I tested ChatGPT, Claude.ai and Claude Code, and checked with Claude Code that the connection survived the 15-minute access-token expiry without asking me to authorize again.

Built with AI, reviewed as if it wasn’t

If you’ve read my article about vibecoding, you can guess my position: “it works” is not enough, especially for an authentication layer.

I used Codex extensively to write the gateway and Claude to review it. The first version started fail-closed: nothing reached a protected upstream until the authentication and authorization logic for it existed.

Build, unit tests and race detection were all green. It still wasn’t ready.

The reviews found problems in the OAuth consent flow, in routing boundaries, and in what happened when a refresh-token rotation succeeded in Valkey but the following replication check failed. That last one stung: it was the same class of token problem that had pushed me away from Pomerium, and my own implementation needed more work there too.

So instead of treating green CI as the finish line, I extended the tests to those failure scenarios. The repository now includes disposable Valkey Sentinel clusters where the tests kill the primary, remove replicas and break the quorum while tokens are being refreshed, then check how clients recover.

AI wrote a lot of the code and a lot of the tests that challenged it. Deciding what was safe enough to deploy was still my job. And none of this replaces an independent security audit.

Migration

I deployed the gateway alongside Pomerium first. Taking down every service while migrating to a gateway meant to improve their availability would have been a bit ironic.

Panda Tasker, my self-hosted task manager, was a useful test case: it already authenticated users by verifying the ES256 identity JWT issued by Pomerium. Instead of changing the application, I made the gateway’s JWT header, issuer, audience and signing algorithm configurable. The migration needed configuration changes, but no changes to Panda Tasker’s code.

Just four days after the outage I removed Pomerium from the cloud cluster. By then the gateway was already serving dozens of protected hosts and routes. A few days later, a second instance went into my homelab.

Results

And the 200 MiB budget?

  • Memory per replica: ~15 MiB resident
  • Idle CPU per replica: ~1 millicore
  • Container image: ~26 MB
  • Go code: ~5,400 lines
  • Direct dependencies: 3

On paper, the new architecture is bigger than the one it replaced. Pomerium ran as three pods; the cloud setup now runs eight: two gateway replicas, three Valkey nodes and three Sentinels.

The weight is a different story. These are the measured values from both of my clusters, next to Pomerium’s three pods right after a restart:

  • Pomerium with clustered Databroker: 3 pods, ~1.1 GiB memory in use (323–415 MiB per pod), 30–90 millicores CPU per pod at rest
  • Panda OAuth Gateway, cloud (HA with Sentinel): 8 pods, ~130 MiB total memory in use, ~95 millicores total CPU at rest
  • Panda OAuth Gateway, homelab (single Valkey): 2 pods, ~14 MiB total memory in use, ~4 millicores total CPU at rest
Memory in use: Pomerium, 3 pods, about 1,100 MiB; Panda OAuth Gateway cloud HA, 8 pods, about 130 MiB; Panda OAuth Gateway homelab, 2 pods, about 14 MiB.
Eight pods, about a tenth of the memory of three.

Eight pods use about a tenth of the memory of three. And that Pomerium figure is a freshly started setup: at the time of the outage, each one was close to 500 MiB. CPU is the one line where the cloud setup isn’t smaller, since Valkey and Sentinel keep checking on each other, but we’re talking about a tenth of a core for the whole stack.

The homelab version is the same gateway with a single Valkey instance and no failover: two pods, less memory than a single browser tab.

I’m not going to turn this into a benchmark: Pomerium does far more, and these are snapshots from my own traffic, not load tests. What matters to me is simpler. The whole HA stack uses less memory than a single freshly started Pomerium pod, and it fits my original budget with a lot of room to spare.

Should you replace Pomerium too?

It depends on what you use it for. Pomerium is a much broader platform. I built something narrower because I only needed Google sign-in, a reverse proxy and OAuth for my MCP servers.

If you need a general identity-aware access platform, this is probably not your replacement. And if you deploy my gateway, you still have to terminate HTTPS correctly, keep the upstreams private and isolate Valkey. A gateway doesn’t protect traffic that goes around it.

It’s now public

After running it on my own infrastructure, I published Panda OAuth Gateway. The repository includes Docker images, a Docker Compose quickstart, configuration examples and the security and high-availability documentation.

It’s licensed under FSL 1.1, and each version converts to Apache 2.0 after two years. It’s free to self-host for personal and internal business use, within the license terms.

GitHub: https://github.com/Leen15/panda-oauth-gateway

Final thoughts

I didn’t set out to write an authentication gateway. I set out to stop a gateway from being the reason everything was down.

The answer wasn’t more replicas or more memory. It was moving the state where it belongs, and keeping the part in front of my applications small enough to restart without anyone noticing.

So far, nobody has.

I’m curious how other self-hosters solved the same problem. A separate gateway for MCP? OAuth in every application? Or something simpler that I missed?

Disclosure: AI tools were used extensively during the development of this project and assisted in drafting this article.