Concurrency with Actors

Bilal Fazlani
May 8, 2020 · 5 min read

Introduction

The actor model of computing was introduced by Carl Hewitt in 1973. This model makes it easy to tackle the problem of concurrency and state management. The model treats an “actor” as a primitive unit of computing.

Actors are lightweight

Actors are lightweight abstractions on top of Threads. We all know that threads are expensive. It is not possible to create of millions of threads on a single machine with typical hardware configuration. This is why we need lightweight abstractions over threads. Actors are cheap and you can create millions of them. This opens up new possibilities, for example, if you have a million entities in your system, you can create an actor for each entity and that actor is responsible for managing operations with that entity. An actor is sleeping by default and only wakes up and consumes CPU when it get’s a message. So even if you spawn millions of actors, only the ones which are processing messages are awake and rest of them are sleeping.

Actor are asynchronous & non-blocking

Unlike the usual “method invocation” interaction familiar to us, communication among actors can only be message based. Message based communication is asynchronous. When actor A sends a message to actor B, execution continues immediately after sending the message without waiting for any feedback or acknowledgement. Any acknowledgement or data from actor B will have to come as a message via mailbox. Meanwhile, Actor A is either sleeping or processing other messages. Asynchronous communication is the key to building highly performant systems. Since Actor A is efficient in its resource usage, it can very well sustain high amounts of load.

Actors allow thread safety

Thread safety for actor state is achieved by two properties of actors:

  • An actor can process only one message at a time

Only one message at time… but concurrently

This was a tricky one and took me a while to understand fully. When you hear “one message at a time” for the first time, it’s easy to build a wrong mental model of actor execution flow.

Actors give you more control

The message passing communication of actors comes with its costs. It is definitely not as straight forwards as method invocation. More complexity means more effort and maintenance cost. What it offers in return is more control.

References

  1. Paper by Carl Hewitt
  2. Hewitt, Meijer and Szyperski: The Actor Model (everything you wanted to know…)
  3. Akka actors
  4. Erlang

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Bilal Fazlani

Written by

Scala | Akka | Functional Programming | Distributed Systems

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Bilal Fazlani

Written by

Scala | Akka | Functional Programming | Distributed Systems

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store