Scaling
Worker pools that grow and shrink with the queue inside each process, more worker nodes across processes, database connections, backpressure, metrics and autoscaling.
On this page
Status. Tested: the in-process pool scaler and the backlog behaviour in the product's package tests against PostgreSQL, and the HA compose topology with one worker. The worker-scaling drill and the agent soak test exist but were not run for these docs. Not yet run: the HorizontalPodAutoscaler and the KEDA ScaledObject against a cluster (both were only rendered). There are no published throughput figures for 0.1.0.
Structam scales in two places: inside each process, where worker pools grow and shrink with the queue, and across processes, by running more structam worker (or serve) nodes. Every node joins the same queues with no configuration.
What the workers do
Agents send inventory to the serve node they are connected to. The gateway writes each message to the ingest queue and acknowledges it after commit; workers on any node then apply it (current state, change detection, configuration versions). Two more pools run background work:
| Pool | Work | Default per node (min to max) |
|---|---|---|
ingest | agent inventory and fetched configuration files | serve: 1 to 4 × GOMAXPROCS; worker and all: 2 to 4 × GOMAXPROCS |
discovery | agentless discovery sweeps | 0 to 2 |
integration | InfraDB hardware enrichment and other integration jobs | 0 to 2 |
Jobs of one agent run in order; jobs of different agents run in parallel. A large discovery sweep runs on its own pool and never starves agent ingest.
The scaler inside each process
Every 2 s each node reads the cluster's queue depth and the age of the oldest queued job for each pool, and sets:
target = clamp(ceil(depth / BACKLOG_PER_WORKER), MIN, MAX) raised by 50 % when the oldest job is older than 30 sIt adds workers at once and retires idle ones only after 30 s above target, so a burst does not make it flap.
| Setting | Default | |
|---|---|---|
STRUCTAM_INGEST_WORKERS_MIN / _MAX | 1 on serve, 2 elsewhere / 4 × GOMAXPROCS | ingest pool per node |
STRUCTAM_INGEST_BACKLOG_PER_WORKER | 25 | queued jobs per ingest worker |
STRUCTAM_DISCOVERY_WORKERS_MIN / _MAX / _BACKLOG_PER_WORKER | 0 / 2 / 5 | |
STRUCTAM_INTEGRATION_WORKERS_MIN / _MAX / _BACKLOG_PER_WORKER | 0 / 2 / 10 | |
STRUCTAM_DB_MAX_CONNS | 0: the pools' maxima + 8, plus 12 on serve and all (at least 16) | connection pool per node |
STRUCTAM_INGEST_MAX_QUEUE | 100 000 | above it, agents are told to retry later |
PostgreSQL connections
Each worker holds a database connection while it works, so the sum of a node's pool maxima is capped at its connection pool less 8. If you set STRUCTAM_DB_MAX_CONNS, the maxima are cut to fit, ingest first.
Add up the pool sizes of every node and keep the total below PostgreSQL's max_connections, with room for administration. A node on 4 CPUs with the defaults uses 16 + 2 + 2 + 8 = 28 connections as a worker, and 40 as serve or all. The compose files raise max_connections to 150 (one node) and 300 (the HA topology).
GOMAXPROCS is the number of CPUs the process may use: on a host or VM all of them, in a container the CPU limit, never the CPU request. A container with only a request on a 32-CPU node would default to 128 ingest workers. The Kubernetes manifests therefore set STRUCTAM_INGEST_WORKERS_MAX to 4 on serve pods (28 connections each) and 8 on worker pods (20 each): with 2 serve pods and at most 8 worker pods that is 2 × 28 + 8 × 20 = 216 connections, and up to 284 while a rolling update surges one serve and two worker pods. Changing a replica count or a pool maximum changes the budget: (serve replicas × serve pool) + (worker maximum × worker pool).
Backpressure
When the ingest queue holds more than STRUCTAM_INGEST_MAX_QUEUE jobs, or commits are stalled (commit stalls), the gateway tells agents to retry later without opening a transaction. Agents keep the data in their on-disk queue (up to 256 MiB or 7 days by default) and send it later.
Metrics
Every node serves /metrics in the Prometheus text format: on STRUCTAM_WORKER_ADDR on workers (no authentication), on the main listener with a token of scope metrics, or on STRUCTAM_METRICS_ADDR without one. Bind the unauthenticated listeners to private addresses.
| Metric | Labels | Meaning |
|---|---|---|
structam_jobs_queue_depth | pool, node | queued and running jobs of the pool, cluster-wide |
structam_jobs_oldest_seconds | pool, node | age of the oldest queued job, cluster-wide |
structam_jobs_workers | pool, node, state (idle, busy) | this node's workers |
structam_jobs_total | pool, outcome | jobs finished by this node (ok, duplicate, retry, dead, lost, dropped, released) |
structam_job_seconds | pool | time to process one job (histogram) |
Every node exports the same cluster-wide depth under its own node label. Aggregate it with max(), never sum():
max(structam_jobs_queue_depth{pool="ingest"})
max(structam_jobs_oldest_seconds{pool="ingest"})
sum by (state) (structam_jobs_workers{pool="ingest"})System → Queues shows the same: depth, oldest job, workers per node, and dead jobs with their retry.
When to add nodes
structam_jobs_oldest_seconds{pool="ingest"}stays above 30 s, or the depth keeps growing while every node's ingest pool is at its maximum: add astructam worker, or raiseSTRUCTAM_INGEST_WORKERS_MAXif CPU and database connections allow.- Many agents (thousands of WebSocket connections) or many browser users: add a
structam servenode behind the load balancer. - Database CPU is the limit: more workers will not help; scale PostgreSQL first.
Autoscaling workers on Kubernetes
Scale the structam-worker Deployment on max(structam_jobs_queue_depth{pool="ingest"}). A starting point is one pod per 200 queued jobs: a worker pod runs up to 8 ingest workers, one per 25 jobs.
HorizontalPodAutoscaler (deploy/kubernetes/worker-hpa.yaml, in the kustomization, 1 to 8 pods) reads an external metric structam_ingest_queue_depth, which a metrics adapter must provide. With prometheus-adapter:
externalRules:
- seriesQuery: 'structam_jobs_queue_depth{pool="ingest"}'
metricsQuery: 'max(structam_jobs_queue_depth{pool="ingest"})'
name: {as: "structam_ingest_queue_depth"}
resources: {namespaced: false}It scales up by at most two pods a minute, and down by one pod every two minutes after five quiet minutes.
KEDA (deploy/kubernetes/keda-scaledobject.yaml, not in the kustomization) queries Prometheus itself and needs no adapter. Use it instead of the HPA, not with it:
kubectl -n structam delete hpa structam-worker --ignore-not-found
kubectl -n structam apply -f deploy/kubernetes/keda-scaledobject.yaml # set serverAddress to your PrometheusA worker pod that is scaled away drains like any node: in-flight jobs get up to 20 s and unfinished claims return to the queue.
Questions the docs do not answer?
Ask the people who build Structam. Installation media and licences come from KazLabs.