Recent Posts
Archives

Posts Tagged ‘ReactiveProgramming’

PostHeaderIcon [DotJs2024] Thinking About Your Code: Push vs Pull

Navigating the currents of performant code demands a lens attuned to flow dynamics, where producers and consumers dance in tandem—or discord. Ben Lesh, a veteran of high-stakes web apps from Netflix’s infrastructure dashboards to RxJS stewardship, shared this paradigm at dotJS 2024. With roots in rendering millions of devices across North America’s bandwidth, Lesh distilled decades of collaboration with elite engineers into a quartet of concepts: producers, consumers, push, pull. These primitives illuminate code’s underbelly, spotlighting concurrency pitfalls, backpressure woes, and optimal primitives for JavaScript’s asynchronous tapestry.

Lesh’s entrée was a bespoke live demo: enlisting audience volunteer Jessica Sachs to juggle M&Ms, embodying production-consumption. Pull—Jessica grabbing at will—affords control but falters asynchronously; absent timely M&Ms, hands empty. Push—Lesh feeding sequentially—frees producers for factories but risks overload, manifesting backpressure as frantic consumption. Code mirrors this: a getValue() invocation pulls synchronously, assigning to a consumer like console.log; for loops iterate pulls from arrays. Yet, actors abound: functions produce, variables consume; callbacks push events, observables compose them.

JavaScript’s arsenal spans quadrants. Pure pull: functions and intervals yield eager values. Push: callbacks for one-offs, observables for streams—RxJS’s forte, enabling operators like map or mergeMap for event orchestration. Pull-then-push hybrids: promises (function returning deferred push) and async iterables (yielding promise-wrapped results), ideal for paced delivery via for await...of, mitigating backpressure in slow consumers. Push-then-pull inverts: signals—Ember computeds, Solid observables, Angular runes—notify changes, deferring reads until render. Lesh previewed TC39 signals: subscribe for pushes, get for pulls, birthing dependency graphs that lazy-compute, tracking granular ties for efficient diffing.

This framework unveils pathologies: thread lockups from unchecked pushes, concurrency clashes in nested callbacks. Lesh advocated scanning code for actors—spotting producers hidden in APIs—and matching primitives to intent. Pull suits sync simplicity; push excels in async firehoses; hybrids temper throughput; signals orchestrate reactive UIs. As frameworks like React lean on signals for controlled reads pre-render, developers gain foresight into bottlenecks, fostering resilient, scalable architectures.

Decoding Flow Primitives in JavaScript

Lesh partitioned primitives into a revealing matrix: pull for immediacy (functions pulling values), push for autonomy (observables dispatching relentlessly). Hybrids like promises bridge, returning handles for eventual pushes; async iterables extend, pacing via awaits. Signals, the push-pull hybrid, notify sans immediate computation—perfect for UI graphs where effects propagate selectively, as in Solid’s fine-grained reactivity or Angular’s zoned eschewal.

Navigating Backpressure and Optimization

Backpressure—producers overwhelming consumers—Lesh dramatized via M&M deluge, solvable by hybrids throttling intake. Signals mitigate via lazy evals: update signals, compute only on get, weaving dependency webs that prune cascades. Lesh urged: interrogate code’s flows—who pushes/pulls?—to preempt issues, leveraging RxJS for composition, signals for reactivity, ensuring apps hum under load.

Links:

PostHeaderIcon [SpringIO2019] Spring I/O 2019 Keynote: Spring Framework 5.2, Reactive Programming, Kotlin, and Coroutines

The Spring I/O 2019 Keynote, featuring Juergen Hoeller, Ben Hale, Violeta Georgieva, and Sébastien Deleuze, offered a comprehensive overview of the latest developments and future directions within the Spring ecosystem. The keynote covered significant themes, including the advancements in Spring Framework 5.2, enhancements in Reactive programming, and the growing importance of Kotlin and coroutines in Spring applications.

The keynote served as a crucial update for the Spring community, highlighting how the framework continues to evolve to meet modern application development needs, from high-performance reactive systems to seamless integration with modern languages like Kotlin.

Spring Framework 5.2 Themes

Juergen Hoeller, co-founder and project lead of the Spring Framework, presented the key themes for Spring Framework 5.2. These themes focused on refining existing capabilities and introducing new features to enhance developer experience and application performance. While specific details were covered, the overarching goal was to continue Spring’s tradition of providing a robust and flexible foundation for enterprise applications.

Improvements to Reactive: Core/UX, R2DBC, RSocket

Ben Hale and Violeta Georgieva discussed the ongoing advancements in Reactive programming within the Spring ecosystem. They highlighted improvements to the core Reactive capabilities, focusing on enhancing user experience (UX) and developer productivity. The session also delved into R2DBC (Reactive Relational Database Connectivity), a specification for reactive programming with relational databases, and RSocket, an application-level protocol for reactive stream communication. These developments underscore Spring’s commitment to building highly scalable and responsive applications.

Kotlin and Coroutines

Sébastien Deleuze focused on the deepening integration of Kotlin and coroutines within Spring. Kotlin’s concise syntax and functional programming features, combined with the power of coroutines for asynchronous programming, offer significant benefits for modern Spring applications. Deleuze demonstrated how these technologies enable developers to write more expressive, performant, and maintainable code, further solidifying Kotlin as a first-class language for Spring development.

The Evolution of the Spring Ecosystem

The keynote collectively showcased Spring’s continuous evolution, driven by innovation and community feedback. The speakers emphasized how Spring is adapting to new paradigms in software development, such as reactive programming and multi-language support, while maintaining its core principles of productivity and flexibility. The discussions provided a roadmap for developers to leverage the latest features and best practices for building next-generation applications.

Conclusion

The Spring I/O 2019 Keynote offered a compelling vision for the future of Spring, demonstrating its adaptability and continued relevance in the rapidly changing landscape of software development. Attendees gained valuable insights into key areas of focus and practical applications of the latest Spring technologies.

PostHeaderIcon [KotlinConf2017] Bootiful Kotlin

Lecturer

Josh Long is the Spring Developer Advocate at Pivotal, a leading figure in the Java ecosystem, and a Java Champion. Author of five books, including Cloud Native Java, and three best-selling video trainings, Josh is a prolific open-source contributor to projects like Spring Boot, Spring Integration, and Spring Cloud. A passionate advocate for Kotlin, he collaborates with the Spring and Kotlin teams to enhance their integration, promoting productive, modern development practices for JVM-based applications.

Abstract

Spring Boot’s convention-over-configuration approach revolutionizes JVM application development, and its integration with Kotlin enhances developer productivity. This article analyzes Josh Long’s presentation at KotlinConf 2017, which explores the synergy between Spring Boot and Kotlin for building robust, production-ready applications. It examines the context of Spring’s evolution, the methodology of leveraging Kotlin’s features with Spring Boot, key integrations like DSLs and reactive programming, and the implications for rapid, safe development. Josh’s insights highlight how Kotlin elevates Spring Boot’s elegance, streamlining modern application development.

Context of Spring Boot and Kotlin Integration

At KotlinConf 2017, Josh Long presented the integration of Spring Boot and Kotlin as a transformative approach to JVM development. Spring Boot, developed by Pivotal, simplifies Spring’s flexibility with sensible defaults, addressing functional and non-functional requirements for production-ready applications. Kotlin’s rise as a concise, type-safe language, endorsed by Google for Android in 2017, aligned perfectly with Spring Boot’s goals of reducing boilerplate and enhancing developer experience. Josh, a Spring advocate and Kotlin enthusiast, showcased how their collaboration creates a seamless, elegant development process.

The context of Josh’s talk reflects the growing demand for efficient, scalable frameworks in enterprise and cloud-native applications. Spring Boot’s ability to handle microservices, REST APIs, and reactive systems made it a popular choice, but its Java-centric syntax could be verbose. Kotlin’s concise syntax and modern features, such as null safety and extension functions, complement Spring Boot, reducing complexity and enhancing readability. Josh’s presentation aimed to demonstrate this synergy, appealing to developers seeking to accelerate development while maintaining robustness.

Methodology of Spring Boot with Kotlin

Josh’s methodology focused on integrating Kotlin’s features with Spring Boot to streamline application development. He demonstrated using Kotlin’s concise syntax to define Spring components, such as REST controllers and beans, reducing boilerplate compared to Java. For example, Kotlin’s data classes simplify entity definitions, automatically providing getters, setters, and toString methods, which align with Spring Boot’s convention-driven approach. Josh showcased live examples of building REST APIs, where Kotlin’s null safety ensures robust handling of optional parameters.

A key innovation was the use of Kotlin’s DSLs for Spring Boot configurations, such as routing for REST endpoints. These DSLs provide a declarative syntax, allowing developers to define routes and handlers in a single, readable block, with IDE auto-completion enhancing productivity. Josh also highlighted Kotlin’s support for reactive programming with Spring WebFlux, enabling non-blocking, scalable applications. This methodology leverages Kotlin’s interoperability with Java, ensuring seamless integration with Spring’s ecosystem while enhancing developer experience.

Key Integrations and Features

Josh emphasized several key integrations that make Spring Boot and Kotlin a powerful combination. Kotlin’s DSLs for Spring Integration and Spring Cloud Gateway simplify the configuration of message-driven and API gateway systems, respectively. These DSLs consolidate routing logic into concise, expressive code, reducing errors and improving maintainability. For example, Josh demonstrated a gateway configuration where routes and handlers were defined in a single Kotlin DSL, leveraging the compiler’s auto-completion to ensure correctness.

Reactive programming was another focal point, with Kotlin’s coroutines integrating seamlessly with Spring WebFlux to handle asynchronous, high-throughput workloads. Josh showcased how coroutines simplify reactive code, making it more readable than Java’s callback-based alternatives. Additionally, Kotlin’s extension functions enhance Spring’s APIs, allowing developers to add custom behavior without modifying core classes. These integrations highlight Kotlin’s ability to elevate Spring Boot’s functionality, making it ideal for modern, cloud-native applications.

Implications for Application Development

The integration of Spring Boot and Kotlin, as presented by Josh, has profound implications for JVM development. By combining Spring Boot’s rapid development capabilities with Kotlin’s concise, safe syntax, developers can build production-ready applications faster and with fewer errors. The use of DSLs and reactive programming supports scalable, cloud-native architectures, critical for microservices and high-traffic systems. This synergy is particularly valuable for enterprises adopting Spring for backend services, where Kotlin’s features reduce development time and maintenance costs.

For the broader ecosystem, Josh’s presentation underscores the collaborative efforts between the Spring and Kotlin teams, ensuring a first-class experience for developers. The emphasis on community engagement, through Q&A and references to related talks, fosters a collaborative environment for refining these integrations. As Kotlin gains traction in server-side development, its partnership with Spring Boot positions it as a leading choice for building robust, modern applications, challenging Java’s dominance while leveraging its ecosystem.

Conclusion

Josh Long’s presentation at KotlinConf 2017 highlighted the transformative synergy between Spring Boot and Kotlin, combining rapid development with elegant, type-safe code. The methodology’s focus on DSLs, reactive programming, and seamless integration showcases Kotlin’s ability to enhance Spring Boot’s productivity and scalability. By addressing modern development needs, from REST APIs to cloud-native systems, this integration empowers developers to build robust applications efficiently. As Spring and Kotlin continue to evolve, their partnership promises to shape the future of JVM development, fostering innovation and developer satisfaction.

Links

PostHeaderIcon [ScalaDaysNewYork2016] The Zen of Akka: Mastering Asynchronous Design

At Scala Days New York 2016, Konrad Malawski, a key member of the Akka team at Lightbend, delivered a profound exploration of the principles guiding the effective use of Akka, a toolkit for building concurrent and distributed systems. Konrad’s presentation, inspired by the philosophical lens of “The Tao of Programming,” offered practical insights into designing applications with Akka, emphasizing the shift from synchronous to asynchronous paradigms to achieve robust, scalable architectures.

Embracing the Messaging Paradigm

Konrad Malawski began by underscoring the centrality of messaging in Akka’s actor model. Drawing from Alan Kay’s vision of object-oriented programming, Konrad explained that actors encapsulate state and communicate solely through messages, mirroring real-world computing interactions. This approach fosters loose coupling, both spatially and temporally, allowing components to operate independently. A single actor, Konrad noted, is limited in utility, but when multiple actors collaborate—such as delegating tasks to specialized actors like a “yellow specialist”—powerful patterns like worker pools and sharding emerge. These patterns enable efficient workload distribution, aligning perfectly with the distributed nature of modern systems.

Structuring Actor Systems for Clarity

A common pitfall for newcomers to Akka, Konrad observed, is creating unstructured systems with actors communicating chaotically. To counter this, he advocated for hierarchical actor systems using context.actorOf to spawn child actors, ensuring a clear supervisory structure. This hierarchy not only organizes actors but also enhances fault tolerance through supervision, where parent actors manage failures of their children. Konrad cautioned against actor selection—directly addressing actors by path—as it leads to brittle designs akin to “stealing a TV from a stranger’s house.” Instead, actors should be introduced through proper references, fostering maintainable and predictable interactions.

Balancing Power and Constraints

Konrad emphasized the philosophy of “constraints liberate, liberties constrain,” a principle echoed across Scala conferences. Akka actors, being highly flexible, can perform a wide range of tasks, but this power can overwhelm developers. He contrasted actors with more constrained abstractions like futures, which handle single values, and Akka Streams, which enforce a static data flow. These constraints enable optimizations, such as transparent backpressure in streams, which are harder to implement in the dynamic actor model. However, actors excel in distributed settings, where messaging simplifies scaling across nodes, making Akka a versatile choice for complex systems.

Community and Future Directions

Konrad highlighted the vibrant Akka community, encouraging contributions through platforms like GitHub and Gitter. He noted ongoing developments, such as Akka Typed, an experimental API that enhances type safety in actor interactions. By sharing resources like the Reactive Streams TCK and community-driven initiatives, Konrad underscored Lightbend’s commitment to evolving Akka collaboratively. His call to action was clear: engage with the community, experiment with new features, and contribute to shaping Akka’s future, ensuring it remains a cornerstone of reactive programming.

Links:

PostHeaderIcon [DevoxxFR2014] Reactive Programming with RxJava: Building Responsive Applications

Lecturer

Ben Christensen works as a software engineer at Netflix. He leads the development of reactive libraries for the JVM. Ben serves as a core contributor to RxJava. He possesses extensive experience in constructing resilient, low-latency systems for streaming platforms. His expertise centers on applying functional reactive programming principles to microservices architectures.

Abstract

This article provides an in-depth exploration of RxJava, Netflix’s implementation of Reactive Extensions for the JVM. It analyzes the Observable pattern as a foundation for composing asynchronous and event-driven programs. The discussion covers essential operators for data transformation and composition, schedulers for concurrency management, and advanced error handling strategies. Through concrete Netflix use cases, the article demonstrates how RxJava enables non-blocking, resilient applications and contrasts this approach with traditional callback-based paradigms.

The Observable Pattern and Push vs. Pull Models

RxJava revolves around the Observable, which functions as a push-based, composable iterator. Unlike the traditional pull-based Iterable, Observables emit items asynchronously to subscribers. This fundamental duality enables uniform treatment of synchronous and asynchronous data sources:

Observable<String> greeting = Observable.just("Hello", "RxJava");
greeting.subscribe(System.out::println);

The Observer interface defines three callbacks: onNext for data emission, onError for exceptions, and onCompleted for stream termination. RxJava enforces strict contracts for backpressure—ensuring producers respect consumer consumption rates—and cancellation through unsubscribe operations.

Operator Composition and Declarative Programming

RxJava provides over 100 operators that transform, filter, and combine Observables in a declarative manner. These operators form a functional composition pipeline:

Observable.range(1, 10)
          .filter(n -> n % 2 == 0)
          .map(n -> n * n)
          .subscribe(square -> System.out.println("Square: " + square));

The flatMap operator proves particularly powerful for concurrent operations, such as parallel API calls:

Observable<User> users = getUserIds();
users.flatMap(userId -> userService.getDetails(userId), 5)
     .subscribe(user -> process(user));

This approach eliminates callback nesting (callback hell) while maintaining readability and composability. Marble diagrams visually represent operator behavior, illustrating timing, concurrency, and error propagation.

Concurrency Control with Schedulers

RxJava decouples computation from threading through Schedulers, which abstract thread pools:

Observable.just(1, 2, 3)
          .subscribeOn(Schedulers.io())
          .observeOn(Schedulers.computation())
          .map(this::cpuIntensiveTask)
          .subscribe(result -> display(result));

Common schedulers include:
Schedulers.io() for I/O-bound operations (network, disk).
Schedulers.computation() for CPU-bound tasks.
Schedulers.newThread() for fire-and-forget operations.

This abstraction enables non-blocking I/O without manual thread management or blocking queues.

Error Handling and Resilience Patterns

RxJava treats errors as first-class citizens in the data stream:

Observable risky = Observable.create(subscriber -> {
    subscriber.onNext(computeRiskyValue());
    subscriber.onError(new RuntimeException("Failed"));
});
risky.onErrorResumeNext(throwable -> Observable.just("Default"))
     .subscribe(value -> System.out.println(value));

Operators like retry, retryWhen, and onErrorReturn implement resilience patterns such as exponential backoff and circuit breakers—critical for microservices in failure-prone networks.

Netflix Production Use Cases

Netflix employs RxJava across its entire stack. The UI layer composes multiple backend API calls for personalized homepages:

Observable<Recommendation> recs = userIdObservable
    .flatMap(this::fetchUserProfile)
    .flatMap(profile -> Observable.zip(
        fetchTopMovies(profile),
        fetchSimilarUsers(profile),
        this::combineRecommendations));

The API gateway uses RxJava for timeout handling, fallbacks, and request collapsing. Backend services leverage it for event processing and data aggregation.

Broader Impact on Software Architecture

RxJava embodies the Reactive Manifesto principles: responsive, resilient, elastic, and message-driven. It eliminates common concurrency bugs like race conditions and deadlocks. For JVM developers, RxJava offers a functional, declarative alternative to imperative threading models, enabling cleaner, more maintainable asynchronous code.

Links:

PostHeaderIcon [DevoxxBE2013] Introducing Vert.x 2.0: Taking Polyglot Application Development to the Next Level

Tim Fox, the visionary project lead for Vert.x at Red Hat, charts the course of this lightweight, high-performance application platform for the JVM. With a storied tenure at JBoss and VMware—where he spearheaded HornetQ messaging and RabbitMQ integrations—Tim unveils Vert.x 2.0’s maturation into an independent powerhouse. His session delves into the revamped module system, Maven/Bintray reusability, and enhanced build tool/IDE synergy, alongside previews of Scala, Clojure support, and Node.js compatibility.

Vert.x 2.0 empowers polyglot, reactive applications, blending asynchronous eventing with synchronous legacy APIs via worker verticles. Tim’s live demos illustrate deploying modules dynamically, underscoring Vert.x’s ecosystem for mobile, web, and enterprise scalability.

Core API Refinements and Asynchronous Foundations

Tim highlights Vert.x’s event-driven core, refined in 2.0 with intuitive APIs for non-JVM languages. He demonstrates verticles—lightweight actors—for handling requests asynchronously, avoiding blocking calls.

This reactive model, Tim explains, scales to thousands of connections, ideal for real-time web apps, contrasting traditional thread-per-request pitfalls.

Module System and Ecosystem Expansion

The new module system, Tim showcases, leverages Maven repositories for seamless dependency management. He deploys a web server via module names, pulling artifacts from Bintray—eliminating manual installations.

This reusability fosters a vibrant ecosystem, with core modules for HTTP, MySQL (via reversed-engineered async drivers), and more, enabling rapid composition.

Build Tool and IDE Integration

Vert.x 2.0’s Maven/Gradle plugins streamline development, as Tim demos: configure a pom.xml, run mvn vertx:run, and launch a cluster. IDE support, via plugins, offers hot-reloading and debugging.

These integrations, Tim notes, lower barriers, allowing developers to iterate swiftly without Vert.x-specific tooling.

Polyglot Horizons: Scala, Clojure, and Node.js

Tim previews Scala/Clojure bindings, enabling functional paradigms on Vert.x’s event bus. Node.js compatibility, via drop-in modules, bridges JavaScript ecosystems, allowing polyglot teams to collaborate seamlessly.

This inclusivity, Tim asserts, broadens Vert.x’s appeal, supporting diverse languages without sacrificing performance.

Worker Verticles for Legacy Compatibility

For synchronous APIs like JDBC, Tim introduces worker verticles—executing on thread pools to prevent blocking. He contrasts with pure async MySQL drivers, offering flexibility for hybrid applications.

This pragmatic bridge, Tim emphasizes, integrates existing Java libraries effortlessly.

Links:

PostHeaderIcon [DevoxxFR2013] Live Coding: A WOA Application in 50 Minutes

Lecturer

Guillaume Bort co-founded Zenexity, specializing in Web Oriented Architecture. Previously a J2EE expert, he developed web frameworks for large enterprises, creating Play Framework to prioritize simplicity. He leads Play’s development.

Sadek Drobi, Zenexity’s CTO, focuses on enterprise applications, bridging problem and solution domains. As a programming languages expert, he contributes to Play Framework’s core team.

Abstract

Guillaume Bort and Sadek Drobi’s live coding demonstrates building a Web Oriented Architecture (WOA) application using Play Framework and Scala. Consuming Twitter’s API, they handle JSON, integrate MongoDB, and stream real-time data via Server-Sent Events to an HTML5 interface. The session analyzes reactive programming, asynchronous handling, and scalability, showcasing Play’s efficiency for modern web apps.

Setting Up the Foundation: Play Framework and Twitter API Integration

Bort and Drobi initiate with Play Framework, creating a project via activator. They configure routes for homepage and stream endpoints, using Scala’s async for non-blocking I/O.

Consuming Twitter’s search API: construct URLs with keywords like “DevoxxFR”, include entities for images. Use WS (WebService) for HTTP requests, parsing JSON responses.

They extract tweet data: user, text, images. Handle pagination with since_id for subsequent queries, building a stream of results.

This setup leverages Play’s stateless design, ideal for scalability.

Building Reactive Streams: Enumerators and Asynchronous Processing

To create a continuous stream, they employ Enumerators and Iteratees. Poll Twitter periodically (e.g., every 5 seconds), yielding new tweets.

Code uses concurrent scheduling:

val tweets: Enumerator[Tweet] = Enumerator.generateM {
  Future {
    // Fetch and parse tweets
    Some(newTweets)
  }
}

Flatten to a single stream. Handle errors with recover, ensuring resilience.

This reactive approach processes data as it arrives, avoiding blocking and enabling real-time updates.

Persisting Data: Integrating MongoDB with ReactiveMongo

For storage, integrate ReactiveMongo: asynchronous, non-blocking driver. Define Tweet case class, insert via JSONCollection.

val collection = db.collection[JSONCollection]("tweets")
collection.insert(tweetJson)

Query for latest since_id. Use find with sort/take for efficient retrieval.

This maintains asynchrony, aligning with WOA’s distributed nature.

Streaming to Clients: Server-Sent Events and HTML5 Interface

Output as EventSource: chunked response with JSON events.

Ok.chunked(tweets &> EventSource()).as("text/event-stream")

Client-side: JavaScript EventSource listens, appending images with animations.

Handle dynamics: form submission triggers stream, updating UI.

This enables push updates, enhancing interactivity without WebSockets.

Optimizing for Scalability: Backpressure and Error Handling

Address overload: use onBackpressureBuffer to queue, drop, or fail. Custom strategies compress or ignore excess.

Play’s Akka integration aids actor-based concurrency.

Implications: Builds resilient, scalable apps handling high loads gracefully.

Collaborative Development: Live Insights and Community Resources

Session highlights rapid prototyping: zero slides, GitHub commits for following.

Drobi comments on concepts like futures unification in Scala 2.10, inter-library interoperability.

They encourage exploring Play docs, plugins for extensions.

This methodology fosters understanding of reactive paradigms, preparing for distributed systems.

Links: