Hacker News new | ask | show | jobs
by cletus 592 days ago
> no object allocated as part of a request should survive longer than the request itself

So I've spent a lot of time doing Hack (and PHP) as well as Java, Python and other languages. For me, as far as serving HTTP requests goes, Hack/PHP are almost the perfect language. Why?

1. A stateless functional core. There's no loading of large libraries, which is an issue with Python and Java in certain paradigms. The core API us just functions that mean startup costs for a non-stateful service are near zero;

2. The model, as alluded to the above quote, basically creates temporary objects and then tears everything down at the end of the request. It's so much more difficult to leak resources this way as opposed to, say, a stateful Java or C++ server. PHP got a lot of hate unjustly for its "global" scope when in fact it's not global at all. "Global" in PHP/Hack is simply request-scoped and pretty much every language offers request-scoping;

3. There's no threading. Hack, in particular, uses a cooperative async/await model. Where you'd normally create threads (eg making a network request), that's handled by the runtime to make an async/await call out of non-blocking I/O. You never have to deal with mutexes, thread starvation, thread pools, lock ups, etc. You never want to deal with that in "application" or "product" code. Never.

So this article is specific to Ruby-on-Rails, which obviously still has persistent objects, hence the need for GC still.

How Facebook deals with this is kinda interesting. Most FB product code uses an in-memory write-through graph database (called TAO, backed to MySQL). There is an entity model in Hack on top of this that does a whole bunch of stuff like enforcing privacy (ie you basically never talk to TAO directly and if you do, you're going to have to explain why that's necessary, and you absolutely never talk to MySQL directly).

But the point is that persistent entities are request-scoped as well (unlike RoR I guess?).

1 comments

If the core API is just functions, how do stateful applications handle connections to this persistent storage? Can you still have a connection pool, or does every request pay the extra latency to start a new connection and re-authenticate?
i think they do now but originally one of the reasons people use mysql with languages that are connection per request was that mysql connections were very cheap.
And why pgbouncer used to be considered an essential part of a Postgres web-app-backend deployment — if your business layer didn’t pool and reuse connections, then having an external shim component that pools and reuses connections would solve a lot of the impedance mismatch.