Jayway Jsonpath can consume most of your service CPU, If you are using it in wrong way.
I have been fortunate enough to work on service optimisation tasks and in optimisation journey have solved few issues that I think has major role in optimising our service.
One of these issue is CPU consumption by Jayway Jsonpath library. Jayway Jsonpath library were using most of service CPU and we were not able to support no of users that we wanted to support on our platform.
After digging around and doing cpu profiling we were able to find out that Jayway jsonpath library were using almost all of our CPU. I have written separate blog on how to do a service cpu/memory profiling using Yourkit tool. Please do take look if you are interested.
Jayway jsonpath library is used to access the json attributes by json path.
In the above example code snippet we have extracted value of json attribute “a” and printed it.
Usage of Jayway Jsonpath library in our project is very complex. Our service act as middleware which call other third party services and converts response of these third party services into our own Json Contract using JayJason path library and return the response to caller of our API’S.
We were using Jayway Jsonpath in wrong way that’s what triggered high CPU usage. We were parsing json object for each json attribute read instead of parsing json once and using it for subsequent attribute read.
After identifying issue with our approach to read attributes value we have modified our code to parse document context only once and use it subsequently to read all the json attribute values.
Below is the example sample code where we have parsed json document once and reused it to read subsequent json attributes.
With this code changes CPU usage of our service has dropped to 80% and we are able to support the no of users that we wanted to support.
Thanks For reading.
Please 👏 or follow me if you want to read more from me.