TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why Events Are A Bad Idea (for High-concurrency Servers)

41 点作者 uriel将近 14 年前

9 条评论

tptacek将近 14 年前
This has been posted to HN a bunch of times over the years:<p><a href="http://www.hnsearch.com/search#request/submissions&#38;q=Events+bad+idea&#38;start=0" rel="nofollow">http://www.hnsearch.com/search#request/submissions&#38;q=Eve...</a><p>What I said about it last time:<p>The problem with the article is that it's superficial. At its core, it's premised on the idea that threads are inherently easier than events, because they produce straight-line code and automatically handle tracking the state needed across a transaction. If that's true, then the article says threads are just as good as events once we solve all the problems that make threads dangerous or slow.<p>But it's not a given that threads are easy. In particular, as anyone who has spent time doing high-performance threaded code will tell you, there's the effort you have to put in to make sure your threaded code is correct, and then the 2x more time you have to put in to make sure that it's efficient; in particular, you have to do synchronization without effectively serializing all your threads. Which is a huge pain.<p>I think this is gradually becoming a moot point, as modern languages --- all of which have some kind of anonymous function and some approximation of closures --- allow us to write evented code that looks and feels like straight-line code. jQ callbacks in Javascript are the most vivid illustration of this idea (I suppose the node.js people are carrying that even further; I haven't played with it).
评论 #2907670 未加载
评论 #2907737 未加载
评论 #2907872 未加载
评论 #2907733 未加载
Peaker将近 14 年前
They acknowledge that shared mutable state is (very) problematic in conjunction with preemptive multitasking, so they're actually arguing for cooperatively-multitasked threads as opposed to events. In both systems, shared-mutable state is being used between the concurrency code units.<p>I think they're neglecting a third option: Shared-immutable state, with very little, explicit, shared-mutable state. This is the model used by Haskell threads, and Erlang processes. Like Events/Cooperative-threads, it does away with the horrible semantics of shared-by-default, and gives the parallelism benefits lost when switching to cooperative multitasking.<p>But I think there are still 2 crucial differences between event-based systems and cooperative threads that I did not see in the paper:<p>* Events make it clear where context-switch/reentrancy points are. When I call a library function, then I generally have a very good idea if it could "call me back" (have I given it callbacks to call me back?) and if not -- I know it is safe to call it and do not have to reason about my internal state in every given call. Even with cooperative threads, a "yield" point may be lurking in any library you're calling. This effectively means that any library call can potentially do anything, including switch to "higher-level" code that may re-enter your own code. This makes re-entrancy considerations far more complicated. It is solvable, though, if you make sure that yield-points are "tagged" with some token that must be passed as an argument. Then, a function signature and call pattern tells you whether or not it can yield to anywhere.<p>* Implementing user-level threads may be very cheap, compared with standard posix threads. But it is still many times more expensive than using explicit data allocations as in event-based programs. Even if using "split stacks", a user-level thread is still going to pay with around a 4Kbyte stack. 1 million threads thus take 4GB of memory before they even do anything. This means that you <i>still</i> have to be wary of creating threads as freely as you would register callbacks -- and requires mixing in event-based programming, or manual event loops, into your threads. This mixture is worse than uniformity of any method.
评论 #2907966 未加载
评论 #2907990 未加载
wmf将近 14 年前
This paper is one part of the long-running threads vs. events debate, and it probably isn't that helpful to read it in isolation. Erlang, JRockit, and Go implemented the kind of scalable green threads proposed in this paper, although I don't think they were inspired by it. Most programmers who care about high concurrency (other than Erlangers) decided to just bite the bullet and accept the difficulty of writing event-driven code rather than try to make threads scale. The fact that people prefer to struggle along with events doesn't negate their difficulty, though.
bcx将近 14 年前
That paper is also 8 years old (or at least the timestamp on that page is way off) -- which was before node even existed.<p>If it was so easy to implement their proposed high performance thread system (disclaimer: haven't read the paper) -- Why don't we see it in production 8 years later?<p>I'd argue that we don't see it in production, because it lacks some of the advantages they propose in the abstract.
评论 #2907541 未加载
评论 #2907531 未加载
评论 #2908237 未加载
schiptsov将近 14 年前
Also good readings: type in google search:<p><pre><code> threads +posix +signals threads +stack +usage thread +safe +function</code></pre>
velshin将近 14 年前
Circa 2003. fast forward to 2011... Youtube, Facebook et al seem to have refuted this. Tornado/epoll + nginx ftw
评论 #2907633 未加载
评论 #2907620 未加载
评论 #2907538 未加载
atarian将近 14 年前
Interesting.. just when I was about to pick up on Node.js too.
评论 #2907477 未加载
评论 #2907533 未加载
iamelgringo将近 14 年前
Anyone from the Node community care to tackle this?
评论 #2907690 未加载
nirvana将近 14 年前
This paper has been proven correct, if you replace the word "threads" with "processes" and note that erlang has provided a solution with the compiler support they advocate. Outside of erlang, (and some equivalently obscure environments) little attempt has been made to make concurrent processes manageable, and this is why you see the predominance of event-based systems. We're in a multi-core era and and you can't really say you're "concurrent" when your event model only runs on a single core.<p>While I think, if your choices are the typical thread support and its problems and an event based system then events are the way to go... I believe the great majority of developers have decided to use node or twisted, et. al, either because they'd never heard of erlang, never understood its strength, or weren't willing to deal with the initially alien looking syntax.<p>I think this is a mistake, but rather than fight it, I'm working on a solution that allows coffeescript to run within a distributed system based on erlang. I've had to provide some work arounds and conventions for the coffeescript developer to follow a sequential programming style, but have the ability for coffeescript to really run concurrently on multiple CPUs or multiple machines, with a common shared state.<p>(I described some of this here: <a href="http://news.ycombinator.com/item?id=2906290" rel="nofollow">http://news.ycombinator.com/item?id=2906290</a> and went into the architecture a bit more here: <a href="http://news.ycombinator.com/item?id=2848647" rel="nofollow">http://news.ycombinator.com/item?id=2848647</a> )<p>Granted, it is easy for me to say it should be done a different way, and until I publish code it is all talk. I'm very aware of this... but I would like to suggest that there really is a difference in erlang, and that events are a compromise that don't inherently actually allow for concurrency. Concurrency is a better solution, isn't that hard to do now (if you write straight up erlang) and I'm working on making it even easier (if you're willing to be constrained a little, bit, but I think the popularity of node.js shows people are willing.)<p>If it seems I'm shilling for my project, I am a little bit, but I'm also wanting to get it out there so that if I'm making a mistake maybe someone can identify it... or be inspired to build a better way on top of it.<p>If you want updates, I have a twitter account @nirvanacore that only talks about this project.
评论 #2907834 未加载
评论 #2907985 未加载