Repository navigation
Description
Getting offers is CPU-heavy pure Python, and although it runs in executor threads, it degrades the responsiveness of the whole server: while it runs, every request and background task on the event loop slows down.
Where the CPU goes
Backends that use ComputeWithAllOffersCached (AWS, GCP, Azure, Lambda, RunPod, and most others) cache all offers and filter them per request. With 8 such backends, the cache holds ~57k offers, ~38k of them GCP. Each filter pass converts every cached InstanceOffer into a gpuhunt.CatalogItem just to call gpuhunt.matches():
dstack/src/dstack/_internal/core/backends/base/offers.py
Lines 215 to 224 in 523a06e
| def filter_offers_by_requirements( | |
| offers: Iterable[InstanceOfferT], | |
| requirements: Optional[Requirements], | |
| ignore_disk: bool = False, | |
| ) -> Iterator[InstanceOfferT]: | |
| query_filter = requirements_to_query_filter(requirements, ignore_disk=ignore_disk) | |
| for offer in offers: | |
| catalog_item = offer_to_catalog_item(offer) | |
| if gpuhunt.matches(catalog_item, q=query_filter): | |
| yield offer |
One pass over the 57k offers takes ~90ms of CPU: ~75% in offer_to_catalog_item() and ~25% in gpuhunt.matches().
The number of passes per request multiplies this:
- Run planning queries offers separately for each candidate fleet, and each query filters the offers of all backends, even when fleets have identical specs:
dstack/src/dstack/_internal/server/services/runs/plan.py
Lines 938 to 955 in 523a06e
| for candidate_fleet_model in candidate_fleet_models: | |
| offers_from_fleet = [] | |
| for offer in await _get_backend_offers_in_fleet( | |
| project=project, | |
| fleet_model=candidate_fleet_model, | |
| run_spec=run_spec, | |
| job=job, | |
| volumes=volumes, | |
| max_offers=max_offers_per_fleet, | |
| full_offers=full_offers, | |
| unallocated_resources=unallocated_resources, | |
| ): | |
| offer_identity = _get_backend_offer_identity(offer) | |
| if offer_identity not in seen_offer_identities: | |
| offers_from_fleet.append(offer) | |
| seen_offer_identities.add(offer_identity) | |
| offers = list(heapq.merge(offers, offers_from_fleet, key=lambda o: o.price)) | |
| return offers |
- Filtering is lazy and stops after enough matches, so requirements that match most offers (e.g. CPU-only) scan only a part of the list, but requirements that match few offers (e.g.
H100:8, ~300 of 57k) scan every offer every time.
Measured for one /runs/get_plan request (H100:8): ~0.2s of CPU in a project with 1 fleet (2 full passes) and ~3.8s with 40 fleets (2.3M offers scanned).
Why it stalls the server
The work runs in executor threads, so it doesn't block the event loop directly, but it holds the GIL. A CPU-bound thread releases the GIL only when another thread has waited for the switch interval (5ms by default). The event loop thread releases the GIL on every syscall (each socket read/write, each poll), and then waits up to 5ms to get it back. So while offers are being filtered, every I/O step on the event loop costs ~5ms instead of microseconds.
A standalone test: an asyncio client doing small TCP round trips takes 0.11ms per round trip, 14.4ms with one thread filtering offers, and 29ms with two. Requests doing tens of DB round trips get 50-100ms+ added, and pipelines slow down the same way.
In a local server with a production-like state (390 fleets, API traffic mix of production at ~1.3 RPS), offer filtering accounted for most of the process CPU. Compared with no get_plan traffic, get_plan requests lowered the apdex of all other endpoints from 0.90 to 0.68 and raised the event loop lag p99 from 18ms to 59ms. Production servers show the same pattern: a bimodal latency where ~1/3 of requests spend 50-95ms before their first DB query.
Proposed changes
- Make each pass cheaper by not converting offers to catalog items, e.g. by matching requirements against
InstanceOfferfields directly (a prototype returned identical results on 57k offers for a grid of requirements and cut a pass from ~90ms to 17-40ms). Caching aCatalogItemfor each cached offer gives the same speedup but adds ~21 MiB per 57k-offer cache, and caches are per project. - Index cached offers by GPU name, so requests that name GPUs check only those offers (
H100:8: ~26ms → ~0.5ms per pass). - Query offers once for candidate fleets that yield identical requirements and backends instead of once per fleet.
- Optionally lower the GIL switch interval (e.g.
sys.setswitchinterval(0.001)), which cuts the waits for any remaining CPU work in threads ~5x (14.4ms → 3.1ms per round trip in the test above; apdex 0.68 → 0.79 in the local server).
Backends with ComputeWithFilteredOffersCached (Vast.ai, Daytona, Lium, Seeweb) are not affected: they cache offers per requirements and only convert matching items.
Activity
r4victor commentedon Nov 25, 2026
Proposed solution for 1 and 2: index the cached offers once per cache refresh instead of filtering the whole list per request, and match offers with a dstack-owned matcher instead of converting them to
gpuhunt.CatalogItem.Offer index
ComputeWithAllOffersCached.get_offers()currently filters every cached offer on every call:dstack/src/dstack/_internal/core/backends/base/compute.py
Lines 289 to 304 in 2e2a2a2
Instead, when the offers cache refreshes, group the cached offers by a key made of fields with few distinct values: GPU vendor, GPU name, GPU memory, GPU count, spot, CPU arch, and CPU count. The index is a list of groups, each holding its key and its offers sorted by price. Groups reference the cached offers, no copies.
A query checks each group's key against the requirements, skips groups that cannot match, and lazily merges the offers of the remaining groups by price, checking each offer.
get_offers()returns the same lazy, price-ordered iterator as today, so its signature and callers don't change.The key covers all GPU, spot, CPU arch, and CPU count checks, so requests constrained by those compare about as many offers as they match. Only price, memory, and disk are left for per-offer checks. Groups stay few: 205 for GCP, 495 for 7 providers combined, and checking all keys takes microseconds.
One offer matcher
OfferMatcheris built once per requirements, has the same semantics asgpuhunt.matches(), and readsInstanceOfferfields directly:matches_offer(offer)is the full check. The index uses it for every offer it returns, and it replacesoffer_to_catalog_item()+gpuhunt.matches()wherever offers are matched today, including existing instances:dstack/src/dstack/_internal/server/services/instances.py
Lines 590 to 596 in 2e2a2a2
dstack/src/dstack/_internal/server/services/instances.py
Lines 725 to 728 in 2e2a2a2
matches_key(key)is the group check. It must never reject a group that contains a matching offer, but it may be looser thanmatches_offer(), so it affects only how much is skipped, not the results.Tests:
matches_offer()agrees withgpuhunt.matches(offer_to_catalog_item(offer))on the real catalog for a grid of requirements, andmatches_offer(o)impliesmatches_key(get_offer_key(o)).Unchanged: disk fitting and
get_offers_post_filter()run on the matched offers as today. Backends withget_offers_modifiers()(Kubernetes, Slurm, GCP with a reservation) keep the current linear path, since modifiers change the fields being matched. Backends withComputeWithFilteredOffersCachedare not affected.Prototype results
55,233 offers from the gpuhunt catalog of 7 providers, 495 groups, index built in 28ms. Results are identical to
filter_offers_by_requirements()in all cases. "Compared" is the number of offers checked to produce all matches (today it is all 55,233); times are for the first 50 matches and for all matches, measured locally.Narrow requests stop scanning the whole catalog: matching offers are the most expensive ones, so today even the first 50 require a near-full pass.
Remaining costs:
spot: auto) match most offers, so getting all matches still touches each of them, and the gain comes only from the cheaper per-offer check. The first N offers are cheap either way.max_priceand memory-heavy requests compare ~29k offers to find 3-5k matches, since price and memory are not in the key. Groups are sorted by price, so a query can stop reading a group atmax_price. Per-group min/max memory inmatches_key()would cover memory if needed.get_offers_post_filter()returns an opaque function, so it still runs on every matched offer and can't be used to skip groups (AWS reservations, RunPod and JarvisLabs multinode). Its cost isn't in the numbers above. Post-filters have to stay cheap per offer, and GCP's isn't: it validatesbackend_datainto a pydantic model for each offer to read one flag, ~11ms for the ~17.7k GCP offers matching default requirements. Reading the flag from the dict directly cuts that to ~0.6ms:dstack/src/dstack/_internal/core/backends/gcp/compute.py
Lines 272 to 285 in 2e2a2a2
Next steps
total_offersandmax_price. Showing the top offers without an exact total, or caching results per requirements until the index is rebuilt, would make broad requests cheap as well.r4victor commentedon Nov 25, 2026
#4528 replaces the
gpuhunt.matches()filtering with a dstack-ownedOfferMatcher, which makes a fullget_offers()pass over ~55k offers about 4x faster (121ms → 31ms with default requirements).Disk fitting remains expensive: if the requested disk differs from the default, every matching offer with a configurable disk is copied to set its disk size, which adds ~100ms per full pass (31ms → 133ms for
disk: 200GB..).