Тёмный

Laravel: Avoid Race Conditions with Atomic Locks in Cache 

Laravel Daily
Подписаться 144 тыс.
Просмотров 16 тыс.
50% 1

Example of when/how race conditions happen and one way to avoid them.
Official Docs on Atomic Locks: laravel.com/do...
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.c...
- Laravel QuickAdminPanel: quickadminpane...
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-...
- My personal Twitter: / povilaskorop

Опубликовано:

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 48   
@stojankukrika7242
@stojankukrika7242 Год назад
Atomic Locks saves me a lot of nerves :) I have an issue charging more than once the same invoice because have more than one instance on AWS and a loot of invoices. One cron job didn't finish and the second one triggered and at the same time, they charge the same invoice, then I find that out in the documentation and now all works awesome!
@vancemicrod9305
@vancemicrod9305 Год назад
I am so amazed by how many useful features Laravel supports out of the box without being overloaded
@EliasGikonyoMogul
@EliasGikonyoMogul Год назад
The beauty of Korop's videos is they bring to light topics that people generally don't know about; the use of examples is what adds the sugar 👏🏾
@GC_WK2
@GC_WK2 Год назад
I think much better way is to use lockForUpdate() when selecting rows inside transaction. Second and other request can't select this rows while first transaction is't done. As I see default wait time is 50sec - then exception "General error: 1205 Lock wait timeout exceeded;". Be sure you are using lockForUpdate only inside transaction - other way it will no take effect.
@GC_WK2
@GC_WK2 Год назад
so you need to select some Hotel etc which would be booked and may be affected by separate request - this will totally prevent race condition
@h0ph1p13
@h0ph1p13 7 месяцев назад
Cool feature. I needed to implement this feature in an enterprise system that does not use Laravel and I went to read the Laravel source code. That helped a lot to implement it in my environment.
@p30mehrdad
@p30mehrdad Год назад
excellent way to show usage of this code. thanks
@mainagmuriithi2772
@mainagmuriithi2772 Год назад
Thanks for this. Super helpful when handling transactions
@bowiemtl
@bowiemtl Год назад
Cool! My only concern is once again relying on seconds as a parameter for the action to be considered complete. It works however and I’m sure there is a way around that. Thanks for introducing the concept to us!
@GMP93
@GMP93 Год назад
You can use $lock->release() to release it immediately. Relying on seconds just make sure the lock wont last forever in case some error.
@bowiemtl
@bowiemtl Год назад
@@GMP93 That's true
@Frank-ou2nr
@Frank-ou2nr Год назад
This doesn't seem like a great solution to me, as you need to specify the amount of seconds that the lock remains active. What would happen if for some reason you system is slower and a transaction exceeds for example the 10 seconds? Then you have the same issue as before. Could happen if you have a sudden influx of users that make your system slower then normal. What would be a more durable way to use this? Maybe setting the wait time higher? I believe the lock also releases when the operation is completed, but not sure.
@Xewl
@Xewl Год назад
This is where the queue comes in.
@usagiyojimbo7475
@usagiyojimbo7475 Год назад
I thought the same. The question is would be better if we will use uniqe index in database to f.ex. prevent register the same events comes from identical parallel actions of app?
@GMP93
@GMP93 Год назад
You can use $lock->release() to release the lock immediately. About transactions exceeding 10 seconds i really dont know a solution, maybe locking it for the time of the request timeout just to be sure? If the first user has an exception, you can use $lock->release() too, so you ensure that another user can acquire the lock immediately even there is some error, having the lock time just to make sure the lock wont last forever
@LuffyX5
@LuffyX5 Год назад
I was thinking about this topic last week because i faced the same thing on project. Thanks 🙏
@Gabriel-iz4kp
@Gabriel-iz4kp Год назад
Thanks for the great example and tips. Just thinking about the lock principles. But wouldn't the shown lock example always lock any booking operation regardless it's the same booking operation? Would it now make more sense to have a qualified name for the lock to booking-by-{booking.date} to allow other booking operations to bypass the locking if there's no conflict potential (at least in case one can pretend if there may be a conflict)?
@LaravelDaily
@LaravelDaily Год назад
Yes cache names can be customized like you're saying
@samirapadidar1687
@samirapadidar1687 Год назад
As always, it's great!!!!!!!!!!
@darwincabarrubias1368
@darwincabarrubias1368 Год назад
apparently i did the same thing as you did but i cant seem to reproduce the issue before using the atomic lock
@hermanzun
@hermanzun 11 месяцев назад
me neither, i tried with sync jobs and database jobs, adding sleep() instead of the job dispatch but no luck, can you share the code @LaravelDaily, thx!
@jihadabdulrazaq338
@jihadabdulrazaq338 8 месяцев назад
Me neither, I could not reproduce it, did you find anything ?
@kenjohnsiosan9707
@kenjohnsiosan9707 Год назад
Very helpful.🙂
@andywong2244
@andywong2244 Год назад
wow didnt know about atomic locks until now! thanks Povilas!
@MrError-ml3lz
@MrError-ml3lz Год назад
This code will stop all bookings even if it is in another date until the booking is done ? Or similar dates only ?
@LaravelDaily
@LaravelDaily Год назад
In this case, it will stop ALL bookings. But you can add different caching names to customize it and lock only by some group.
@MrError-ml3lz
@MrError-ml3lz Год назад
@@LaravelDaily Thank you so much
@section31
@section31 Год назад
Yeah would probably but the data that needs to be unique has part of the atomic lock name as a checksum or something is my guess
@jihadabdulrazaq338
@jihadabdulrazaq338 8 месяцев назад
I can't produce the same result if lock has not used, how come the execution will wait for the job result ! (even it's inside a database transaction)? Am I missing something ?
@raj-kal
@raj-kal Год назад
Thanks for the information
@Niboros
@Niboros Год назад
Does an atomic lock need a duration time? In this case I would naively think user2 just needs to wait until user1 is done, no mater the duration of the process. Also, in your example you have your validation in the controller. I would like to place my validation in a validation request class. How would I do that while still keeping the lock code? Thanks for the example.
@indrapyakurel3717
@indrapyakurel3717 Год назад
The official document says "If the lock is not available at the moment you request it, you may instruct Laravel to wait for a specified number of seconds. If the lock can not be acquired within the specified time limit, an Illuminate\Contracts\Cache\LockTimeoutException will be thrown:"
@RafaleJCW
@RafaleJCW Год назад
What happen if you put 20 seconds on your sleep function? You will have 2 successfull booking right ?
@LaravelDaily
@LaravelDaily Год назад
In this case, yes. Your Cache period should be appropriate to how much time the operations actually take. Also some of those could be put into the queue, like sending emails or forming the PDF.
@selzwawy4181
@selzwawy4181 Год назад
Thank you so much, i liked it 😃
@baratahmed1491
@baratahmed1491 10 месяцев назад
Thank you so much.
@kennedymwenda3357
@kennedymwenda3357 4 месяца назад
Can the same be achieved with database locks?
@fdelval2
@fdelval2 Год назад
Great!
@emekatimothyiloba699
@emekatimothyiloba699 Год назад
Thanks
@gazorbpazorbian
@gazorbpazorbian Год назад
I love you sooo much
@lloricode
@lloricode Год назад
what about using form request?
@jamshidbekyuldoshev7542
@jamshidbekyuldoshev7542 Год назад
interesting question @LaravelDaily
@user-xe7gv2nf5n
@user-xe7gv2nf5n Год назад
I searched for a full year to know how to render (no internet) page, like youtube does, if a user enter youtube page with no internet, it will render no internet page, how to do it in laravel? is there any idea
@user-xe7gv2nf5n
@user-xe7gv2nf5n Год назад
render custom offline page
@GC_WK2
@GC_WK2 Год назад
you need to know about "service workers" they can work offline
@vancemicrod9305
@vancemicrod9305 Год назад
Service workers and PWA (progressive web app) is what you're looking for
@user-xe7gv2nf5n
@user-xe7gv2nf5n Год назад
@@vancemicrod9305 thanks a lot ❤️
Далее
Laravel solved race conditions
14:13
Просмотров 15 тыс.
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel: Create Public API with Cache and Rate Limits
12:18
Cache Eloquent Query Results to Load Pages Instantly
5:43
Why Don't We Have A Laravel For JavaScript?
12:36
Просмотров 98 тыс.
Laravel + Livewire todo app (and so much more)
16:41
Просмотров 42 тыс.