From "The Pragmatic Programmer"
🎧 Listen to Summary
Free 10-min PreviewDecoupled Concurrency with Actors and Blackboards
Key Insight
To avoid the complexities of shared mutable state, alternative concurrency models like Actors and Blackboards promote decoupled interactions. An actor is an independent, lightweight virtual processor with its own private local state and a dedicated mailbox. It processes messages one at a time to completion, and in response, can create new actors, send messages to other known actors, and update its internal state. Actors inherently execute concurrently and asynchronously, sharing no direct state. Messages are one-way; if a response is expected, the sender includes its own mailbox address for the reply. This model simplifies concurrency because the code remains consistent whether running on a single processor, multiple cores, or distributed networked machines.
The Actor model eliminates the need for explicit concurrency handling or rigid end-to-end procedural logic, as actors react autonomously based on received messages. For example, in a diner system implemented with actors, a Customer actor sends a 'hungry for pie' message to a Waiter actor, who then dispatches a 'get slice' message to a Pie Case actor. The Pie Case, maintaining its private count of pie slices, either sends a slice to the Customer and notifies the Waiter for billing, or informs the Waiter of unavailability, leading the Waiter to apologize to the Customer. The Erlang language is a prime example of an actor-based system, featuring lightweight processes that communicate via message passing, offer isolation, and include a robust supervision system for fault tolerance and 'hot-code loading,' contributing to its reputation for nine nines availability.
Blackboards represent another decoupled approach, functioning as a central, shared data store that allows independent 'agents' (processes or actors) to post facts, observations, and evidence, and retrieve information through pattern matching. This 'laissez faire' concurrency enables agents with diverse expertise to contribute and process data without explicit coordination, gradually working towards a solution. This model is particularly effective for complex, asynchronous workflows, such as mortgage application processing, where data arrives in an unpredictable order and facts trigger relevant rules. Modern messaging systems like Kafka and NATS, with features like persistence and pattern matching, can serve as blackboard-like platforms. While these decoupled models simplify concurrency, they introduce challenges such as increased difficulty in reasoning about indirect interactions, requiring centralized message format repositories, sophisticated tracing tools, and potentially more complex deployment and management due to a higher number of granular components.
📚 Continue Your Learning Journey — No Payment Required
Access the complete The Pragmatic Programmer summary with audio narration, key takeaways, and actionable insights from Andrew Hunt, David Thomas.