Skip to content

Consider DB Query for Expired Prebuilds at Scale #18217

@ssncferreira

Description

@ssncferreira

Description

This issue tracks a possible future improvement to the way expired prebuilds are filtered in the system.
Currently, we use in-memory filtering to separate expired from running (non-expired) prebuilds when constructing the PresetSnapshot:

func filterExpiredWorkspaces(preset database.GetTemplatePresetsWithPrebuildsRow, runningWorkspaces []database.GetRunningPrebuiltWorkspacesRow) (nonExpired []database.GetRunningPrebuiltWorkspacesRow, expired []database.GetRunningPrebuiltWorkspacesRow) {
if !preset.Ttl.Valid {
return runningWorkspaces, expired
}
ttl := time.Duration(preset.Ttl.Int32) * time.Second
if ttl <= 0 {
return runningWorkspaces, expired
}
for _, prebuild := range runningWorkspaces {
if time.Since(prebuild.CreatedAt) > ttl {
expired = append(expired, prebuild)
} else {
nonExpired = append(nonExpired, prebuild)
}
}
return nonExpired, expired
}

This approach is simple and performs well at the current scale. However, if the number of running prebuilds increases significantly, it may become a performance concern.

Context

Potential Improvement:

  • Introduce a dedicated database query to fetch expired workspaces.
  • Update the current GetRunningPrebuiltWorkspaces database query to only return non-expired workspaces.
  • This would avoid loading all running prebuilds into memory and manually filtering them.
  • Likely to be more efficient at larger scale.

While in-memory filtering is sufficient for now, introducing a DB query helps future-proof the system and ensures consistent performance in larger on-prem installations.

Action Items

  • Monitor performance as prebuild usage increases.
  • Revisit this issue if:
    • Prebuild count grows significantly.
    • Memory usage or latency becomes a concern in prebuild snapshot construction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions