Concurrency
Diffusion™ is a multi-threaded server and utilizes concurrent processing to achieve maximum performance. Java™ NIO technology is utilized so that a separate thread is not required for each concurrent connection and very large numbers of concurrent connections can be handled.
Because Diffusion is a multi-threaded environment it is necessary to have an understanding of concurrency issues when configuring Diffusion for best performance.
This section discusses issues of threading and concurrent processing.
User threads
Server-side components using the Diffusion Java API can make use of the Java threads API to schedule tasks for processing of their own in a separate thread of processing.
You can execute any object of a class that implements the RunnableTask interface using one of the ThreadService.schedule methods. You can to request a one-off execution of a task, periodic execution at a given interval or execution according to a schedule. Periodic processing can be important to publishers that pull data updates from elsewhere.
Such tasks issued using the thread service are executed using threads from the background thread pool.
Alternatively, users can define their own thread pools to use using the thread service and execute tasks using these thread pools.
NIO Threads
Each connector that is configured in etc/Connectors.xml comprises a connector thread that listens for incoming socket connections, accepts them and registers them with an acceptor thread that handles any incoming data notifications. Message decodin is run in the inbound thread pool. Connector and acceptor threads are occupied for the minimum amount of time and are completely non-blocking.
Though performance can be improved in extreme case by adjusting the numbers of these NIO threads, no significant processing occurs within them.
Client multiplexers
A client multiplexer is a separate thread which is responsible for processing messages, queuing for clients (conflating if necessary), taking messages from client queues and sending them to the client or clients. A number of these multiplexers can be configured to improve concurrent processing when there are a large number of clients.
The number of multiplexers can be configured. By default, the number of multiplexers is the same as the number of available cores on the host system of the Diffusion server .
Multiplexers typically batch these output messages into output buffers according to the output buffer size configured for the client connectors.
Thread pools
Diffusion maintains a number of configurable thread pools which are used for a number of purposesFor more information, see Thread pools. Thread pools can also be accessed programmatically using the ThreadService class of Diffusion server API . Refer to the API documentation for more information about this.
The various types of thread pools are as follows:
Inbound thread poolThis is used to obtain a thread to process any inbound message received on an NIO connection. The maximum number of threads configured for this pool must cater for the maximum required concurrency for incoming requests.
Diffusion does not maintain a separate thread for each client connection but rather passes each inbound request from a connection to the inbound thread pool for processing.
For example, when a client subscribes, the input processing happens on an inbound thread from the pool, the subscribe method and topic loader methods are run in one of these threads.
Connector inbound thread poolsIndividual connectors can configure their own separate inbound thread pool to override the use of the default. This cannot be required if you want different behaviors for each connector or if there are a lot of connectors. Due to locking on the inbound thread pool, you get better performance if each connector to have its own inbound thread pool.
Background thread poolThe background thread pool is used for executing scheduled tasks. These tasks can be issued by Diffusion .
Diffusion uses scheduled tasks for various reasons such as retrying connections. If a Diffusion server cannot connect to another server and there is a retry policy, a scheduled task will be used to retry the connection.
User thread poolsWithin the Java threads API user can define thread pools that can be used for multi-threaded processing.