yiisoft / yiisoft/db

Retry DB transaction on deadlock in multi-master replication

Open
#82 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
216
Forks
51
Avg merge
3d 10h
Merged PRs (30d)
2

Description

Feature request for discussion.

When the DB server uses multi-master replication, conflict can occur when clients of different replication masters write to the same row.

In the case of Galera replication (Dealing with Multi-Master Conflicts):

When two transactions come into conflict, the later of the two is rolled back by the cluster. The client application registers this rollback as a deadlock error. Ideally, the client application should retry the deadlocked transaction, but not all client applications have this logic built in.

If this is implemented it should be above the PDO layer but below the APIs that the application uses to access the DB. Hence I wonder if Yii might usefully provide this feature. I guess yii\db\Command::queryInternal() is roughly where this might happen.

If this were implemented it would

  • be optional and turned off by default
  • catch deadlock error exceptions from PDO
  • retry a deadlocked transaction a configurable number of times, with the default number of times being, say, 2
  • throw the caught exception if the number of retries is exhausted
  • possibly wait a small time before retrying, I am not sure if this is useful

I imagine it would be fairly easy to implement but I it might be quite hard to properly test.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with yii\db\Command::queryInternal() and review how PDO deadlock exceptions are currently surfaced. Define the optional, disabled-by-default retry behavior, configurable retry count, exhausted-retry exception handling, and a test strategy for multi-master deadlock scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.