Preventing Duplicate Requests in Laravel: The AtomicLockMiddleware

How to Handle Multiple Request Collisions and Keep Your Laravel App Running Smoothly

Hafiq Iqmal
6 min readOct 21, 2024

If you ever developed a web app in Laravel, you must have faced one such problem which is rather common: handling of duplicate requests currently being processed. It’s that trivial problem, but it may lead to data inconsistency, a failed transaction, or-what’s worse-a poor user experience. Imagine your application handling some form submission twice. You’d end up with duplicate records or maybe even duplicated payments, which would be a huge headache both for you and your users.

For this, we need to ensure each request should be processed one at a time, especially when the action is sensitive, such as making any payment or updating something. One of the solutions is by using an atomic lock. In this post, I am going to walk you through how I implemented a simple a middleware in Laravel to handle such scenarios. This middleware will allow processing of only one request from any particular user at one time for specific actions.

Let’s take a closer look into how it works and why you might want to use it in your projects.

Why Should You Care About Duplicate Requests?

--

--

Hafiq Iqmal
Hafiq Iqmal

Written by Hafiq Iqmal

Technical Lead | Software Engineer | Laravel Enthusiasts | Tech writer | UiTM Alumni | Husband | Proud father of a beautiful daughter

Responses (2)