While I was designing a movie ticket booking system as response to interview question ,…
- User
While I was designing a movie ticket booking system as response to interview question , I proposed two microservices - ticket-Waiting-Service (responsible for blocking the seats till payment is done ) and ticket-booking -service (responsible for booking ) with their individual DBs .
DB is mysql with master slave architecture . master node for write requests
I have kept showId+seatNumber as primary key in waiting table and booking tables.
Now each micorservice is deployed at multiple nodes.
Assuming the scenario that for a superhit movie there can be more than 100 concurrent requests trying to book same seat then 1 will get preference and others will fail with DuplicateKeyException. On the same lines there can be more concurrent requests ,
for example for seat A-112 - got 100 requests - 1 got through 99 failed for seat B-112 - got 100 requests - 1 got through 99 failed for seat C-112 - got 100 requests - 1 got through 99 failed for seat D-112 - got 100 requests - 1 got through 99 failed for seat E-112 - got 100 requests - 1 got through 99 failed .
so at a given DB write node - got 500 requests out of which 5 succeeded and rest 495 resulted in DuplicateKeyException. and this can happen a lot of times.
Can this design make DB to crash ?Share Prompt
- Assistant
Yes, this design has the potential to cause a database crash due to the high volume of concurrent requests resulting in DuplicateKeyExceptions. When the number of concurrent requests is high, it can lead to contention issues in the database, which can cause locks to be held for a long time, ultimately leading to a database crash.
Additionally, having both the ticket-Waiting-Service and ticket-booking-service deployed at multiple nodes can further increase the load on the database, which can make the contention issues worse.
To prevent database crashes, you may consider implementing optimistic concurrency control or some other form of locking mechanism to ensure that only one request can modify a particular record at a time. This would help to avoid contention issues and prevent DuplicateKeyExceptions from occurring. You may also consider using a load balancer to distribute the load across multiple database nodes to reduce the load on individual nodes.
- User
How to prevent database crashes using the AWS services for the condition explained aboveShare Prompt
- Assistant
To prevent database crashes when designing a movie ticket booking system on AWS, you can use various services and features provided by AWS. Here are some recommendations:
- Amazon RDS Multi-AZ: Amazon RDS Multi-AZ provides a high availability feature that automatically creates a synchronous standby replica of your primary database instance in a different Availability Zone (AZ). This feature ensures that the standby replica can take over automatically in case of a failure of the primary instance, minimizing downtime and ensuring high availability.
- User
using aws services like rds how To prevent database crashes, you may consider implementing optimistic concurrency control or some other form of locking mechanism to ensure that only one request can modify a particular record at a time. This would help to avoid contention issues and prevent DuplicateKeyExceptions from occurring. You may also consider using a load balancer to distribute the load across multiple database nodes to reduce the load on individual nodes.Share Prompt
- Assistant
Yes, you can implement optimistic concurrency control or locking mechanisms in combination with AWS services like RDS to prevent database crashes due to contention issues and prevent DuplicateKeyExceptions from occurring. Here are some ways to implement these mechanisms:
- Optimistic Concurrency Control: Optimistic concurrency control involves checking for conflicts before committing changes to the database. In the context of a movie ticket booking system, you can implement optimistic concurrency control by including a version number or timestamp column in your database table. Each time a user attempts to book a ticket, the service checks the version number or timestamp and ensures that it matches the version number or timestamp of the seat the user is trying to book. If there is a conflict, the booking fails, and the user is asked to try again. You can use RDS with optimistic concurrency control by enabling versioning on your tables.
- User
Explain the locking mechanism for Oracle, MySQL,PostgreSQL,RDS with SQL code: Locking Mechanisms: Locking mechanisms involve acquiring locks on records before modifying them to prevent other users from modifying the same records simultaneously. In the context of a movie ticket booking system, you can implement locking mechanisms by acquiring locks on the records of seats that a user is trying to book. You can use RDS with locking mechanisms by using the FOR UPDATE clause in your SQL statements.Share Prompt
- Assistant
Sure, here's an explanation of locking mechanisms for Oracle, MySQL, PostgreSQL, and RDS with SQL code:
- Oracle Locking Mechanism:
In Oracle, you can use the SELECT...FOR UPDATE statement to lock records for update. When a record is locked for update, no other user can modify it until the lock is released. Here's an example SQL code:
sqlSELECT * FROM seats WHERE seat_number = 'A-112' FOR UPDATE;In this example, we are acquiring a lock on the record of seat A-112 before modifying it. Other users attempting to book the same seat will have to wait until the lock is released.
- User
How to design by taking into account Note that locking mechanisms can impact the performance of your database, particularly in high-traffic scenarios. You should carefully consider your locking strategy and use it judiciously to prevent contention issues and ensure data consistency.Share Prompt
- Assistant
Here are some best practices for designing a database system that takes into account the potential impact of locking mechanisms on performance:
- Use locking mechanisms judiciously: As noted, locking mechanisms can have a significant impact on database performance, particularly in high-traffic scenarios. To minimize this impact, you should use locking mechanisms only when necessary and release locks as soon as possible.
- Use optimistic concurrency control where possible: In some cases, optimistic concurrency control may be a better option than locking mechanisms. With optimistic concurrency control, the database checks for conflicts between transactions when they are committed, rather than acquiring locks beforehand. This can improve performance in scenarios where conflicts are rare.