10,000 ft overview
Let"s start by looking at Aeron Transport from a very high cấp độ to introduce some terminology.
Bạn đang xem: Aeron for the working programmer
Assume we have a sending application that wants to send messages khổng lồ a receiving application using Aeron. Anapplication is referred to lớn by Aeron as an Aeron Client & the terms are interchangeable. In this example,Application 1 sends messages to lớn Application 2, which is on a different machine.
Application 1(Aeron Client 1)Media Driver 1Shared Memory
Sending machine(writing to lớn a)publicationsendingsending
Application 2(Aeron Client 2)Media Driver 2Shared Memory
Receiving machinereceivingreceiving(reading from a) subscription
APIAPINetwork
Intro
Step 1Step 2Step 3Step 4
Use the tabs above to step through the animation.
Application 1 sends a message by writing it khổng lồ a shared memory buffer that has beendedicated for it to lớn send messages lớn Application 2. It does this by calling into Aeron
Client API code (application code is yellow; Aeron code is blue). This is known as writingto a publication.
Aeron provides a component called the truyền thông media Driver (usually referred to as just the Driver) that monitors theshared memory buffer & does the driving of the network. Media Driver 1 on the sending machine polls the sharedmemory buffer, reads the message & sends it in a UDP packet to truyền thông Driver 2 on the receiving machine.This is known as sending.
Receiving messages is the opposite of sending. Media Driver 2 polls the network for new UDP packets, reads the
UDP packet into a shared memory buffer dedicated to messages received from Application 1. This isknown as receiving.
Application 2 polls the shared memory buffer for new messages & reads the message. This is known as reading froma subscription.
A few notes
Even at this very high level, it"s worth pointing out a few things.
IPC
Aeron can send messages between applications on the same machine, known as IPC (Inter-Process Communication). Whendoing so, there is no need to lớn incur the overhead of sending messages via UDP. The truyền thông media Driver still has some adminresponsibilities, including creating the shared memory buffer in the first place, but it is not directly involved inmessage transfer. The sending application writes messages khổng lồ the shared memory buffer and the receiving applicationpolls the same buffer to lớn receive messages.
This is very similar lớn passing messages across threads via a ring buffer, except this also works across processes.Both sending và receiving applications use the same shared memory buffer & access it no differently lớn theirlocal heap memory.
Using IPC, the publication & subscription steps are present, but the sending and receiving stepsare not.
Application 1(Aeron Client 1)Media Driver
Shared Memory
Same machinepublication
Application 2(Aeron Client 2)subscription
APIAPI
Intro
Step 1Step 2
Unidirectional
Communications are unidirectional. If the receiving application needs to lớn respond to the sending application, itneeds khổng lồ set up a similar channel in the reverse direction. The messages would probably also utilise a protocol thatcontained a correlation id so responses could be matched with requests.
One truyền thông Driver per machine
A single truyền thông media Driver is required per machine that runs applications using Aeron. Several applications on the samemachine can read / write their shared memory buffers & a single truyền thông media Driver can service them all. The Media
Driver"s job is lớn ship messages from the shared memory buffers onto the network và vice versa.
Media Driver execution
The media Driver can run in a standalone process. It can also run embedded within the (Java) application if required.Either way, the diagram above remains the same. The only thing that changes is how the truyền thông Driver is given processingresources. In fact, there are two implementations of the truyền thông media Driver: one written in C, the other in Java. Whenrunning standalone, either can be used.
Low overhead sending / receiving
The truyền thông Driver usually runs in its own process / thread. That means the only overhead incurred by an application whensending a message is writing it khổng lồ a shared memory buffer. That gives the application thread more time to do otherapplication-level processing. The shipping of network traffic is all done in the background by the truyền thông Driver. Receiving data has similar low overhead - just polling a subscription memory buffer to lớn see if it contains new messages.
Xem thêm: Tuổi Mậu Dần 1998 Mua Xe Màu Gì: Nam Và Nữ 1998 Mua Xe May Màu Gì Năm 2023 ?
What is Aeron?
Aeron is a high tốc độ message transport. It allows messages to be sent over the network via UDP (unicast or multicast) or between processes via IPC in shared memory.
Aeron is the brainchild of Martin Thompson & Todd Montgomery. Martin was the co-founder & CTO of LMAX và is a high-performance specialist. Todd was CTO of 29West where he was the chief designer of their low latency messaging products. They’ve been working on Aeron over the past 2 years and have just released version 1.0.
It is made up of clients & a truyền thông media driver. The clients encode/decode messages và pass them khổng lồ the truyền thông media driver which runs in its own process và is responsible for sending and received messages over the network. The clients communicate with the media driver through shared memory using lock-free algorithms.
As well as the C# client, there are also clients written in Java & C++.
Show me some numbers
At the time of writing, when using C# Aeron client và the Java media driver, it is capable of sending & receiving about 30 million messages per second. We used HDRHistogram.Net khổng lồ measure latency by measuring the roundtrip time of a message. 99% of messages roundtrips are under 25 microseconds. You can check these numbers yourself by running the media driver with the Ipc
Throughput sample and the Ping & Pong samples.
Once it’s up và running, it does not allocate any memory which means that latency is more predictable because no time is spent garbage collecting.
Show me some code
There is a full Hello World example here. Here’s a breakdown of the most important parts.
Publisher
The client connects to lớn the truyền thông media driver & instructs it khổng lồ prepare a advertiser which will send messages lớn another media driver listening on UDP port 40123. Messages can be split into different streams & sent to the same channel.
string channel = "aeron:udp?endpoint=localhost:40123";int stream
Id = 42;Aeron aeron = Aeron.Connect()Publication publication = aeron.Add
Publisher(channel, stream
Id);
Creating a message
Part of the reason Aeron is able khổng lồ achieve such high throughput is by not creating objects when sending messages. Instead of newing up a DTO, Aeron uses byte arrays lớn represent messages. UnsafeBuffer is a wrapper that has methods for writing various types.
Unsafe
Buffer buffer = new Unsafe
Buffer(new byte<256>);buffer.Put
String
Without
Length
Utf8(0, “Hello World!”);
Sending a message
To send the message, just pass the message buffer along with the offset at which the message starts và the length of the message to lớn the publisher.publisher.Offer(buffer, 0, message.Length);
Subscriber
The next code fragment sets up a subscription, it causes the truyền thông media driver khổng lồ listen on the endpoint defined by channel, in this case udp://localhost:40123 and pick up messages sent lớn the relevant streamId. You pass it a message handler (which will be explained in the next section) & a fragment limit which defines the maximum number of fragments lớn process in one poll.
Subscription subscription = aeron.Add
Subscription(channel, stream
Id);subscription.Poll(message
Handler, fragment
Limit
Count); // see message
Handler below
Processing a message
The FragmentHandler defines what happens to lớn messages that are received by a subscription. In this case, we know the message consists of a string so we can read it out directly. For more complex scenarios, for instance when messages might be too large and need khổng lồ be split up, a Fragment
Assembler can be used to reassemble the message.
Fragment
Handler message
Handler = (buffer, offset, length, header) => Console.Write
Line(buffer.Get
String
Without
Length
Utf8(offset, length)); ;
Where can I get it?
The Aeron client is available from NuGet.
PM> Install-Package Aeron.Client
Alternatively, the source code is up on Git
Hub:
https://github.com/Adaptive
Consulting/Aeron.NET/
You can also get a copy of the Java driver through Nu
Get:
PM> Install-Package Aeron.Driver