简体   繁体   English

使用Laravel处理预订系统中的并发

[英]Handling concurrency in a booking sistem with laravel

I'm developing a REST api in Laravel 5.2 with postgresql that handles bookings for diferent establishments that have an start_time and an end_time. 我正在使用Largre 5.2开发带有PostgreSQL的REST api,该api处理具有start_time和end_time的不同场所的预订。 The user can see in the frontend the diferents times available in a certain establishments and make a booking. 用户可以在前端看到某些场所的可用时间并进行预订。 Here is the problematic situation: 这是有问题的情况:

  1. Customer A wants to book an establishment. 客户A想要预订一个营业所。 Sees that the establishment A is free from 15:00 hs to 20:00 hs 看到场所A从15:00 hs到20:00 hs是免费的
  2. Customer A selects the 16:00 to 17:00 time. 客户A选择16:00到17:00时间。 (Have to press a button to confirm the booking) (必须按下按钮以确认预订)
  3. Customer B wants to book an establishment. 客户B想要预订一个营业所。 Sees that the establishment A is free from 15:00 hs to 20:00 看到场所A在15:00 hs至20:00之间免费
  4. Customer B selects the 16:00 to 17:00 apointment (Again, have to press a button to confirm the booking) 顾客B选择16:00到17:00的约会(再次,必须按下一个按钮以确认预订)
  5. Customer A confirms the booking 客户A确认预订
  6. If customer B confirms the booking, we will have 2 bookings at the same time. 如果客户B确认预订,我们将同时进行2个预订。

Have read a lot about queues in laravel and I'm not really sure if that is the correct way to solve this problem. 已经阅读了很多有关laravel中的队列的信息,但我不确定这是否是解决此问题的正确方法。

The establishments doesnt have arbitrary hours so the customers can pick up a 1 or 2 or 3 hours booking. 场所没有任意时间,因此客户可以选择1或2或3个小时的预订。

This is my booking model 这是我的预订模式

    Schema::create('bookings', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->timestamp('start_time');
        $table->timestamp('end_time');
        $table->integer('user_id')->nullable();
        $table->integer('establishment_id')->nullable();
        $table->string('name')->nullable();
        $table->string('phone_number')->nullable();

    });

Comments point to the fact that atomic data changes should be managed by the database. 评论指出原子数据更改应由数据库管理的事实。 There are a few tools to help with this in most modern relational databases, eg transactions, foreign keys, composite keys and constraints. 在大多数现代关系数据库中,有一些工具可以帮助解决此问题,例如事务,外键,组合键和约束。 Eloquent and the query builder both have some support for all the above. 雄辩的和查询生成器都对以上所有内容都提供了一些支持。

Using transactions you can lock a database table or tables whilst you read/write data. 使用事务,您可以在读取/写入数据时锁定一个或多个数据库表。 In the event of a failure all changes can be rolled back. 发生故障时,所有更改都可以回滚。 This means you can carry out multiple dependent operations/changes with a guarantee that they will fail/succeed together and that external data changes cannot occur during that transaction. 这意味着您可以执行多个相关的操作/更改,并确保它们一起失败/成功,并且在该事务期间不会发生外部数据更改。

Because you are not restricted to single hour bookings a simple composite key will not work but you can use transactions to query if a slot is available and then book the slot within the same transaction. 因为您不限于单小时预订,所以简单的组合键将不起作用,但是您可以使用事务查询空位是否可用,然后在同一笔交易中预订空位。 This will be compatible with multiple rdb implementations via laravel / qb. 这将通过laravel / qb与多个rdb实现兼容。

Another, possibly more performant approach would be to use composite keys with unique constraint and just book multiple 1 hour slots behind the scenes - this would restrict booking times to multiples of whatever booking unit you use, eg 1 hour units would not allow for a 1 1/2 hour booking, you would need 30 minute units 另一种可能是性能更高的方法是使用具有唯一约束的复合键,并且仅在后台预订多个1小时的时间段-这会将预订时间限制为您使用的任何预订单位的倍数,例如1小时单位不允许1 1/2小时预订,您需要30分钟

laravel transactions laravel交易

laravel create composite indexes laravel创建复合索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM