Handling multi-tenancy in PySpark applications with dynamic resource scaling
Instruction: Discuss how to architect PySpark applications to support multi-tenancy, focusing on dynamic scaling of resources based on tenant demand.
Updated
Separate tenant authorization, application scheduling and cluster capacity. Partitioning files by tenant ID can help pruning, but it does not prevent a tenant with broad credentials from reading another partition or isolate CPU/memory.
For mutually untrusted workloads, use separately authorized applications and compute boundaries appropriate to the threat model. Give each workload least-privilege storage identities and enforce limits through the cluster manager or admission system. A shared Spark session exposes shared driver state, caches and credentials unless a separate trusted service strictly constrains what users can execute.
Within one trusted application, Spark's fair scheduler can share task execution among pools using weights and minimum shares. Those settings are not per-tenant memory isolation or a hard maximum-resource guarantee, and minimum shares still depend on available resources. Across applications, use the cluster manager's queues/quotas and policy. Preserve job-group/pool context correctly when submitting concurrent PySpark work; the documented threading limitations matter.
Spark dynamic allocation adjusts the application's executor count in response to workload signals. It does not automatically allocate a separate executor budget for each tenant or provision cloud machines. Coordinate executor bounds and supported shuffle preservation with node autoscaling, startup latency and overall quotas. Scaling cannot fix every hot key, slow sink or unbounded query.
Add trusted tenant-aware admission control, concurrency limits, cancellation/timeouts and backpressure. Measure per-tenant queue delay, completion latency, failure rate and resource use with bounded metric cardinality. Usage counts such as daily active users are not a direct measure of executor demand.
Test noisy-neighbor bursts, denied cross-tenant access, cancellation, credential rotation, scale-down and recovery. Report both fairness and cost under representative demand, and keep enough capacity for the required service target rather than claiming autoscaling guarantees it.
Scheduling and fair pools, Spark security, Dynamic allocation configuration