the blog for developers

Facebooks BigPipe Done in Java

BigPipe is a way of thinking for web pages. It introduces the concept of pagelets, small parts of the website. BigPipe was implemented at Facebook:

BigPipe is a fundamental redesign of the dynamic web page serving system. The general idea is to decompose web pages into small chunks called pagelets, and pipeline them through several execution stages inside web servers and browsers. This is similar to the pipelining performed by most modern microprocessors: multiple instructions are pipelined through different execution units of the processor to achieve the best performance.

BigPipe renders the structure of a webpage and then adds the content via JavaScript, a kind of inlined AJAX. The result is that users see content earlier and progressivly. Just reload a Facebook page and see how different parts are loaded. This reduces latency and especially changes the perception of users: They think the website is faster. A side effect of splitting the page into pagelets is how each pagelet can be rendered in parallel with asynchronous IO on the server or with an IO thread pool.

With threads, message piping or a worker model very long running service calls do not stop the page from loading, it will only prevent showing the pagelet that is slow. Each thread can have a timeout and you can kill long running or blocking service calls – a SLA for page calls.

Sam Pullaras mustache.java follows the same principles, breaking rendering into parts each with it’s own thread to reduce latency.

I’ve implemented a proof of concept of BigPipe in Java (should run as-is in every servlet container):

This will result in the following HTML code – see how content2 is rendered before content1. Due to threads the order in which content arrives is non-deterministic.

Some problems I’ve encountered:

  • You need the correct content type, content encoding and doc versiom, otherwise the page will not render progressivly
  • Your framework needs to enable flushing the output at certain points, so content is pushed to the browser

I would like to have some framework support and I’m taking a look into how to do this in Play.

About the author

stephan Stephan Schmidt has been working with internet technologies for the last 20 years. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own. You can find him on Google +

Discuss on Hacker News Vote on HN