Files

@n8n/backend-module-execution-limits

Backend feature module. The n8n runtime loads the module through the lazy import in packages/cli/src/modules/modules.manifest.ts.

pnpm turbo typecheck --filter=@n8n/backend-module-execution-limits
pnpm turbo lint --filter=@n8n/backend-module-execution-limits
pnpm turbo test --filter=@n8n/backend-module-execution-limits

Go through Turbo. It builds this package's workspace dependencies first.

Read docs/backend/how-to/build-a-backend-module.md for the module lifecycle, REST controller, service, repository, entity, configuration, and testing patterns.

Licence

The module has no licence flag. Its init registers the fold step that counts runs in execution_limit_usage, and that step must run on every instance. Gate each licensed part (a controller, the limit check) with @Licensed or LicenseState.

Usage reads

Read usage through ExecutionUsageService only, so each reader sees the same count.

  • getCachedUsage(scope) reads memory. Use it at an execution start. null means that the usage is not known, for example for a scope that has no limit.
  • getUsage({ scope, period, at }) reads the database. Use it for a report.

The usage API reads through ExecutionLimitsService.getInstanceUsage(). That method is the one place that selects the period unit and the limit for the instance usage.

A period is a sum of UTC days. A week starts on Monday. Main and webhook processes hold the usage in memory, and a timer reloads it every 5 seconds. The count can be up to about 10 seconds old. If a reload fails, the last count stays in use until a reload succeeds or the period ends, so it can be older.

Limit policies

A policy is stored in execution_limit_policy and has a period and a maximum. A named policy is assigned to projects in execution_limit_policy_assignment, and to a scope such as newProjects in execution_limit_policy_scope_assignment. A policy with no name holds the project-specific limits of the one project that it is assigned to.

A project gets its limit from its policy, and counts its own executions over the period of that policy. If a project has several policies, the lowest daily rate applies. The instance limit and its period are in the features.executionLimits settings row.

Write shared policies through ExecutionLimitPolicyService, and assignments and project-specific limits through ExecutionLimitAssignmentService. Each has its own repository. Every write runs through ExecutionLimitWriter, which locks the settings row. Policy writes add no audit row and emit no event, on purpose: a follow-up change adds their audit. Each policy write still reloads the limits in memory.

  • A project and a scope have one policy. An assignment replaces the policy that is there.
  • Unassigning a named policy from a project leaves a different policy or project-specific limits in place.
  • Project-specific limits and a named policy replace each other. When a project loses its project-specific limits, the assignment repository deletes that policy, so no policy with no name stays without its project.
  • Only a team project can get a policy. A personal project is rejected.
  • A null maximum is no limit. A new or changed limit cannot be above the instance limit when its period fits in the instance period: the same period, or a day. A week does not fit in a month. A lower instance limit is accepted later, and the policy stays as it is.
  • A new team project gets no policy. It still gets a row in project_execution_limit from defaultProjectMaxExecutions, but nothing reads that table, so the row sets no limit.
  • A deleted policy removes its assignments.
  • A project delete takes no lock when the project has no policy. Project-specific limits that are set on the project at the same moment can stay as a policy with no project. It has no effect.

Limit check

ExecutionLimitPolicyCheck runs at the executionAdmission policy point. init registers it at boot, and it checks the licence on each call.

  • A run that does not count toward the limit is allowed before any read. The counting rule is countsTowardExecutionLimit in @n8n/backend-services.
  • A run is refused when its project or the instance has reached its limit. The violation gives the scope, the limit, the usage and the reset time.
  • The check reads memory only. It writes nothing. A refused run gets no execution row, so it adds nothing to the usage.
  • The check fails open. If the limits or the usage are not loaded, or a read throws, it allows the run and logs.
  • Execution limits apply to workflow runs only, so agent runs are not checked.

Usage retention

A daily system task deletes the rows of execution_limit_usage and execution_limit_instance_usage that are older than N8N_EXECUTION_LIMITS_USAGE_RETENTION_DAYS (default 430, from 31 to 36500). The task runs one time for the cluster and does not need a licence, because the fold step writes rows on every instance. The minimum is the longest limit period, in days.

Import rules

  • Do not import from packages/cli or from the n8n package.
  • Declare each workspace and third-party import in this package.
  • Export APIs for other packages from src/index.ts.
  • Keep the module entrypoint in the ./module export. Do not export it from src/index.ts. Importing the entrypoint runs the @BackendModule decorator.
  • Use dynamic imports inside the module entrypoint. This keeps disabled modules unloaded.
  • Keep TypeORM imports in entity and repository files. A repository composes a file-private BaseRepository table and exposes only use-case methods. It does not extend BaseRepository.