{"id":468,"date":"2024-11-29T20:35:17","date_gmt":"2024-11-29T19:35:17","guid":{"rendered":"https:\/\/www.systemdeveloper.nl\/tech\/?p=468"},"modified":"2024-11-29T20:35:18","modified_gmt":"2024-11-29T19:35:18","slug":"efficient-database-operations-in-a-galera-cluster-with-php","status":"publish","type":"post","link":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/","title":{"rendered":"Efficient Database Operations in a Galera Cluster with PHP"},"content":{"rendered":"<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">Working with distributed systems like a Galera cluster introduces unique challenges when managing database operations. Deadlocks, serialization issues, and transaction conflicts can easily disrupt your application&rsquo;s performance if not handled properly. Recently, I implemented a robust PHP class to handle CRUD (Create, Read, Update, Delete) operations efficiently in such an environment. Here&rsquo;s how it was designed and why it&rsquo;s worth considering for your own projects.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"the-challenge\">The Challenge<\/h2>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">In a Galera cluster, database transactions must be designed to handle:<\/p>\n\n\n<ol class=\"wp-block-list wp-block-list\">\n<li>\n<strong>Deadlocks<\/strong>: <br>Common in highly concurrent environments, especially when multiple nodes try to access or modify the same data.<\/li>\n\n\n\n<li>\n<strong>Serialization Failures<\/strong>: <br>Occur when transactions conflict due to simultaneous updates.<\/li>\n\n\n\n<li>\n<strong>Retry Logic<\/strong>: <br>Essential for handling conflicts without user disruption.<\/li>\n\n\n\n<li>\n<strong>Distributed Locking<\/strong>: <br>Ensures data consistency across all cluster nodes.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">Without careful planning, these issues can result in performance bottlenecks, inconsistent data, or even application downtime.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"the-solution\">The Solution<\/h2>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">To address these challenges, I built a PHP class to:<\/p>\n\n\n<ol class=\"wp-block-list wp-block-list\">\n<li><strong>Use transactions and locking effectively.<\/strong><\/li>\n\n\n\n<li><strong>Implement automatic retry mechanisms for deadlocks.<\/strong><\/li>\n\n\n\n<li><strong>Support logging for debugging and insights.<\/strong><\/li>\n\n\n\n<li><strong>Read configuration, including custom ports (e.g. Galera Load Balancer), from an&nbsp;<code>.ini<\/code>&nbsp;file.<\/strong><\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">This solution ensures database operations are reliable, even in a distributed environment.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"design-overview\">Design Overview<\/h2>\n\n\n<h3 class=\"wp-block-heading\" id=\"key-features\">Key Features<\/h3>\n\n\n<ol class=\"wp-block-list\">\n<li>\n<strong>CRUD Methods<\/strong>:<ul class=\"wp-block-list wp-block-list wp-block-list\">\n<li>\n<code>sqlRetrieve<\/code>: Fetch records, returning an array of results or an empty array if none are found.<\/li>\n\n\n\n<li>\n<code>sqlUpdate<\/code>: Update records, returning the number of affected rows.<\/li>\n\n\n\n<li>\n<code>sqlDelete<\/code>: Delete records, returning the number of rows deleted.<\/li>\n\n\n\n<li>\n<code>sqlInsert<\/code>: Insert records, returning the ID of the new record.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\n<strong>Transaction Management<\/strong>:<ul class=\"wp-block-list wp-block-list\">\n<li>Begin, commit, and roll back transactions automatically.<\/li>\n\n\n\n<li>Retry operations when deadlocks or serialization issues occur.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\n<strong>Logging<\/strong>:<ul class=\"wp-block-list wp-block-list\">\n<li>Logs messages at different levels (<code>debug<\/code>,&nbsp;<code>info<\/code>,&nbsp;<code>warning<\/code>,&nbsp;<code>error<\/code>) for debugging and monitoring.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\n<strong>Configuration from&nbsp;<code>.ini<\/code>&nbsp;File<\/strong>:<ul class=\"wp-block-list wp-block-list\">\n<li>Database credentials, including custom ports, are read dynamically.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-php-class\">The PHP Class<\/h3>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">Here&rsquo;s the complete implementation of the class:<\/p>\n\n\n<pre class=\"wp-block-code\"><div class=\"copy-to-clipboard\">\n<span>Copied!<\/span><button class=\"click-to-copy-button\" title=\"Copy to clipboard\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewbox=\"0 0 32 32\" stroke=\"currentcolor\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" width=\"24\" height=\"24\" fill=\"none\">\n  <path d=\"M12.9975 10.7499L11.7475 10.7499C10.6429 10.7499 9.74747 11.6453 9.74747 12.7499L9.74747 21.2499C9.74747 22.3544 10.6429 23.2499 11.7475 23.2499L20.2475 23.2499C21.352 23.2499 22.2475 22.3544 22.2475 21.2499L22.2475 12.7499C22.2475 11.6453 21.352 10.7499 20.2475 10.7499L18.9975 10.7499Z\"><\/path>\n  <path d=\"M17.9975 12.2499L13.9975 12.2499C13.4452 12.2499 12.9975 11.8022 12.9975 11.2499L12.9975 9.74988C12.9975 9.19759 13.4452 8.74988 13.9975 8.74988L17.9975 8.74988C18.5498 8.74988 18.9975 9.19759 18.9975 9.74988L18.9975 11.2499C18.9975 11.8022 18.5498 12.2499 17.9975 12.2499Z\"><\/path>\n  <path d=\"M13.7475 16.2499L18.2475 16.2499\"><\/path>\n  <path d=\"M13.7475 19.2499L18.2475 19.2499\"><\/path>\n<\/svg><\/button><textarea><?php class MySQLDatabase {\n    private $pdo;\n    private $maxRetries = 3;\n\n    public function __construct($configFile) {\n        $this->loadConfig($configFile);\n    }\n\n    private function loadConfig($configFile) {\n        if (!file_exists($configFile)) {\n            $this-&gt;log('error', \"Configuration file not found: $configFile\");\n            throw new Exception(\"Configuration file not found: $configFile\");\n        }\n\n        $config = parse_ini_file($configFile);\n        if (!$config || !isset($config['host'], $config['port'], $config['dbname'], $config['user'], $config['password'])) {\n            $this-&gt;log('error', \"Invalid configuration file: $configFile\");\n            throw new Exception(\"Invalid configuration file: $configFile\");\n        }\n\n        try {\n            $dsn = \"mysql:host={$config['host']};port={$config['port']};dbname={$config['dbname']};charset=utf8mb4\";\n            $this-&gt;pdo = new PDO($dsn, $config['user'], $config['password']);\n            $this-&gt;pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n        } catch (PDOException $e) {\n            $this-&gt;log('error', \"Database connection failed: \" . $e-&gt;getMessage());\n            throw new Exception(\"Database connection failed: \" . $e-&gt;getMessage());\n        }\n\n        $this-&gt;log('info', \"Database connection established.\");\n    }\n\n    public function sqlRetrieve($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $stmt-&gt;fetchAll(PDO::FETCH_ASSOC) ?: [];\n        });\n    }\n\n    public function sqlUpdate($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $stmt-&gt;rowCount();\n        });\n    }\n\n    public function sqlDelete($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $stmt-&gt;rowCount();\n        });\n    }\n\n    public function sqlInsert($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $this-&gt;pdo-&gt;lastInsertId();\n        });\n    }\n\n    private function executeQueryWithRetry($callback) {\n        $retries = 0;\n        while ($retries maxRetries) {\n            try {\n                $this-&gt;pdo-&gt;beginTransaction();\n                $result = $callback();\n                $this-&gt;pdo-&gt;commit();\n                return $result;\n            } catch (PDOException $e) {\n                $this-&gt;pdo-&gt;rollBack();\n                if ($e-&gt;getCode() == '40001') { \/\/ Deadlock\n                    $this-&gt;log('warning', \"Deadlock detected. Retrying... (Attempt \" . ($retries + 1) . \")\");\n                    $retries++;\n                    usleep(100000); \/\/ Wait 100ms before retry\n                } else {\n                    $this-&gt;log('error', \"Query failed: \" . $e-&gt;getMessage());\n                    throw $e;\n                }\n            }\n        }\n        throw new Exception(\"Query failed after maximum retries.\");\n    }\n\n    private function log($level, $message) {\n        $timestamp = date('Y-m-d H:i:s');\n        echo \"[$timestamp] [$level] $message\" . PHP_EOL;\n    }\n}<\/textarea>\n<\/div><code>&lt;?php\n\nclass MySQLDatabase {\n    private $pdo;\n    private $maxRetries = 3;\n\n    public function __construct($configFile) {\n        $this-&gt;loadConfig($configFile);\n    }\n\n    private function loadConfig($configFile) {\n        if (!file_exists($configFile)) {\n            $this-&gt;log('error', \"Configuration file not found: $configFile\");\n            throw new Exception(\"Configuration file not found: $configFile\");\n        }\n\n        $config = parse_ini_file($configFile);\n        if (!$config || !isset($config['host'], $config['port'], $config['dbname'], $config['user'], $config['password'])) {\n            $this-&gt;log('error', \"Invalid configuration file: $configFile\");\n            throw new Exception(\"Invalid configuration file: $configFile\");\n        }\n\n        try {\n            $dsn = \"mysql:host={$config['host']};port={$config['port']};dbname={$config['dbname']};charset=utf8mb4\";\n            $this-&gt;pdo = new PDO($dsn, $config['user'], $config['password']);\n            $this-&gt;pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n        } catch (PDOException $e) {\n            $this-&gt;log('error', \"Database connection failed: \" . $e-&gt;getMessage());\n            throw new Exception(\"Database connection failed: \" . $e-&gt;getMessage());\n        }\n\n        $this-&gt;log('info', \"Database connection established.\");\n    }\n\n    public function sqlRetrieve($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $stmt-&gt;fetchAll(PDO::FETCH_ASSOC) ?: [];\n        });\n    }\n\n    public function sqlUpdate($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $stmt-&gt;rowCount();\n        });\n    }\n\n    public function sqlDelete($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $stmt-&gt;rowCount();\n        });\n    }\n\n    public function sqlInsert($sql, $params = []) {\n        return $this-&gt;executeQueryWithRetry(function() use ($sql, $params) {\n            $stmt = $this-&gt;pdo-&gt;prepare($sql);\n            $stmt-&gt;execute($params);\n            return $this-&gt;pdo-&gt;lastInsertId();\n        });\n    }\n\n    private function executeQueryWithRetry($callback) {\n        $retries = 0;\n        while ($retries &lt; $this-&gt;maxRetries) {\n            try {\n                $this-&gt;pdo-&gt;beginTransaction();\n                $result = $callback();\n                $this-&gt;pdo-&gt;commit();\n                return $result;\n            } catch (PDOException $e) {\n                $this-&gt;pdo-&gt;rollBack();\n                if ($e-&gt;getCode() == '40001') { \/\/ Deadlock\n                    $this-&gt;log('warning', \"Deadlock detected. Retrying... (Attempt \" . ($retries + 1) . \")\");\n                    $retries++;\n                    usleep(100000); \/\/ Wait 100ms before retry\n                } else {\n                    $this-&gt;log('error', \"Query failed: \" . $e-&gt;getMessage());\n                    throw $e;\n                }\n            }\n        }\n        throw new Exception(\"Query failed after maximum retries.\");\n    }\n\n    private function log($level, $message) {\n        $timestamp = date('Y-m-d H:i:s');\n        echo \"[$timestamp] [$level] $message\" . PHP_EOL;\n    }\n}<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\"><strong>Example Use Case<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><div class=\"copy-to-clipboard\">\n<span>Copied!<\/span><button class=\"click-to-copy-button\" title=\"Copy to clipboard\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewbox=\"0 0 32 32\" stroke=\"currentcolor\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" width=\"24\" height=\"24\" fill=\"none\">\n  <path d=\"M12.9975 10.7499L11.7475 10.7499C10.6429 10.7499 9.74747 11.6453 9.74747 12.7499L9.74747 21.2499C9.74747 22.3544 10.6429 23.2499 11.7475 23.2499L20.2475 23.2499C21.352 23.2499 22.2475 22.3544 22.2475 21.2499L22.2475 12.7499C22.2475 11.6453 21.352 10.7499 20.2475 10.7499L18.9975 10.7499Z\"><\/path>\n  <path d=\"M17.9975 12.2499L13.9975 12.2499C13.4452 12.2499 12.9975 11.8022 12.9975 11.2499L12.9975 9.74988C12.9975 9.19759 13.4452 8.74988 13.9975 8.74988L17.9975 8.74988C18.5498 8.74988 18.9975 9.19759 18.9975 9.74988L18.9975 11.2499C18.9975 11.8022 18.5498 12.2499 17.9975 12.2499Z\"><\/path>\n  <path d=\"M13.7475 16.2499L18.2475 16.2499\"><\/path>\n  <path d=\"M13.7475 19.2499L18.2475 19.2499\"><\/path>\n<\/svg><\/button><textarea>$db = new MySQLDatabase('database.ini');\n\n\/\/ Retrieve records\n$records = $db-&gt;sqlRetrieve(\"SELECT * FROM users WHERE active = :active\", ['active' =&gt; 1]);\n\n\/\/ Insert a new record\n$newId = $db-&gt;sqlInsert(\"INSERT INTO users (name, email) VALUES (:name, :email)\", [\n    'name' =&gt; 'John Doe',\n    'email' =&gt; 'john.doe@example.com'\n]);\n\n\/\/ Update records\n$updatedRows = $db-&gt;sqlUpdate(\"UPDATE users SET active = :active WHERE id = :id\", [\n    'active' =&gt; 0,\n    'id' =&gt; $newId\n]);\n\n\/\/ Delete records\n$deletedRows = $db-&gt;sqlDelete(\"DELETE FROM users WHERE id = :id\", ['id' =&gt; $newId]);<\/textarea>\n<\/div><code>$db = new MySQLDatabase('database.ini');\n\n\/\/ Retrieve records\n$records = $db-&gt;sqlRetrieve(\"SELECT * FROM users WHERE active = :active\", ['active' =&gt; 1]);\n\n\/\/ Insert a new record\n$newId = $db-&gt;sqlInsert(\"INSERT INTO users (name, email) VALUES (:name, :email)\", [\n    'name' =&gt; 'John Doe',\n    'email' =&gt; 'john.doe@example.com'\n]);\n\n\/\/ Update records\n$updatedRows = $db-&gt;sqlUpdate(\"UPDATE users SET active = :active WHERE id = :id\", [\n    'active' =&gt; 0,\n    'id' =&gt; $newId\n]);\n\n\/\/ Delete records\n$deletedRows = $db-&gt;sqlDelete(\"DELETE FROM users WHERE id = :id\", ['id' =&gt; $newId]);<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"lessons-learned\">Lessons Learned<\/h2>\n\n\n<ol class=\"wp-block-list wp-block-list\">\n<li>\n<strong>Retry Mechanisms Are Essential<\/strong>: <br>Deadlocks are inevitable in distributed systems, and retrying intelligently is the best way to handle them.<\/li>\n\n\n\n<li>\n<strong>Use Explicit Transactions<\/strong>: <br>Explicit transactions give more control over data consistency and reduce the likelihood of conflicts.<\/li>\n\n\n\n<li>\n<strong>Logging Matters<\/strong>: <br>Proper logging is invaluable for debugging and optimizing database operations.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n<p class=\"wp-block-paragraph wp-block-paragraph\" style=\"\">This PHP class provides a robust solution for handling database operations in Galera clusters. By combining retry logic, transactions, and clear logging, it ensures reliability and ease of maintenance. If you&#8217;re working in a similar environment, consider adopting or adapting this approach for your projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with distributed systems like a Galera cluster introduces unique challenges when managing database operations. Deadlocks, serialization issues, and transaction conflicts can easily disrupt your application&rsquo;s performance if not handled properly. Recently, I implemented a robust PHP class to handle CRUD (Create, Read, Update, Delete) operations efficiently in such an environment. Here&rsquo;s how it was &hellip;<\/p>\n","protected":false},"author":1,"featured_media":469,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,52],"tags":[26,53,54],"class_list":["post-468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howto","category-programming","tag-galera","tag-php","tag-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Efficient Database Operations in a Galera Cluster with PHP - SystemDeveloper.NL<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Efficient Database Operations in a Galera Cluster with PHP - SystemDeveloper.NL\" \/>\n<meta property=\"og:description\" content=\"Working with distributed systems like a Galera cluster introduces unique challenges when managing database operations. Deadlocks, serialization issues, and transaction conflicts can easily disrupt your application&rsquo;s performance if not handled properly. Recently, I implemented a robust PHP class to handle CRUD (Create, Read, Update, Delete) operations efficiently in such an environment. Here&rsquo;s how it was &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/\" \/>\n<meta property=\"og:site_name\" content=\"SystemDeveloper.NL\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/quan.tora.16\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-29T19:35:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-29T19:35:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"John Timmer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Timmer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/\"},\"author\":{\"name\":\"John Timmer\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#\\\/schema\\\/person\\\/5760c2ed5300c56d8ef01dfb00a9763b\"},\"headline\":\"Efficient Database Operations in a Galera Cluster with PHP\",\"datePublished\":\"2024-11-29T19:35:17+00:00\",\"dateModified\":\"2024-11-29T19:35:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/\"},\"wordCount\":402,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp\",\"keywords\":[\"Galera\",\"PHP\",\"Programming\"],\"articleSection\":[\"Howto\",\"Programming\"],\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/\",\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/\",\"name\":\"Efficient Database Operations in a Galera Cluster with PHP - SystemDeveloper.NL\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp\",\"datePublished\":\"2024-11-29T19:35:17+00:00\",\"dateModified\":\"2024-11-29T19:35:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp\",\"contentUrl\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp\",\"width\":900,\"height\":675},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/efficient-database-operations-in-a-galera-cluster-with-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Efficient Database Operations in a Galera Cluster with PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#website\",\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/\",\"name\":\"www.systemdeveloper.nl\",\"description\":\"NextGen IT\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#organization\",\"name\":\"www.systemdeveloper.nl\",\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/qt-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/qt-logo.png\",\"width\":1346,\"height\":1230,\"caption\":\"www.systemdeveloper.nl\"},\"image\":{\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/quan.tora.16\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/#\\\/schema\\\/person\\\/5760c2ed5300c56d8ef01dfb00a9763b\",\"name\":\"John Timmer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/cropped-D6E27035-6864-4270-8D72-0D8C0C59F370-96x96.jpeg\",\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/cropped-D6E27035-6864-4270-8D72-0D8C0C59F370-96x96.jpeg\",\"contentUrl\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/cropped-D6E27035-6864-4270-8D72-0D8C0C59F370-96x96.jpeg\",\"caption\":\"John Timmer\"},\"sameAs\":[\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\"],\"url\":\"https:\\\/\\\/www.systemdeveloper.nl\\\/tech\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Efficient Database Operations in a Galera Cluster with PHP - SystemDeveloper.NL","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/","og_locale":"en_US","og_type":"article","og_title":"Efficient Database Operations in a Galera Cluster with PHP - SystemDeveloper.NL","og_description":"Working with distributed systems like a Galera cluster introduces unique challenges when managing database operations. Deadlocks, serialization issues, and transaction conflicts can easily disrupt your application&rsquo;s performance if not handled properly. Recently, I implemented a robust PHP class to handle CRUD (Create, Read, Update, Delete) operations efficiently in such an environment. Here&rsquo;s how it was &hellip;","og_url":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/","og_site_name":"SystemDeveloper.NL","article_publisher":"https:\/\/www.facebook.com\/quan.tora.16","article_published_time":"2024-11-29T19:35:17+00:00","article_modified_time":"2024-11-29T19:35:18+00:00","og_image":[{"width":900,"height":675,"url":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp","type":"image\/webp"}],"author":"John Timmer","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Timmer","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#article","isPartOf":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/"},"author":{"name":"John Timmer","@id":"https:\/\/www.systemdeveloper.nl\/tech\/#\/schema\/person\/5760c2ed5300c56d8ef01dfb00a9763b"},"headline":"Efficient Database Operations in a Galera Cluster with PHP","datePublished":"2024-11-29T19:35:17+00:00","dateModified":"2024-11-29T19:35:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/"},"wordCount":402,"commentCount":0,"publisher":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/#organization"},"image":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp","keywords":["Galera","PHP","Programming"],"articleSection":["Howto","Programming"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/","url":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/","name":"Efficient Database Operations in a Galera Cluster with PHP - SystemDeveloper.NL","isPartOf":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#primaryimage"},"image":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp","datePublished":"2024-11-29T19:35:17+00:00","dateModified":"2024-11-29T19:35:18+00:00","breadcrumb":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#primaryimage","url":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp","contentUrl":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/d121994e-4690-42ac-85de-3c1007321ece-e1732908902436.webp","width":900,"height":675},{"@type":"BreadcrumbList","@id":"https:\/\/www.systemdeveloper.nl\/tech\/efficient-database-operations-in-a-galera-cluster-with-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.systemdeveloper.nl\/tech\/"},{"@type":"ListItem","position":2,"name":"Efficient Database Operations in a Galera Cluster with PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.systemdeveloper.nl\/tech\/#website","url":"https:\/\/www.systemdeveloper.nl\/tech\/","name":"www.systemdeveloper.nl","description":"NextGen IT","publisher":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.systemdeveloper.nl\/tech\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/www.systemdeveloper.nl\/tech\/#organization","name":"www.systemdeveloper.nl","url":"https:\/\/www.systemdeveloper.nl\/tech\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/www.systemdeveloper.nl\/tech\/#\/schema\/logo\/image\/","url":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/qt-logo.png","contentUrl":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/qt-logo.png","width":1346,"height":1230,"caption":"www.systemdeveloper.nl"},"image":{"@id":"https:\/\/www.systemdeveloper.nl\/tech\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/quan.tora.16"]},{"@type":"Person","@id":"https:\/\/www.systemdeveloper.nl\/tech\/#\/schema\/person\/5760c2ed5300c56d8ef01dfb00a9763b","name":"John Timmer","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/cropped-D6E27035-6864-4270-8D72-0D8C0C59F370-96x96.jpeg","url":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/cropped-D6E27035-6864-4270-8D72-0D8C0C59F370-96x96.jpeg","contentUrl":"https:\/\/www.systemdeveloper.nl\/tech\/wp-content\/uploads\/2024\/11\/cropped-D6E27035-6864-4270-8D72-0D8C0C59F370-96x96.jpeg","caption":"John Timmer"},"sameAs":["https:\/\/www.systemdeveloper.nl\/tech"],"url":"https:\/\/www.systemdeveloper.nl\/tech\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/posts\/468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":1,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":470,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/posts\/468\/revisions\/470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/media\/469"}],"wp:attachment":[{"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.systemdeveloper.nl\/tech\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}