Evicting the stalest seat for a new device
A customer with a three-seat license buys a new laptop and activation fails: "seat limit reached." The honest fix is not to raise the cap or make them hunt for a deactivate button — it is to let the new device reclaim the seat nobody is using. This post builds the admit-or-evict transaction in a Keyright seat store: why you evict by last-seen and never by activation date, the min-staleness guard that stops it from kicking a device that is merely mid-session, the serializable claim that keeps two simultaneous activations from both picking the same victim, and how the evicted device learns it was replaced on its next check-in.
A support ticket that every seat-based licensing system eventually collects: a customer on a three-seat license buys a new laptop, installs the product, activates — and gets seat limit reached. They are not pirating anything. They have three seats and they are using two of them; the third is a laptop that died eight months ago and will never check in again. The seat is real, the usage is not, and the only way out the product offers is a support request or a deactivation screen they cannot find.
The reflex fix is to raise the cap or add a “manage your devices” page. Both are worth having, but neither addresses the actual shape of the problem: a seat is occupied by a device that is gone, and a present device wants in. The right primitive is to let the new activation reclaim the seat nobody is using — automatically, deterministically, and without ever kicking a device that is actually running.
The real question
A Keyright device seat is a row: a device fingerprint, when it was activated, and when it last checked in.
public sealed record Seat(
Guid LicenseId,
string DeviceFingerprint,
DateTimeOffset ActivatedAt,
DateTimeOffset LastSeenAt); // server-stamped on every revalidation
When a device activates and the license is already at its cap, the engine has three choices: fail, raise the cap, or evict. Eviction is the interesting one, and it turns on a single question that is easy to get backwards: which seat do you reclaim? Get that wrong and you deactivate the machine the customer uses every morning while leaving the dead laptop in place.
Evict by last-seen, never by activation date
The intuitive answer — “drop the oldest seat” — is wrong, because “oldest” almost always means oldest by ActivatedAt, and the first device a customer activated is frequently their primary. The seat you want is the one that has been silent the longest: the minimum LastSeenAt.
The admit-or-evict transaction
The whole decision has to be atomic. Counting seats, picking a victim, and inserting the new seat are a single unit of work under a row lock, so that two devices activating in the same second cannot both conclude there is room.
public async Task<AdmitResult> AdmitAsync(
Guid licenseId, string fingerprint, int seatCap, TimeSpan minStaleness, CancellationToken ct)
{
await using var tx = await _store.BeginSerializableTransactionAsync(ct);
// 1. A re-activation of a device that already holds a seat is a refresh, not a new seat.
var existing = await _store.FindSeatAsync(licenseId, fingerprint, ct);
if (existing is not null)
{
await _store.TouchLastSeenAsync(existing, _clock.UtcNow, ct);
await tx.CommitAsync(ct);
return AdmitResult.Reactivated(existing);
}
var seats = await _store.ListSeatsForUpdateAsync(licenseId, ct); // takes the row lock
if (seats.Count < seatCap)
{
var seat = await _store.InsertSeatAsync(licenseId, fingerprint, _clock.UtcNow, ct);
await tx.CommitAsync(ct);
return AdmitResult.Admitted(seat);
}
// 2. Cap is full. The only reclaimable seat is the stalest one past the guard.
var cutoff = _clock.UtcNow - minStaleness;
var victim = seats
.Where(s => s.LastSeenAt < cutoff)
.OrderBy(s => s.LastSeenAt)
.FirstOrDefault();
if (victim is null)
{
await tx.RollbackAsync(ct);
return AdmitResult.AllSeatsActive(seats.Count); // fail closed, don't kick a live device
}
await _store.DeleteSeatAsync(victim, ct);
var reclaimed = await _store.InsertSeatAsync(licenseId, fingerprint, _clock.UtcNow, ct);
await tx.CommitAsync(ct);
return AdmitResult.Reclaimed(reclaimed, evicted: victim);
}
Four decisions in that method carry the whole design:
The requesting device gets short-circuited first. If the fingerprint already holds a seat, this is a refresh — a reinstall, a new version, a cleared cache. Touch LastSeenAt and return; never consume a second seat or evict anyone for a device that is already seated.
ListSeatsForUpdateAsync takes the lock. Everything after it reads a consistent, exclusive view of the seat set. A second concurrent AdmitAsync blocks here until this transaction commits, then re-runs its count and victim selection against the new reality — so it sees the seat this call just filled and makes its own, correct decision instead of racing to the same victim.
The victim is LastSeenAt-ordered and guarded. cutoff is now minus minStaleness; a seat seen more recently than that is filtered out entirely. The stalest surviving candidate wins. Set minStaleness to a small multiple of your revalidation interval — if clients check in hourly, a guard of two or three hours means a seat is only ever a victim once it has genuinely missed several check-ins.
No eligible victim means fail closed. When every seat is fresh, there is nothing to reclaim and the activation is refused with AllSeatsActive — a distinct result the SDK can turn into “you are already using all N devices” rather than a generic error. The engine never evicts a device that is actively running.
What the evicted device sees
Eviction is a server-side state transition; the evicted device finds out on its own schedule, at its next revalidation. Its stored seat token no longer corresponds to any live seat row, so the service answers with a reclaimed result instead of a fresh lease:
public LicenseStatus OnRevalidate(SeatCheckResponse resp)
{
if (resp.Outcome == SeatOutcome.Reclaimed)
{
// The seat was given to another device. This is a deactivation, not a ban.
_state.EnterGraceOrLockout(reason: LostSeatReason.ReclaimedElsewhere);
return LicenseStatus.SignedOut(
"This device was signed out because the license was activated on another device.");
}
return LicenseStatus.Active(resp.Entitlements);
}
That path is deliberately the same one you already use for a seat lost to any cause: the device enters its grace window if one is configured, keeps working until the window closes, and shows a message the customer can act on. The whole-license signature is still valid — only this device’s claim on a seat was reassigned — so the fix is a single click to re-activate, which, if the customer is genuinely over their seat count, is where the natural “you need more seats” conversation happens.
Caveats, honestly
Thrashing is real when the cap is genuinely exceeded. If a customer runs four active devices on three seats, auto-eviction turns into musical chairs: each device reclaims a seat, gets reclaimed in turn, and nobody stays signed in. The min-staleness guard prevents the pathological version — fresh seats are untouchable, so the ping-pong can only involve seats that are actually idle — but it does not create seats that were not bought. Eviction is a device-replacement convenience, not a way to stretch a license. Instrument the reclaim rate per license; a license that evicts constantly is a sales signal, not a bug.
Fingerprint drift can masquerade as a new device. If a machine’s fingerprint shifts — a disk swap, a VM re-clone, an OS reinstall — it looks like a stranger asking for a seat and may reclaim its own stale row. That is usually harmless (the device re-seats itself) but it inflates your reclaim metrics. Tolerant fingerprinting, covered in machine fingerprinting and hardware drift, keeps a device recognised as itself across small changes.
Clocks are the server’s, not the client’s. LastSeenAt is stamped by the service on each check-in, never sent by the client, so a device cannot forge a recent heartbeat to dodge eviction or an old one to frame a rival seat. Victim selection is only as trustworthy as the timestamps it orders by — keep them server-authoritative.
Takeaway
A full seat cap is not automatically a violation; it is frequently just bookkeeping about a device that no longer exists. Reclaim seats by silence, not by age, guard the reclaim so a mid-session device is never the victim, make the whole admit-or-evict step a single serializable transaction, and let the displaced device discover its new state through the same grace path as any other lost seat. The customer with the new laptop activates and gets on with their work — which is the entire point of selling them a seat in the first place.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.