MENU

Fun & Interesting

Building a model reservation system with atomic locks

Aaron Francis 8,138 4 days ago
Video Not Working? Fix It Now

We're using Laravel's atomic locks and database-generated columns to build a reliable reservation system—without race conditions or extra tables. I'll show you how to hook into Laravel’s cache lock system, add polymorphic relationships, and use generated columns to power powerful query scopes. Atomic Locks Video: https://youtu.be/jGb5zIgwL4c Follow me: Twitter: https://twitter.com/aarondfrancis LinkedIn: https://www.linkedin.com/in/aarondfrancis Website: https://aaronfrancis.com - find articles, podcasts, courses, and more. Our courses: High Performance SQLite: https://highperformancesqlite.com Mastering Postgres: https://masteringpostgres.com High Leverage Rails: https://highleveragerails.com Screencasting: https://screencasting.com Chapters: 00:00 - What is an atomic lock? 02:00 - Two real-world locking scenarios 03:10 - Desired API design using reservation scopes 04:44 - Basic cache lock implementation 06:54 - Improving lock key visibility in database 08:34 - Moving toward database query integration 11:05 - Setting up polymorphic relationships 12:13 - Reusing Laravel’s cache_locks table 15:00 - Generating reservation keys programmatically 19:00 - Using generated columns to parse reservation keys 22:00 - Verifying morph relationship and query behavior 25:00 - Handling timing and collision issues 29:00 - Making key parsing more robust 32:00 - Filtering out expired or invalid locks 34:00 - Implementing unreserved and reserveFor scopes 38:00 - Building a reserve() method on the model 40:00 - Live testing reservation behavior 41:30 - Wrapping up: robust, multi-database reservation system

Comment