Headscale SaaS Seamless User Switching Between Cluster Nodes
Once you operate a sharded Headscale SaaS cluster, horizontal scaling is only half the operational story. Sooner or later you will need to rebalance a hot shard, drain a node for maintenance, or move an enterprise tenant onto a dedicated shard—without forcing every user to log in again.
This guide documents a production-tested pattern for near-seamless tenant migration across Headscale shards: scoped database migration + hot-reloaded Traefik routing + surgical TCP disconnect + automatic client reconnection.
Architecture Recap
In a clustered Headscale SaaS deployment built on cluster_id sharding:
- Each tenant or user namespace is accessed through a stable hostname (for example
hsa.demo.com) pointing to a reverse proxy layer powered by Traefik. - Traefik routes incoming HTTPS/Noise control traffic to the backend Headscale shard currently responsible for that tenant.
- Backend records in PostgreSQL are partitioned by
cluster_id, making ownership transfer explicit and auditable.

Why Updating Proxy Routes Alone Is Not Enough
A subtle pitfall trips up many teams during their first live migration: Tailscale and Larktun clients hold long-lived TCP/HTTP2 control streams to the Headscale server.
Even after Traefik hot-reloads its dynamic routing table to point hsa.demo.com at headscale-cluster4, every already-connected client keeps speaking over its existing TCP socket directly to headscale-cluster3. New connections land on cluster 4, while existing devices linger on cluster 3—creating a split-brain control plane until those old sockets close.

Waiting passively for idle timeouts can take minutes or hours. Restarting the entire source Headscale shard would disrupt every other tenant hosted on that node. The clean answer is to close only the migrated tenant's active sockets on the old shard right after switching routes.
Step-by-Step Seamless Migration Workflow
Step 1: Migrate Tenant Records to the Target Shard
Export the target user's records (nodes, IP allocations, routes, pre-auth keys, and policy state) from the source shard, update cluster_id to the destination shard, and import cleanly inside a transaction:
# Export user-specific records from the source shard
pg_dump -h node3-db -U headscale -t nodes -t ip_addresses \
--where="user_id = (SELECT id FROM users WHERE name = 'hsa')" \
> hsa_data.sql
# Reassign cluster_id from shard 3 to shard 4 and import into target
sed -i 's/cluster_id: 3/cluster_id: 4/g' hsa_data.sql
psql -h node4-db -U headscale -d headscale < hsa_data.sql
If both shards share a single PostgreSQL cluster, this step simplifies to a single atomic
UPDATE ... SET cluster_id = 4 WHERE user_id = ...transaction, eliminating dump/restore drift completely.
Step 2: Hot-Update Traefik Routing
Update Traefik's dynamic file provider so all new handshakes for hsa.demo.com route to headscale-cluster4:
http:
routers:
hsa-router:
rule: "Host(`hsa.demo.com`)"
service: "headscale-cluster4"
entryPoints: ["https"]
Trigger or verify the dynamic reload without restarting Traefik:
curl -X POST http://traefik/api/providers/file?dynamic=true
At this point, any fresh control connection immediately lands on shard 4.
Step 3: Surgically Terminate Old Control Sockets
Now close only the lingering TCP sockets belonging to the migrated client(s) on the source shard using Linux ss -K (which invokes kernel SOCK_DESTROY cleanly without disturbing neighboring tenants):
ss -K "dport = 7890 and src 203.0.113.5"
Because the client detects the TCP RST immediately and already holds a valid machine/node key that now exists on shard 4, it automatically dials hsa.demo.com, passes through Traefik's updated route, and resumes its map stream on headscale-cluster4 within seconds—with no manual login or re-authentication required.

Safety Checks and Rollback Tips
Before running automated migrations in production, build three guardrails into your migration script:
- Verify target shard readiness first: Confirm the destination Headscale node has loaded the migrated
nodesandpoliciesrows before callingss -K. - Preserve existing WireGuard data paths: Existing peer-to-peer WireGuard tunnels continue forwarding packets during the 1–5 second control-plane reconnect, so active SSH or RDP sessions typically stay alive without dropping.
- Keep a clean rollback path: If target verification fails, revert the Traefik router back to
headscale-cluster3and restore the originalcluster_idbefore terminating sockets.
Observed Migration Metrics
Across live shard-rebalancing runs using this pattern:
- End-to-end migration time: typically under 15 seconds
- Control-plane reconnect window: typically 1–5 seconds
- User re-authentication rate: near 0% (existing node keys remain valid)