js_shared_dict_zone
Defines a shared memory zone for storing data accessible by NJS scripts within the NGINX stream context.
Description
The js_shared_dict_zone directive allows you to create a shared memory zone specifically for use with NJS scripts in the stream context of NGINX. This directive specifies one or more shared dictionaries that can be accessed by JavaScript via the NJS module. By enabling shared memory storage, it allows scripts to share data across different worker processes, which can be particularly useful for caching data or other shared state management operations.
The syntax requires at least one argument, which is the name of the memory zone, and can optionally include size definitions. This enables fine control over how much memory is allocated and how the data is structured for efficient access. The stream context is where this directive is placed, ensuring that it is relevant when handling TCP or UDP data streams. The benefits of using shared dictionaries include reduced overhead for repeated data requests and allowing NJS scripts to maintain state over multiple connections.
When configuring js_shared_dict_zone, proper consideration of the storage needs and memory allocation strategies is important, as excessive memory usage can lead to decreased performance. Each shared dictionary operates independently, meaning you can define multiple zones for different purposes based on application needs.
Config Example
js_shared_dict_zone my_cache 10m;
Ensure the specified memory size is sufficient for the expected data load, as exceeding limits can cause performance degradation.
Remember to configure access permissions properly when sharing data across worker processes.
Avoid using large objects that may cause significant overhead during transmission.