Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Explain verticles and the event bus in Vert.x and when you would use them to structure a service.

A verticle is Vert.x's unit of deployment: a class whose handlers all run on one event loop context and never concurrently, so its own state needs no locks. We scale by deploying several instances, which spreads them across event loops. The event bus carries messages between verticles by address, as point-to-point, publish-subscribe or request-reply, within one JVM or across a cluster, with codecs defining how bodies are serialised. I use them for decoupled in-process components and simple clustering, and use Kafka or another broker when messages must be durable across services.

Why this question is asked

Vert.x gives us two structuring tools that look unusual to developers coming from Spring: verticles instead of beans, and an event bus instead of method calls. Used well, they give a service thread-safe components without locks and loose coupling between parts. Used for the wrong job, the event bus becomes a fragile message queue. Interviewers ask about them to see whether we understand the concurrency guarantee and the limits. In this answer, we will recap the threading model, define verticles and their guarantee, scale them with instances, look at the event bus and its three messaging styles, codecs and clustering, and finish with when to use them.

1What a Verticle Is and Why Its State Needs No Locks

A verticle is a class, usually extending AbstractVerticle, that Vert.x deploys and starts. When it is deployed, Vert.x assigns it a context bound to one event loop thread. Every handler the verticle registers, for HTTP requests, timers or event bus messages, runs on that context. The key guarantee follows from this: two handlers of the same verticle instance never run at the same time. So a field such as a counter or a cache map inside the verticle can be changed without synchronized, locks or atomic types.

Java
public class PriceVerticle extends AbstractVerticle {
private final Map<String, BigDecimal> prices = new HashMap<>(); // no locks needed
@Override
public void start() {
vertx.eventBus().<JsonObject>consumer("prices.update", msg -> {
prices.put(msg.body().getString("sku"), new BigDecimal(msg.body().getString("price")));
});
vertx.eventBus().<String>consumer("prices.get", msg -> {
BigDecimal p = prices.get(msg.body());
if (p == null) msg.fail(404, "unknown sku"); else msg.reply(p.toPlainString());
});
}
}
// Both handlers run on the same event loop, one at a time, so the HashMap is safe.

Notice that this is the actor idea in a simple form: the verticle owns its state, and others interact with it only by sending messages. It is also why blocking inside a verticle is so harmful: it stops all of that verticle's handlers, plus others sharing its event loop. A worker verticle, deployed with the worker threading model, gives the same no-concurrency guarantee but runs on worker threads, for code that must block.

2Scaling by Deploying Several Instances

One verticle instance uses one event loop, so it can use at most one core. To use more cores, we deploy several instances of the same class. Each instance gets its own context, usually on a different event loop, and its own copy of the fields.

Java
vertx.deployVerticle("com.shop.HttpVerticle", new DeploymentOptions().setInstances(8));
// 8 instances on 8 event loops; all listen on port 8080, Vert.x spreads connections between them

For HTTP servers this is very convenient: all instances can listen on the same port, and Vert.x distributes incoming connections round-robin among them. For a stateful verticle like PriceVerticle, though, several instances would each have their own map, so updates sent point-to-point would reach only one. Stateful verticles therefore either stay single instance, receive updates by publish so every copy sees them, or partition the data, for example by sending each sku to an address derived from its hash.

3The Event Bus and Its Three Messaging Styles

The event bus is Vert.x's built-in messaging system. Consumers register a handler on an address, a plain string such as prices.update. Senders do not know who listens; they only know the address. Three styles are available. send delivers a message to one consumer of the address, picked round-robin if there are several. publish delivers it to every consumer of the address. request sends to one consumer and returns a Future that completes with the reply, with a timeout of 30 seconds by default.

Java
EventBus bus = vertx.eventBus();
bus.publish("prices.update", new JsonObject().put("sku", "A-1").put("price", "9.90")); // to all consumers
bus.send("audit.log", "price A-1 changed"); // to one consumer
bus.<String>request("prices.get", "A-1")
.onSuccess(reply -> System.out.println(reply.body())) // 9.90
.onFailure(err -> System.out.println(err.getMessage())); // unknown sku, or a timeout after 30 s
Step throughRequest-reply over the event bus
HttpVerticleEvent busPriceVerticle
request("prices.get", "A-1")
deliver to one consumer of prices.get
look up A-1 in its own map
reply("9.90")
Future completes on HttpVerticle's context

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

Each verticle's handlers run on its own context; the bus carries messages between them.

Notice that the reply arrives on the sender's own context, so the sender's handlers also stay single-threaded. Request-reply over the event bus feels like a method call but keeps the components decoupled: PriceVerticle could move to another JVM in the cluster without the caller changing.

4Codecs and Clustering

A codec defines how a message body is turned into bytes and back. Vert.x has built-in codecs for strings, buffers, JsonObject, JsonArray and primitive types. For our own classes we register a MessageCodec, or, more simply, send JsonObject or a serialised form. Within one JVM, Vert.x may pass objects without serialising them, depending on the codec, which is fast but means sender and receiver must not modify a shared mutable object; immutable bodies avoid the problem.

With a cluster manager, such as Hazelcast or Infinispan, several Vert.x instances join one event bus. Addresses become cluster-wide, and messages to a consumer on another node are serialised with the codec and sent over TCP. This gives simple clustering without extra infrastructure. Codec and cluster APIs have changed between major versions, so check your version.

5When to Use Them, and When to Use a Broker

Verticles and the event bus are good at structuring one service, or a small group of nodes, into components that are independently single-threaded and loosely coupled. Typical uses are separating the HTTP layer from a stateful in-memory component, fanning a price update out to all instances with publish, pushing events to browser clients through the SockJS bridge, and simple clustering of a few nodes.

But the event bus is not a durable message broker. Delivery is at most once: if no consumer is registered, a message is dropped, if a node crashes, messages in flight are lost, and there is no persistence, replay or consumer offset. So for messaging between services, especially where losing a message means losing an order or a payment, we use Kafka or another broker. The event bus is for in-process and in-cluster communication where a lost message is acceptable or simply retried by the caller.

6Follow-up questions

Can two instances of the same verticle run at the same time?

Yes. Each instance has its own context and may run on a different event loop, so two instances can execute in parallel. The guarantee is per instance: one instance's handlers never run concurrently with each other. Shared state between instances therefore needs messages, shared data structures from Vert.x such as SharedData, or ordinary thread-safe classes.

What happens when request() gets no reply?

If no consumer is registered for the address, the Future fails quickly with a no-handlers error. If a consumer exists but never replies, the Future fails with a timeout after the default 30 seconds, or after the timeout set in DeliveryOptions. Callers should always handle both cases, usually with a shorter timeout suited to the operation.

How does this compare to Spring's application events?

Spring application events are in-process notifications between beans, usually delivered synchronously on the caller's thread. The Vert.x event bus is asynchronous, delivers each message on the receiver's own context, supports request-reply and can span a cluster. Both are for decoupling inside an application, and neither replaces a durable broker.

7Conclusion

In this answer, we saw that a verticle is a deployment unit whose handlers run on one context and never concurrently, so its state needs no locks, and that we scale by deploying several instances across event loops. The event bus carries messages by address with send, publish and request-reply, locally or across a cluster, with codecs for serialisation. Verticles and the event bus suit decoupled in-process components and simple clustering, while durable cross-service messaging belongs to Kafka or another broker.

What to say in the interviewAbout 80 seconds aloud

Open with

A verticle is Vert.x's unit of deployment, usually a class extending AbstractVerticle. When it's deployed, Vert.x binds it to a context on one event loop, and every handler it registers runs on that context, so the handlers of one instance never run concurrently.

Then, if they want more

That means state inside the verticle, like a HashMap of prices, needs no locks, much like an actor. To use more cores I deploy several instances with setInstances; for HTTP verticles they can all listen on the same port and Vert.x spreads connections between them. Stateful verticles need care, because each instance has its own state.

The event bus connects verticles by address. send delivers to one consumer, round-robin; publish delivers to all; request sends to one and returns a Future with the reply, with a 30 second default timeout. Replies come back on the sender's context, so everything stays single-threaded.

Codecs define how bodies are serialised, and with a cluster manager like Hazelcast the bus spans several JVMs. I'd use them to structure a service into decoupled components and for simple clustering or fan-out. But the event bus is at-most-once with no persistence, so for durable messaging between services I'd use Kafka or another broker.

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 verticles and the event bus in Vert.x and when you would use them to structure a...": https://scrivio-interview-library.netlify.app/java/other-java-frameworks/ca67365fff/