Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce latency of data sent through a REST api

I have an application which obtains data in JSON format from one of our other servers. The problem I am facing is, there is is significant delay when when requesting for this information. Since a lot of data is passed (approx 1000 records per request where each record is pretty huge) is there a way that compression would help reducing the speed. If so which compression scheme would you recommend.

I read on another thread that they pattern of data also matters a lot on they type of compression that needs to be used. The pattern of data is consistent and resembles the following

 :desc=>some_description
 :url=>some_url
 :content=>some_content
 :score=>some_score
 :more_attributes=>more_data

Can someone recommend a solution to how I could reduce this delay. They delay is approx 6-8 seconds. I'm using Ruby on Rails to develop this application and the server providing the data uses Python for the most part.

like image 220
Sid Avatar asked Jan 23 '23 09:01

Sid


1 Answers

I would first look at how much of this 8s delay is related to:

  1. Server side processing (how much took for the data to be generated) There are a lot of techniques to improve this time, including:

    • DB indexes

    • caching

    • a faster to_json library

Some excellent resources are the NewRelic podcasts on Rails scalability http://railslab.newrelic.com/2009/02/09/episode-7-fragment-caching

  1. Transmission delay(how much time took for the data to be sent between the server and the client)

    • if the keys are pretty much the same, you may implement the sollution from Compression algorithm for JSON encoded packets? ; You may want to look at https://github.com/WebReflection/json.hpack/wiki/specs-details and http://www.nwhite.net/?p=242

    • in addition to this, you may also compress (gzip) it from your frontend server http://httpd.apache.org/docs/2.0/mod/mod_deflate.html http://wiki.nginx.org/NginxHttpGzipModule

    • If the data structure is constant and you can also try to implement a binary service, that is much much faster, includes compression, but also more difficult to mantain, like thrift: http://www.igvita.com/2007/11/30/ruby-web-services-with-facebooks-thrift/

    • If this is suitable to your needs, maybe you can make some kind of a versioning/cache system server-side, and send only the records that were modified (but that is pretty heavy to implement)

like image 80
Vlad Zloteanu Avatar answered Jan 31 '23 09:01

Vlad Zloteanu