SaaS & Architecture

Reliable background jobs in multi-tenant systems

Carry explicit tenant context through asynchronous work.

Anant Dhiman · · 1 min read

Tenant-specific tasks moving through a shared queue and workers
On this page

A worker can process tasks belonging to different tenants over its lifetime. Request-scoped context from a web process is not automatically available in a separate worker.

Implementation approach

Include a trusted tenant identifier and resource identifier in the job payload. Resolve the tenant at execution time, authorize or validate the operation as appropriate, and clear context in a finally block.

Example

try {
    $context->activate($tenantId);
    $handler->process($recordId);
} finally {
    $context->clear();
}

Validation and edge cases

Run jobs for two tenants consecutively in the same worker. Simulate missing tenants, timeouts and duplicate delivery, and check that retries cannot repeat irreversible effects.

Key takeaway

Make the queue payload small and use an idempotency strategy for externally visible operations.

← All insights

Related articles