benhaney

benhaney

Jsonrs - High performance JSON library using a Rust NIF

Jsonrs is a JSON library like Jason or Poison, but it’s faster and uses much less memory. This is thanks to the core of it being a Rust NIF leveraging Rust’s excellent serialization framework, serde.

Recently, Jsonrs started using rustler_precompiled, so you no longer need an entire Rust toolchain in your build pipeline in order to start using Jsonrs in your project! This was a significant blocker for a lot of people in adopting Jsonrs before, so I’m excited that there’s such a good solution to it now, and I hope it breathes some life back into Jsonrs.

Since the main purpose of Jsonrs is performance, I’ll talk a little about that here so you can evaluate if it’s worthwhile for your use-case:

  • The primary benefit of Jsonrs, and what it was originally built to solve, is reducing memory usage when encoding large objects into large JSON strings.
  • While encoding large objects that resulted in ~80MB JSON strings, Jason was using gigabytes of peak memory and resulting in OOM-kills. Jsonrs was created to fix this particular production issue, and was able to do the same encode using just the 80MB of the resulting string (plus 12 bytes of additional memory), and at around 5 times the speed.
  • Jsonrs’s performance advantage shrinks as the data being worked with gets smaller and less complex. You’re only going to see a big difference with fairly large or complex JSON.
  • OTP 24’s JIT noticeably improved the run time performance of Jason and Poison, so the performance gap has gotten a bit smaller since Jsonrs was first created. The memory usage gap doesn’t seem to have changed much from what I’ve seen, and is still very dramatic.

So the target demographic for Jsonrs is applications doing large JSON encodes, and these applications are likely to see a huge improvement in memory usage and a smaller but still significant improvement in speed.
I used to say that it isn’t worth using Jsonrs for people who aren’t dealing with encoding fairly large objects. If you aren’t going to see a huge difference in performance, why deal with the friction of building a Rust NIF in your project?
But now that precompiled libraries are available for common platforms, the barrier to using Jsonrs is much lower, and it might now be worth it for more people to use. I expect that you won’t see much of a difference if your application isn’t in the target demographic, but if you ever wanted to try it out anyway, now is a great time!

Most Liked

benhaney

benhaney

Oh yeah that definitely sounds like an ideal use case. If you try out Jsonrs for that I’d love to hear about it.

cjbottaro

cjbottaro

We have this exact use case. We use Elixir to read Postgres CDC and write transactions as JSON to Nats Jetstream. We just kept increasing our Koob pods memory limit to work around the problem.

adammokan

adammokan

Looking at the metrics overnight and digging into the amount of data processed - I see nearly a perfect 50% decrease in memory usage on our average inputs and in the largest 10% of the input payloads it is showing close to a 65% memory decrease.

Unfortunately the data we process could be considered “consistently inconsistent” and really depends on our customers’ behavior in various other platforms as we integrate with dozens of 3rd party systems and synchronize / process that data. Meaning that we have a range of input documents that can be as small as 50kb and as large as 25mb in size. My current customer workload averages just over five million of these transactions each day.

Another thing I should note is that my changes were purely around JSON decoding as that was the biggest area for improvement due to that wide range of document sizes and inconsistency there.

So take all of this as a grain of salt, given the variables at play.

adammokan

adammokan

Great library @benhaney !

I rolled this into strategic portions of my data processing systems yesterday and I’m honestly a bit shocked at the difference I’m seeing on the memory usage.

kartheek

kartheek

Nice - can you share some metrics if it is possible?

Where Next?

Popular in Libraries Top

mhanberg
I just released the first version of Temple: an HTML DSL for Elixir and Phoenix! You can read this blog post or the docs for more info...
New
zoltanszogyenyi
Hey everyone :wave: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-source U...
New
mathieuprog
Hello :wave: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First and fore...
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
Qqwy
Solution is a library to help you with working with ok/error-tuples in case and with-expressions by exposing special matching macros, as ...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Sub Categories:

We're in Beta

About us Mission Statement