Skip to content

Clustering support for PHP-LRPM, including different sharding strategies

License

Notifications You must be signed in to change notification settings

vrza/php-lrpm-cluster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP-LRPM Clustering

Clustering support for PHP-LRPM, including different sharding strategies.

Usage

You should extend the abstract PHPLRPM\Cluster\ShardingConfigurationSource class and implement a nullary (zero-argument) constructor which will be initializing instances of PHPLRPM\ConfigurationSource, PHPLRPM\Cluster\Providers\ClusterConfigurationProvider and PHPLRPM\Cluster\Filters\ShardingConfigurationFilter:

namespace MyNamespace;
use PHPLRPM\Cluster\ShardingConfigurationSource;

class MyLRPMWorkerShardingConfiguration extends ShardingConfigurationSource
{
    public function __construct()
    {
        //instance of PHPLRPM\ConfigurationSource
        $this->confSource = new MyLRPMWorkerConfiguration()
        //instance of PHPLRPM\Cluster\Providers\ClusterConfigurationProvider
        $this->clusterConfProvider = new EnvironmentBasedClusterConfigurationProvider();
        //instance of PHPLRPM\Cluster\Filters\ShardingConfigurationFilter
        $this->configurationFilter = new BalancedShardingConfigurationFilter();
    }
}

Then you may provide MyLRPMWorkerShardingConfiguration as a regular ConfigurationSource for PHP-LRPM, e.g.

lrpm '\MyNamespace\MyLRPMWorkerShardingConfiguration'

Sharding Strategies

You can either write your own ShardingConfigurationFilter or use one of the built-in filters.

Balanced

Use the BalancedShardingConfigurationFilter when either:

  • scaling involves restarting the entire cluster

  • perfect load balancing is necessary

Pros Cons

Perfect load balancing

Potentially large number of disrupted processes when rescaling

Rendezvous Hashing

Use the RendezvousHashingShardingConfigurationFilter when both:

  • minimal worker process disruption is highly desirable

  • load balancing biased towards certain nodes is tolerable

Pros Cons

Minimal disruption when rescaling

Biased load balancing

Simple hashing

Use the HashingShardingConfigurationFilter to achieve:

  • decent (but not perfect) load balancing

  • some (but not complete) worker process disruption when cluster is rescaled

Pros Cons

Strikes a balance between load balancing and disruption

Guarantees neither perfect load balancing nor minimal disruption

Cluster Configuration and Providers

Cluster configuration parameters are

  • total number of nodes in the cluster

  • zero-based index of current node

You can either write your own ClusterConfigurationProvider or use one of the built-in providers.

Environment-based Cluster Configuration Provider

Use the EnvironmentBasedClusterConfigurationProvider to load cluster configuration from environment variables PHP_LRPM_NUMBER_OF_INSTANCES and PHP_LRPM_INSTANCE_NUMBER. Default variable names can be overridden.

File-based Cluster Configuration Provider

Use the FileBasedClusterConfigurationProvider to load cluster configuration from a JSON configuration file, e.g.:

{
    "numberOfInstances": 3,
    "instanceNumber" : 2
}

Installation

Assuming you have PHP Composer installed, and that the composer executable is in your $PATH:

composer require php-lrpm/php-lrpm-cluster

About

Clustering support for PHP-LRPM, including different sharding strategies

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages