OTP as the Core of Your Application Part 2
The post implements the actor-model parts of a book store application in Elixir. It adds a Registry to map book UUIDs to process IDs, a DynamicSupervisor to start and track book processes, and a GenServer per book to keep state in memory while persisting changes after each update. It also wires Phoenix routes and context functions so the API can use either the database or the actor-based implementation.
It then hydrates application state on startup by loading all books from the database and starting one process per book. A stress test script sends mixed list, read, and order requests, and the post reports lower latency and flatter response times for the actor-based version, while noting that the approach fits independent resources better than tightly coupled or query-heavy workloads.
Reading notes#
- Uses a local Registry to look up a book process by book ID and drop stale entries when a process terminates
- Uses a DynamicSupervisor to start one
BookProcessper book and to restart transient children when needed - Stores each book as GenServer state so reads and writes can happen serially and atomically per book
- Uses
handle_continue/2to persist state changes to the database after replying to the caller - Adds Phoenix routes for listing books, showing one book, and ordering a copy
- Provides parallel
Bookscontext functions for actor-based calls and for database-only calls - Hydrates the application at startup by loading all books and starting their processes before the endpoint comes up
- Uses a uniform stress test over book IDs to compare the two implementations under load
- Reports faster average response times and lower variance for the actor-model version than for the database-centered version
- Notes that the approach works best when actors are independent and the database is still better for complex queries and tightly coupled transactions
