使用 ruby 进行套接字编程是个好主意吗?
我选择的语言是 Ruby,但由于 twitter,我知道 Ruby 无法处理大量请求。使用它进行套接字开发是一个好主意吗?或者我应该像 twitter 开发人员那样使用像 erlang 或 haskell 或 scala 这样的函数式语言吗?
My language of choice is Ruby, but I know because of twitter that Ruby can't handle a lot of requests. It is a good idea using it for socket development? or Should I use a functional language like erlang or haskell or scala like twitter developers did?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我工作的公司在我们的网站上使用 Ruby。到目前为止,我们已经处理了超过 34,000,000,000 次点击。我们每天处理大约 10,000,000 次点击没有问题。每天的点击峰值已超过40,000,000次。
可扩展性取决于很多因素。例如,与读取相比,我们的数据库的写入百分比高得不成比例。虽然大多数网站的读取率约为 90%,写入率为 10%,但我们更接近 50%-50%。我的观点是,可扩展性受到很多因素的影响。如果您受到数据库限制(网络应用程序经常出现这种情况),那么无论您使用什么语言,您都将等待数据库。
如果您正在考虑处理大规模数据,则需要考虑很多事情。分片数据库、memcached 等。您的应用程序使用的语言只是一个方面,而且通常(但并非总是)是可扩展性的一个小方面。
Ruby 对您来说可能是一个不错的选择,但其他语言也有很多值得喜欢的地方。例如,Erlang 努力让错误恢复变得更容易。
The company I work for uses Ruby for our web site. We have so far handled a little over 34,000,000,000 hits. We have no problem handling around 10,000,000 hits per day. Peak hits have exceeded 40,000,000 hits per day.
Scalability depends on a lot of factors. Our databases do a disproportionately high percentage of writes compared to reads, for example. While most websites do about 90% reads to 10% writes, we are closer to 50%-50%. My point is that scalability is affected by a lot of factors. If you are database-limited, as is often the case for web apps, it won't matter what language you use, you'll be waiting on your database.
There's a lot to think about if you are looking at handling large scales. Sharding databases, memcached, etc. etc. etc. etc. The language you use for your application is just one aspect, and often, though not always, a small aspect of scalability.
Ruby may be a good option for you, but there's a lot to like in other languages. Erlang tries hard to make it easier to recover from errors, for example.
我不确定 Twitter 团队学到的有关 Ruby(更具体地说,Rails)和扩展的任何“经验教训”是否适用于您的项目。他们看到的流量比大多数人合理预期的要多得多。
至于套接字和 Ruby,请查看我喜欢 Unicorn 因为它是 Unix。这是一本关于在 Ruby 中使用套接字的非常有趣的读物。
I'm not sure that any "lessons" that the Twitter team has learned about Ruby (more specifically, Rails) and scaling would apply to your project. They're looking at WAY more traffic than most people can reasonably expect to see.
As far as sockets and Ruby go, check out I like Unicorn because it's Unix. It's quite an interesting read about doing sockets in Ruby.
我想先提供一些背景信息。我对 Scala 社区非常活跃,对于任何项目我都会选择 Scala 而不是 Ruby。
因此,话虽如此,请继续使用 Ruby,除非您确实遇到了障碍。如果 Ruby 是您选择的语言,那么您可能永远不会对您提到的选择感到满意,尤其是静态类型的语言。
学习一门新语言可能是件好事,这样如果您需要替代语言,可以有一些可以依靠的东西。对于你的情况,我推荐 Clojure 或 Erlang。 Scala 是一种优秀的静态类型、面向对象语言,具有函数式编程优势。它可能比其他类型更容易学习,但是真正喜欢动态类型的人并不容易转换为静态类型。
至于 Haskell,它是最棒的语言之一(并且比同样棒的替代语言得到更好的支持和流行),并且可以像其他语言一样打开你的思维。掌握起来也很困难。
I'd like to provide a bit of context first. I'm pretty active with the Scala community, and I would choose Scala over Ruby for any project.
So, having said that, keep with Ruby unless you actually hit barrier. If Ruby is your language of choice, it might just be that you'll never be happy with the choices you mention, particularly the statically typed ones.
It might be good to learn a new language, to have something to fall back on if you need an alternative. In your case, I'd recommend Clojure or Erlang. Scala is a good statically typed, OO language with functional programming perks. It might be easier to learn than the others, but people who really like dynamic typing don't convert to static typing easy.
As for Haskell, it's one of the most awesome languages out there (and much more well support and popular than the equally awesome alternatives), and can open your mind like nothing else. It's also tough to master.
如果 Ruby 是您最喜欢的语言,那么这是一个好主意。使用你所知道的和你喜欢的总是更好
If ruby is your favorite language, yes it is a good idea. It is always better to use what you know and what you like
虽然您可能会从 Erlang 等函数式语言中获得更好的性能,但 Ruby 的适用性实际上取决于您想要实现的目标。例如,你要处理多少个请求可能是第一个问题,如果使用 Erlang 的性能优势并没有多大区别,使用你熟悉的语言,如果不需要,为什么要学习一门新语言呢?
Whereas you may get better performance from a functional language such as Erlang the suitability of Ruby will really depend on what you are trying to achieve. For example how many requests are you going to be handling is probably the first question, if the performance benefits of using Erlang don't make much difference use something you are comfortable with, why learn a new language if you don't have to?
如果您使用 Haskell、Erlang 或 Scala 等快速并发语言,您至少可以选择继续使用自己喜欢的高级语言。对于 Ruby,性能瓶颈意味着无论如何都要切换到编译的 C(或 Haskell,或...)来提高速度。
Ruby 的优点是拥有良好的前端框架。
You at least have the option of staying in your favorite high level language if you use a fast, concurrent language like Haskell, Erlang or Scala. With Ruby, performance bottlenecks will mean switching to compiled C (or Haskell, or ...) for speed anyway.
Ruby has the advantage of good frontend frameworks.
我还在许多项目中使用了 Ruby,不过我最近转向了 Scala 并且非常喜欢它。对于 Ruby 中的网络内容,我听说过(但自己从未尝试过)的一件事是 EventMachine。它使用了 Reactor 模式,就像扭曲一样,看起来很扎实。
I have also used Ruby for many projects though I've recently moved to Scala and like it quite a bit. One thing that I've heard good things about (but never tried myself) for network stuff in Ruby is EventMachine. It uses the Reactor Pattern just like twisted and it seems quite solid.
关键是要有一个 C/C++ 低级库来为您执行套接字复用。套接字多路复用使 TCP 服务器进程真正成为多用户。 C 中的此类库(这正是您想要的)可以是 libevent/libev... 以及 c++ boost::asio 中的库。 Python 已经扭曲了它在幕后执行的操作。
如果您获得这样的库并在 ruby 中使用它,您应该能够很好地实现大多数套接字程序。在支持多进程而不是多线程的 UNIX 操作系统上尤其如此。
The key is to have a low level library in C/C++ that does the socket multiplexing for you. Socket multiplexing is what makes a TCP server process truly multi-user. such libraries in C (which is what you want) could be libevent/libev... and in c++ boost::asio. Python has twisted that does it behind the scenes.
If you get such a library and use it in ruby you should be able to implement most socket programs fairly well. This is especially true on UNIX oses which favour multi-process to multi-threading.
最近编写了一个使用 Ruby 和 Java 套接字的项目(实际上现在仍在这样做),我会说不。除非您打算编写一个基本的阻塞聊天服务器,否则 ruby 套接字实现的文档很少。我发现用 C 或 Java 编写更简单,Ruby 封装了本机套接字,您可能会想知道现在到底如何使用它。之前我用C写过很多windows、Linux等平台上的socket代码,压力较小。
我的 Ruby 代码现在非常小并且运行良好,达到这一点确实很痛苦。
Having recently written (actually still doing so now), a project using sockets with Ruby and Java I would say no. The ruby socket implementation is poorly documented unless you plan on writing a basic blocking chat server. I found writing in C or Java simpler, Ruby wraps up native sockets and your kinda left wondering how the hell to use it now. I have previously written plenty of socket code on windows, Linux and other platforms in C, with less stress.
My Ruby code now is very small and works well, getting to that point was a real pain.