Practicing Reactive Architecture and RxJava in Youzan Retail
By adopting an event‑driven reactive architecture and leveraging RxJava’s asynchronous, message‑oriented programming, Youzan Retail decouples services, parallelizes complex initialization and data‑transformation tasks, reduces response time and coupling, and enhances robustness, scalability, and throughput for its rapidly growing retail platform.
With the rapid development of Youzan Retail business, system and business complexity increase. The article discusses solving coupling between multiple services after system serviceization, improving response time and throughput, and ensuring robustness and stability.
1. Practicing Reactive Architecture
Reactive architecture means business components and functions are event‑driven , each component runs asynchronously and can be deployed in parallel and distributed.
Advantages include:
Significant reduction of internal coupling.
Simplified parallel programming by using event propagation, avoiding low‑level concurrency constructs and solving issues such as deadlocks.
Improved safety and speed of method calls.
Natural support for domain modeling of complex business systems.
The architecture is used to solve distributed problems caused by multiple remote calls, especially in long‑task scenarios.
When a new store is created in a chain, a series of initialization tasks (shop, staff, warehouse, product, inventory, etc.) are triggered. Dependencies exist, e.g., product depends on warehouse initialization (see Figure 1).
Figure 1: Chain new‑store system dependency graph
After shop initialization, the chain system sends a success message; downstream systems respond and return acknowledgments. The chain system controls overall progress and error handling, keeping systems loosely coupled.
Figure 2: Chain system new‑store message‑driven diagram
2. RxJava Practice in Youzan Retail
RxJava is a library for writing asynchronous, message‑driven programs. It is widely used in Android for UI rendering and server communication. Its core concepts are reactive programming, events, and asynchrony.
2.1 Reactive programming clarifies complex business logic
During the migration from Youzan Micro‑Mall to Youzan Retail, product data must be transformed. The process includes initializing shop info, converting product types, and creating supply‑chain entries. RxJava enables concurrent handling of these steps, as shown in the code snippet.
Observable.zip(
callAsync(()->process inventory),
callAsync(()->update product store channel),
callAsync(()->create product‑store association),
(sku1,sku2,sku3)->sku
).blockingFirst();The full processing pipeline is:
UpgradeItem.listItems(manager, shop)
.flatMap(item-> fromCallable(()->update to retail product type))
.flatMap(item-> fromCallable(()->concurrent product operations), true)
.flatMap(item-> productStreamToSkuStream, true)
.flatMap(sku-> fromCallable(()->save retail product))
.flatMap(sku-> fromCallable(()->post‑save operations), true)
.subscribeOn(Schedulers.io());Figure 3: Micro‑Mall upgrade to Retail flow (concurrent sections highlighted).
2.2 Combining multiple services and data sources
Micro‑service architecture splits domains into separate systems, which can increase response time (rt) and error risk. RxJava helps lower rt through concurrency and provides concise automatic fallback handling.
Example code for loading product attributes concurrently:
// Only mergers that are included will be loaded
List
validMergers = Observable.fromIterable(skuAttrMergers)
.filter(loader -> request.getAttributes().contains(loader.supportAttribute().getValue()))
.toList()
.blockingGet();Further code shows loading attribute data, merging results, and handling errors with onErrorResumeNext :
Observable.fromIterable(productInfoLoaders)
.flatMap(loader -> Observable.fromCallable(() -> async load product info))
.onErrorResumeNext(Observable.empty())
.subscribeOn(Schedulers.io(), false, loaderCount)
.blockingSubscribe();3. Conclusion
The article presents how reactive architecture and RxJava are applied in Youzan Retail. System‑to‑system communication uses message middleware, while internal asynchronous processing relies on RxJava. The approach improves robustness, flexibility, and scalability for increasingly complex retail scenarios.
Youzan Coder
Official Youzan tech channel, delivering technical insights and occasional daily updates from the Youzan tech team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.