高性能REST API - 哪種語言/堆棧?

[英]High-performance REST API - Which language/stack?

I am developing a site using Ruby on Rails. I would like to develop the REST API (JSON) separately to maximize performance, the Rails stack just takes away too much.

我正在使用Ruby on Rails開發一個站點。我想分別開發REST API(JSON)以最大限度地提高性能,Rails堆棧只會帶走太多。

Are there any performance benchmarks out there? What do you think would yield the best performance? I am currently thinking about the following, because that's what I have experience with. Is there anything else I should consider? It should be lightweight.


  • node.js
  • Scala Spray (http://spray.io/)
  • Scala Spray(http://spray.io/)

  • Ruby Sinatra


4 个解决方案



Summary: Maximize your performance as a developer. Use stack you know best. First make it work, then make it fast.


Are there any performance benchmarks out there?


There are all kinds of benchmarks out there. Let's say, node.js can handle 100k HPS (helloworlds per second) and Sinatra can only do 80k. What does it tell you? Nothing.

那里有各種各樣的基准。比方說,node.js可以處理100k HPS(每秒helloworlds),而Sinatra只能處理80k。它告訴你什么?沒有。

Also, sometimes higher performance comes at a great cost. Take ruby C extensions, for example. Sure, C runs faster than Ruby, but it prevents other threads from running on other cores (because of GIL).

此外,有時更高的性能需要付出巨大的代價。以ruby C擴展名為例。當然,C運行速度比Ruby快,但它阻止其他線程在其他核心上運行(因為GIL)。

So, don't choose tech only by benchmark figures from the internet. There are so many factors to consider besides raw HPS number.


LINK: If you think Rails is too heavy, you should try rails-api. It's basically a stripped down version of Rails (you don't need things like cookie authentication or MSIE rendering helpers in an api server).


Personal story

I run an API server that handles some load. First version was written with Rails. Then I thought "Hey, Rails Is Bloated (c), let's rewrite everything with Sinatra". And then I had waves of frustration coming one after another. It turned out that Rails does a lot of small but helpful things which I didn't appreciate. I gave up, rewrote it with Rails again (applying lessons learned) and lived happily ever since.




This falls into the 'should I rewrite my apache/php site in C with a customized webserver because it's slow' bucket.

這應該是'我應該用C語言服務器重寫我的apache / php網站,因為它很慢'。

I vote for picking your favorite stack to work in and utilize good caching and using whatever opcode optimization you've available if your code isn't compiled. Then make sure you've got adequate server resources and load balance horizontally if needed to handle the traffic.


I've yet to see many API's whose primary reason for poor performance is the choice of technology stack unless they're handling facebook/twitter size traffic or doing computationally intensive work.

我還沒有看到許多API,其性能不佳的主要原因是技術堆棧的選擇,除非他們處理facebook / twitter大小流量或進行計算密集型工作。

You'll see in just about any stack you choose if properly utilized more latency occur in:


  1. Transport of unnecessarily large amounts of data to the client
  2. 將不必要的大量數據傳輸到客戶端

  3. Performance hits due to poor SQL or other data source fetching slowness
  4. 由於SQL或其他數據源的緩慢導致性能下降

I'd also highly recommend if you're already using a RoR stack to stick with it. Trying to optimize and code in different stacks will generally result in weaker code in both--keep your dev's brains focused in one pool of futz at a time.

如果你已經使用RoR堆棧堅持下去,我也強烈推薦。嘗試優化和編碼不同的堆棧通常會導致兩者中的代碼較弱 - 讓你的開發人員的大腦一次集中在一個futz池中。



It really depends on your usecases and team experience/preferences - BUT, if you are truly open-ended about whatever stack, our personal experience was:

這真的取決於你的用例和團隊經驗/偏好 - 但是,如果你對任何堆棧真的是開放式的,我們的個人經驗是:

  • Scala + Scalatra + Jetty was simple and very fast.
  • Scala + Scalatra + Jetty簡單而且非常快。

  • Node + Express was surprisingly close in speed - but with the benefit (in our case) of flexible Javascript code.
  • Node + Express的速度令人驚訝地接近 - 但具有靈活的Javascript代碼的好處(在我們的例子中)。

  • Ruby was much less performant than either, but had cool code.
  • Ruby的性能要低得多,但代碼很酷。

Scala was a winner because our shop has lots of experience in the JVM. Hoever, we hope to leverage node more in the future do to the simplicity.


I'm sure experienced Ruby guys could do improve and tune things, but it wasn't worth the learning-curve for us and a non-starter.




Check out spray -- I like it because it is lightweight and well-designed (modular, asynchronous, sane API). For web service stuff most web frameworks just come with too much baggage.

檢查噴霧 - 我喜歡它,因為它重量輕,設計精良(模塊化,異步,理智的API)。對於Web服務而言,大多數Web框架都帶有太多的包袱。



粤ICP备14056181号  © 2014-2021 ITdaan.com