Home > Java, Optimization > Optimizing response time for JSON queries

Optimizing response time for JSON queries

I’ve recently been in the situation where I needed to reduce the loading time of some JSON queries. I’ve remembered some very practical and easy to implement strategies to significantly reduce the response time.

In order, they are:

  • Implement caching

There are 2 options for implementing caching: client-side and server-side.

You could implement client-side caching by saving the results of your queries directly in Javascript. This reduces the response time by effectively eliminating all communication with the server. The first request for data will always last longer, for each client, but the others will happen instantly. The downside is there’s an increase in the memory your page will use. This will have significant impact on slower systems. Also, if you’re working in a one-page web application, the cache will be one more thing to guard against creating memory leaks. As far as client-side caching goes, you can always try to let the browser cache responses.

Server-side caching, on the other hand, won’t eliminate the communication time between the client and the server, but it will eliminate the one between the server and the database. The first request for data will last longer, while all subsequent ones will happen faster, no matter the client. This will also increase the memory usage, but this time on the server. As a Java developer, I can say it’s much easier to correctly manage memory server-side than client-side. There are also frameworks to help you implement a more polished cache, which offer features like cache invalidation, time to keep an object in cache, maximum size of the cache etc.

  • GZip responses server-side

In my case, GZipping responses helped reduce the size from 8.8 MB to 2,2 MB. This was an extreme case, but it happened nonetheless. This could also help improve user experience on mobile devices, where the total monthly allowed data transfer is limited. In terms of size, this is probably the most efficient reducer. I’ve found this very helpful example on-line, which implements GZipping for Servlets: http://tutorials.jenkov.com/java-servlets/gzip-servlet-filter.html

  • Flatten response

Let’s say you’re returning a list of JSON objects, and they have a structure like this:

    prop1 : "value1",
    prop2 : "value2",
    propn : "valuen"

Let’s say you’re always returning 100 of these rows. This means that the total response size will be around (16 x n x 100) characters.

You could return these in a table-like structure. The response would become a list of lists. The first element in the list would be an array with all the property names (similar to a table header). Subsequent rows would each contain the values for the respective properties, in the same order. The resulting structure would be something like this:

    [prop1, prop2, ..., propn],
    ["value1", "value2", ..., "valuen"]

The total size of the response, for 100 rows, in this case would be (5 x n + 8 x n x 100). If we compare this result with the previous one we will reach a 50% reduction. To have a rule of thumb regarding this: the size of the response will be reduced by the ratio of field names from the total number of characters.

I didn’t include in this article other “standard” approaches like minifying Javascript and CSS resource files. They are better known in general. If you have any other original ideas regarding this subject, please leave a comment below.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s