Skip to content

"Monad I Love You Now Get Out Of My Type System" by Gjeta Gjyshinca (Strange Loop 2022)

Gjeta GjyshincaOhad

Blue TeamStrange Loop45 minOpen on YouTube ↗

Demonstrates monad practitioners Love You Now Get Out Of enterprise Type System by Gjeta Gjyshinca (Strange Loop 2022) and evaluates key implications for security practitioners.

Summary

### Overview In this talk from Strange Loop 2022, Gjeta Gjyshinca (software developer at Morgan Stanley) examines the friction caused by using monadic concurrency abstractions, specifically Scala Futures, in large codebases. Working on a core platform supporting over 12 million lines of Scala and around 1,000 developers, the team aimed to decouple business logic from execution mechanics without forcing developers to deal with invasive type wrappers. Gjyshinca presents "Optimus," Morgan Stanley's internal managed runtime and platform that provides automatic concurrency, batching, and caching through simple annotations rather than type-level monadic acrobatics.

### Key Topics - **Type-System Pollution via Futures**: In idiomatic Scala, introducing Futures transforms straightforward synchronous code into nested for-comprehensions, sequence-of-futures transformations, and complex nested types (such as `Future[Seq[Try[T]]]`). This leaks execution concerns into API signatures, damages readability, and complicates refactoring. - **Limitations of Complex Functional Alternatives**: Advanced solutions like free monads and applicatives with effects push execution to the program boundaries via interpreters, but require significant category theory knowledge. In massive commercial codebases with developers from varied backgrounds (such as Java or quantitative finance), these approaches impede velocity and hiring. - **The Analogy of Managed Runtimes**: Modern languages delegate memory management to runtimes via garbage collection rather than making pointers explicit in the type system (e.g., Eden pointers). Similarly, database engines use declarative languages like SQL to abstract away execution plans, concurrency, and locking without exposing them to user queries. - **The Optimus Runtime and `@node`**: Optimus introduces the `@node` annotation in Scala. Marking a method with `@node` serves as a contract that the function is referentially transparent, allowing the runtime to handle concurrency, I/O batching, and caching automatically while keeping standard return types intact.

### Technical Details - **Compile-Time Transformations**: The `@node` annotation triggers a Scala compiler plugin that uses `scala-async` techniques to transform annotated methods into state machines at compile time. - **Runtime Scheduling & Batching**: At runtime, a custom scheduler executes these state machines. While awaiting I/O calls, the scheduler keeps the CPU active and aggregates independent database queries into batched operations, which significantly reduces network roundtrips. - **Referential Transparency and Caching**: The runtime pairs referential transparency with append-only, bi-temporal data stores. Because querying at a fixed point in time guarantees deterministic results, Optimus transparently caches repeated queries without making redundant database calls. - **Ecosystem Challenges & Tooling**: Omitting monadic types from method signatures means synchronous methods can inadvertently sit between `@node` calls, creating serialized execution bottlenecks. The platform addresses this through compiler warnings and profilers, while exploring lightweight JVM threads (Project Loom) to dispatch threads cleanly without blocking execution threads.

### Takeaways Optimus demonstrates that concurrency, caching, and batching can be delegated to a smart runtime rather than burdening the type system. By relying on referential transparency guarantees via simple metadata (`@node`), teams can write concise, declarative business logic while achieving high-throughput, non-blocking parallel execution. Morgan Stanley has been working to open-source the platform (initially nicknamed Optimus Prime) after decoupling proprietary components and renaming packages.

Related talks