Serverless computing whether on AWS Lambda, Azure Functions, or Google Cloud Functions plays a vital role in modern distributed systems. By abstracting infrastructure away from application code, serverless platforms allow developers to focus on business logic. These functions scale automatically and can be highly cost-efficient when applied to the right use cases.
In serverless platforms, applications may be started many times a day and often run only for a few seconds. They are scaled up and down automatically and may be paused or frozen between requests. This behavior does not only affect performance; it also directly impacts cost, since billing is often based on execution time and allocated memory.
Java has traditionally struggled in this model, but Java 25 marks a significant step forward. Improvements in garbage collection, startup performance, and concurrency make Java far better suited for modern serverless workloads. Virtual Threads and Structured Concurrency allow developers to build high-throughput, low-latency serverless functions using a clear and readable programming model.\
This article explores how Java 25 redefines the way serverless functions can be implemented on the JVM.
The Serverless Challenge for Java
Serverless functions are event-driven by nature. They are invoked in response to HTTP requests, message queue updates, scheduled triggers, or stream-processing events. These workloads share several common characteristics:
\
- Unpredictable traffic patterns\
- Short-lived executions\
- Strict latency requirements\
- Strong sensitivity to cold-start time\
Earlier Java versions often struggled in this environment. Depending on the framework used, cold-start time alone could be a major obstacle. Expensive platform threads limited scalability for I/O-heavy workloads, and garbage collection pauses further amplified latency concerns. Together, these factors made Java a challenging choice for latency-sensitive serverless applications.\
Java 25 addresses these issues at the platform level.
Faster Startup and Cold-Start Improvements
Cold-start latency has long been one of the biggest concerns for Java-based serverless functions. Java 25 builds on several ongoing improvements in JVM startup performance, class loading, and runtime initialization. A major long-term effort in this area is Project Leyden, which is particularly relevant for serverless environments where startup time and memory footprint matter more than almost anything else.
Project Leyden aims to reduce startup time by shifting a significant portion of JVM initialization work from runtime to build time without abandoning the JVM. Instead of compiling Java applications into fully native executables, Leyden keeps them running on the JVM but allows them to start from a pre-optimized state.\
The idea is straightforward: if the JVM already knows which classes will be loaded, how they are linked, and what the application’s startup behavior looks like, it does not need to repeat that work on every invocation. This reduces repeated class loading, linking, and initialization during cold starts.
The result is faster startup and lower memory usage, which directly improves cold-start performance. Leyden can be thought of as pre-warming the JVM at build time. Applications are analyzed and prepared ahead of time so that, when a serverless platform spins up a new instance, the JVM can begin executing application code much sooner.
Importantly, Leyden preserves familiar Java features such as reflection and dynamic class loading. This makes adoption easier, as teams do not need to redesign their applications or give up widely used Java libraries. Frameworks that already minimize runtime reflection and classpath scanning such as Micronaut and newer Spring configurations are particularly well positioned to benefit as Leyden matures.\
In the context of serverless functions, Project Leyden represents Java’s long-term strategy for competing with languages known for fast startup, such as Go and Node.js, while retaining Java’s strengths in performance, tooling, and ecosystem.
Garbage Collection and Predictable Latency
Garbage collection plays a significant role in Java performance within serverless environments. Java 25 continues to improve the JVM’s ability to deliver predictable latency under short-lived, bursty workloads.\
Traditional garbage collectors such as Parallel GC or even G1 GC rely on stop-the-world pauses to reclaim memory. While these pauses can be short, they are often unpredictable. In serverless functions, even small pauses can increase request latency, trigger timeouts, or increase cost, since execution is billed with fine-grained precision.
Java 25 emphasizes concurrent, low-latency garbage collection, where most GC work is performed while application threads continue to run. This is achieved through techniques such as concurrent marking and relocation, read and write barriers, and metadata embedded in object references. The trade-off is slightly higher CPU and memory overhead, but the benefit is far more predictable application behavior.\
One of the most important collectors in this space is Z Garbage Collector (ZGC). ZGC is now generational, separating short-lived objects from long-lived ones. This is particularly beneficial for serverless functions, which tend to allocate many temporary objects during request handling. ZGC is designed to keep pause times very short and largely independent of heap size, helping to maintain consistent response times even during traffic spikes. It achieves this using techniques such as colored pointers to track object state with minimal global coordination.
Java 25 also includes Shenandoah GC, which performs most garbage collection work concurrently with application threads. Shenandoah typically delivers pause times in the low-millisecond range and supports compressed object pointers, reducing memory usage. This can be especially attractive in serverless environments where memory allocation directly affects cost.
For Java-based serverless functions that require consistent and predictable response times, these low-latency collectors significantly reduce GC-related latency. While they may consume slightly more CPU or memory, the improved predictability and smoother execution make Java 25 a much better fit for modern serverless workloads.
Leaner Java Objects for Serverless Workloads
Another important improvement in Java 25 is Compact Object Headers, introduced through JEP 519. Every Java object carries a header containing metadata such as locking information and garbage-collection state. Historically, these headers were relatively large, contributing to higher memory usage.
In serverless functions that process requests, parse JSON, or construct response objects, large numbers of small, short-lived objects are common. Compact Object Headers reduce the size of these headers, allowing more objects to fit into the same heap space.\
- In a serverless context, this leads to:\
- Lower overall memory footprints\
- Faster garbage collection cycles\
- Improved cache locality and CPU efficiency
These benefits reduce execution time during cold starts and burst traffic, and can lower infrastructure costs. Compact Object Headers complement low-latency garbage collectors by making heaps denser and more efficient, further strengthening Java 25’s suitability for serverless architectures.
Virtual Threads for Scalable Concurrency
Virtual Threads are lightweight threads managed by the JVM rather than the operating system. They are inexpensive to create and can support very high levels of concurrency when used with Loom-aware libraries.\
In a serverless environment, this is a major advantage. Each function invocation can be processed in its own virtual thread using a straightforward, synchronous programming style. When a virtual thread performs a blocking I/O operation, the JVM transparently suspends it and frees the underlying carrier thread to perform other work.
This model enables:\
- Efficient handling of I/O-heavy workloads\
- High concurrency without thread exhaustion\
- Simple, readable code without reactive abstractions
For serverless platforms that frequently spin up and tear down execution environments, virtual threads significantly improve resource utilization and scalability.\
Structured Concurrency for Reliable Execution
Serverless functions often perform multiple tasks within a single invocation, such as validation, enrichment, and downstream service calls. Coordinating these tasks reliably especially in the presence of failures can be difficult.
Structured Concurrency provides a disciplined approach to managing multiple concurrent operations as a single unit of work. Tasks launched within a structured scope are automatically tracked, and failures propagate in a predictable way.
For serverless functions, this enables:\
- Clear lifecycle management for concurrent tasks\
- Automatic cancellation of related work on failure\
- Simpler error handling and improved reliability\
Rather than manually coordinating futures or callbacks, developers can express parallelism directly while preserving correctness and clarity.
Simplifying Serverless Architecture with Java 25
** Taken together, Java 25’s features reduce the need for complex frameworks or custom concurrency libraries in serverless applications. With Java-native concurrency, blocking I/O becomes safe and scalable. Parallel execution is easier to reason about, and startup time improves through better memory management and runtime optimizations.
Developers can write clear, domain-oriented Java code while still meeting the performance, scalability, and cost-efficiency demands of modern serverless platforms.\
Conclusion
Serverless computing demands concurrency, efficiency, and predictable performance. With Java 25, these requirements are no longer at odds with simplicity or readability on the JVM.
Virtual Threads and Structured Concurrency enable scalable and safe execution models. Improvements in startup performance and garbage collection reduce latency and operational risk. Together, these advancements make Java a far stronger candidate for serverless workloads than in previous releases.
Java 25 represents a meaningful turning point for serverless development on the JVM making it possible to build performant, maintainable, and scalable serverless functions using native Java capabilities with confidence.\
This article is part of the JAVAPRO magazine issue:
From AI as a Feature tu AI as Infrastructure
Move beyond AI experimentation and into AI engineering.
Explore the architectures, platforms, and operational practices required to build trustworthy AI systems at scale. From governance and observability to modern Java infrastructure, this edition examines the foundations of production-ready AI.
**Discover the edition **→





