Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

Laravel Concurrency: Atomicity, Transactions, and Locking

Two customers try to purchase a product at exactly the same time. If the application isn't designed for concurrency, both requests might read stock = 1 and both complete the purchase. This is where atomicity, transactions, and locking become important. 1. Atomicity Atomicity is one of the four ACID properties: Atomicity Consistency Isolation Durability Atomicity means that a group of database…

Two customers attempting to purchase the same product simultaneously can result in both requests reading the same stock value of 1 and both completing the purchase, leading to an incorrect outcome. This issue highlights the importance of atomicity, transactions, and locking in application design.

Atomicity ensures that a group of database operations are treated as a single indivisible unit. Either all operations succeed, or all operations are rolled back. In the example provided, creating an order and a payment together can be made atomic using a transaction.

Transactions in Laravel can be created using the DB::transaction() method. If all database operations within a transaction succeed, a COMMIT is issued, finalizing the changes. However, if an exception occurs, a ROLLBACK is executed, undoing the changes.

Race conditions occur when multiple requests access shared data concurrently, and the result depends on the timing of those operations. For instance, two requests might both read the stock value as 1, leading to incorrect behavior.

To prevent race conditions, Laravel offers two types of locking: pessimistic and optimistic. Pessimistic locking assumes that conflicts are likely to happen and locks the data before working with it, using lockForUpdate() or sharedLock() methods. LockForUpdate() is commonly used when a row needs to be read and modified, while sharedLock() is used when a protected read of data is needed.

Optimistic locking, on the other hand, assumes that conflicts are relatively rare and doesn't lock the row. Instead, it checks if the data has been modified since it was read. This approach uses a version column to detect changes made by other requests.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

MCP Servers for Figma, Jira, GitLab and Playwright

People typing "figma mcp server" or "jira mcp server" into search are usually after the same thing: a connection between a tool they use every day and the AI assistant they already work in.

  • MCP servers connect AI assistants with data sources like Figma, Jira, GitLab, and Playwright.
  • Figma offers local and remote MCP server options, with remote recommended for data protection.
  • GitLab's MCP server is built-in since version 18.3, requiring GitLab Duo and access to MCP.

Cross-Region S3 Replication Without the Gotchas (2026)

Cross-Region S3 Replication Without the Gotchas (2026) Cross-region S3 replication copies objects from a source bucket to a destination in another region, continuously.

  • Cross-region S3 replication copies objects asynchronously, leading to eventual consistency.
  • Versioning must be enabled on both source and destination buckets for replication to function.

More from Monday 17 August →