How to prevent race conditions in a reservation system

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Learning ACID rules for storing anything in a databases is very crucial, same with understanding and creating database tables using 3rd normal form

xeliqa
Автор

Great video, glad you mentioned isolation levels and versioning as a possible solution.

Joshua-ycei
Автор

This would help me a lot in my side project I mentioned in your last video . Thank you so much

ardianhotii
Автор

@WebDevCody relying on the time passed from the app might cause a double reservation 'once in a million, i.e. next tuesday' due to clock skew between the servers. Those race conditions would also typically occur when you have high traffic spike and therefore there's a high chance your servers and network will be overloaded and then it could in theory take more than 5 seconds between 'now = new Date' and the final update statement

tajkris
Автор

You should be able to create some kind of database constraint that backs up this code and makes it impossible to have multiple people booking the same seat at the same time. A unique index across multiple columns may do the trick. depending on your schema.

btrewern
Автор

You can have race conditions on the frontend. I wrote a test script that worked very fast. Add two products to the cart. But the cart only showed one.

TauvicRitter
Автор

Depending on what DB you are using I think you can go with repeatable read instead of serializable as you arw not doing a range query and you probably should not worry about phantom reads. I think this way the code will be cleaner and shorter. The issue with optimistic concurrency control is that in the short period between checking if somebody has modified the record and actually updating it, there can be an update which is missed

kirilmilanov
Автор

Can this example be applied if two people are trying to buy the same product assuming there is only one quantity of the product in the inventory.

dinosouravtv
Автор

I am not sure I understand why you need a transaction here. As you pointed out the part that prevent multiple updates is the where clause. Correct me if I am wrong but a transaction is useful when you try to make multiple queries and revert the whole thing if one of them fails.

I guess to make sure a seat is reserved only once, you could use the seatId as a unique value for a row in some seat_reservation table. Once the reserveration expires you could use a TRIGGER to delete that row. Again not sure that is best practice either.

Elvis-is-king-ls
Автор

One thing I would like to see is if we want to check reservation history by user or by seat. I would like to see that from tables perspective.

uzair
Автор

what vs code theme are you using? I really loved it.

prajapatinayan
Автор

Nice video. Just curious why you mentioned versioning separetly from your current implementation. The latter seems like versioning using time. Maybe because the prior is more general solution?

Sgene
Автор

Why do you need the `lte()` comparison at line 33? You are doing this comparison at the line 23. I thought that if you encapsulated your whole code into a DB transaction, the line 23 is enough because the DB would block somehow tables during the transaction. Or just the condition on line 23 is redundant because the line 33 deals with the same condition?

ak-otwn
Автор

I like how you use the lock to query the entity. Another idea is successive posts to acquire a lock with the same user will increase the timeout, but that depends on use case.

andrewwashburn
Автор

What's the purpose of the if clause at 23-25 since you're already throwing an error for future expirestAt via the where clause? Is this just an early return to prevent unnecessary db queries?

matthewcullum
Автор

Ive never been able to understand the concept of isolation level, especially when there is a lock! 5:06

mehdi-vlnn
Автор

What are the pros/cons of doing it this way, instead of setting it up via a queue of some kinda in say AWS?

PraiseYeezus
Автор

wooww i Just facing the same issue, i was using knexjs and forUpdate with retry on deadlock response

hariyadi
Автор

I learned a lot from your video, so thanks for doing it. You asked for feedback and one thing that struck me is that it looks like you're using exceptions for control flow which is generally considered an antipattern. Use exceptions for things that you absolutely can't handle. Exceptions are a bit too much like 'goto' statements and have the same drawbacks.

fiskebent
Автор

As someone who rarely codes (by following a tutorial) this title was wild.😂

bluemuffincap