Categories: Ruby on Rails

Fast JSON API serialization with Ruby on Rails

by Shishir Kakaraddi, Srinivas Raghunathan, Adam Gross and Ryan Johnston

We are pleased to announce the open source release of Fast JSON API gem geared towards Ruby on Rails applications.

Introduction

Fast JSONAPI is aimed at providing all the major functionality that Active Model Serializer (AMS) provides, along with an emphasis on speed and performance, by meeting a benchmark requirement to be 25 times faster than AMS. The gem enforces performance testing as a discipline.

Active Model Serializer is a great gem and Fast JSON API was inspired by it when it comes to declaration syntax and features. But, AMS begins to slow down when a model has one or more relationships. Compound document, AKA sideloading, on those models makes AMS slow down further. Throw in a need for infinite scroll on the UI, and AMS’s slowness starts becoming visible to users.

Why optimize serialization?

JSON API serialization is often one of the slowest parts of many well implemented Rails API’s. Why not provide all the major functionality that AMS provides and with great speed?

Features:

  • Declaration syntax similar to Active Model Serializer
  • Support for belongs_to, has_many and has_one
  • Support for compound documents (included)
  • Optimized serialization of compound documents
  • Caching
  • Instrumentation with Skylight integration (optional)

How do you write a serializer using Fast JSONAPI?

We like the familiar way Active Model Serializers lets us declare our serializers. Declaration syntax of fast_jsonapi is similar to AMS.

class MovieSerializer
include FastJsonapi::ObjectSerializer
 attributes :name, :year
has_many :actors
belongs_to :owner, record_type: :user
belongs_to :movie_type
end

How fast is it compared to Active Model Serializers?

Performance tests indicates a 25–40x speed gain over AMS, essentially making serialization time negligible on even fairly complex models. Performance gain is significant when the number of serialized records increases.

Difference in performance

Don’t believe us? You can run the benchmark tests for yourself. Refer to readme.

Dependency

JSON API is the anti-bikeshedding tool.

Future Work

We plan to add more features to the gem. We welcome suggestions, improvements, corrections and additional tests.


Fast JSON API serialization with Ruby on Rails was originally published in Netflix TechBlog on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source

https://medium.com/netflix-techblog/fast-json-api-serialization-with-ruby-on-rails-7c06578ad17f

vinova

Share
Published by
vinova

Recent Posts

Guide to Using AI in Recruitment Effectively in 2024

The recruitment picture is changing rapidly, and AI in recruitment is at the forefront of…

20 hours ago

What is Multimodal AI? 10 Creative Applications and Real-World Examples

Multimodal AI is a groundbreaking technology that combines multiple modalities, such as text, images, and…

2 days ago

Top 10 AI Applications in the Energy Sector for 2024

Artificial intelligence (AI in the energy) sector is revolutionizing how we produce, distribute, and consume…

3 days ago

Top Mobile App Monetization Strategies for 2024

Nowadays, monetization application is the end game of mobile app development. Whether you're an indie…

4 days ago

Top Reasons Why Mobile Apps Fail and Lessons for Success

Nowadays, many mobile apps fail to make an impact. From poor research to bad user…

5 days ago

Comprehensive Guide to VisionOS App Development 2024 for Beginners

Apple's VisionOS, the operating system powering the Vision Pro headset, is paving the way for…

6 days ago