Translated from medium.com/graalvm/gra…

Facebook is using GraalVM to speed up its Spark workload and reduce memory and CPU usage. Read on for their migration stories, performance improvement results, and future plans.

The technology behind Facebook

With 2.8 billion monthly active users, Facebook is one of the most visited platforms in the world. To ensure reliability and high performance under this load, the engineering team employs a variety of technologies, including Java, JavaScript, Flow Hack, PHP, Python, c++, and more.

Facebook uses Java in key areas such as big data (Spark, Presto, etc.), back-end services, and mobile devices. Prior to migrating to GraalVM, the team used the Oracle JDK and OpenJDK on Java 8 and Java 11.

At this scale, any performance improvements add significant value — they improve the user experience and reduce infrastructure costs. This is why the engineering team has been looking for ways to improve application performance and decided to evaluate GraalVM to determine if it is a faster Java runtime.

Why GraalVM

  • Since performance was a major concern, the Facebook team decided to evaluate GraalVM as their Java runtime environment to see if it would improve the performance of their Java applications. GraalVM provides advanced optimizations such as partial escape analysis and inline heuristics. Thanks to this, many Java/JVM applications can get an immediate performance boost simply by switching to GraalVM. The Facebook team also observed that GraalVM showed significant year-over-year improvement in benchmarks such as SpecJVM2008 and DaCapo compared to C2.
  • In addition, the GraalVM compiler was written from scratch using Java in a modular and extensible manner. This makes maintenance easy while adding incremental improvements. This is important for Facebook as the team considers a long-term investment in GraalVM.
  • The community. The GraalVM project has a vibrant open source community, with many organizations and individuals contributing to the project and forming its roadmap. It’s also easy to find help and support in the community.

Run Java and Spark on GraalVM

The Facebook team used the GraalVM community as an alternative to OpenJDK. In this scenario, migrating to GraalVM is very simple — you just switch the runtime environment, and no application code changes are required. This transformation makes the application run faster thanks to GraalVM’s advanced performance optimizations without any manual tuning.

Apache Spark is a unified big data processing and analysis engine with built-in streams, SQL, machine learning, and graphics processing modules. It can process data very quickly, but many teams are looking for ways to further optimize its performance. One of the easiest ways to do this is to run the Spark workload on GraalVM. Thanks to a specific set of compiler optimizations (which we’ll discuss in more detail later), GraalVM can significantly speed up Spark’s workload. Apache Spark benchmarks in the Renaissance benchmark suite showed an average acceleration rate of 1.1 times for the community, 1.42 times for the enterprise, and 4.84 times for some benchmarks.

For Facebook, Spark is the largest SQL query engine in its data warehouse, running on aggregated computing storage clusters. Due to the huge amount of data, efficiency and cost reduction is a priority.

They will begin their assessment in early 2020. With initial benchmarks showing good results, the team pushed GralVM into production and has been monitoring its performance and reliability.

In terms of performance, they observed a reduction in CPU usage of about 10%, which has been consistent since launch.

How does GraalVM accelerate the Spark workload

Some of the optimizations that have contributed the most to Spark performance are:

  • Polymorphic inlining. Traditional inlining works only if the compiler can determine the target method for a method call. GraalVM goes beyond this by gathering additional analysis information that allows abstract methods to be inlined as well.
  • Partial escape analysis. The idea of partial escape analysis is to remove unnecessary object allocations by performing scalar substitutions in branches where objects are not escaped, and to ensure that objects exist in the heap in branches where they must be escaped. This reduces both the memory footprint of the application and the CPU load caused by GC. This optimization is even more important in a data-intensive application like Spark. In particular, according to Facebook’s observations, GraalVM reduces CPU consumption by five times in methods such as Java /lang/ double-.valueof.
  • Advanced speculative optimizations in GraalVM produce faster machine code by taking advantage of dynamic run-time feedback. By speculating that certain parts of the program will not run during program execution, the GraalVM compiler can specialize the code and make it more efficient. For Spark, this optimization works particularly well by eliminating branches (such as long if-then-else chains), simplifying control flow, reducing the number of dynamic checks in the body of the loop, and establishing alias constraints, leading to further optimizations.

Based on this evaluation, the Facebook team moved most of its CPU-intensive big data services to GraalVM. They also observed an increase in Presto’s >5% CPU and GC pause times after switching to GraalVM. Next, the team plans to push GraalVM to other memory binding services to benefit from escape analytics optimizations. The team also plans to contribute to the project and the community.

They are also exploring opportunities to use other GralVM features, such as Native Image and the Truffle Framework.

conclusion

Thanks to advanced compiler optimizations, GraalVM can significantly speed up many Java and Scala workloads. In particular, the Spark workload is expected to increase by 10-42% by converting GraalVM to a JDK distribution.

Interestingly, engineers at Twitter, another popular social media platform, shared similar journeys and observations. After moving the Scala workload to GraalVM, they observed significant performance improvements, such as a 19.9% reduction in P99 latency thanks to the GraalVM compiler. For a platform like Twitter or Facebook, this performance improvement will increase as the platform scales.

To start using GraalVM in your applications, visit graalvm.org/docs/getting-started/.

Recently began its own open source projects itwanger/JavaLearnSource: LearnJava open source code of an online learning website (github.com) welcome to start