PoolByte

Database Transactions and Concurrent Updates

· PoolByte Technology

Database Transactions and Concurrent Updates

If an order record is created but its rows are not, or a user action leaves two separate records conflicting, the problem is not always a slow query. Sometimes the application processes changes that should be completed together as separate jobs. Database transactions, i.e. transaction structure, are used to define this consistency boundary.

The business rule determines the transaction boundary

An invoice draft and its items can be parts of the same job. Either the required records must be completed together, or an incomplete draft must not become available. MySQL COMMIT and ROLLBACK document describes the behavior for committing and rolling back changes. The suitability of the storage engine and the commands used for this behavior is also checked.

Initiating a transaction does not resolve all errors. The local database cannot undo the operation performed by an external service. For example, the limit for sending to the document service and local record updating is different. The application should track these two outcomes with separate cases; The uncertainty of one should not be covered up by the appearance of success of the other.

If two users change the same record

One team member might edit the project budget while the other keeps the same old screen open. A version field or appropriate locking approach is required if the last writer is not to silently overwrite the other change. With optimistic concurrency control, the application may ask for reevaluation by checking whether the record has changed since the user read it.

Keep the scope short when row locking is required. InnoDB locking read description shows the difference between a normal read and a locking read. Waiting for a response from the user or making a long network request within a locked process may cause other processes to wait. It should be clear what the design protects and for how long.

How the application handles deadlocks

A mutual wait can occur when two processes lock resources in different orders. Accessing resources in consistent order and keeping transactions short reduces this possibility. However, the application may need to recognize the relevant database error and retry within safe limits. Repeating every mistake indefinitely is not a solution.

Consider the external effects of the job being retried. Two notifications may be sent to the user or the same external request may occur again, even though there is only one record in the database. Tracking local changes and external delivery of work separately makes it clear what to repeat in case of errors. Simply writing “fail” in the log is insufficient for review; job ID and state transition are more useful.

A real test of consistency

  • Examine whether an error occurs in the middle of the job and any partial records remain.
  • Update the same record simultaneously with two separate connections.
  • Verify lock wait and retry limit.
  • Check that the readable result after successful operation meets the business rule.
  • Check that the local record remains in a clear state when the external service is not responding.

Index optimization can help improve the time; The transaction design determines the conditions under which the records will be considered correct. In order for an application to be not only fast but also consistent in simultaneous use, two issues should be evaluated separately.