Over a million developers have joined DZone.

Simple Event Driven Design

Peter Lawrey provides insight into a simple event-driven design, including how a tool like Chronicle Queue can be the perfect choice for transport.

· Performance Zone

Download Forrester’s “Vendor Landscape, Application Performance Management” report that examines the evolving role of APM as a key driver of customer satisfaction and business success, brought to you in partnership with BMC.


Developers often ask about the performance or efficiency of a system or their code. What does this really mean?

  • My code is so efficient, only a coding god could understand how it works.
  • My code is really clever but unmaintainable. The next developer will re-write it anyway.
  • My code is really efficient for the machine but inefficient for the developer.
  • My code is really simple to understand which means the developer is more efficient, and the code is more than fast enough and easy to fix if not.

So instead of asking yourself how fast you can make the code and how many clever tricks you can put into it, ask yourself; how simple can I make this and still be more than fast enough?

Simple Event Processing

For me, the simplest event processing is a method call which doesn't return anything. This is simple to translate into an asynchronous messaging transport e.g.

public interface EventProcessor {
  void event(MyEventData data);
  void eventTwo(MyEventData2 data); 

This is pretty simple, one component produces an event by calling a method, another component consumes or processes this event by providing an implementation.

How Simple Is It?

You can step from the producer component to the consumer component in your debugger using one button.

A couple of lines of code is needed to setup a unit test with a producer which calls your consumer.

MyConsumer mc = new MyEventProcessor(); 
MyProducer mp = new MyProducer(mc);

You can mock the Event Processor with any mock tool and check the producer creates the events you expect.  You can mock the producer by calling the methods on the consumer in the unit test.

How Does it Perform?

You might think this has next to no overhead, one component just calls another.  But even a method call has overhead, which is why the JIT supports inlining.  This can mean that the overhead is notional, and even less than that if the methods are optimised together (ie it can be faster than the sum of the two methods would take individually)

Am I Missing Something?

Actually, there is an awful lot missing, but not relevant to the business requirements:

  • a transport.
  • monitoring
  • serialization
  • fail over
  • service discovery
  • security

These are separate concerns and usually not part of the essential complexity of the application.

What Transports Could I Use?

There is so many to choose from, it's impossible to know what will be right for all cases in the future.  For this reason, the choice of transport (or a lack of a transport) should be a configuration detail.  The only essential part of your design should be that the transport can be replaced easily without having to touch your business logic.

What an Example of a Transport?

A low latency, high throughput solution is to use Chronicle Queue. Repeating myself; you only need use this when it makes sense, or you could use any other transport.

What Chronicle Queue does:

  • persist every message for replay-ability and to check bug fixes.
  • low latency serialization with support for schema changes, and human readability for validation
  • logging and monitoring.

The last point is important. If you are already persisting every action your component takes and every state change, you shouldn't need any additional logging in normal operation. Any downstream component can re-create the state it is interested in without touching the component producing that information.

How Does Chronicle Queue do This?

Chronicle Queue uses two components;

  • A writer which implements your interface. Each method call writes a message.
  • A reader which call an implementation of your interface. Each message calls the corresponding method.

Note: this strategy can be used for just about any transport.  What Chronicle Queue gives you is low latency recording or all messages for replay and replacement for logging.

How Does This All Perform?

If you use a flexible serialization like YAML, Binary YAML or JSON, and your component doesn't do much, you can expect to get a throughput of around 100,000 messages per second without much tuning.  If you use a lower level binary protocol, short messages and multiple threads, you can get over 10 Million messages per second.

You have the option to do all this GC free, but this can complicate your design so you are likely to create some garbage, but you have the option to reduce it as required.

Other Notable Transports

Aeron is a low latency UDP-based transport.

Chronicle Queue Enterprise supports replication and remote access over TCP.

Chronicle Websocket Jetty supports access to JSON via WebSocket for message rates around 100K/s


You should start your design and testing, focusing on the key components you actually need. You should allow your design to work for any transport, and have the option to replace one for the other.

In unit testing and debugging, it can be useful to have no transport to show that the components still work with a minimum of complexity.

See Forrester’s Report, “Vendor Landscape, Application Performance Management” to identify the right vendor to help IT deliver better service at a lower cost, brought to you in partnership with BMC.

java,event driven programming,event driven architecture,chronicle,jit

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}