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.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt has more than 15 years of internet technology experience and 10 years experience in agile. 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.

10 Tweets

Leave a reply.

Comments

I am not entirely sure if this reflects the entirety of bigpipe design since I couldn’t see any specific evidence of pipeline of several execution stages .. perhaps the example could be extended to demonstrate that too.

On a separate note, I think it would be specifically useful to combine this with completely non blocking web serving capabilities as in Tornado The two approaches could perhaps be structured to well complement each other.

@Dhananjay: I’m not sure they really do pipelining for pagelets, they consider all pagelets as small instructions in an instruction pipeline, which they can execute in parallel or reorder.

But Facebook hasn’t been very clear on this, also not in their examples.

Async: Yes, totally.

This was a proof of concept of the loading or parts.

In real life one should use a network of actors that either run on threads or “wait” async for data, then push data to the next actors and so on until the request is finished. This is what I would like to have going.

[...] Code Monkeyism: Facebooks BigPipe Done in Java RT @codemonkeyism: Just blogged: "Facebooks BigPipe Done in Java" http://bit.ly/d5RDtg #latency #IO #threading (tags: via:packrati.us latency threading IO) [...]

[...] his blog post for the Java servlet [...]

_efwe_

hi there,

i know this is just test-code, but
i have one central question about it.

can one really use the outputstream of the response in a parallel manner?

on the other hand i found the use of
taskList.add(callable) in a loop and a subsequent executor.invokeAll(taskList) not looking too good, because the actual computation is deferred until the last piece is prepared.

we went for another solution where we did executor.submit(callable) and wait for the completition with the help of a Map with Future entries.

anyways. very interesting thoughts about splitting up the computation of page-content.

~fw

@FW: 1. outputstream should be safe from what I’ve read, I had a lock in there and might put it back, but I think it’s not needed from reading the doc.

2. the nice thing about invokeAll is that you can limit the duration of invokeAll (for all calls), this is much harder with submit (I had submit before and could only getting by creating a thread pool each time and then force the pool to shutdown).

[...] Schmidt had already written Facebook BigPipe for Java, but using a synchronous servlet model, not asynchronous. I decided to implement it using Jetty [...]

Leave a Reply

What people wrote somewhere else:

RT @codemonkeyism: Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

RT @codemonkeyism: Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

RT @codemonkeyism: Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

RT @codemonkeyism Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

RT @codemonkeyism: Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

RT @codemonkeyism: Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

RT @codemonkeyism: Just blogged: “Facebooks BigPipe Done in Java” http://bit.ly/d5RDtg #latency #IO #threading

This comment was originally posted on Twitter

Facebooks #BigPipe Done in #Java http://bit.ly/a1hJkH A good reading #yam

This comment was originally posted on Twitter

Code Monkeyism: Facebooks BigPipe Done in Java: BigPipe is a way of thinking for web pages. It introduces the conc… http://bit.ly/cUhSmI

This comment was originally posted on Twitter

A small proof of concept of Facebook’s BigPipe implemented in Java. Much more simple one can imagine ! http://bit.ly/aWwZLv

This comment was originally posted on Twitter

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?