How to Utilize Mappedbus: A High Throughput, Low Latency Message Bus

Aug 24, 2021 | Programming

Welcome to the world of Mappedbus, a Java-based message bus designed for high throughput and low latency applications! In this article, we’ll walk you through everything you need to know to get started, including setup, usage, and troubleshooting tips. Let’s dive in!

What is Mappedbus?

Mappedbus is a memory-mapped and shared memory transport system inspired by Java Chronicle. It efficiently supports multiple writers, ensuring that the order of messages produced by different processes is preserved. This makes it perfect for scenarios where message order is critical.

Features

  • Inter-process Communication (IPC) via message passing
  • Support for memory mapped files or shared memory as transport
  • Capability to handle both object-based and byte array-based messages

Getting Started with Mappedbus

To begin using Mappedbus, follow these steps:

  1. Download mappedbus.jar from the release tab, or clone the project and build it from source using Ant.
  2. Set up your reader and writer in the code.

Usage Instructions

Here’s how you can set up readers and writers:

java MappedBusReader reader = new MappedBusReader(tmptest, 100000L, 32);
reader.open();

java MappedBusWriter writer = new MappedBusWriter(tmptest, 100000L, 32);
writer.open();

Using Memory-Mapped Files

The file tmptest is stored on disk and memory mapped by the library. For shared memory usage, point it to a file in /dev/shm such as devshmtest. Messages are lazily persisted to disk with memory-mapped files, whereas messages in shared memory are stored in RAM.

Read and Write Messages

You can read and write messages either by using objects or byte arrays. Here’s how:

Using Objects:

java PriceUpdate priceUpdate = new PriceUpdate();
writer.write(priceUpdate);

while (true) {
    if (reader.next()) {
        int type = reader.readType();
        if (type == 0) {
            reader.readMessage(priceUpdate);
        }
    }
}

Using Byte Arrays:

byte[] buffer = new byte[32];
writer.write(buffer, 0, buffer.length);

while (true) {
    if (reader.next()) {
        int length = reader.readBuffer(buffer, 0);
    }
}

Examples

Mappedbus provides several examples for object-based and byte array-based reading and writing.

The object-based example involves the following:

java -cp mappedbus.jar io.mappedbus.sample.object.ObjectWriter 0...
java -cp mappedbus.jar io.mappedbus.sample.object.ObjectReader...

It sends a PriceUpdate message with three fields: source, price, and quantity.

Performance Testing

You can run performance tests using:

rm -rf tmptest;
java -cp mappedbus.jar io.mappedbus.perf.MessageReader tmptest...
Elapsed: 5660 ms
Per op: 70 ns
Ops: 14131938

Understanding the Architecture

The Mappedbus project cleverly addresses synchronization issues among multiple writers, each running in its own process. Here’s how:

  • The first eight bytes create a field called the limit, indicating how much data has been written.
  • Writers use atomic operations to update the limit, while readers poll to check for new records.
  • Each record has a status flag, ensuring that readers only process committed messages.
  • Writers and readers perform compare-and-swap operations to maintain data integrity.

Troubleshooting Tips

If you encounter any issues while using Mappedbus, consider the following troubleshooting steps:

  • Ensure that you are using the correct version of Java.
  • Check your memory settings if you face performance issues.
  • Verify the file paths if messages aren’t being read or written properly.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Closing Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Now that you have all the necessary information, it’s time to experiment with Mappedbus and unleash its capabilities! Happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox