Background

Implementing Multi Writer Replication has always been a challenge, before AWS we tried many tools and configurations to support multiple masters and slaves, however, we end update two to 3 days after that replication has crashed and data is becoming inconsistent.

Introduction:

Amazon Aurora is one of the nicest features AWS has, it has incredibly increased the performance of MySQL, also it reduces the management overhead, not to mention Aurora serverless which is an amazing auto-scaled service which can adapt automatically to your load.

One of the recent features introduced by Aurora is the Multi-Master feature, previously there was only one master where your application and read and write into, however that was always the bottleneck to have one writer, with Multi-Master, you have the ability to have multiple writers.

 

Aurora Multi-Master:

This feature has become available in August 2019 despite the fact it was announced in 2017, it took around 2 years to make it real, so we can see how complex is this feature.

In this article, we won’t talk much about how Aurora Multi-Master works, but more on how to integrate your application with, especially Laravel and Magento, if you’re interested in getting more details about Aurora we recommend you to check this article from AWS blog.

 

Application High availability with Aurora Multi-Master:

One of the considerations is to make sure that your platform or application supports multiple readers and writers. and having the ability to distribute the read/write operations to different targets, in addition, to be able to monitor and detect failed writes and exclude them from the distribution of the connections

.

 

 

 

 

 

 

 

Implement Laravel with Aurora Multi-Master:

In your database.php file, you can assign multiple readers and writers in Laravel, this can be achieved by adjusting the file with the below structure

 

'mysql' => [
    'read' => [
        'host' => [
            'aurora-reader-1',
            'aurora-reader-2',
        ],
    ],
    'write' => [
        'host' => [
            'aurora-writer-1',
            'aurora-writer-2',
         ],
    ],
    'sticky'    => true,
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
],

 

Integrate Magento 2 with Aurora Multi-Master:

Magento 2 Commerce has Multi-master support natively you can archive that by:

  1. Installing Magento Commerce with a single master database (named magento)
  2. Creating two additional master databases for checkout and OMS (named magento_quote and magento_sales)
  3. Configuring Magento Commerce to use the checkout and sales databases

for more details on how to implement it, you can refer to this article

 

for Magento 2 Community Version, you can specify custom connection for a specific module, for example, in your .env you can add a new connection


'db' => array (
..
'custom' => array (
'host' => 'localhost',
'dbname' => 'custom_database',
'username' => 'custom_user',
'password' => 'custom_password',
'engine' => 'innodb',
'initStatements' => 'SET NAMES utf8;',
'active' => '1',
),
),
),
'resource' => array (
'default_setup' => array (
'connection' => 'default',
),
'custom' => array(
'connection' => 'custom'
)
),
)
then configure resource model to use the new connection using di.xml the configuration sets a new resource name to the resource model.

<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Pronko\CustomModule\Model\ResourceModel\Article">
        <arguments>
            <argument name="connectionName" xsi:type="string">custom_setup</argument>
        </arguments>
    </type>
</config>
and then in your resource code

public function getConnectionName($resourceName)
{
    $connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION;
 
    $resourceName = preg_replace("/_setup$/", '', $resourceName);
    // more code ...
}

one of the cool extensions for Magento 2 is MySQL Cluster, however it does not seems to be maintained, we’d love to see a similar module for the community version.

 

Conclusion:

with Multi-Master setup we can finally get rid the horizontal scaling limitation we always suffer from with relational database such as MySQL, Thanks for AWS to bring this 🙂

 

 

Related Post

Leave a Comment

We are a Professional AWS Managed company of experienced talented engineers. We are top skilled in AWS Architecture, DevOps, Monitoring and Security Solutions.

AWS PS Copyright © 2019 Designed by Laraship