---
title: Building a model reservation system with atomic locks
url: "https://aaronfrancis.com/videos/building-a-model-reservation-system-with-atomic-locks"
path: "/videos/building-a-model-reservation-system-with-atomic-locks"
type: video
author: Aaron Francis
published: 2025-05-02
topic: "Laravel & PHP"
keywords: [atomic locks, reservation system, database generated columns, race conditions, polymorphic relationships, cache locks, laravel, lock key visibility, collision handling, query scopes, cache locks table, morph relationships]
youtube_url: "https://www.youtube.com/watch?v=czHUFCWVY0c"
chunked: true
full_bytes: 41530
section_count: 18
full_url: "/videos/building-a-model-reservation-system-with-atomic-locks/full.md"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Building a model reservation system with atomic locks

## Sections (40.6 KB full document — follow any link to read verbatim)

This page is long enough to hit agent truncation cliffs if fetched all at once. Pick the section matching your question below to read it 100% verbatim (< 35 KB each), or open the [full unsplit document (40.6 KB)](/videos/building-a-model-reservation-system-with-atomic-locks/full.md).

### 1. What is an atomic lock? (00:00–02:18 · 2.4 KB)
Atomic locks solve coordination problems by preventing multiple worker processes from claiming the same resource; a single process reserves a lock while others are blocked out.
Keywords: `atomic locks`, `atomic`, `lock`, `import`, `four`, `process`, `case scenario`, `four times`
[Read section 1 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/1-what-is-an-atomic-lock.md)

### 2. Two real-world locking scenarios (02:19–03:12 · 1.0 KB)
Real-world use cases include preventing duplicate CSV imports and enforcing wait intervals (e.g., syncing external providers every 4+ hours) without adding multiple timestamp columns.
Keywords: `two real-world`, `real-world locking`, `locking scenarios`, `real-world`, `locking`, `scenarios`, `four hours`, `action`
[Read section 2 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/2-two-real-world-locking-scenarios.md)

### 3. Desired API design using reservation scopes (03:13–04:53 · 1.6 KB)
Desired end-state API uses query scopes like `unreservedFor('syncing')`, `reserveFor('syncing', duration)`, and `limit(10)` to elegantly filter and lock models at the database level.
Keywords: `desired api`, `api design`, `reservation scopes`, `desired`, `design`, `api`, `reservation`, `scopes`
[Read section 3 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/3-desired-api-design-using-reservation-scopes.md)

### 4. Basic cache lock implementation (04:53–07:06 · 2.2 KB)
Laravel's `Cache::lock()` creates atomic locks in the database with a 5-second default expiry; the `cache_locks` table uses a unique key column to enforce atomicity.
Keywords: `cache lock`, `lock`, `cache`, `basic cache`, `lock implementation`, `basic`, `implementation`, `five seconds`
[Read section 4 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/4-basic-cache-lock-implementation.md)

### 5. Improving lock key visibility in database (07:06–08:53 · 1.7 KB)
Removing the cache key prefix makes lock keys more readable in the database without affecting functionality when no other apps write to the same cache store.
Keywords: `key`, `database`, `improving lock`, `lock key`, `key visibility`, `lock`, `improving`, `visibility`
[Read section 5 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/5-improving-lock-key-visibility-in-database.md)

### 6. Moving toward database query integration (08:54–11:09 · 2.1 KB)
A `reservable` trait adds `reserveFor()` scope using wishful thinking and polymorphic relationships to query models without existing reservations under a specific key.
Keywords: `query`, `moving toward`, `toward database`, `database query`, `query integration`, `moving`, `toward`, `database`
[Read section 6 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/6-moving-toward-database-query-integration.md)

### 7. Setting up polymorphic relationships (11:10–12:27 · 1.1 KB)
Polymorphic relationships allow many different models (users, imports, files, podcasts) to be reserved by reusing Laravel's `cache_locks` table with a custom `CacheLock` model.
Keywords: `polymorphic relationships`, `polymorphic`, `relationships`, `different models`, `cache locks`, `locks table`, `table`, `models`
[Read section 7 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/7-setting-up-polymorphic-relationships.md)

### 8. Reusing Laravel’s cache_locks table (12:28–15:11 · 2.4 KB)
A `reservations()` morph-many relationship on models queries `cache_locks` filtered by future expiration timestamps, avoiding reimplementation of atomic locks.
Keywords: `laravel`, `reusing laravel`, `reusing`, `table`, `cache lock`, `morph map`, `expiration`, `morph`
[Read section 8 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/8-reusing-laravels-cache-locks-table.md)

### 9. Generating reservation keys programmatically (15:11–19:02 · 3.4 KB)
Morph maps in `AppServiceProvider` map model classes to small integers (1=User, 2=Podcast) to generate compact keys like `1:1:sync` in the database.
Keywords: `keys`, `generating reservation`, `reservation keys`, `keys programmatically`, `reservation`, `generating`, `programmatically`, `morph map`
[Read section 9 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/9-generating-reservation-keys-programmatically.md)

### 10. Using generated columns to parse reservation keys (19:03–22:04 · 3.0 KB)
Generated columns in PostgreSQL, MySQL, and SQLite automatically parse the cache lock key into `model_type`, `model_id`, and `type` without manual code management.
Keywords: `generated columns`, `columns`, `generated`, `parse reservation`, `reservation keys`, `sqlite`, `parse`, `reservation`
[Read section 10 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/10-using-generated-columns-to-parse-reservation-key.md)

### 11. Verifying morph relationship and query behavior (22:04–25:19 · 2.9 KB)
Generated columns are read-only and database-enforced; the morph relationship correctly maps `model_type` and `model_id` columns to query reserved models.
Keywords: `morph`, `verifying morph`, `morph relationship`, `query behavior`, `query`, `verifying`, `relationship`, `behavior`
[Read section 11 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/11-verifying-morph-relationship-and-query-behavior.md)

### 12. Handling timing and collision issues (25:20–29:08 · 3.8 KB)
An `is_reservation` Boolean generated column checks if the key starts with `reservation:` to prevent collisions, and uses CASE/regex to safely extract model_id and type despite colons in keys.
Keywords: `handling timing`, `collision issues`, `timing`, `collision`, `issues`, `key part`, `big int`, `reservation`
[Read section 12 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/12-handling-timing-and-collision-issues.md)

### 13. Making key parsing more robust (29:08–32:16 · 2.9 KB)
Regex-based extraction replaces the reservation prefix and model identifiers to safely isolate the key type (e.g., 'sync:payments'), handling multiple colons correctly.
Keywords: `key`, `making key`, `key parsing`, `robust`, `parsing`, `sync payments`, `reservation`, `sync`
[Read section 13 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/13-making-key-parsing-more-robust.md)

### 14. Filtering out expired or invalid locks (32:16–34:20 · 1.8 KB)
An `unreservedFor()` scope filters by `is_reservation=true` and future expiration to identify available models, with an after-query callback to solve race conditions.
Keywords: `locks`, `invalid locks`, `filtering`, `expired`, `invalid`, `go`, `cache locks`, `go ahead`
[Read section 14 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/14-filtering-out-expired-or-invalid-locks.md)

### 15. Implementing unreserved and reserveFor scopes (34:20–38:04 · 3.2 KB)
The `unreservedFor()` scope returns available models, and `reserveFor()` calls a `reserve()` method on each model to atomically lock them, filtering out failures from race conditions.
Keywords: `reservefor`, `unreserved`, `implementing unreserved`, `reservefor scopes`, `afterquery`, `scopes`, `syncing`, `reserve`
[Read section 15 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/15-implementing-unreserved-and-reservefor-scopes.md)

### 16. Building a reserve() method on the model (38:05–40:06 · 1.8 KB)
A `reserve()` method generates a reservation key using morph class and model ID, acquires the cache lock, and returns a filtered collection of successfully locked models.
Keywords: `reserve`, `model`, `method`, `reservation`, `lock`, `filter`, `cache lock`, `key`
[Read section 16 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/16-building-a-reserve-method-on-the-model.md)

### 17. Live testing reservation behavior (40:07–41:41 · 1.2 KB)
Live demo shows reserving 10 users for 24 hours by purpose ('syncing' vs 'update payment info'), verifying that subsequent queries return no available users until locks expire.
Keywords: `live testing`, `testing reservation`, `reservation behavior`, `testing`, `reservation`, `behavior`, `payment info`, `add`
[Read section 17 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/17-live-testing-reservation-behavior.md)

### 18. Wrapping up: robust, multi-database reservation system (41:41–43:36 · 2.1 KB)
Generated columns on Laravel's cache_locks table create a cross-database reservation system (SQLite, MySQL, Postgres, MariaDB) with no timing issues, using atomic locks for race-condition safety.
Keywords: `multi-database reservation`, `reservation system`, `reservation`, `wrapping`, `robust`, `multi-database`, `system`, `database`
[Read section 18 verbatim](/videos/building-a-model-reservation-system-with-atomic-locks/sections/18-wrapping-up-robust-multi-database-reservation.md)

---

## Related on aaronfrancis.com

- [Laravel's atomic locks](/videos/laravels-atomic-locks) *(Video · 2023-11-10 · shared: atomic locks, laravel, race conditions)*
- [About](/about) *(Page · 2026-11-25 · shared: laravel, postgres, sqlite)*
- [Why Most Developers Overcomplicate Everything w/ Aaron Francis](/podcast/why-most-developers-overcomplicate-everything-w-aaron-francis-855db0fe) *(Podcast · 2025-03-20 · shared: laravel, sqlite, postgres)*
- [Aaron Francis: Building and Teaching Cool Things](/podcast/aaron-francis-building-and-teaching-cool-things-39697cbb) *(Podcast · 2025-03-11 · shared: laravel, sqlite, postgres)*
- [Aaron Francis is putting PHP in Your JS Files](/podcast/aaron-francis-is-putting-php-in-your-js-files-6f3117a7) *(Podcast · 2025-03-05 · shared: laravel, postgres, sqlite)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.
- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `atomic locks` ([A-B](/llms/keywords/a-b.txt)), `reservation system` ([Q-R](/llms/keywords/q-r.txt)), `database generated columns` ([D-E](/llms/keywords/d-e.txt)), `polymorphic relationships` ([O-P](/llms/keywords/o-p.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
