The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Class::TLB - Transparent load balancing for any resource class.


Version 0.01


    my $tlb = Class::TLB->new() ;

build a set of resource (dummy for instance) and register them

    foreach my $i ( 1 .. 3 ){
      $tlb->tlb_register(Class::TLB::Dummy->new($i)) ;

You can now use the object $tlb the same way you would use a single instance of resource.

Example with instances of Class::TLB::Dummy:

    # doSomething, oneFail and doFail are implemented in the Dummy class.
    $tlb->doSomething() ;

The $tlb object will automatically balance the usage on the set of resources given and will avoid temporary resource failures:

    $tlb->oneFail() ; # This call is ok because only one resource will fail.

    $tlb->doFail()  ; # This call will confess an error because there is an 
                      # implementation error in the resource that makes it fail all the time.

Usage scenario:

You can use a Class::TLB wrapper to balance the usage of a set of similar distant resources.

In case the distant connection breaks in one of them, your client code will not suffer from it since Class::TLB will avoid single resources failures.

For this to work, your resource must die or confess in case of disconnection.

In case there is a logical flaw in a resource method, Class::TLB will die with the error when you call it.

Because Class::TLB will attempt to use each resource instance and fail if all of them are failing.


Fail, but fail fast

If your resources represent a distant service accessed through the network, make sure that the connection failure dies quickly.

Long connection timeouts can cause waiting queries to accumulate in your application and can lead to an interruption of service, even if the other resources of the pool are perfectly healthy.

In particular, if your resources use cURL to connect to the distant service, make sure you set a short CURLOPT_CONNECTTIMEOUT (or CURLOPT_CONNECTTIMEOUT_MS) option.


Your managed resources can not implement any of the methods implemented in Class::TLB.

All Class::TLB methods are prefixed with 'tlb_', making a collision very unlikely.




Overrides the UNIVERSAL::isa method to allow client code to transparently call isa method on balanced resources.

Usage: if ( $this->isa('Class::TLB::Dummy')){ ... }


Overrides the UNIVERSAL::can method to allow client code to transparently call can method on balanced resources.

Usage: if ( $this->can('doSomething')){ ... }


Returns the class of resources being load balanced.

usage: my $class = $tlb->tlb_class() ;


Returns an instance of resources being load balanced.


Returns the usage statistic hash of all sources.

usage: my $hcount = $tlb->tlb_usecount() ;


Registers a new resource to be managed by this load balancer.

The first call of this methods records the expected resource class. Subsequent calls will fail if the given resource is from a different class.

Usage: $tlb->tlb_register($resource);


Jerome Eteve, <jerome at>


Please report any bugs or feature requests to bug-class-tlb at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc Class::TLB

You can also look for information at:



Copyright 2010 Jerome Eteve, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.