如何减少通过 REST API 发送数据的延迟
我有一个应用程序从我们的其他服务器之一获取 JSON 格式的数据。我面临的问题是,请求此信息时存在明显的延迟。由于需要传递大量数据(每个请求大约 1000 条记录,其中每条记录都非常大),因此有一种压缩方法可以帮助降低速度。如果是这样,您会推荐哪种压缩方案。
我在另一个线程上读到,数据模式对它们也很重要需要使用的压缩类型。数据模式是一致的,类似于以下内容
:desc=>some_description
:url=>some_url
:content=>some_content
:score=>some_score
:more_attributes=>more_data
有人可以推荐一个解决方案来减少这种延迟吗?他们的延迟大约是 6-8 秒。我使用 Ruby on Rails 来开发这个应用程序,提供数据的服务器大部分使用 Python。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我首先会看看这 8 秒的延迟有多少与以下内容相关:
服务器端处理(生成数据需要花费多少时间)
这次有很多技巧需要改进,包括:
数据库索引
缓存
更快的 to_json 库
一些优秀的资源是 Rails 上的 NewRelic 播客可扩展性http://railslab。 newrelic.com/2009/02/09/episode-7-fragment-caching
传输延迟(数据在服务器和客户端之间发送需要多长时间)
如果密钥几乎相同,您可以实现 JSON编码数据包的压缩算法? ;您可能需要查看 https://github.com/WebReflection/json。 hpack/wiki/specs-details 和 http://www.nwhite.net/? p=242
除此之外,您还可以从前端服务器压缩(gzip)它
http://httpd.apache.org/docs/2.0/mod/mod_deflate。 html
http://wiki.nginx.org/NginxHttpGzipModule
如果数据结构是不变的,您也可以尝试实现二进制服务,这要快得多,包括压缩,但也更难以维护,例如 thrift:
http://www.igvita.com /2007/11/30/ruby-web-services-with-facebooks-thrift/
如果这适合您的需求,也许您可以在服务器端制作某种版本控制/缓存系统,并仅发送已修改的记录(但这实现起来相当繁重)
I would first look at how much of this 8s delay is related to:
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
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)
gzip 可能会显着减小文本数据并优化加载速度。 YSlow 也推荐了它。
gzip might significantly reduce the size of text data and optimize load speeds. It's also recommended by YSlow.