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.

Where Next?

Popular in Announcing Top

deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
dominicletz
Hi, I thought I had posted my library before but seems I hadn’t. The project is still in early stages but it’s growing and so I think it...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have ...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New
josevalim
Hello everyone, We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement