Skip to content

[Feature] Dynamic, memory-aware scaling for Antivirus workers to maximize throughput and prevent OOM #4084

Open

Description

@suse-coder

Is your feature request related to a problem? Please describe.

When file scanning queues get backed up and no new files are processed, even though our clamd backends are sitting at less than 5% saturation.

This bottleneck happens because the current ANTIVIRUS_WORKERS setting is static. When the active workers encounter large files or complex archives, they get blocked until those specific scans are finished, preventing any new (and potentially very small) files from being scanned in the meantime.

According to the documentation, memory consumption is calculated roughly as max file size x workers x factor 8 - 14. Because of this, I cannot simply increase ANTIVIRUS_WORKERS to a high number (e.g., 500) to utilize the idle clamd resources. Doing so would risk a severe Out Of Memory (OOM) crash if multiple large files were to be processed at the same time.

Describe the solution you'd like

I would like the antivirus service to dynamically scale the number of active workers based on the currently available memory and the size of the files in the queue, rather than relying on a blind, static worker count.

Specifically:

  1. The service should automatically detect how much memory is available (either via host metrics or container Cgroup limits).
  2. Before spawning a worker for a queued file, the service should estimate the memory impact based on that specific file's size (e.g., current_file_size x safety_factor).
  3. If there is sufficient free memory, it should dynamically spawn more workers (allowing hundreds of concurrent workers if the queue is full of small files, thus actually saturating clamd).
  4. If memory is running low, it should hold off on spawning new workers—or pause processing a large file—until existing workers finish and free up RAM.

Ultimately, the system should maximize worker concurrency while strictly preventing OOM situations from happening.

Describe alternatives you've considered

  • Statically increasing ANTIVIRUS_WORKERS (e.g., to 500): This is not viable because it relies on luck. While it would fix the bottleneck for small files, a sudden influx of 500MB+ files would immediately trigger an OOM kill.
  • Keeping ANTIVIRUS_WORKERS low: This is the current workaround to prevent OOM, but it results in heavily underutilized hardware (clamd < 5% usage) and stalled queues whenever large archives are scanned.
  • Over-provisioning RAM: Allocating enough RAM to cover the worst-case scenario for 500 workers (which would require Terabytes of RAM based on the documentation's formula) is not financially or architecturally feasible.

Additional context

From the current documentation regarding Memory Considerations:

The antivirus service can consume considerable amounts of memory. This is relevant to provide or define sufficient memory for the deployment selected. To avoid out of memory (OOM) situations, the following equation gives a rough overview based on experiences made. The memory calculation comes without any guarantee, is intended as overview only and subject of change.

memory limit = max file size x workers x factor 8 - 14

With: ANTIVIRUS_WORKERS == 1

  • 50MB file --> factor 14 --> 700MB memory
  • 844MB file --> factor 8,3 --> 7GB memory

Because the service already knows the size of the file before passing it to the worker, implementing a memory-budget scheduler/semaphore based on this exact formula would solve both the OOM risk and the worker starvation issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions