Natan Silnitsky
Backend Infra Developer, Wix.com
Building a Large Java
Codebase with Bazel
natans@wix.com twitter@NSilnitsky linkedin/natansilnitsky github.com/natansil
BIGGER CODE
LONGER BUILD
1 h
1
2
3
4
5
1
2
3
4
1
2
3
1
2
7
0
1
0
1
Lines of code
2
5
0 7
Maven modules
Build
time
EXAMPLE WIX REPO
1 h-2h
Maven
1-2 m
Bazel
WIX
BACKEND
~250 developers
~1100 micro-services
>10M LoC, Scala ,Java, Node.js
K’s of builds every day
BIGGER CODE
LONGER BUILD
POSSIBLE
SOLUTIONS
01
#1 Skip tests!
mvn install -DskipTests
gradle build -x test
But you compromise on quality… more production bugs
#2 Cache module outputs
● Big overhead when there are
many modules
Unreliable caching Better caching solution
Maven Gradle
#3 Code breakup
Monolith
Microservices
Code
Git repos
Each repo built
separately
Faster build time
#3 Code breakup
What about Inter-repo
dependencies?
● Common libs (infra, sdk clients)
● Service libs (api, testkit)
Code
Git repos
Each repo built
separately
Faster build time
Semantic versioning
breaking
change
new
feature
bug
fix
1 . 3 . 1
MAJOR . MINOR . PATCH
* 3rd party
<dependency>
<groupId>com.wix.foo</groupId>
<artifactId>api</artifactId>
<version>1.3.1</version>
</dependency>
dependencies {
Implementation
'com.wix.foo:api:1.3.1'
}
Semantic versioning
Maven Gradle
● #code-paths
Semantic versioning…?
Why not
● #code-paths
● Integration issues
Semantic versioning…?
Why not
V5 V8
+Incompatible change
V10
+ Bug fix
Supplier provides v11 with v5+fix Client change code to work with v8
● #code-paths
● Integration issues
● Code repetitions
Semantic versioning…?
Why not
V5 V5
+ Bug fix
● #code-paths
● Integration issues
● Code repetitions
● Keeps getting violated
Semantic versioning…?
Why not
1 . 3 . 1
breaking
change
new
feature
bug
fix
1 . 3 . 0
<dependency>
<groupId>com.wix.foo</groupId>
<artifactId>api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Wix used Maven snapshot versions instead
BUILD TOOL
Maven
BUILD SERVER
TeamCity
Maven Maven Maven Maven
Maven Maven Maven Maven
Maven Maven Maven Maven
Maven Maven Maven Maven
Maven Maven Maven Maven
+ Pool of agents
+ Distribute work
+ Jobs that run maven
with clean cache
+ Dependencies
TeamCity
Maven
Maven
Maven
Maven
Maven
buildable
buildablebuildable
buildable
buildable
+ Ensure to build against
the latest SNAPSHOT
dependency
TeamCity
building
blockedblocked
blocked
blocked
+ Block while dependency
is building
Maven
Maven
Maven
Maven
Maven
TeamCity
* binary incompatibility
broken
blockedblocked
blocked
blocked
+ Block while dependency
is broken
Maven
Maven
Maven
Maven
Maven
TeamCity
TeamCity
Maven
Maven
Maven
Maven
Maven
building
blockedblocked
blocked
blocked
+ Block while dependency
is building
USE
BAZEL
02
Build everything from source
Divide your code to smaller build units
Heavy use of caching and parallelism
In Bazel
You control the level of granularity
to build.
java_project
├── WORKSPACE
└── src
└── main
└── java
└── com
├── example
│ ├── A.java
│ ├── B.java
│ └── C.java
└── example2
├── D.java
├── E.java
└── F.java
The basic build unit is
called a target
java_project
├── WORKSPACE
└── src
└── main
└── java
└── com
├── example
│ ├── A.java
│ ├── B.java
│ ├── BUILD
│ └── C.java
└── example2
├── D.java
├── E.java
└── F.java
Target
Targets are instances
of rules.
A RULE
f(A,B,C)f( )
A
B
C
source files | dependencies | tools configurations
Sandbox
A RULE
f(A,B,C)
OUTPUTs
f( )
A
B
C
f(A,B,C)
f( )
A
B
C
SAME
Maven /
Gradle
BAZEL
MANY SMALL TARGETS
MUCH MORE PARALLEL
Maven /
Gradle
BAZEL
MUCH MORE PARALLEL
Maven /
Gradle
Compilation Test Run
BAZEL
Pending
INCREMENTAL
✔ ✔ ✔
✔ ✔ ✔
✔ ✔ ✔ ✔
BAZEL$ bazel build //...
Efficiency is attained not by doing things faster,
But by avoiding things that should not be done in
the first place
-Venkat Subramaniam-
GOOGLE CLOUD / OSS
REMOTE EXECUTION
100s
Workers
-CI/local tradeoff
✔ ✔ ✔
✔ ✔ ✔
✔ ✔ ✔ ✔
BAZEL
Cache
1 hmaven local clean
IT’S WORTH IT
1 hmaven local clean
20 minbazel local clean
8.5 minbazel remote clean
IT’S WORTH IT
bazel fully
cached
0.1 min
HOW
DID WE
MIGRATE?
03
How did we migrate?
Changed code to work
with newer 3rd party
versions
Auto Migrator Tests that failed in
sandbox
Consolidated
~1000 repos -> ~50
https://github.com/wix/exodus/
Splits Maven Modules
to bazel targets
DEMO
How did we migrate?
Changed code to work
with newer 3rd party
versions
Auto Migrator Tests that failed in
sandbox
Consolidated
~1000 repos -> ~50
E.g. Writing to non tmp
destinations,Access
external network endpoint
*Tech debt
How did we migrate?
Changed code to work
with newer 3rd party
versions
Auto Migrator Tests that failed in
sandbox
Consolidated
~1000 repos -> ~50
How did we migrate?
Changed code to work
with newer 3rd party
versions
Auto Migrator Tests that failed in
sandbox
Consolidated
~1000 repos -> ~50
50 Repos work together
like 1 virtual mono-repo
Virtual Mono-Repo & Bazel Talk by Ittai Zeidman from Wix
WORKING
WITH
BAZEL
04
Remote Build Execution is fast and reliable
Remote Build Execution is fast and reliable
Pre-submit - reduces noise
Remote Build Execution is fast and reliable
Pre-submit - reduces noise
Very Rich Build results API (Build events protocol)
Google Cloud build Bazel UI
Intellij support
Wix is working on improvements
No Sandbox support / RBE for Mac
Harder to maintain build configuration
Wix built plugin features - OSS SOON
LabelDex
Indexing Server
✔
✔ ✔ ✔
Target Labels
to JARs
Google Cloud
Build Results
Intellij
Plugin
OSS SOON
LabelDex
OSS SOON
SmartAutoComplete + AutoDep
Automatically fix dependencies in BUILD files
by parsing errors from bazel build result log + index server.
Inspired by Pants
OSS SOON
DepFixer
Internal
JARs
External
JARs
Build result log
USE
BAZEL
Build everything from source
Divide your code to smaller build units
Heavy use of caching and parallelism
Faster Builds & Feedback
More Frequent Cross Org
Changes/Refactoring
Improved local experience
BAZEL
IS WORTH IT
Migration Stories
Migrating to Bazel? 5 crucial questions... - My Blog Series on Medium
We Switched from Maven to Bazel and Builds Got 10x Faster - Redfin
Migrating From Maven to Bazel at Salesforce
Thank You
natans@wix.com twitter@NSilnitsky linkedin/natansilnitsky github.com/natansil
Slides & More
slideshare.net/NatanSilnitsky
medium.com/@natansil
twitter.com/NSilnitsky
Q&A
natans@wix.com twitter@NSilnitsky linkedin/natansilnitsky github.com/natansil

Building a Large Java Codebase with Bazel - Natan Silnitsky