Share
Embed
Micro Modular Architecture with Bazel
iOSDC Japan 2020 09/21 11:30 Track E
「Bazelを利用したMicro Modular Architecture」
Ryo Aoyama
September 21, 2020
Transcript
64 slides
-
Agenda • Drawbacks of Monolith • Micro Modular Architecture •
What is Bazel? • The power of Bazel with micro modules -
FeatureA FeatureB FeatureC FeatureD FeatureZ … Networking DesignSystem Analytics Database
Architecture etc… Entity Monolithic Application All features are concentrated in one or a few modules. -
Monolithic Application Build time increases as the product grows: Build
time can increase linearly or exponentially. -
Coupling complexity between features: Monolithic Application Coupling complexity between features
gradually increases and there is no going back. -
Barriers to evolutionary potential: Monolithic Application The introduction of new
technology affects the entire app and is pretty costly. And also, it does not have the ability to evolve progressively. -
Micro Modular Architecture Our coined term showing a highly modularized
application structure. = -
The idea is to split our application into hundreds of
smaller modules. -
Concepts One module has only one screen (aka view controller).
All the layers needed for the screen are also in the same module. • Module by feature -
Concepts The screens do not depend on each other and
work independently. • Independent between features -
Concepts Create one module for just one functionality. Larger functionalities
consist of a composition of these smaller modules. • Fine-grained shared functionality modules -
Modularization Group Applications Features Libraries Domain Grouping modules to give
order to the way they depend on each other. -
Application Feature Feature Feature Feature Feature Common Common Common Common
Modularization Group The modules in this group contain an App Delegate, and that's it. It might also contain DI to feature modules. Applications -
Application Feature Feature Feature Feature Feature Common Common Common Common
Modularization Group Features Each vertical feature module is independent and can only depend on modules in the Domain and Libraries groups. -
Application Feature Feature Feature Feature Feature Common Common Common Common
Modularization Group Domain (Library) Contains modules of functionality shared by several feature modules. It also allows modules to have service-specific information. -
Application Feature Feature Feature Feature Feature Common Common Common Common
Modularization Group Libraries Contains modules of generalized functionality. It cannot have service-specific information. -
Modularization Group Applications Features Libraries Domain Grouping modules and make
them unidirectional dependencies. Circular dependency Complexity -
Bazel • Fast + Reliable + Scalable + Extensible •
Multiple language support • Starlark language for build scripts • Remote Caching • Open Source https://github.com/bazelbuild/bazel -
Example of Benefits My product completes clean build, unit test,
and land to testers in around 1m 30s in the shortest. But it's still too early stage for a sample. -
Example of Benefits Pinterest https://medium.com/pinterest-engineering/developing-fast-reliable-ios-builds-at-pinterest-part-one-cb1810407b92 Local Build: CI Build: Beta
Distribution: Build Success Rate: 4m 38s -> 3m 38s, 21% improved 10m 24s -> 7m 34s, 27% improved 14m 32s -> 7m 52s, 45% improved 80% -> 97%-100% -
LINE Example of Benefits https://engineering.linecorp.com/en/blog/improving-build-performance-line-ios-bazel Minimum: Maximum: Average: 28m 24s
-> 4m 24s, 85% improved 35m 25s -> 26m 32s, 25% improved 30m 58s -> 14m 32s, 53% improved -
Example of Benefits Amazingly, these improvements have been brought to
each individual developer by remote caching. -
Build Scripts WORKSPACE Defines external dependencies. BUILD Contains build scripts
for 1~N modules. .bazelrc Configuration file for project-specific options. xxx.bzl Defines utility extension for build scripts which will be loaded from the BUILD. -
How to build load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "build_bazel_rules_apple", sha256
= “….", url = “https://github.com/bazelbuild/rules_apple/releases/download/x.x.x/rules_apple.x.x.x.tar.gz”, ) load( "@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies", ) apple_rules_dependencies() load( "@build_bazel_rules_swift//swift:repositories.bzl", "swift_rules_dependencies", ) swift_rules_dependencies() load( "@build_bazel_apple_support//lib:repositories.bzl", "apple_support_dependencies", ) apple_support_dependencies() WORKSPACE -
Library How to build load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") swift_library( name = "Sample",
srcs = glob(["Sources/**/*.swift"]), data = glob(["Resources/**/*"]), visibility = ["//visibility:public"], ) BUILD -
Test How to build load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") load("@build_bazel_rules_apple//apple:ios.bzl", "ios_unit_test") swift_library( name
= "TestLib", srcs = glob(["Tests/**/*.swift"]), ) ios_unit_test( name = "Test", deps = [":TestLib"], ) BUILD -
Application How to build load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application") load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") swift_library( name
= "AppLib", srcs = glob(["App/**/*.swift"]), ) ios_application( name = "App", app_icons = glob(["Assets.xcassets/AppIcon.appiconset/**"]), bundle_id = "com.example.app", families = [ "iphone", "ipad", ], infoplists = ["Info.plist"], launch_storyboard = "Launch.storyboard", minimum_os_version = "14.0", deps = [":AppLib"], ) BUILD -
How to build $ bazel build //… Build all targets
$ bazel build //package-name:target-name Build specified target -
Carthage CocoaPods • pinterest/PodToBUILD • bazel-ios/cocoapods-bazel • with build scripts:
swift_library, objc_library • with build scripts: apple_dynamic_framework_import Our Choice Third-party Library -
index-import by Lyft https://github.com/lyft/index-import Bazel + Xcode Replaces the paths
in the index file created by Bazel so that it can be used for Xcode. -
Downside • High adoption cost • High learning cost •
iOS is not officially supported • Some Xcode features have been disabled -
Aggressive Caching Once a module is built, it won't be
rebuilt until the code in that module is changed. -
Aggressive Caching Caches are created per module. Micro modular architecture
maximizes cache efficiency. = -
Test Results Caching That’s can be ignored by bazel test
//… --nocache_test_results -
Remote Caching Once it's built somewhere, all developers don't have
to rebuild it. Build once, reuse everywhere: -
Remote Caching Caches can be minimized by bazel build //…
——remote_download_minimal -
Remote Caching Now, all we need is ample network bandwidth
instead of Mac Pro. -
Static Linking Too many dynamic frameworks can have a negative
impact on the launch speed of your app. -
Static Linking Bazel compiles all modules into static libraries, so
micro modules has no negative impacts. -
Dependency Diagrams Bazel can track all dependency graphs and parse
them with the bazel query command. It can be output as a graphical diagram with Graphviz. -
• Micro Modular Architecture maximizes the synergy with Bazel, and
vice versa. Conclusion • Build speed has room for improvement in software architecture as well as hardware performance. • Bazel will dramatically improve our developer experience. -
References • Bazel Document • Faster Together: Uber Engineering’s iOS
Monorepo • How Tokopedia Achieved 1000% Faster iOS Build Time • Improving Build Performance of LINE for iOS with Bazel • Developing fast & reliable iOS builds at Pinterest (Part one) • kastiglione/bazel-xcode-demo-swift-driver https://eng.uber.com/ios-monorepo https://engineering.linecorp.com/en/blog/improving-build-performance-line-ios-bazel https://medium.com/tokopedia-engineering/how-tokopedia-achieved-1000-faster-ios-build-time-7664b2d8ae5 https://medium.com/pinterest-engineering/developing-fast-reliable-ios-builds-at-pinterest-part-one-cb1810407b92 https://bazel.build https://github.com/kastiglione/bazel-xcode-demo-swift-driver