the browser session service · lesson 1 of 7 · sources checked 2026-11-22
the service interface: lease, quota, and time to live
This lesson gives the calls, the timers, and the error answers of the lease through which a client gets a browser.
purpose
A session service keeps browsers on its own hosts and gives them to many clients. Each client must know what it can ask for, how long it can keep a browser, and the answer when no browser is free. Without a written interface, one client can hold the whole pool.
objectives
- Write the create, renew, and release calls of a lease, with their fields.
- Explain the difference between the hard time to live and the idle timeout.
- Select the error answer for each create call that the service refuses.
- Name the pattern that a fixed time to live can leave in the records of a site.
new terms
- endpoint gateway
- A proxy in front of the remote debugging endpoints that checks a token for each lease.
- time to live (TTL)
- The maximum age of a lease, after which the service ends the session.
- idle timeout
- The maximum time that a lease can stay without a client command.
how it works
A client never starts a browser itself. It asks the service for a lease: one browser session for one client, for a limited time. The service holds the browser, and the client only sends commands to it.
Each client belongs to a tenant. The tenant has a quota and its own isolation rules.
the three calls
The interface has three calls:
- Create. The client asks for a browser session.
- Renew. The client keeps the lease open while it works.
- Release. The client gives the browser back.
The create call has these fields:
| field | example value | what the service does with it |
|---|---|---|
| Tenant | tenant-a | It finds the quota and the isolation rules. |
| Browser class | engine and version | It selects a browser of the correct build. |
| Protocol and client version | cdp, 1.3 | It refuses a client that the browser cannot serve. |
| egress class | a class word, not a provider | It binds one exit path to the browser. |
| idempotency key | k1 | A retry gets the same lease, not a second browser. |
The answer holds a lease ID, an endpoint, a short-lived token, and an expiry. The endpoint goes through the endpoint gateway, and the token goes in a header, not in the address.
two timers
A lease has two timers. They do two different jobs.
The time to live (TTL) is the hard limit. It starts at the create call, and a renew call does not move it. For example, with a time to live of 1,800 s, the lease ends at 1,800 s also when the client renews each minute.
The idle timeout ends a lease that gets no command. Each command or renew call starts it again. For example, with an idle timeout of 120 s, a client that stops at 80 s loses the browser at 200 s.
Thus the time to live sets a limit on how long each lease lives. The idle timeout gives back the browser of a client that stopped with no release call.
the quota
The quota of a tenant has two fields: the maximum concurrent browser sessions, and the maximum session hours each day. For example, a tenant can have 2 concurrent browser sessions and 40 session hours each day.
The quota stops one tenant from the use of the whole warm pool. The share of the pool for each of many tenants is a different rule. It is in Quotas and fairness (not yet published).
the error answers
The service refuses a create call at once, with a reason and a retry time. A queue that holds the call hides the wait from the client. A fast answer lets the client wait or stop.
| error | cause | retry time |
|---|---|---|
quota_exceeded | The tenant has its maximum of open leases. | When the quota can be free again |
no_capacity | The pool has no free browser. | A short time, for example 30 s |
protocol_mismatch | The browser cannot serve this protocol or client version. | None: change the client |
A browser server can accept only one client version of its protocol. Then a newer client cannot attach. For this reason, the create call names the protocol, CDP or WebDriver BiDi, and the client version. The service refuses the call before it holds a browser.
what the site sees
The site sees the commands of the browser, not the lease. For example, it can see which protocol domains the client turns on. A fixed time to live can also show: each long browser session ends after the same time.
the two sides
| automation side | detection side |
|---|---|
| A lease with an idempotency key stops a client retry from a second browser. A quota keeps one tenant from the whole pool. A fast "no capacity" answer lets the client wait or stop. | A fixed hard time to live gives browser sessions that end after the same time. A site that records the length of each visit can see a peak at that value. The site can see the protocol domains that the client turns on. It does not see the lease. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| A hard time to live that renew cannot move | No lease holds a browser for ever. | A long task must stop and start a new lease. | For each lease |
| An idle timeout that renew moves | The service gets back the browser of a client that stopped. | A slow client must send renew calls. | For each lease |
| Refuse at once with a retry time | The client knows the state and can wait or stop. | The client must have a wait rule. | When the pool is full or the quota is used |
| Hold the call in a queue | The client makes one call only. | The client cannot see the wait, and its own timer can end first. | Only with a short queue limit that the client knows |
| Name the protocol and the client version in the create call | A wrong client fails before the service holds a browser. | The client must send two more fields. | For each create call |
diagram
The first diagram shows the parts of the service.
the parts of a browser session service, from the client call to the records
- Lease APITakes the create, renew, and release calls. Gives a lease ID, an endpoint, a token, and an expiry.
- Quota storeHolds the concurrent browser sessions and the session hours of each tenant.
- Pool managerKeeps the warm pool, selects a browser for each lease, and recycles old browsers.
- Host agentsStart and stop the browser processes on each host, and report their state.
- BrowsersOne browser context or one browser process for each lease.
- Egress sessionsOne exit address for each browser. Two tenants never share one.
- Records and metricsThe lease events, the wait time, the utilization, and the cost per session hour.
The second diagram shows the states of one lease. Each change has its timer or its call.
the states of one lease and the timer of each change
The third diagram shows one lease from the create call to the reset of the browser.
one lease from the create call to the reset of the browser
The endpoint gateway checks the token of each lease before it forwards a command.
code example
The example is a lease service in memory. It uses synthetic tenants, a fake clock, and no network. The warm pool has 3 browsers, and each tenant has a quota of 2 open leases.
A lease service with a quota check, a hard time to live, an idle timeout, and an idempotency key.
// A lease service in memory: synthetic tenants, a fake clock, no network.
type Protocol = "cdp" | "bidi";
type CreateCall = { tenant: string; protocol: Protocol; clientVersion: string; idempotencyKey: string };
type Lease = { id: string; tenant: string; endpoint: string; expiresAt: number; idleUntil: number; open: boolean };
type Answer = { ok: true; lease: Lease } | { ok: false; error: string; retryAfterS: number };
const TTL_S = 1800; // hard time to live: renew does not move it
const IDLE_S = 120; // idle timeout: renew moves it
const SLOTS = 3; // warm browsers in the pool
const QUOTA = 2; // concurrent leases for each tenant
const ACCEPTS: Record<Protocol, string[]> = { cdp: ["1.3"], bidi: ["2"] };
let now = 0, next = 1;
const leases = new Map<string, Lease>();
const byKey = new Map<string, string>();
const openLeases = (tenant?: string) => [...leases.values()].filter((l) => l.open && (!tenant || l.tenant === tenant));
function create(c: CreateCall): Answer {
const known = byKey.get(c.tenant + "/" + c.idempotencyKey);
if (known) return { ok: true, lease: leases.get(known)! }; // a retry gets the same lease
if (!ACCEPTS[c.protocol].includes(c.clientVersion)) return { ok: false, error: "protocol_mismatch", retryAfterS: 0 };
if (openLeases(c.tenant).length >= QUOTA) return { ok: false, error: "quota_exceeded", retryAfterS: 60 };
if (openLeases().length >= SLOTS) return { ok: false, error: "no_capacity", retryAfterS: 30 };
const id = "lease-" + next++;
const lease = { id, tenant: c.tenant, endpoint: "gw.lab.example/" + id, expiresAt: now + TTL_S, idleUntil: now + IDLE_S, open: true };
leases.set(id, lease);
byKey.set(c.tenant + "/" + c.idempotencyKey, id);
return { ok: true, lease };
}
function renew(id: string): void {
const l = leases.get(id); // the idle time moves; the time to live does not
if (l && l.open) l.idleUntil = Math.min(now + IDLE_S, l.expiresAt);
}
function reap(): string[] {
const ended: string[] = [];
for (const l of openLeases()) {
const why = now >= l.expiresAt ? "time to live" : now >= l.idleUntil ? "idle timeout" : "";
if (why) { l.open = false; ended.push(`${l.id} ended (${why})`); }
}
return ended;
}
const show = (a: Answer) =>
a.ok ? `ok ${a.lease.id} at ${a.lease.endpoint}, expires t=${a.lease.expiresAt}` : `error ${a.error}, retry after ${a.retryAfterS} s`;
const call = (tenant: string, key: string, protocol: Protocol = "cdp", v = "1.3") =>
console.log(`t=${now} ${tenant} ${key}: ` + show(create({ tenant, protocol, clientVersion: v, idempotencyKey: key })));
call("tenant-a", "k1");
call("tenant-a", "k1"); // the same key again
call("tenant-a", "k2");
call("tenant-a", "k3");
call("tenant-b", "k4", "bidi", "3");
call("tenant-b", "k5");
call("tenant-b", "k6");
for (const t of [100, 200, 1900]) {
now = t;
renew("lease-1");
console.log(`t=${now} reap: ${reap().join("; ") || "none"}`);
}output
One line for each create call and for each reap step at a later time.
t=0 tenant-a k1: ok lease-1 at gw.lab.example/lease-1, expires t=1800
t=0 tenant-a k1: ok lease-1 at gw.lab.example/lease-1, expires t=1800
t=0 tenant-a k2: ok lease-2 at gw.lab.example/lease-2, expires t=1800
t=0 tenant-a k3: error quota_exceeded, retry after 60 s
t=0 tenant-b k4: error protocol_mismatch, retry after 0 s
t=0 tenant-b k5: ok lease-3 at gw.lab.example/lease-3, expires t=1800
t=0 tenant-b k6: error no_capacity, retry after 30 s
t=100 reap: none
t=200 reap: lease-2 ended (idle timeout); lease-3 ended (idle timeout)
t=1900 reap: lease-1 ended (time to live)The output shows that the second call with the key k1 gets the same lease. It also shows the three error answers. The renew calls keep lease-1 open after the idle timeout of the other two leases, but the time to live ends it at t=1900.
failure modes
| problem | cause | correction |
|---|---|---|
| A client retry starts a second browser, and the first browser stays held. | The create call has no idempotency key. | Send one key for each intended lease. Give back the same lease for the same key. |
| One tenant holds each browser of the pool. | The service has no quota for each tenant. | Set a limit of concurrent browser sessions and of session hours each day. |
| A browser stays held after its client stopped. | The lease has a time to live but no idle timeout. | Add an idle timeout that each command or renew call starts again. |
| A lease lives for ever. | Each renew call moves the time to live. | Let renew move only the idle timer. |
| A client waits and its own timer ends first. | The service holds the call in a queue with no limit. | Refuse at once with a reason and a retry time. |
| A client cannot attach after it gets a lease. | The browser server accepts only one client version. | Name the protocol and the client version in the create call, and refuse a mismatch first. |
check questions
A client sends a create call two times with the same idempotency key, because the first answer did not arrive. What does the service give back?
- A second lease with a second browser
- The same lease as for the first call
- The error quota_exceeded
answer
b. The same lease as for the first call
The idempotency key identifies one intended lease. A retry with the same key gets the same lease, so no second browser is held.
Explain the difference between the hard time to live and the idle timeout of a lease.
sample answer
The time to live starts at the create call, and a renew call does not move it. It sets a limit on the age of each lease. The idle timeout ends a lease that gets no command, and each command or renew call starts it again. It gives back the browser of a client that stopped with no release call.
A lease has a time to live of 1,800 s and an idle timeout of 120 s. The client renews each minute. When does the lease end?
- At 120 s
- Never, because the client renews
- At 1,800 s
answer
c. At 1,800 s
Renew moves only the idle timer. The time to live is a hard limit, thus the lease ends at 1,800 s.
The browsers of the pool accept only CDP. A client asks for a lease with WebDriver BiDi. Which answer does the service give?
- protocol_mismatch, with no retry time
- no_capacity, with a retry time of 30 s
- A lease, and the attach fails later
answer
a. protocol_mismatch, with no retry time
The service refuses a protocol or client version that the browser cannot serve. It refuses before it holds a browser, and a retry does not help.
Name the pattern that a fixed hard time to live can leave in the records of a site.
sample answer
Long browser sessions end at the same age. A site that records the length of each visit can see a peak at the value of the time to live.
exercise
exercise Do this task on paper. Use the synthetic rules and calls below. The pool has 2 free browsers. Each tenant has a quota of 1 open lease. The browsers accept only cdp with the client version 1.3.
| call | tenant | protocol and version | idempotency key |
|---|---|---|---|
| 1 | tenant-x | cdp, 1.3 | a1 |
| 2 | tenant-x | cdp, 1.3 | a1 |
| 3 | tenant-x | cdp, 1.3 | a2 |
| 4 | tenant-y | bidi, 2 | b1 |
| 5 | tenant-y | cdp, 1.3 | b2 |
| 6 | tenant-z | cdp, 1.3 | c1 |
- Do the calls in the order of the table.
- For each call, write the lease ID or the error answer.
- For each error answer, write the cause.
sample answer
The answer uses the rules and the six calls of the task.
Call 1 gets lease-1. Call 2 has the same key, thus it gets lease-1 again. Call 3 gets quota_exceeded, because tenant-x already has 1 open lease.
Call 4 gets protocol_mismatch, because the browsers accept only cdp. Call 5 gets lease-2. Call 6 gets no_capacity, because the 2 browsers are held by lease-1 and lease-2.
sources
- My model of the lease interface, the two timers, and the error answers of a browser session service, 2026-11 (not public).