Singleton Service Pattern

Learn the Singleton Service design pattern and its usage.

Intent

This pattern is to ensure that only a single instance of a service can be active at a time.The Singleton Service pattern is also known as the HA Singleton in the clustered JBoss EAP.

Context and problem

We have seen how the Service Instance pattern can help us improve a service’s performance and availability. The system has increased throughput and availability by running multiple instances of the same service and having a request dispatcher capable of load balancing the requests to all available service instances. The system’s availability also increases because if an instance of the service becomes unavailable, the request dispatcher detects this and forwards future requests to running instances. There are certain use cases where only one instance of a service is allowed to run at a time. For example, if we have a scheduled job (a trigger-based service) and run multiple instances of the same job, every instance would trigger at the scheduled intervals rather than having only one trigger fired as expected by the business.

Another example would be if the service performs polling on certain resources (a file system or database), and we want to ensure that only a single instance, and maybe even a single thread performs the polling and processing. In all these plus similar situations, we need some control over how many instances (usually, only one) of a particular service are active at a time, regardless of how many instances have been started. The Service Instance pattern still can be useful to achieve high availability in these cases, but there is an extra capacity needed to ensure that only one instance of a service is active at a given moment. ...