r/programming • u/freebit • Oct 27 '17
Netflix: Removing client-side React.js improved performance by 50%
https://twitter.com/NetflixUIE/status/923374215041912833161
u/clarle Oct 27 '17 edited Oct 27 '17
Hey, I work on the team at Netflix that gave the talk on React in the signup flow in the tweet.
We still love React and are using it everywhere else - it's only been removed client-side from the landing page, where we're optimizing it for initial load. It's still being rendered using React on the server.
The full talks are available here if people want to watch them:
https://www.youtube.com/watch?v=V8oTJ8OZ5S0&t=11m30s
Thought I'd also provide some more context on some common questions that people have asked.
Why are you using React to render a landing page instead of static HTML?
The Netflix landing page is a lot more dynamic than most people think it is.
It's our most heavily A/B-tested page in the signup flow, with even some machine learning models being used to customize the messaging and imagery that you get depending on location, whether or not you were a previous Netflix member, device type, and a lot more. Even beyond that, Netflix supports almost 200 countries now, and there's a different combination of localization, legal challenges, and value messaging for each one. We end up sharing a lot of the logic and UI for these A/B testing and localization challenges throughout the signup flow, mainly through React components.
The example I always love to give is the <TermsOfUse/> component that we have, which to a Netflix customer signing up is literally one or two checkboxes on the UI, but has some of the most complicated logic in the codebase due to the vast number of countries and user states we support. Because of all this, it's more valuable for us to share these common React components across the entire signup process, both the landing page and the rest of the flow, which is a single-page React and Redux application.
We've seen a lot of conversion value though in improving the performance of the landing page, especially in countries with slower connections, but we don't also want to re-duplicate a lot of the shared UI logic that we have.
The tradeoff that we decided to make is to server-render the landing page using React, but also pre-fetching React / Redux / the code for the rest of the signup flow while on it. This optimizes first load performance, but also optimizes the time to load for the rest of the signup flow, which has a much larger JS bundle size to download since it's a single-page app.
What's the performance metric that's being used?
It's TTI (Time to Interactive), when the user can fully interact with the page. This is different than TTR (Time to Render) for us, when the user can fully view the page. There's more information in the talk about the differences.
Why not Service Workers or some other pre-loading / caching mechanism?
We have been experimenting with it, but it's mainly the lack of some browser support - Safari is the main one. Generally the Netflix signup flow needs to have more legacy browser support than the Netflix member experience. Lots of people sign up on a pretty old browser, but only ever watch Netflix on the native mobile apps or a TV device.
Feel free to comment here or tweet at Tony Edwards or myself if there's any other questions that we can help answer.
Though from a lot of experience, 140 characters isn't always enough to provide enough context for JavaScript framework discussions. ;)
If these sort of performance and UI challenges seem interesting to you, our team is also hiring for UI engineers and an engineering manager!
Senior Software Engineer (React, Node): https://jobs.netflix.com/jobs/864767
Senior Software Engineer (Android): https://jobs.netflix.com/jobs/864766
Engineering Manager: https://jobs.netflix.com/jobs/865119
Cheers!
4
u/digitallimit Oct 27 '17
Thanks for the clarifications! The title freaked me out at first, but this reasoning I can follow.
2
1
-2
u/Drifts Oct 27 '17
Thanks for the explanation!
Quick question that may or may not be related: Any time I rewind by a small amount of time (approximately 10 seconds or more), the stream loads and gets stuck at 99% for anywhere between 15 seconds to sometimes up to a minute (!!). (I have a super fast connection). Is this related? Or some other issue?
23
u/KManRules1331 Oct 27 '17
I mean, isn't this the point of React though? You're supposed to pay a loading cost up front to reduce loading costs as the user uses the application. Of course the time to interaction of a fresh React page is going to be slower than a static web page with a little javascript, but the purpose of the framework is to reduce loading times of moving to subsequent pages in the application.
2
u/manys Oct 27 '17
I'm not sure what point you're trying to make, is it that nobody else knows how react works?
1
u/KManRules1331 Oct 27 '17
I'm just saying this for those who are writing off frameworks like this for vanilla js. If you only measure a framework by one of it's downsides and write it off as such, you're missing out of the benefits that it provides. I realize Netflix is doing a "hybrid" solution where they attach the event listeners and interaction before they fetch all of React, but for those who are writing off React as just another bulky framework to load aren't seeing the forest for the trees.
1
u/manys Oct 29 '17
for those who are writing off React as just another bulky framework
How many uses of React do you think are overprovisioned? Do you think this might have something to do with reacting to the platform spread its use implies?
That is, if it's common in an industry to use something where it's not really necessary, can you blame people for being conservative about recommending it?
2
u/occz Oct 27 '17
It's one of many things you'd want React for. In this particular case they are trying to optimize for one very specific metric though - get the signup page ready for the user to use as soon as possible, presumably so that they dont lose user interest or something similar.
Once they meet that goal they can load react after that if necessary for the page.
16
u/vcarl Oct 27 '17
Clickbait headline. Another phrasing could be "Reducing JS bundle size reduced time-to-interactive on the logged-out homepage by 50%" but that's not the kind of thing that grabs your attention.
In the talk, he mentions that they're still using React to render on the server and they lazy-load the JS bundle after initial load, so they're still using React. Because of Netflix's massive scale and because they want to have the best possible experience for low performance, low bandwidth environments for their marketing homepage, microoptimizations like shaving a few hundred ms off TTI matter. If you use this as an argument against using React, you've read the headline but not seen the content.
2
1
u/kT_Madlife Oct 27 '17
Has anyone used Airbnb’s hypernova for server side rendering? If so how is it?
1
u/cadaci Oct 27 '17
Well a landing page can be cache so yeah removing react that needs to load a library makes sense.
1
-17
u/eggn00dles Oct 27 '17
every 6 months a whole new abstraction of javascript come out for what gain? a 50% performance reduction apparently.
im convinced the majority of people who love frameworks do so out of job insecurity. you're inherently valuable if you can spout the trendiest things in JS. you're irreplaceable if you master the convoluted insanity and forced complexity these frameworks introduce.
30
u/n8n8baby Oct 27 '17
Trying to do anything of any complexity with vanilla JS is even more insane than learning a framework. These frameworks were generally written to solve real problems of scale. Granted, some people use heavier frameworks than they should, but writing a large web application in vanilla or JQuery is just foolish.
7
u/venuswasaflytrap Oct 27 '17
I'm currently supporting a home-spun jquery based nightmare. I would happily have some framework based clunkiness instead
2
u/eggn00dles Oct 27 '17
es6 has made it a lot easier to compose well written organized modular code. web components promises to offer many of the things frameworks do now.
i think you could handle anything outside of FB, Netflix, etc... with polymer and es6
3
1
u/PaintItPurple Oct 27 '17
How confident are you that Polymer would give any speedup once you've written enough code for feature parity? Because I don't feel very sure of that at all, and suspect it might actually make things slower, and I also suspect it would lead to more code and less nice code on my part.
1
u/n8n8baby Oct 27 '17
Web components promise to, but don’t actually exist yet. Using a framework gets you closer to that now, and they are generally all targeting the spec, which will make migration reasonably easy when they are finally born.
Netflix is actually not a great example. It does not have a lot of user interaction. There are tons of potential web apps much more complex in that regard, which is what frameworks are good for.
1
u/TimvdLippe Oct 27 '17
Web components are natively supported in both Safari and Chrome stable. 1+ Billion devices that do not require polyfills.
1
u/n8n8baby Oct 27 '17 edited Oct 29 '17
a) Safari doesn't support shadow DOM, which is a foundational requirement for proper web components. *edit: I was wrong here. There was a previous version of Shadow DOM that was only supported in Chrome.
b) Even new software, being developed right now, still needs to support Edge, Firefox, and, if you are business-facing, IE11.
1
u/TimvdLippe Oct 29 '17
a) Safari supports shadow DOM v1 (http://caniuse.com/#feat=shadowdomv1). v0 was only ever implemented in Chrome
b) That is a valid concern. For Firefox it is under development, IE under consideration. Hopefully they are caught up soon :)
1
9
u/andrewingram Oct 27 '17
Are you really convinced of this, or is it just fun to imagine that it's the case?
Every developer i've worked with, whilst excited about shiny new tech, only adopts it when it solves a problem they're having.
2
u/andrewingram Oct 27 '17
I'd like to add that when i've seen people adopt the wrong tech, it's down to inexperience, or rather not understanding the wider picture of problems that need to be solved. This means that they tend to "unsolve" a load of hidden problems when they switch to the new shiny.
3
u/absurdlyastute Oct 27 '17
It's too expensive and time consuming to keep implementing tech. I'm convinced this is a contrived scenerio as well.
3
u/againstmethod Oct 27 '17
Sorry but components bound to a nice unified state representation is not an invented benefit. React and redux are valuable.
1
u/absurdlyastute Oct 27 '17
Being able to search and find a solution to an issue online is probably the greatest part of adopting a popular framework. Netflix will likely have less than a handful people that own the knowledge to their tech stack. That's very unproductive.
1
u/boneheaddigger Oct 27 '17
You're right. Let's just do away with frameworks and reinvent the wheel on every project. We'll just get the co-op to whip something up over the weekend. I mean, how hard could it be...its all just typing anyways...
1
u/freebit Oct 27 '17
Let's just do away with frameworks and reinvent the wheel on every project.
Please stop attacking a Straw Man.
Here is how attacking a Straw Man works:
Person 1 asserts proposition X.
Person 2 argues against a superficially similar proposition Y, falsely, as if an argument against Y were an argument against X.
"Let's...reinvent the wheel on every project." --Said No Sane Person Ever
2
u/eggn00dles Oct 27 '17
Do you really think the way the angular 2 beta was handled was smooth? Right now you have the internet diluted with solutions and code blocks that don't actually work because of the constant breaking changes.
You have tons of people invested in angularJS but instead of evolve that more sensibly it's a complete bottom to top rewrite. People spent lots of time learning that and then they burned them when the carpet is pulled out from under them.
It's not unreasonable to desire some stability and sensibility in adopting new methodologies. Also a more controlled beta would have helped tremendously.
2
u/boneheaddigger Oct 27 '17
I assume that response was for me...
Look, I just made a joke. But if you really want to get into it...I really don't care about angular beta 2. Way WAY too new for my line of work. But we do use React and NodeJS, because instead of handwriting our own framework like we use to do, we pick something that has most of the functionality that we'd be writing anyways. It's all a trade off...we get projects up and running faster, at the cost of using someone else's code base.
Recreating all of that ourselves, while offering exact requirements matching and allowing us to fully control the code base, would make projects take longer and simple deliver us back into the same position...eventually we'd just create most components once and reuse them. Essentially, we'd create our own proprietary framework that is kinda sorta supported when devs have time, which they never have the time.
Frameworks aren't about creating job security. They actually make it easier to just pull someone off the street and have them start developing. "Know Angular/React/NodeJS? Great! Read through our custom code. We'll get you started on a project next week when you're ready..." vs "Know JavaScript? Great! Start reading through this code, get a really good feel for how everything works, ask this senior dev (who we pay a fortune for since we can't afford to lose all that tribal knowledge) any questions you have, and when you're done in 2 months we'll get you started on a project...".
If you're basing your project on the ability to just move to the next version of a framework with no problems, you're gonna have a bad time. If you're basing your project on a custom framework that you're expecting to be perfect out of the gate and never have to be rewritten, you're gonna have a bad time. If your basing your project on a beta using code snippets found on the internet, you gonna have a really REALLY bad time.
So before you start any project, you need to decide whether you want to use an old reliable well tested framework that's hard to migrate off of, a proprietary framework that won't be nearly as well tested and nearly impossible to migrate off of, or a bleeding edge framework that is barely tested but will probably be supported longer than the old well tested version. If you have another choice, I'd love to hear about it.
1
u/n8n8baby Oct 28 '17
Just because Angular 2 was a botch job doesn’t mean frameworks are bad. Ember handles upgrades fairly reasonably, even if there are a few bugs that slip in from time to time.
1
u/boneheaddigger Oct 27 '17
It's not a straw man. He was arguing that frameworks were a bad thing due to what he says is forced complexity. I just made a joke about it.
Seriously, not everything is an argument waiting to happen. Sometimes a cigar really is a cigar.
0
Oct 27 '17
[deleted]
58
u/thecollegestudent Oct 27 '17
Nope. Static web page where we don't need to load some giant framework? Makes sense to delay loading it until later.
0
u/reactnativecoach Oct 27 '17
They switched to server side rendering React
22
u/Nimelrian Oct 27 '17
Then you haven't listened to the talk...
They have been using universal rendering before as well, but now they don't include the whole React bundle where it's not needed (as on the landing page). It still gets prefetched for the other pages once you're on the page though.
0
Oct 27 '17
[deleted]
1
u/freebit Oct 27 '17
Fucking JavaScript is a cancer to this world.
"Fucking JavaScript is
a cancer toeating this world." FTFY :)3
u/atomic1fire Oct 27 '17
Eh, I just assume that developers use Javascript because it's the lowest possible target between both servers and browser, which means there's parity there that you wouldn't get with PHP or some other language. You could realistically build the frontend and backend together using the same code.
As it's the lowest possible target, there's a lot of development and as a result also probably a lot of subpar projects and tradeoffs.
I think it's neat because of what people can build with it, but I can understand if people think it's inefficient because people are using it for anything and everything.
2
5
7
Oct 27 '17
[removed] — view removed comment
17
u/Nimelrian Oct 27 '17
The server renders the initial HTML using React and sends that to the client. From there, the client (can) "rehydrate" the pre-rendered DOM using React, which basically hooks into the already existing DOM tree. This saves time / reduces perceived wait time on the initial render, since you don't have to request the model data necessary for rendering.
So instead of doing:
Request page from server
Server sends barebone HTML file with React app bundle reference
Client requests React app bundle
Server sends app bundle
App needs data for first render
Client requests data from server
Server gathers the data and sends it to client
Client finally renders (Content ready + interactive)
(You can skip steps 5 - 7 if you send the initial data inside the HTML, e.g. as a data-attribute)
You do:
Request page from server
Server gathers data, renders it using React, sends the complete HTML with React app bundle reference
Client requests React app bundle, but already shows the complete, information containing DOM (Content ready)
Server sends app bundle
App hooks into existing DOM (Interactive)
2
6
5
u/dschooh Oct 27 '17
The HTML of a client-side React application is just a JavaScript include. The script executes and builds the DOM for your app in your browser. Since Node can execute JavaScript server-side you can use your existing React code-base and send the initial DOM via HTML to your browser. Subsequent user interaction can then be handled via usual client-side React.
Advantages: Fast initialization, search-engine friendly front pages, no changes to code base.
3
u/i_spot_ads Oct 27 '17
If it’s the same thing as Angular Universal (which it probably is) when a user makes a request to a url, what they do is emulate a browser on the server, render the html with react on the server, send html to the user (without react), then send the js bundle (with react) which injects itself to the html and transitions to client rendering, that way user can interact with the interface faster because he doesn’t have to wait for the js bundle to download and render the html.
This shit it pretty difficult to implement and maintain, most of the time it’s not even worth it.
2
u/occz Oct 27 '17
Given some data it generates some HTML. I guess the biggest difference between client-side and server-side react is that the data does not change server-side (on a per-request basis, you can of course change the data between different requests), you only render it once and then you're done.
211
u/DoListening Oct 27 '17
It only talks about the landing page. The full tweet goes
The very next tweet says they still use React on subsequent steps.