How Automatic Streaming in Mule 4 Beta Works
Learn about the new types of repeatable streams that are now available in Mule 4 with examples that are related to beer.
Join the DZone community and get the full member experience.Join For Free
streaming in is now as easy as drinking beer! there are incredible improvements in the way that mule 4 enables you to process, access, transform, and stream data. for streaming specifically, mule 4 enables multiple parallel data reads without side effects and without the user caching that data in memory first.
a lot of people are not familiar with the concept of streaming. so before we get into the specifics of streaming with mule 4, let’s first go through a couple of use cases that highlight its value.
example 1: http > 2 files
in this simple flow, you receive a content from http (let’s say, a post with a json), and then you write it on two files. what is the result you get after running this? the first file gets written correctly. the second file gets created, but its content is empty.
example 2: http > logs > file
this example receives the same json post, but this time, it logs it and then writes it to the file. the output of this flow is what you’d expect. the content gets logged and the file gets written. but is the behavior correct? the short answer is no.
the long, but concise, reason is that in order to log the payload, the logger has to consume the stream completely, which means that the full contents of it will be loaded into memory. by the time the message reaches the file connector, the content is fully in the memory. most of the time, this is not a problem; but if the content is too big and you’re loading it into memory, there’s a good chance the app might run out of memory — threatening the stability of the app.
example 3: http > scatter-gather > whatever
now, let's try the same example but with a scatter-gather component (for explanatory purposes only). this scenario simply fails. a stream cannot be concurrently consumed by two different threads, therefore the component has only two options:
load the whole stream into memory (like logger does).
scatter-gather does the latter.
this is the part where we really need to understand the implications of the meaning of streaming. there are two problems with consuming a stream:
- it can only be read once.
- it cannot be read in parallel.
heraclitus said that you cannot take a bath in the same river twice. this is because each time you take a bath, the collection of drops that make up that river is never the same. the same is true for drinking a pint of beer. each sip that you take is one sip you cannot take again. the same thing happens to streams.
the idea of a stream is that in order to avoid loading a potentially large chunk of data into memory completely, you start loading it sip by sip. this means that while you’re still “digesting” (i.e. processing) the first sip, the second sip is already going through your throat (aka network, disk io, etc.). not only does this save memory, but it also increases performance. the problem is that the sips cannot be taken back!
going back to example 1, after the first file-outbound “drinks” the stream in order to process it (write it to disk), the stream becomes empty (no more beer in it). in order to make the example work, you need to place an <object-to-string/> transformer before the first file-outbound processor. this is not obvious and will force mule to fully load the contents of the stream into memory.
also in example 2, the logger has to load the whole thing into memory and replace the message payload. again, everything got loaded in memory.
introducing repeatable streams
but what if there was a way to get the pint full again with the same beer?
in mule 4, you no longer need to worry about answering the following questions:
- which components are streaming and which are not?
- is the stream consumed at this point?
- at which position is the stream at anyway?
- what does streaming even mean?
mule 4 now makes sure that any component that needs to read a stream, will be able to do so regardless of which components already tampered with it. the stream will always be available and will be in its start position.
file store repeatable streams
file store repeatable streams require buffering, and we have different buffering strategies. mule now keeps a portion of contents in memory. if the stream contents are smaller than the size of that buffer, then we’re fine. if the contents are larger, mule backs up the buffer’s content to disk and then clears the memory. this is now the default strategy in mule 4.
in memory repeatable streams
you can also take an in memory strategy. when streaming in this mode, mule will never use the disk to buffer the contents. if you exceed the buffer size, the message will fail.
<file:read path="bigfile.json"> <repeatable-in-memory-stream initialbuffersize="512" buffersizeincrement="256" maxbuffersize="2048" bufferunit="kb"/> </file:read>
reading streams in parallel
so far so good! but we only addressed examples 1 and 2. example 3 remains unsolved.
let’s go back to our beer anecdote. so we’re back at the bar and we’re having our pint of beer. suppose the pint contains 500 cc of beer. since it’s a small world, you happen to run into an old friend at the bar and you start sharing your beer. using straws, you can drink in parallel, but you’ll never drink the same sip as your friend. and, because you are sharing, when the beer is done, you didn’t have your full 500cc, which means you lost some of the content.
the same thing happens with streams. if two threads are reading from the same stream at the same time, one thread will take some bytes and the other will take other bytes, but none of them have the full content. therefore, content has been corrupted.
the new repeatable streams framework in mule 4 solves this automatically. all repeatable streams support parallel access. mule 4 will automatically make sure that when component a reads the stream, it doesn’t generate any side effects in component b, eliminating dirty reads!
disabling repeatable streams
although uncommon, there are cases in which you might want to disable this functionality and use plain old streams. for example, your use case may not really require this and you don’t want to pay for the extra memory or performance overhead. again, you can disable this using the following:
<file:read path="bigfile.json"> <non-repeatable-stream /> </file:read>
note that by disabling this feature, all the pitfalls of examples 1, 2 and 3 are rendered current even if using mule 4
raw bytes stream is not the only case of streaming that mule 4 supports. back in 2013, mule 3.5 was released and we introduced the concept of auto-paging connectors. this was a feature that allowed connectors, such as salesforce, to transparently access paginated data. this is a form of streaming! under the hood, the connector fetches the first page, and when it’s consumed, it fetches the next one, discarding the prior pages from memory. in essence, this is exactly the same as streaming a file from ftp.
file store auto paging
by default, you will now get a buffer that holds a number of objects into memory and uses the disk to buffer the rest:
<sfdc:query query="dsql:..." />
just like before, you can also do this fully in memory:
<sfdc:query query="dsql:..."> <repeatable-in-memory-iterable initialbuffersize="100" buffersizeincrement="100" maxbuffersize="500" /> </sfdc:query>
note, however, that buffer sizing requires a different approach here. in the prior examples, all buffer sizes were measured in bytes (or a derivative unit such as kb). in this case, we talk about instance count.
in order for the filestore strategy to use the disk as a buffer, it needs to serialize the streamed objects. does this mean it only works on objects that implement the java.io serializable interface? not at all. just like the batch module, this feature uses the kryo framework to serialize things that the jvm cannot serialize by default. even though kryo does a lot of black magic, it’s neither all powerful nor a silver bullet. some things just can’t be serialized, so try to keep your objects simple!
Published at DZone with permission of Mariano Gonzalez, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.