Do you make use of term_to_binary?

Joe asked this on EF a few years ago:

Wondering whether people make use of it in Erlang - do you use it?

Here’s what he had to say about it:


When I’m going to use term_to_binary , first thing out of my mind is dets and there a question too

Great idea!

Finally, I need term_to_binary

1 Like

Not anymore so much. Mostly use term_to_iovec/1 now :slight_smile:


In Zotonic we are using term_to_binary regularly to serialize arbitrary data structures. For backing stores, but also for data that is send along user requests (we are adding signatures for those).


It is a great way to “pass by reference” if copying messages between processes becomes a botlleneck. Admittedly with some overhead.

For example, you may have a pool of workers and a dispatcher, and a stream of relatively large messages coming in. In a naive implementation your message will be copied into the dispatcher only to be immediately copied again into one of the workers. If you, instead, transform the message into a binary all you copy between the processes is a reference. You just need to leave enough of a message as a term in order to dispatch it appropriately.


Oh that’s clever! Thanks for the tip

1 Like

Also, base64:encode(term_to_binary(Anything)) is a great way to piggyback Erlang things over almost any (text-based or string capable) protocol.

For example, we had a need to ship Prometheus metrics from IoT devices in the field, which already had an existing semi-persistent web socket channel open talking JSON-RPC. What did we do? Exported the data from the Prometheus library on device, base64-encoded it and shipped it in a JSON field in a web socket packet. Then we just unpacked it on the server side and sent it to Prometheus when it was scraping for metrics.


Apache CouchDB uses term_to_binary to store all its data on disk.

1 Like

The only thing you have to be aware of here is that when you want to inspect data then you have to unpack the binary into a copy of its original data. So while you might save copying the actual message you still have to make a copy when you view the data. I personally think it is probably more efficient to let the BEAM copy instead of first making the binary then unpacking it every process.

However one situation where it is more efficient is when you want to pass the message through a chain of processes who don’t need to unpack just pass it on. E.g when processing pictures or video.