<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lukasz Seremak</title>
    <description>The latest articles on DEV Community by Lukasz Seremak (@llseremak).</description>
    <link>https://dev.to/llseremak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3252027%2Fbcd00d13-ed32-49bb-83a2-23ba90e28b01.jpg</url>
      <title>DEV Community: Lukasz Seremak</title>
      <link>https://dev.to/llseremak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/llseremak"/>
    <language>en</language>
    <item>
      <title>Serverless Functions in Java – Does It Even Make Sense?</title>
      <dc:creator>Lukasz Seremak</dc:creator>
      <pubDate>Mon, 09 Jun 2025 19:24:44 +0000</pubDate>
      <link>https://dev.to/llseremak/serverless-functions-in-java-does-it-even-make-sense-2k7h</link>
      <guid>https://dev.to/llseremak/serverless-functions-in-java-does-it-even-make-sense-2k7h</guid>
      <description>&lt;p&gt;Java is rarely the first choice for serverless functions. Interpreted languages like &lt;em&gt;JavaScript, Python, and Go&lt;/em&gt; dominate the space due to their &lt;em&gt;faster startup times&lt;/em&gt; compared to applications running on the JVM.&lt;/p&gt;

&lt;p&gt;In this article series, I’ll explore cases where writing serverless functions in Java can be not only &lt;em&gt;feasible&lt;/em&gt; but actually &lt;em&gt;beneficial&lt;/em&gt;. We'll also examine how to &lt;em&gt;optimize cold start time&lt;/em&gt; and what techniques can help build &lt;em&gt;efficient serverless solutions&lt;/em&gt; using Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Exactly Is a Serverless Function?
&lt;/h2&gt;

&lt;p&gt;First, let's break down the term &lt;em&gt;function&lt;/em&gt;. In mathematics, a function defines the relationship between an &lt;em&gt;input value&lt;/em&gt; (&lt;em&gt;x&lt;/em&gt;) and an &lt;em&gt;output value&lt;/em&gt; (&lt;em&gt;f(x)&lt;/em&gt;), where for any given &lt;em&gt;x&lt;/em&gt;, there’s always a unique result. We can express this as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(x) = x² + 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if &lt;em&gt;x = 2&lt;/em&gt;, we get &lt;em&gt;f(2) = 7&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A serverless function operates on a similar principle – it takes an &lt;em&gt;input&lt;/em&gt;, processes it, and returns a &lt;em&gt;defined result&lt;/em&gt;. It doesn't retain history or store state (&lt;em&gt;stateless&lt;/em&gt;) – it runs only when &lt;em&gt;invoked&lt;/em&gt;, executes the required computation, and then &lt;em&gt;disappears&lt;/em&gt;, consuming &lt;em&gt;zero system resources&lt;/em&gt; when idle.&lt;/p&gt;

&lt;p&gt;This is, of course, a simplified view, but it captures the essence of what &lt;em&gt;serverless functions&lt;/em&gt; truly are.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does a Serverless Function Work?
&lt;/h2&gt;

&lt;p&gt;Imagine you're running an online store and need a mechanism to calculate &lt;em&gt;discounted prices&lt;/em&gt; on demand. Instead of deploying a &lt;em&gt;full application server&lt;/em&gt;, you can create a serverless function that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Takes the base price and discount percentage&lt;/em&gt; as input.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Calculates the final discounted price&lt;/em&gt; using a predefined formula.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Returns the result&lt;/em&gt; to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Serverless functions can be triggered in multiple ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Via an API&lt;/em&gt;, when a user sends an HTTP request.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Through a scheduled job&lt;/em&gt;, for tasks like daily data analysis.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;In response to an event&lt;/em&gt;, such as a new file added to a database or a message arriving in a queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The Magic Begins – Scaling from Zero to... Sky Is the Limit
&lt;/h1&gt;

&lt;p&gt;One of the &lt;em&gt;biggest advantages&lt;/em&gt; of serverless functions is &lt;em&gt;automatic scaling&lt;/em&gt; – from &lt;em&gt;zero&lt;/em&gt; when there's no traffic, to &lt;em&gt;thousands of concurrent instances&lt;/em&gt; when demand spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does This Work in Practice?
&lt;/h2&gt;

&lt;p&gt;Let's return to our &lt;em&gt;discount calculator&lt;/em&gt; example. Imagine your store allows customers to enter a product price to see the discount applied. During the day, traffic is low – just a few requests per hour. But in the evening, your store runs a &lt;em&gt;flash sale&lt;/em&gt;, and suddenly thousands of users flood in to check prices.&lt;/p&gt;

&lt;p&gt;With a &lt;em&gt;traditional infrastructure&lt;/em&gt;, you’d need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Predict traffic spikes&lt;/em&gt; (which is tricky – it could be 500 users or 50,000).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Manually scale up resources&lt;/em&gt; to handle peak demand.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Pay for all the infrastructure&lt;/em&gt;, even if traffic turns out lower than expected.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With &lt;em&gt;serverless functions&lt;/em&gt;, it's &lt;em&gt;different&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a &lt;em&gt;single request&lt;/em&gt; arrives, only &lt;em&gt;one instance&lt;/em&gt; of the function runs.&lt;/li&gt;
&lt;li&gt;If the store receives &lt;em&gt;10,000 requests in a few minutes&lt;/em&gt;, the cloud &lt;em&gt;automatically spins up 10,000 instances&lt;/em&gt;, each handling one request.&lt;/li&gt;
&lt;li&gt;When the sale &lt;em&gt;ends&lt;/em&gt; and users stop checking prices, all instances &lt;em&gt;shut down&lt;/em&gt;, meaning &lt;em&gt;zero resource consumption and zero cost&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  You Only Pay for What You Use
&lt;/h2&gt;

&lt;p&gt;Thanks to the &lt;em&gt;pay-as-you-go&lt;/em&gt; model, you don’t pay for idle infrastructure – &lt;em&gt;only for actual function execution time&lt;/em&gt;. If no users access your application all day, &lt;em&gt;your cost is zero&lt;/em&gt;. If for an hour, 100,000 requests flood in, functions will &lt;em&gt;only run as needed&lt;/em&gt;, without wasting resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sky Is the Limit – Effortless Scaling Beyond Traditional Infrastructure
&lt;/h2&gt;

&lt;p&gt;Classic applications require &lt;em&gt;manual scaling&lt;/em&gt; or autoscalers, predicting demand and managing resources. Serverless removes this overhead. If &lt;em&gt;100, 1,000, or even 100,000 instances&lt;/em&gt; are needed, the system &lt;em&gt;handles it automatically&lt;/em&gt;. While cloud providers impose some limits, for most use cases, &lt;em&gt;serverless enables scaling that traditional setups struggle to match&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Reality Isn’t So Perfect
&lt;/h1&gt;

&lt;p&gt;Serverless functions sound &lt;em&gt;too good to be true&lt;/em&gt; – they scale automatically, require &lt;em&gt;zero server management&lt;/em&gt;, and you &lt;em&gt;only pay for execution&lt;/em&gt;. Sounds amazing, right? Well… not everything is that perfect. Serverless comes with challenges and &lt;em&gt;limitations&lt;/em&gt; that developers should be aware of.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cold Start – JVM’s Biggest Pain Point
&lt;/h2&gt;

&lt;p&gt;One of the biggest drawbacks of serverless functions is the &lt;em&gt;cold start&lt;/em&gt;, which refers to the time needed to spin up a function &lt;em&gt;after a period of inactivity&lt;/em&gt;. Why does this matter?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Interpreted languages&lt;/em&gt; (Python, JavaScript) &lt;em&gt;start almost instantly&lt;/em&gt;, while &lt;em&gt;Java&lt;/em&gt;, running on the &lt;em&gt;JVM&lt;/em&gt;, requires &lt;em&gt;loading the runtime environment&lt;/em&gt;, which can take several &lt;em&gt;seconds&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;em&gt;interactive applications&lt;/em&gt;, even a &lt;em&gt;few-second delay&lt;/em&gt; is problematic, especially if functions handle &lt;em&gt;user-facing API requests&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Execution Time Limits
&lt;/h2&gt;

&lt;p&gt;Most cloud providers &lt;em&gt;set a maximum execution time&lt;/em&gt; for serverless functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;AWS Lambda&lt;/em&gt; – Up to &lt;em&gt;15 minutes&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Azure Functions&lt;/em&gt; – Up to &lt;em&gt;10 minutes&lt;/em&gt; (Consumption Plan).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Google Cloud Functions&lt;/em&gt; – Typically &lt;em&gt;several minutes&lt;/em&gt;, depending on configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means &lt;em&gt;serverless isn’t suited for long-running processes&lt;/em&gt; – for complex tasks, a &lt;em&gt;dedicated application server&lt;/em&gt; or distributed processing system is often a better choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stateless Nature
&lt;/h2&gt;

&lt;p&gt;Serverless functions are &lt;em&gt;stateless&lt;/em&gt; – every time they run, they start in a &lt;em&gt;fresh environment&lt;/em&gt;. They can’t retain data &lt;em&gt;between executions&lt;/em&gt;, so if persistent storage is required, you need to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Databases&lt;/em&gt; (e.g., DynamoDB, Firebase).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Queues and message streams&lt;/em&gt; (e.g., Kafka, SQS).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Cloud storage&lt;/em&gt; (e.g., S3, Cloud Storage).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Debugging and Diagnostics
&lt;/h2&gt;

&lt;p&gt;Debugging &lt;em&gt;serverless functions&lt;/em&gt; can be &lt;em&gt;challenging&lt;/em&gt;, as there’s &lt;em&gt;no direct access&lt;/em&gt; to the infrastructure. Instead, developers must rely on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Cloud provider logs&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Performance monitoring tools&lt;/em&gt; to analyze issues in real time.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Local testing frameworks&lt;/em&gt; like &lt;em&gt;AWS SAM&lt;/em&gt; or &lt;em&gt;LocalStack&lt;/em&gt; for simulating execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Serverless Discount Calculator Project
&lt;/h1&gt;

&lt;p&gt;As part of this series, we’ll implement a &lt;em&gt;serverless application for dynamic discount calculations&lt;/em&gt; using &lt;em&gt;AWS Lambda, API Gateway, and SQS&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture Overview
&lt;/h2&gt;

&lt;p&gt;Our system follows an &lt;em&gt;event-driven&lt;/em&gt; architecture with key components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;REST API Gateway (AWS API Gateway)&lt;/em&gt; – Handles &lt;code&gt;PUT /calculatePrice&lt;/code&gt;, forwarding user ID and base price to Lambda.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;AWS Lambda – Discount Calculation&lt;/em&gt; –

&lt;ul&gt;
&lt;li&gt;Fetches &lt;em&gt;personal discount&lt;/em&gt; via &lt;em&gt;external REST API&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Retrieves &lt;em&gt;global discount&lt;/em&gt; from another API (which updates dynamically).&lt;/li&gt;
&lt;li&gt;Computes the final &lt;em&gt;discount&lt;/em&gt; and returns it via API Gateway.&lt;/li&gt;
&lt;li&gt;Sends a message to &lt;em&gt;AWS SQS&lt;/em&gt; with discount details.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;AWS Lambda – Data Storage&lt;/em&gt; –

&lt;ul&gt;
&lt;li&gt;Listens to &lt;em&gt;SQS queue&lt;/em&gt;, storing data in the database.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mv08xtqidd99zyww13u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mv08xtqidd99zyww13u.png" alt="Image description" width="800" height="543"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;If you're interested, &lt;em&gt;subscribe to my profile&lt;/em&gt; – the &lt;em&gt;next part&lt;/em&gt; of this series is coming soon! 🚀&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
