-
Notifications
You must be signed in to change notification settings - Fork 97
Description
Problem Description
The Relay currently utilizes a worker thread pool (via piscina) to parallelize CPU-intensive tasks such as block processing and Ethereum log fetching. While beneficial for high-throughput environments, this architecture imposes a high memory floor. Each worker thread initializes a separate V8 Isolate, contributing significantly to the process's Resident Set Size (RSS).
Current Behavior & Impact
In resource-constrained deployments (e.g., containers with a 128MB RAM limit), the relay is prone to frequent OOMKilled events.
Observations:
- Isolate Overhead: Each worker thread adds roughly 20-30MB of RSS baseline overhead.
- Peak Spikes: Inter-thread communication using
postMessageinvolves the Structured Clone algorithm, which creates temporary data copies, leading to memory surges during heavy request processing. - OOM Vulnerability: With 2-4 workers enabled (the default), the idle RSS baseline often hovers around 140MB+, making it impossible to operate stably within a strict memory limited environment.
Proposed Solution
Introduce a configuration flag, WORKERS_POOL_ENABLED, to allow operators to explicitly disable the worker pool.
- Logic: When
WORKERS_POOL_ENABLED=false, theWorkersPoolservice should bypass Piscina and execute task handlers directly on the main event loop. - Dynamic Loading: Use dynamic imports to load worker logic only when needed, maintaining a clean separation of concerns.
- Optimization: This "Single-Threaded" or "Local Bypass" mode reduces the idle RSS, providing sufficient headroom for heap growth and network buffers within a memory limited container.
Rationale
While multi-threading improves performance on multi-core systems, process stability is the primary requirement for low-tier deployments. Providing a mechanism to opt-out of worker overhead allows the Hedera JSON-RPC Relay to support a wider range of hardware specifications and reduces the total cost of ownership (TCO) for lite relay operators.
Acceptance Criteria
- New
WORKERS_POOL_ENABLEDconfiguration variable (defaulttrue). -
WorkersPool.tsupdated to support local, main thread execution. - Error handling utility (
WorkersErrorUtils) extracted to prevent circular dependencies in local mode. - Validation that the relay survives acceptance tests on a memory limited environment with workers disabled.