Skip to departures
Hpjadul
MAC END

Modal's Serverless Platform: Unpacking the Infrastructure Behind `modal-http`

Departs
Crew
Eric Zhang
Passengers
608

Modal's `modal-http` stack combines HTTP and WebSocket support in a serverless model, enabling complex workloads while addressing key scaling challenges.

Modal's Serverless Platform: Unpacking the Infrastructure Behind `modal-http`

Modal has developed an HTTP and WebSocket stack that integrates deeply with the architecture of serverless functions on its platform. This service, dubbed modal-http, facilitates web requests and is designed to handle the complexities of modern web applications.

Simple schematic with modal-http at the center

Building modal-http wasn't straightforward due to the vast array of edge cases associated with HTTP. To tackle these challenges, Modal chose Rust, a programming language known for its speed and safety features. The recent introduction of full WebSocket support enables real-time bidirectional messaging capabilities, thereby extending the usability of serverless functions.

Deployment of simple web functions to a *.modal.run URL is efficient; for instance, minimal code in Python allows for quick setups. With the current deployment time averaging around 0.747 seconds, developers can rapidly iterate and test their applications. Additionally, Modal’s architecture supports large-scale compute workloads, exemplified through a video processing endpoint that can utilize resources for up to 30 minutes and allocate up to 32 GB of memory across eight CPUs.

Diagram of two requests, HTTP GET on top and HTTP POST on the bottom

Understanding Modal's Serverless Challenge

Traditionally, serverless computing frameworks impose numerous limitations on function execution and resource allocation. For example, AWS Lambda restricts execution times to a maximum of 15 minutes, limits deployment package sizes to 50 MB, and allocates up to 10 GB of memory. In contrast, Google Cloud Run offers 32 GB of memory and 75 Mbps of bandwidth, while the Cloudflare Workers have even stricter parameters with a maximum of 128 MB of memory. These constraints pose significant challenges for modern, resource-intensive workloads such as machine learning training and data processing.

In stark contrast, Modal allows its containers to utilize up to 64 CPUs, 336 GB of memory, and 8 Nvidia H100 GPUs, alongside the ability to download hundreds of gigabytes of model weights. The capacity for quick spin-up and shutdown times is critical for minimizing downtime and operational costs, with billing calculated by the second. This unleashes a new level of flexibility for users who require significant computational power without the conventional restrictions of serverless platforms.

Diagram of an HTTP POST request with events marked

A Distributed Approach: Autoscaling and Load Balancing

At its core, the concept of Modal's serverless platform revolves around autoscaling and load balancing. The analogy to a factory is apt: an increased number of incoming requests demands the deployment of additional computational resources, while a decrease in demands allows for the gradual shutting down of these resources.

This principle applies across various serverless architectures. Whether deploying through a Platform as a Service (PaaS) or an orchestrator like Kubernetes, two main principles prevail:

Diagram of a disconnected HTTP request
  1. Autoscaling: Automating the scaling of application instances in response to workload metrics—like CPU utilization and response time.
  2. Load Balancing: Efficiently distributing incoming network traffic across multiple servers to optimize resource use and minimize response times.

Modal's design diverges from traditional methods by translating incoming HTTP requests directly into function calls, bypassing the need for a reverse proxy, which is common in most serverless systems. This shift allows Modal to utilize faster communication paths that are optimized for function-to-function interactions.

Translating HTTP to Function Calls

Traditional serverless platforms often see HTTP requests served via a reverse proxy setup, but Modal's approach considers functions as first-class citizens. Users can invoke functions directly without needing to set up REST APIs, simplifying the development process significantly.

Diagram of a long-running request with one 303 See Other response

This is achieved through a streamlined architecture that treats both web requests and direct function calls uniformly, which enables Modal to maintain high performance for both types of applications. Notably, this results in an optimized path for in-network function calls, bolstering the speed and efficiency of operations.

By adopting this method, Modal can also fully support real-time data streaming and complex workloads flexibly using CPUs and GPUs. The service is strategically engineered to prioritize systems engineering for heavy-duty function calls, positioning itself for substantial computational tasks.

Diagram of a WebSocket connection

HTTP Protocol Implementation

A sound understanding of HTTP protocols underpins how Modal translates web requests into function invocations. The HTTP model follows a request-response architecture wherein clients send requests to servers, which process these requests and return responses.

This involves several nuances inherent to the protocol's structure. For example, while HTTP/1.0 operated with a separate TCP stream for each request, HTTP/1.1 introduced persistent connections, and later versions like HTTP/2 enhanced this further with multiplexing.

A browser sends a request to modal-http

To implement effective function dispatching, Modal chose to represent HTTP requests as events, leveraging the ASGI (Asynchronous Server Gateway Interface) protocol, commonly utilized in Python web frameworks such as Flask and FastAPI. This approach ensured compatibility while allowing for an efficient event-driven mechanism within Modal’s distributed runtime.

For example, when a POST request is made, the system translates it into ASGI events where headers are processed, triggering function execution, which streamlines incoming request handling, including backpressure management, and memory utilizations effectively. Such architecture elevates Modal above traditional serverless limitations, supporting payload sizes of up to 4 GiB versus the typical limits imposed by contemporaneous systems.

Full path of a request through L4 NLB and Caddy

Handling Edge Cases and Errors

Implementing this system comes with its array of challenges. Modal must manage error handling proactively, particularly in scenarios such as client disconnections mid-request or server outages. The architecture is built to gracefully terminate processes and appropriately report issues back to the users.

For instance, if a client disconnects during a request, a corresponding ASGI event (`http.disconnect`) signals the function to halt execution, thereby protecting resources from undue consumption and potential failures due to incomplete data transfers.

Moreover, like any complex system, Modal must contend with idle timeouts—particularly for long-running functions. To address potential timeout failures that might arise from HTTP clients exceeding their preset limits (often capped at 300 seconds), Modal issues temporary redirects after a certain duration of inactivity, thus extending the backend's operation time while shielding users from unexpected failures. This innovative approach effectively allows support for processing tasks that may take longer than the client-side limitations.

WebSocket Connections and Their Integration

Modal additionally addresses request types that involve WebSocket connections, which initiate through an HTTP handshake mechanism before establishing a real-time communication channel. Like the HTTP handling, WebSocket connections are managed using Rust and involve translating HTTP requests into ASGI events tailored for WebSocket handling.

The result is seamless communication between Modal functions and WebSocket connections, allowing for the exchange of ongoing messages post-handshake. This extends the operational capability, melding traditional request-response models with a dynamic conversation format that can be resource-intensive, particularly relevant for applications that require real-time updates such as chat applications or live data feeds.

Leveraging Open-Source Infrastructure

While the heavy-lifting of Modal’s CPU-intensive tasks is managed by proprietary runtime infrastructure, the foundational elements stand upon tried-and-tested open-source technologies. The deployment of modal-http and the service purchased rely on standard tools like Kubernetes and the Caddy web server for essential functionality, such as network load balancing and TLS termination.

This infrastructure enables Modal to efficiently handle vast quantities of HTTP requests while simultaneously scaling services to match user demands without recomplicating the core serverless experience.

Lessons and Future Directions

In conclusion, Modal has positioned itself within the serverless landscape by challenging traditional model conventions. By transcending limitations typically seen in serverless deployments, Modal's `modal-http` empowers developers to build more complex applications responsive to user needs through optimized HTTP and WebSocket protocols.

With the robust capacity of supporting both real-time and intensive workloads, along with innovative strategies to manage requests and error handling, Modal significantly enhances the serverless function landscape, providing a promising infrastructure suitable for a myriad of modern applications. As the platform expands its geographical footprint, users can look forward to consistently low-latency operations and elevated service reliability across the board.

Further advancements hint at potential integrations that may seamlessly translate between TCP port exposure and traditional REST methodologies, ensuring Modal continues providing a flexible, powerful environment responsive to developers' evolving needs.

Special thanks to the Modal team, whose insights significantly shaped this overview, spotlighting the intricate workings behind `modal-http` in the journey towards redefining serverless function capabilities.

Source: Eric Zhang · modal.com
Arrived · Hpjadul

Also departing from this gate