Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

Explain the actor model. How would you implement a simple actor in plain Java, and what do frameworks such as Akka or Apache Pekko add?

An actor is an object that owns private state and a mailbox and processes its messages one at a time, so its state needs no locks; actors interact only by sending each other messages. In plain Java, an actor can be a class with a BlockingQueue mailbox drained by one dedicated thread, a virtual thread, or a single-threaded executor. Frameworks such as Akka and Apache Pekko add supervision hierarchies for failure handling, location transparency, clustering, sharding and persistence; Akka moved to a source-available licence in 2022, and Pekko is the Apache-licensed fork. The costs are harder debugging of message flows and extra patterns for request-reply.

Why this question is asked

Locks are the default tool for shared state in Java, and they become hard to get right as soon as several pieces of state and several threads are involved. The actor model takes a different route: it removes sharing instead of guarding it. It powers systems from telecom switches to game servers and trading platforms, and interviewers ask about it to see whether we understand the model itself, can build a small version without a framework, and know what the frameworks really add. In this answer, we will recap the model, build a simple actor in plain Java, add request-reply, look at what Akka and Pekko provide, and weigh the costs.

1A Quick Recap of the Actor Model

The actor model treats an actor as the unit of concurrency. An actor has three things: private state that no other code can touch, a mailbox, which is a queue of incoming messages, and behaviour, the code that handles one message. The runtime guarantees that an actor processes one message at a time. So while it handles a message, nothing else can read or change its state, and its code can be written as if it were single-threaded, without locks.

Actors interact only by sending messages, which are immutable data. Sending is asynchronous: the sender puts a message in the recipient's mailbox and continues. In response to a message, an actor can change its own state, send messages to other actors, and create new actors. Concurrency comes from having many actors, each sequential inside, running in parallel.

2A Simple Actor in Plain Java

Here is a minimal actor that holds an account balance. Messages are records in a sealed interface, the mailbox is a LinkedBlockingQueue, and one virtual thread drains it. Because only that thread ever touches balance, the field needs no synchronisation.

Java
sealed interface AccountMsg permits Deposit, Withdraw, GetBalance, Stop {}
record Deposit(long cents) implements AccountMsg {}
record Withdraw(long cents) implements AccountMsg {}
record GetBalance(CompletableFuture<Long> reply) implements AccountMsg {}
record Stop() implements AccountMsg {}
final class AccountActor {
private final BlockingQueue<AccountMsg> mailbox = new LinkedBlockingQueue<>();
private long balance; // touched only by the actor's own thread
AccountActor() {
Thread.ofVirtual().name("account-actor").start(this::run);
}
void tell(AccountMsg msg) { mailbox.add(msg); } // asynchronous: returns at once
private void run() {
try {
while (true) {
switch (mailbox.take()) { // one message at a time
case Deposit d -> balance += d.cents();
case Withdraw w -> { if (w.cents() <= balance) balance -= w.cents(); }
case GetBalance g -> g.reply().complete(balance);
case Stop s -> { return; }
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
AccountActor account = new AccountActor();
account.tell(new Deposit(10_000));
account.tell(new Withdraw(2_500));
CompletableFuture<Long> reply = new CompletableFuture<>();
account.tell(new GetBalance(reply));
System.out.println(reply.join()); // 7500
Step throughMessages through an actor's mailbox
Caller threadsMailboxActor thread
tell(Deposit 10000)
tell(Withdraw 2500)
tell(GetBalance(reply))
take(): Deposit, balance 10000
take(): Withdraw, balance 7500
take(): GetBalance
reply.complete(7500)

7 messages in order. Play them, or step through them one at a time.

Callers only enqueue; one thread applies messages in order, so the state needs no lock.

Notice that a thousand threads can call tell at the same time, and the balance is still correct, because the queue serialises the messages and only one thread processes them. The thread-safe part is the mailbox, which java.util.concurrent already provides. A single-threaded executor would work the same way, with each tell becoming executor.execute(() -> handle(msg)), and a dedicated platform thread works too; virtual threads make it cheap to have thousands of actors that each block on take(). Starting the thread in the constructor is acceptable here only because run() touches nothing that the constructor initialises after the start; in production code a factory method would start it after construction.

3Request-Reply Needs Its Own Pattern

Pure actors only send messages, so asking a question needs a way back. In our example, the message carries a CompletableFuture that the actor completes, which is the plain Java version of what Akka calls the ask pattern. Alternatives are to include a reference to the sender's own mailbox so the actor replies with a message. Either way, the caller must deal with the possibility that no reply ever comes, so asks always need a timeout, for example reply.orTimeout(1, TimeUnit.SECONDS). This is one of the real costs of the model: what would be a method call returning a value becomes a message, a correlation and a timeout.

4What Akka and Apache Pekko Add

Our forty lines capture the core, but a production actor system needs much more, and that is what frameworks provide. Supervision is the first: actors form a hierarchy, and when a child actor throws, its parent decides whether to restart it with fresh state, stop it or escalate. Failures become contained and handled by design, instead of a dead thread nobody notices. Location transparency means an actor reference works the same whether the actor runs in the same JVM or on another machine, so messages can cross the network without changing the code that sends them.

On top of that, clustering lets actors run across several nodes that detect each other's failures; cluster sharding distributes many entity actors, such as one per customer, across the cluster by id and moves them when nodes join or leave; and persistence lets an actor store its state changes as events and recover after a restart. Frameworks also add efficient schedulers that run millions of actors on a few threads, mailbox options with bounds, and streams with backpressure.

Licensing matters for the choice. In September 2022, Lightbend changed Akka's licence from Apache 2.0 to the Business Source License, which requires a commercial licence for many production uses. The community responded with Apache Pekko, a fork of Akka 2.6 under the Apache 2.0 licence, maintained at the Apache Software Foundation, with largely compatible APIs under different package names.

Plain Java actor versus Akka or Pekko
CapabilityPlain Java actorAkka / Apache Pekko
One message at a time, no locksYesYes
Failure handlingWrite it yourselfSupervision hierarchies, restarts
Remote actorsNoLocation transparency
Many nodes, entities by idNoClustering and sharding
State recoveryNoEvent-sourced persistence
LicenceYour codeAkka: BSL since 2022; Pekko: Apache 2.0

5The Costs of the Model

Actors trade one kind of difficulty for another. Locks disappear, but the flow of a business operation is now spread across messages between actors, so a stack trace shows only the current message handler, not the chain of messages that led to it. Debugging needs correlation ids in messages, tracing and good logging. Request-reply needs the patterns described above, with timeouts everywhere. Mailboxes can grow without bound if an actor is slower than its senders, so bounded mailboxes or backpressure are needed. And message ordering is guaranteed only between a given pair of actors, which surprises people who expect a global order. For a service whose state fits comfortably in a database with transactions, those costs usually outweigh the benefits; actors shine for many independent, stateful, high-rate entities.

6Follow-up questions

How is an actor different from an object with synchronized methods?

A synchronized object makes callers wait for the lock and run the code on their own threads, so a slow method blocks every caller, and calls between several such objects can deadlock. An actor's callers never wait: they enqueue and continue, and the actor's own thread runs the code. Without blocking calls between actors there is no lock ordering to get wrong.

Do virtual threads make actor frameworks unnecessary?

They make the plain Java actor cheap: one virtual thread per actor is affordable even for many thousands of actors. But they do not provide supervision, remoting, clustering, sharding or persistence, which are the main reasons to adopt a framework. For in-process actors with simple failure handling, virtual threads plus a mailbox may be enough.

7Conclusion

In this answer, we saw that an actor owns private state and a mailbox and handles one message at a time, so its state needs no locks, and that actors communicate only by sending messages. We built one in plain Java with a sealed message interface, a BlockingQueue and a single virtual thread, and used a CompletableFuture in a message for request-reply. Akka and Apache Pekko add supervision, location transparency, clustering, sharding and persistence; Akka changed to a source-available licence in 2022 and Pekko is the Apache-licensed fork. The costs are harder debugging of message flows and extra patterns for replies and backpressure.

What to say in the interviewAbout 90 seconds aloud

Open with

An actor is a unit of concurrency with private state, a mailbox and behaviour. It processes messages from its mailbox one at a time, so while it handles a message nothing else touches its state, and I can write it without locks.

Then, if they want more

Actors never share state; they only send each other immutable messages, asynchronously. In plain Java I would build one with a BlockingQueue as the mailbox and a single thread draining it, which on Java 21 can be a virtual thread, or a single-threaded executor.

The messages are records in a sealed interface, so the run loop is a switch over message types. Many threads can call tell concurrently, the queue serialises them, and only the actor thread touches the balance. For request-reply, the message carries a CompletableFuture that the actor completes, and the caller always applies a timeout.

Frameworks like Akka and Apache Pekko add what production systems need: supervision hierarchies, so a parent decides how to restart a failed child; location transparency, so the same actor reference works across machines; clustering and sharding of entity actors by id; and persistence through event sourcing.

Akka changed its licence to the Business Source License in 2022, and Pekko is the Apache-licensed fork of Akka 2.6. The costs are that flows are spread over messages, so debugging needs correlation ids and tracing, request-reply needs extra patterns, and mailboxes need bounds.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the actor model. How would you implement a simple actor in plain Java, and what...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/f0951f1bc2/