Connecting to child site that is load balanced on multiple web servers

I manage several WordPress websites including some that are very high traffic websites. These high traffic websites are hosted on multiple web servers to load balance the traffic (web servers point to the same database). When someone visits the URL, the load balancer directs the traffic to one of the web nodes to reduce the load on a single server.

Now when I connect the child site using the URL, a random node gets connected to the MainWP dashboard and only that node receives updates, but the other nodes don’t.

Is there a way to connect to the individual web server nodes separately (using IP or some other method) so I can manage all web servers nodes for a single WordPress website from the MainWP dashboard?

Update: I was able to add unique URLs to reach the site on each web servers, like:
server1.website.com
server2.website.com
server3.website.com

Now the problem is adding them as unique sites in MainWP Dashboard since the web servers all point to the same Database.

Adding the 1st URL goes through normally. On adding the 2nd URL, I get the following error:

Error detected: This site already contains a link. Please deactivate and reactivate the MainWP plugin.
Hint: Go to the child site, deactivate and reactivate the MainWP Child plugin and try again.

I get that because the connection was completed in the DB by the 1st web server URL, and closed once it connected. Since the web servers share the DB it’s not open for more connections.

Is there a way around it, to let other web servers of the same site connect to the same source MainWP dashboard?

@bogdan @bojan Tagging MainWP staffers for visibility --I hope that is okay (If not I apologize and can take it down)

@Neo this is a different setup than I have seen before, I’ll run it by the devs in the morning, they have left for the day.

@dennis, Thank you for looking into it!

Hi @Neo,

I just checked this interesting case.

In case you connect the site as a single instance, actions you take in your Dashboard reflect only to one file system depending on the load balancer’s direction.

In case you try to connect each file system and as a separate site in your Dashboard, only the first one gets connected because once the first one is connected and public & private OpenSSL keypair is set, DB is locked for further connections (this one of the MainWP security measures that ensures that one child site can be connected only to one MainWP Dashboard at the same time).

For this kind of setup, we do not have out of box solution, but this surely is an interesting case that we have our dev team looking into and looking for a solution. For a start, they are working on a hook that should help.

As soon as I get more info from them, I will update you.

Thanks

Hi @Neo, I just got an update from our dev team and I am passing it to you.

They added a new hook to the system which can help you connect site(s).

Along with this version of the MainWP Dashboard, you should use this snippet in the Custom Dashboard extension, in PHP Section.

add_filter( 'mainwp_get_balance_site_connection_info', 'mycustom_mainwp_get_balance_site_connection_info', 10, 1 );
function mycustom_mainwp_get_balance_site_connection_info( $url ) {
    $balance_urls = array(
        'https://site1.com/',
        'https://site2.com/',
        'https://site3.com/',
    );
    if ( in_array( $url, $balance_urls ) ) {
        $one_added_url = 'https://addedsite.com/';
        $result        = \MainWP\Dashboard\MainWP_DB::instance()->get_websites_by_url( $one_added_url );
        if ( $result ) {
            $site = current( $result );
            return array(
                'pubkey'   => $site->pubkey,
                'privkey'  => $site->privkey,
                'uniqueId' => $site->uniqueId,
            );
        }
    }
    return false;
}

Please remember to change the URLs in the snippet that we used as examples.

If you file system instances can be reached as separate subdomains as you said:
server1.website.com
server2.website.com
server3.website.com

After you add the first one, you need to use that subdomain in the $one_added_url variable, the other subdomains, you need to list in the $balance_urls array, and after that try to connect them all.

MainWP was not designed for this type of set up and we have no way of testing it. Theoretically, this code snippet should allow things to work for you but again it is untested so be sure to test it and use it at your own risk.

@bogdan Thank you and the MainWP team for the swift response! I will test it out.

May I ask if the modified MainWP Dashboard version will be supported with updates? Or when MainWP comes out with a new version and if I update this version will get overwritten?

Thanks for that question, @Neo.

To be clear, our dev team created this custom solution specifically for you. Since this is a first-of-its-kind request, we’re not yet committing to integrating it into our core product or providing ongoing support.

Here’s our current stance:

  1. We need to ensure it works flawlessly in your setup.
  2. We’ll need time to better test it on our end.
  3. Your feedback will be crucial in determining next steps.

For now, please go ahead and install it. We’re eager to hear how it performs for you. Your experience will help guide our decisions about potential future support and updates.

Keep in mind, if you update to a new MainWP version, it might overwrite these custom modifications. We’ll cross that bridge when we come to it.

Let us know how it goes!

@dennis Understood, thank you for that clarification.

I removed existing entry for load balanced site and started fresh. I am getting the same error as before. Here are the steps I followed:

  1. Disabled MainWP plugin: Renamed standard mainwp to mainwp_disabled
  2. Added modified MainWP plugin: Copied over mainwp to /plugins/mainwp. Made sure this version is enabled in Plugins page
  3. Added Custom Dashboard extension
  4. Added PHP snippet provided. Here is what my PHP snippet looks like (providing a redacted version of URLs here)
add_filter( 'mainwp_get_balance_site_connection_info', 'mycustom_mainwp_get_balance_site_connection_info', 10, 1 );
function mycustom_mainwp_get_balance_site_connection_info( $url ) {
    $balance_urls = array(
        'https://server2.website.com/',
        'https://server3.website.com/',
    );
    if ( in_array( $url, $balance_urls ) ) {
        $one_added_url = 'https://server1.website.com/';
        $result        = \MainWP\Dashboard\MainWP_DB::instance()->get_websites_by_url( $one_added_url );
        if ( $result ) {
            $site = current( $result );
            return array(
                'pubkey'   => $site->pubkey,
                'privkey'  => $site->privkey,
                'uniqueId' => $site->uniqueId,
            );
        }
    }
    return false;
}
  1. Enabled MainWP child plugin on child site www.website.com (gets disabled when I removed site from MainWP dashboard)
  2. Added https://server1.website.com/ successfully
  3. Tried adding https://server2.website.com/ on MainWP dashboard, but getting same error as before:
    Error detected: This site already contains a link. Please deactivate and reactivate the MainWP plugin.
    Hint: Go to the child site, deactivate and reactivate the MainWP Child plugin and try again.

Hi @Neo,

The steps you took look good.
Our dev team will check this again and I will let you know when I get some update from them.

Thanks

1 Like

@Neo, our dev team sent me a new update, please try this version
https://www.dropbox.com/scl/fi/sdoz0hjahl95w4uqleedj/mainwp.zip?rlkey=dudacdf00wabv4r4afvpx2izt&st=bw9764dr&dl=0
with the same code snippet

1 Like

Thanks @bogdan. I replaced the mainwp plugin with the updated version you provided, it’s still giving the same error:

Error detected: This site already contains a link. Please deactivate and reactivate the MainWP plugin
Hint: Go to the child site, deactivate and reactivate the MainWP Child plugin and try again

Hi @Neo, thanks for letting me know.

Would you mind opening a helpdesk ticket or joining us on Discord so we can collect more info so our devs can continue?

Thanks!

Sounds good, will do. Is there a channel I should post on after joining Discord?

I have a question that popped into my head while reading this thread: Why does OP not simply declare an exception to his load balancer that maps the IP of the MainWP Dashboard to a specific target server permanently? Surely one of the servers is the file master and the other ones are synced to it? Seems a bit odd to have the folks here compensate for OP’s esoteric setup. I know, I know… I exaggerate ;D

Hi @Neo,

Thank you for your patience as our development team explored potential solutions.

After a thorough investigation and multiple attempts, we have determined that it is not possible to achieve the desired functionality with your current configuration at this time. The MainWP Plugin is not designed to support environments with a single database and multiple file systems distributed across different servers.

Additionally, we believe it would be inappropriate to pursue this further as we cannot be 100% certain of how it would function. Given the high-traffic nature of your website, we do not want to jeopardize its stability or performance.

We understand this might be disappointing news, and we regret any inconvenience this may cause.

1 Like

Welcome SVT!

I was wondering about this setup too. It seems like this setup would cause problems with every plugin, not just MainWP Child. Not to mention some other functions in WordPress like media uploads or auto-updates.

I certainly see the desirablility of using MainWP to help manage a load-balanced setup like this. I admit I have no experience using load balancing with WordPress, but my intuition is that in addition to using a single database I would want to use a shared filesystem. Or at least separate filesystems that are synced in some way. If using a shared filesystem, then it wouldn’t matter which node is connected with MainWP because they would all function the same. If the filesystems are synced, then the idea from @svt of having an exception for the MainWP Dashboard to always target the same “authoritative” node makes sense. And maybe a set of hooks to trigger a filesystem sync after MainWP child does any updates to the filesystem.

Just throwing some ideas out there. :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.