<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mina on Medium]]></title>
        <description><![CDATA[Stories by Mina on Medium]]></description>
        <link>https://medium.com/@minadev?source=rss-d69531287448------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ssfIDqEiz4qHg8KzX39zWA@2x.jpeg</url>
            <title>Stories by Mina on Medium</title>
            <link>https://medium.com/@minadev?source=rss-d69531287448------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 14:51:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@minadev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java Virtual Threads: Understanding JDK 21 Limitations Before the JDK 25 LTS Release]]></title>
            <link>https://medium.com/@minadev/java-virtual-threads-understanding-jdk-21-limitations-before-the-jdk-25-lts-release-7b3754228e44?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b3754228e44</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Mon, 01 Sep 2025 07:11:42 GMT</pubDate>
            <atom:updated>2025-09-01T07:11:42.047Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding Virtual Threads</h3><p>Java Virtual Threads were introduced as a preview in JDK 19 and became stable in JDK 21. They promised to handle highly concurrent applications with thousands of lightweight tasks.</p><p>But first, let’s understand the problem they solved.</p><p>Traditional Java threads are expensive. Each thread maps directly to an OS thread, making them resource-heavy and limiting scalability.</p><p>Virtual threads changed this. They’re lightweight threads managed by the JVM, not the OS. You can create millions of them. Here’s how they work:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JceWzOsYaQdHCGrZ.png" /></figure><ol><li><strong>Virtual threads</strong> run on top of <strong>carrier threads</strong> (platform threads)</li><li>When a virtual thread blocks (waiting for I/O), it gets “unmounted” from its carrier</li><li>The carrier thread is free to run other virtual threads</li><li>When the blocking operation completes, any available carrier can “mount” the virtual thread again</li></ol><p>This design makes virtual threads perfect for I/O-heavy applications — exactly what most web applications are.</p><p>However, JDK 21 had a significant limitation: <strong>pinning</strong>. Virtual threads could get stuck to their carrier threads during certain operations, defeating their main benefit.</p><h3>The Synchronized Keyword: A Quick Refresher</h3><p>The synchronized keyword has been Java&#39;s go-to for thread safety since day one. It does two things:</p><ol><li><strong>Mutual exclusion</strong>: Only one thread can execute the synchronized code at a time</li><li><strong>Memory visibility</strong>: Changes made by one thread are visible to others</li></ol><pre>// Method-level synchronization<br>public synchronized void updateCounter() {<br>    counter++;<br>}</pre><pre>// Block-level synchronization<br>public void processData() {<br>    synchronized(lockObject) {<br>        // Critical section<br>        sharedResource.update();<br>    }<br>}</pre><p>Simple, reliable, and widely used in legacy codebases. But this is where virtual threads has major issue.</p><h3>The Pinning Problem in Java 21</h3><p>In Java 21, virtual threads had a major limitation. When a virtual thread entered a synchronized block and performed a blocking operation, it got &quot;pinned&quot; to its platform thread.</p><h3>When Pinning Leads to Deadlocks</h3><p>The pinning problem wasn’t just about performance, it could cause complete application deadlocks. Here’s how:</p><ol><li>Virtual threads enter synchronized blocks and get pinned to carrier threads</li><li>They make blocking calls (database, I/O) while still pinned</li><li>All available carrier threads become occupied with pinned virtual threads</li><li>New virtual threads can’t get carrier threads to run on</li><li>The app freezes, a typical deadlock.</li></ol><p>Here’s what happened:</p><pre>synchronized(lock) {<br>    // Database call or I/O operation<br>    String data = database.fetch(); // This caused pinning in Java 21<br>    return processData(data);<br>}</pre><p>When pinned, the virtual thread couldn’t release its underlying platform thread, even while waiting. This defeated the whole purpose of virtual threads, being lightweight and allowing one platform thread to serve many virtual threads.</p><h3>What Causes Pinning?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x3mhJPGaFDUR3PWoV5VtSw.png" /></figure><h3>The Java 24 Improvment</h3><p>Java 24 fixed the biggest pinning issue: <strong>synchronized blocks no longer pin virtual threads</strong>.</p><p>Now when a virtual thread blocks inside a synchronized block, it can release its carrier thread. Any available platform thread can later resume the work when the blocking operation completes.</p><h3>Real Performance Impact</h3><p>Here’s what this pinning problem actually cost in real applications.</p><p><strong>Performance Test: 5,000 Virtual Threads with Database Calls</strong> In my benchmark, each thread acquired a lock, did some processing, then made a simulated database call (5ms delay) inside a synchronized block:</p><ul><li><strong>Java 21</strong>: 31–32 seconds total</li><li><strong>Java 24</strong>: 0.463 seconds total</li></ul><p>Why such a massive difference? In Java 21, each virtual thread stayed pinned to its carrier thread during the database wait. With limited carrier threads (typically matching CPU cores), most virtual threads had to queue up, waiting for a carrier to become available.</p><p>In Java 24, virtual threads release their carriers during the database call, letting those carriers serve other threads immediately.</p><p>This represents a 98% performance improvement just from upgrading Java versions. So it is clear: Java 21’s pinning problem made virtual threads unsuitable for applications using synchronized blocks with I/O operations. Java 24 resolves this issue entirely.</p><h3>Why This Matters</h3><p>If you’re building applications that:</p><ul><li>Handle high concurrent loads</li><li>Use legacy code with synchronized blocks</li><li>Make database calls or external service calls</li><li>Need better resource utilization</li></ul><p>Then Java virtual threads in 24+ are a game-changer. You get the scalability benefits without rewriting your synchronized code.</p><h3>Looking Ahead: Java 25 LTS (September 2025)</h3><p>With Java 25 LTS releasing in just less than a month (September 16, 2025), this is the perfect time to understand virtual threads’ evolution. Java 25 will offer:</p><ul><li><strong>Long-term support stability</strong> for enterprise adoption</li><li><strong>Mature virtual thread implementation</strong> with Java 24’s pinning fixes included</li><li><strong>Production-ready confidence</strong> for mission-critical applications</li></ul><p>If you’re planning your next LTS upgrade, understanding this virtual thread journey from Java 21’s limitations to Java 24’s breakthrough helps you make informed decisions.</p><h3>Bottom Line</h3><p>Java 21 introduced virtual threads but had the pinning problem. Java 24 solved it. Java 25 LTS delivers the mature version.</p><p>If you tried virtual threads in Java 21 and hit performance issue, Java 24+ is your answer. If you haven’t tried them yet, Java 25 LTS is the right time to start.</p><p>The pinning problem is resolved. Virtual threads are now ready for high-throughput production applications with no more choosing between legacy code compatibility and performance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b3754228e44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kafka with Avro Schemas]]></title>
            <link>https://levelup.gitconnected.com/mastering-kafka-with-avro-schemas-c7645b5b0610?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7645b5b0610</guid>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[avro-schema]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[avro]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Wed, 29 Jan 2025 19:28:24 GMT</pubDate>
            <atom:updated>2025-01-31T14:03:14.185Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with distributed systems like Apache Kafka or other data pipelines, ensuring smooth communication between producers and consumers is crucial. This is where <strong>Apache Avro</strong>, its schemas, and the concept of schema evolution come into play.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7IQsAWli3yXzbSpJ" /></figure><p>In this guide, I will cover:</p><ul><li>What is Avro?</li><li>What is a schema, and why do you need it?</li><li>How schema evolution ensures compatibility and simplifies data changes.</li><li>Tools and plugins for integrating Avro into your build process.</li><li>What are schema registries?</li><li>How to upload schemas to AWS Glue and implement them in your application.</li></ul><p><em>You can find the source code in this GitHub repository: </em><a href="https://github.com/minarashidi/spring-kafka-avro-demo"><em>spring-kafka-avro-demo</em></a><em>.</em></p><h3><strong>What is Avro?</strong></h3><p>Apache Avro is an open-source data serialization system designed for efficient data exchange between systems and programming languages. Created by Doug Cutting, the founder of Hadoop, Avro is lightweight, schema-based, and perfect for handling large-scale data.</p><p>It serializes data into a compact binary format, making it fast for transmission and storage. Its schemas are written in JSON, allowing producers and consumers to define and agree on the data structure.</p><p>Avro works with many programming languages, including Java, Python, C++, and more.</p><h4>Why Use Avro?</h4><p>Avro simplifies data handling and communication in distributed systems by offering:</p><ul><li><strong>Compact Binary Format</strong>: Reduces data size for efficient storage and transmission.</li><li><strong>Schema-Based</strong>: Each piece of data comes with a schema, ensuring clarity and consistency.</li><li><strong>Schema Evolution</strong>: As your data needs change, Avro handles those changes seamlessly, allowing your systems to evolve without breaking compatibility.</li><li><strong>Cross-Language Compatibility</strong>: Supports Java, Python, C++, and other languages, enabling smooth integration across diverse systems.</li><li><strong>Efficiency and Simplicity</strong>: Avoids repetitive field names, optimizes performance, and works without requiring pre-generated code.</li></ul><h3>What is a Schema, and Why Do You Need It?</h3><p>In Avro, a <strong>schema</strong> is a JSON file (commonly .avsc) that defines the structure of your data. It serves as a blueprint, describing the fields, data types, and rules that both producers and consumers must follow when exchanging data.</p><h4>Example Avro Schema (User.avsc):</h4><pre>{<br>  &quot;type&quot;: &quot;record&quot;,<br>  &quot;name&quot;: &quot;User&quot;,<br>  &quot;fields&quot;: [<br>    { &quot;name&quot;: &quot;id&quot;, &quot;type&quot;: &quot;int&quot; },<br>    { &quot;name&quot;: &quot;name&quot;, &quot;type&quot;: &quot;string&quot; },<br>    { &quot;name&quot;: &quot;email&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null }<br>  ]<br>}</pre><h4>Why Do You Need a Schema?</h4><ul><li><strong>Consistency</strong>: Ensures that producers and consumers agree on the structure and types of data.</li><li><strong>Validation</strong>: Validates that data conforms to the schema before serialization or deserialization.</li><li><strong>Compact Encoding</strong>: Avro encodes only the data (not field names) during serialization, relying on the schema to decode it.</li></ul><h3>Why Is Schema Management Important in Kafka?</h3><p>Managing <strong>data schemas</strong> in Kafka ensures smooth communication between producers and consumers, especially as data structures evolve.</p><p>Without schema management, you face several challenges:</p><ul><li><strong>Risk of Failure</strong>: Consumers may struggle to process messages if the schema changes unexpectedly.</li><li><strong>Increased Maintenance</strong>: Every schema update would require you to manually update and redeploy both the producer and consumer.</li></ul><p><strong>Example:</strong> Consider a producer sending product updates to a Kafka topic, while a consumer uses that data to update inventory records in a database. If the producer adds a new field, such as “productDescription,” and the consumer isn’t aware of the change, the consumer might fail to process the data correctly.</p><p>Schema management helps ensure that both the producer and consumer understand the data, even as the schema changes over time.</p><h3>What is Schema Evolution?</h3><p>Schema evolution allows you to change your data structure (schema) over time while maintaining compatibility between producers and consumers.</p><p>There are three main types of schema compatibility:</p><h4>Backward Compatibility</h4><p>✅ New producers → Old consumers (Works)<br>Old consumers can still read data from new producers, ensuring they don’t break.<br>Example: Adding a new optional field won’t affect older consumers since they’ll simply ignore it.</p><h4>Forward Compatibility</h4><p>✅ Old producers → New consumers (Works)<br>New consumers can still read data from old producers, ensuring they can process older data.<br>Example: Adding a default value for a new field allows newer consumers to handle missing values from older producers.</p><p>In short:</p><ul><li><strong>Backward compatibility</strong> focuses on producers evolving the schema without breaking old consumers.</li><li><strong>Forward compatibility</strong> focuses on consumers handling older schemas from legacy producers.</li></ul><h4>Full Compatibility</h4><p>✅ Backward + Forward Compatibility (Both Work)</p><p>Any change must ensure that <strong>older consumers and older producers can still interact</strong> with newer versions of the schema.</p><p>This means adding optional fields with defaults and avoiding breaking changes like removing fields.</p><h4>How Does Avro Enable Schema Evolution?</h4><p>Avro supports schema evolution through:</p><ul><li><strong>Default Values</strong>: New fields can be added with default values to avoid breaking older consumers.</li><li><strong>Nullable Fields</strong>: Union types like [&quot;null&quot;, &quot;string&quot;] make fields optional or nullable.</li><li><strong>Flexible Field Order</strong>: Field order changes are allowed as long as names are consistent.</li><li><strong>Field Removal</strong>: Fields can be removed if consumers don’t rely on them.</li></ul><h3>Example: Handling Changes Without Breaking Compatibility</h3><ol><li><strong>Initial Schema</strong>:</li></ol><pre>{<br>  &quot;type&quot;: &quot;record&quot;,<br>  &quot;name&quot;: &quot;Product&quot;,<br>  &quot;fields&quot;: [<br>    { &quot;name&quot;: &quot;id&quot;, &quot;type&quot;: &quot;int&quot; },<br>    { &quot;name&quot;: &quot;name&quot;, &quot;type&quot;: &quot;string&quot; },<br>    { &quot;name&quot;: &quot;price&quot;, &quot;type&quot;: &quot;double&quot; }<br>  ]<br>}</pre><p>This schema defines a Product with an id, name, and price.</p><p>Now, suppose we need to make some changes:</p><ol><li><strong>Add a new field</strong> (category) without breaking older consumers.</li><li><strong>Rename an existing field</strong> (price → productPrice) while maintaining compatibility.</li></ol><p>Here’s the updated schema:</p><pre>{<br>  &quot;type&quot;: &quot;record&quot;,<br>  &quot;name&quot;: &quot;Product&quot;,<br>  &quot;fields&quot;: [<br>    { &quot;name&quot;: &quot;id&quot;, &quot;type&quot;: &quot;int&quot; },<br>    { &quot;name&quot;: &quot;category&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;productPrice&quot;, &quot;type&quot;: &quot;double&quot;, &quot;aliases&quot;: [&quot;price&quot;] }<br>  ]<br>}</pre><h4>What Changed?</h4><p><strong>1. New Field Added::</strong><br>category was added with a default value of null.<br>This is <strong>backward-compatible</strong> because old consumers will ignore it, and new consumers can handle missing values.</p><p><strong>2. Renaming a Field with Aliases (Safe Renaming in Avro):</strong><br>The field price was renamed to productPrice.<br>Adding &quot;aliases&quot;: [&quot;price&quot;] ensures older consumers expecting price can still read the data.</p><p>Without aliases, older consumers would break since they wouldn’t recognize productPrice.<br>Avro maps old records using price to productPrice, making this a <strong>backward-compatible</strong> change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*pGZwwYqJYvTztXkj.png" /></figure><p>Now Let’s create a project to see how we can use Avro schema: <a href="https://github.com/minarashidi/authentication">https://github.com/minarashidi/authentication</a></p><h3>Project setup</h3><p><em>Start by creating a new Spring Boot project using Spring Initializr, selecting the following dependencies. Import the project into your preferred IDE.</em></p><p>Here in my pom.xml, added the avro dependency:</p><pre> &lt;dependency&gt;<br>      &lt;groupId&gt;org.apache.avro&lt;/groupId&gt;<br>      &lt;artifactId&gt;avro&lt;/artifactId&gt;<br>      &lt;version&gt;1.11.3&lt;/version&gt;<br> &lt;/dependency&gt;</pre><h4><strong>Difference Between Gradle and Maven with Avro</strong></h4><p>Whether you’re using Gradle or Maven, you’ll need a plugin to simplify and automate working with Apache Avro. Here’s why:</p><p><strong>Why You Need an Avro Plugin</strong></p><ol><li><strong>Code Generation</strong>:<br> The plugin automatically generates Java classes from Avro schemas (.avsc). These schemas define your data&#39;s structure, and the plugin ensures you get ready-to-use Java classes without manual effort.</li><li><strong>Seamless Build Integration</strong>:</li></ol><ul><li>Works effortlessly with the build process.</li><li>Detects .avsc files in predefined directories.</li><li>Includes generated sources automatically in the compilation process.</li></ul><p><strong>3. Simplifies Configuration</strong>:<br> The plugin manages dependencies on Avro tools and runtime libraries for you. No need to set up or configure these manually.</p><p>Without the plugin, you’d need to handle Avro code generation manually — running the compiler yourself, managing generated files, and integrating them into your build process. That’s extra work and introduces more chances for errors. The plugin saves time and reduces boilerplate.</p><h4>Gradle vs. Maven for Avro</h4><p>Both Gradle and Maven support Avro through plugins, but they differ slightly in approach:</p><p><strong>Maven</strong><br>Maven uses the Avro Maven Plugin. You add it to the build section of your pom.xml and configure goals like generate-sources manually.</p><p>Example Maven configuration:</p><pre>&lt;build&gt;<br>    &lt;plugins&gt;<br>      &lt;plugin&gt;<br>        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>      &lt;/plugin&gt;<br>      &lt;plugin&gt;<br>        &lt;groupId&gt;org.apache.avro&lt;/groupId&gt;<br>        &lt;artifactId&gt;avro-maven-plugin&lt;/artifactId&gt;<br>        &lt;version&gt;${avro.version}&lt;/version&gt;<br>        &lt;executions&gt;<br>          &lt;execution&gt;<br>            &lt;phase&gt;generate-sources&lt;/phase&gt;<br>            &lt;goals&gt;<br>              &lt;goal&gt;schema&lt;/goal&gt;<br>            &lt;/goals&gt;<br>            &lt;configuration&gt;<br>              &lt;sourceDirectory&gt;src/main/resources/avro&lt;/sourceDirectory&gt;<br>              &lt;outputDirectory&gt;${project.basedir}/src/main/java/com/mina/dev/kafka/avro&lt;/outputDirectory&gt;<br>            &lt;/configuration&gt;<br>          &lt;/execution&gt;<br>        &lt;/executions&gt;<br>      &lt;/plugin&gt;<br>    &lt;/plugins&gt;<br> &lt;/build&gt;</pre><p><strong>Gradle</strong><br>With Gradle, you can use the com.github.davidmc24.gradle.plugin.avro plugin, which integrates naturally into Gradle’s build lifecycle.</p><p>Example Gradle configuration:</p><pre>plugins {<br>    id &#39;java&#39;<br>    id &#39;com.github.davidmc24.gradle.plugin.avro&#39; version &#39;1.8.0&#39;<br>}<br><br>avro {<br>    // Customize configuration (optional)<br>    stringType = &quot;String&quot; // Use Java String instead of Utf8<br>    enableDecimalLogicalType = true<br>}<br><br>dependencies {<br>    implementation &#39;org.apache.avro:avro:1.11.2&#39;<br>}</pre><p>When you apply this plugin, it sets up the default source directory for Avro files (src/main/avro), automatically compiles Avro schemas, and adds the generated classes to the source directory.</p><h3>Create the Avro Schema</h3><p>Create the Avro schema at src/main/resources/avro/product.avsc:</p><pre>{<br>  &quot;type&quot;: &quot;record&quot;,<br>  &quot;name&quot;: &quot;ProductAvroDto&quot;,<br>  &quot;fields&quot;: [<br>    { &quot;name&quot;: &quot;id&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;name&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;price&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;double&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;currency&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;category&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;subCategory&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;createdAt&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null },<br>    { &quot;name&quot;: &quot;updatedAt&quot;, &quot;type&quot;: [&quot;null&quot;, &quot;string&quot;], &quot;default&quot;: null }<br>  ]<br>}</pre><h4>Convert Product Object to Avro DTO</h4><p>Next, we create a class that converts a Product object to an Avro DTO.</p><pre>package com.mina.dev.kafka.avro;<br><br>import com.mina.dev.kafka.domain.Product;<br>import java.time.LocalDateTime;<br>import java.time.format.DateTimeFormatter;<br>import lombok.experimental.UtilityClass;<br><br>@UtilityClass<br>public class AvroDataFactory {<br><br>  private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSSSS&#39;Z&#39;&quot;);<br><br>  public static ProductAvroDto createProductAvroDto(Product product) {<br><br>    return ProductAvroDto.newBuilder()<br>        .setId(String.valueOf(product.getId()))<br>        .setName(product.getName())<br>        .setPrice(product.getPrice())<br>        .setCurrency(product.getCurrency())<br>        .setCategory(product.getCategory())<br>        .setSubCategory(product.getSubCategory())<br>        .setCreatedAt(product.getCreatedAt() != null ? formatDateTime(product.getCreatedAt()) : null)<br>        .setUpdatedAt(product.getUpdatedAt() != null ? formatDateTime(product.getUpdatedAt()) : null)<br>        .build();<br>  }<br><br>  private static String formatDateTime(String dateTime) {<br>    // Assuming dateTime is in ISO-8601 format<br>    return DATE_TIME_FORMATTER.format(LocalDateTime.parse(dateTime));<br>  }<br>}</pre><h4>Kafka Producer</h4><p>Now, let’s create a Kafka producer that sends the Avro DTO.</p><pre>package com.mina.dev.kafka.avro;<br><br>import com.mina.dev.kafka.domain.Product;<br>import lombok.AllArgsConstructor;<br>import lombok.extern.slf4j.Slf4j;<br>import org.apache.avro.specific.SpecificRecordBase;<br>import org.apache.kafka.clients.producer.ProducerRecord;<br>import org.springframework.kafka.core.KafkaTemplate;<br><br><br>@AllArgsConstructor<br>@Slf4j<br>public class AvroProductEventProducer {<br><br>  private final KafkaTemplate&lt;String, Object&gt; kafkaTemplate;<br>  private final String productAvroTopic;<br><br>  /**<br>   * Converts a Product object to an Avro DTO and sends it to the Kafka topic asynchronously.<br>   *<br>   * @param product The product data to be sent.<br>   */<br>  public void sendProductEvent(Product product) {<br>    log.info(&quot;Preparing to send product to Kafka: {}&quot;, product);<br>    ProductAvroDto dto = AvroDataFactory.createProductAvroDto(product);<br>    sendAsync(productAvroTopic, dto, product.getId());<br>  }<br><br>  /**<br>   * Sends a record asynchronously to a Kafka topic.<br>   *<br>   * @param topic     The Kafka topic to send the message to.<br>   * @param data      The Avro data object.<br>   * @param productId The product ID, used as the key for the message.<br>   */<br>  private void sendAsync(String topic, SpecificRecordBase data, Long productId) {<br>    try {<br>      ProducerRecord&lt;String, Object&gt; producerRecord = new ProducerRecord&lt;&gt;(topic, null, String.valueOf(productId), data);<br>      kafkaTemplate.send(producerRecord).whenComplete((result, failure) -&gt; {<br>        if (failure == null) {<br>          log.info(&quot;Successfully sent product event for productId {}&quot;, productId);<br>        } else {<br>          log.error(&quot;Failed to send product event for productId {}&quot;, productId, failure);<br>        }<br>      });<br>    } catch (Exception e) {<br>      log.error(&quot;Unable to send product event for productId {}&quot;, productId, e);<br>    }<br>  }<br>}</pre><p>Also we needed to initialize avroProductEventProducer bean with the KafkaTemplate in our config file.</p><pre> @Bean<br>  public AvroProductEventProducer avroProductEventProducer(KafkaTemplate&lt;String, Object&gt; avroKafkaTemplate,<br>      KafkaProperties kafkaProperties) {<br>    return new AvroProductEventProducer(avroKafkaTemplate, kafkaProperties.producer().topic()<br>    );<br>  }<br><br>  @Bean<br>  KafkaTemplate&lt;String, Object&gt; avroKafkaTemplate(KafkaProperties kafkaProperties) {<br>    var props = new HashMap&lt;String, Object&gt;();<br>    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.bootstrapServers());<br>    props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true); // Ensure safe producer behavior<br>    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());<br>    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName()); // Use Avro Serializer<br><br>    var producerFactory = new DefaultKafkaProducerFactory&lt;&gt;(props);<br>    return new KafkaTemplate&lt;&gt;(producerFactory);<br>}</pre><h4>REST API for Testing</h4><p>Finally, create a REST API endpoint to test the producer:</p><pre>package com.mina.dev.kafka.controller;<br><br>import com.mina.dev.kafka.avro.AvroProductEventProducer;<br>import com.mina.dev.kafka.domain.Product;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.web.bind.annotation.PostMapping;<br>import org.springframework.web.bind.annotation.RequestBody;<br>import org.springframework.web.bind.annotation.RestController;<br><br>@RestController<br>public class EventController {<br><br>  @Autowired<br>  private AvroProductEventProducer producer;<br><br>  @PostMapping(&quot;/events&quot;)<br>  public String sendMessage(@RequestBody Product product) {<br>    producer.sendProductEvent(product);<br>    return &quot;event published!&quot;;<br>  }<br>}</pre><h4>Run the application:</h4><p>./mvnw spring-boot:run</p><h4><strong>Test with Postman:</strong></h4><ul><li>Set method to POST and URL to <a href="http://localhost:8080/events.">http://localhost:8080/events.</a></li><li>Add this JSON in the body:</li></ul><pre>{<br>   &quot;id&quot;:123,<br>   &quot;name&quot;:&quot;Sample Product&quot;,<br>   &quot;price&quot;:19.99,<br>   &quot;currency&quot;:&quot;USD&quot;,<br>   &quot;category&quot;:&quot;Electronics&quot;,<br>   &quot;subCategory&quot;:&quot;Mobile&quot;,<br>   &quot;createdAt&quot;:&quot;2025-01-21T12:00:00&quot;,<br>   &quot;updatedAt&quot;:&quot;2025-01-21T12:00:00&quot;<br>}</pre><p>The response should be: &quot;Event published!&quot;</p><h3>Role of a Schema Registry</h3><p>A <strong>Schema Registry</strong> is a central service that stores and manages Avro schemas. It ensures that producers and consumers are using compatible schemas and and tracks schema versions.</p><h4>Benefits of a Schema Registry:</h4><ol><li><strong>Validation</strong>: Ensures that producers and consumers use compatible schemas, so consumers can process messages even when the schema evolves.</li><li><strong>Versioning</strong>: Tracks different schema versions and their compatibility.</li><li><strong>Simplified Management</strong>: Centralizes schema storage and eliminates duplication.</li></ol><h3>How It Works:</h3><p>The <strong>producer</strong> serializes the data using Avro, registers the schema in the registry, and sends the schema ID with each message. The <strong>consumer</strong> retrieves the schema by ID from the registry and deserializes the message, ensuring compatibility.</p><h3>How It Works in Practice</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PqwpeRzZkIAL-ptDwnGKbg.png" /></figure><h4>Producer:</h4><ol><li>Serializes the product data using the Avro schema.</li><li>Registers the schema in the schema registry, which assigns a unique schema ID.</li><li>Sends the serialized data along with the schema ID to Kafka.</li></ol><h4>Consumer:</h4><ol><li>Reads the schema ID from the Kafka message.</li><li>Fetches the corresponding schema from the schema registry.</li><li>Deserializes the message using the schema, ensuring compatibility.</li></ol><h4>Some of Popular Schema Registry Options:</h4><ul><li><strong>AWS Glue Schema Registry</strong>: Integrates with AWS services like Kinesis and MSK.</li><li><strong>Confluent Schema Registry</strong>: Designed for Kafka; supports Avro, JSON, and Protobuf.</li><li><strong>Azure Schema Registry</strong>: Works with Azure Event Hubs and Kafka.</li></ul><h3>How to Upload Your Schema to AWS Glue Schema Registry</h3><h4>Step 1: Set Up Your AWS Glue Schema Registry</h4><ol><li>Go to the AWS Console and open the Glue Schema Registry: <a href="https://aws.amazon.com/glue/">https://aws.amazon.com/glue/</a></li></ol><p>2. Create a New Schema Registry:</p><ul><li>Click on the <strong>Create registry</strong> button.</li><li>Provide a name for the schema registry, for example, MyAvroRegistry.</li><li>Set the compatibility level to FULL or BACKWARD based on your needs.</li><li>Click <strong>Create</strong>.</li></ul><h4>Step 2: Upload Avro Schema to AWS Glue Schema Registry</h4><p>You can upload your schema to AWS Glue using the AWS Glue SDK or through the AWS Console.</p><h4>Option 1: Using AWS Console</h4><ol><li>Go to the <strong>Schema Registry</strong> section in AWS Glue.</li><li>Select the schema registry you just created.</li><li>Click on <strong>Add schema</strong> and select <strong>AVRO</strong> as the format.</li><li>Paste the Avro schema definition into the provided field (you can use the schema from your project).</li><li>Click <strong>Save</strong>.</li></ol><h4>Option 2: Using AWS SDK for Java</h4><p>Here’s a Java class to upload an Avro schema to AWS Glue Schema Registry:</p><pre>package com.mina.dev.kafka.avro;<br><br>import jakarta.annotation.PostConstruct;<br>import jakarta.annotation.PreDestroy;<br>import java.util.Map;<br>import lombok.AllArgsConstructor;<br>import lombok.extern.slf4j.Slf4j;<br>import software.amazon.awssdk.services.glue.GlueClient;<br>import software.amazon.awssdk.services.glue.model.CreateSchemaRequest;<br>import software.amazon.awssdk.services.glue.model.EntityNotFoundException;<br>import software.amazon.awssdk.services.glue.model.GetSchemaRequest;<br>import software.amazon.awssdk.services.glue.model.RegisterSchemaVersionRequest;<br>import software.amazon.awssdk.services.glue.model.RegistryId;<br>import software.amazon.awssdk.services.glue.model.SchemaId;<br>import software.amazon.awssdk.services.glue.model.SchemaVersionStatus;<br><br>@Slf4j<br>@AllArgsConstructor<br>public class GlueSchemaUploader {<br><br>    private final GlueClient client;<br>    private final String registryName;<br>    private final Map&lt;String, String&gt; schemas;<br><br>    @PreDestroy<br>    public void closeClient() {<br>        if (client != null) {<br>            try {<br>                client.close();<br>                log.info(&quot;Glue client closed successfully.&quot;);<br>            } catch (Exception e) {<br>                log.warn(&quot;Failed to close Glue client.&quot;, e);<br>            }<br>        }<br>    }<br><br>    @PostConstruct<br>    public void uploadSchemas() {<br>        schemas.forEach(this::uploadSchema);<br>    }<br><br>    private void uploadSchema(String schemaName, String schemaDefinition) {<br>        log.info(&quot;Uploading schema: {}&quot;, schemaName);<br><br>        Long version = getSchemaVersion(schemaName);<br>        if (version == null) {<br>            log.info(&quot;Schema {} not found. Creating a new schema.&quot;, schemaName);<br>            createSchema(schemaName, schemaDefinition);<br>        } else {<br>            log.info(&quot;Schema {} already exists with version {}. Checking for updates.&quot;, schemaName, version);<br>            uploadSchemaDefinition(schemaName, version, schemaDefinition);<br>        }<br>    }<br><br>    private void createSchema(String schemaName, String schemaDefinition) {<br>        var response = client.createSchema(CreateSchemaRequest.builder()<br>            .schemaName(schemaName)<br>            .dataFormat(&quot;AVRO&quot;)<br>            .compatibility(&quot;FULL&quot;)<br>            .schemaDefinition(schemaDefinition)<br>            .registryId(RegistryId.builder().registryName(registryName).build())<br>            .build()<br>        );<br><br>        log.info(&quot;Created schema: {} | Version: {} | Status: {}&quot;,<br>            schemaName, response.latestSchemaVersion(), response.schemaStatus());<br>    }<br><br>    private void uploadSchemaDefinition(String schemaName, Long existingVersion, String schemaDefinition) {<br>        var response = client.registerSchemaVersion(RegisterSchemaVersionRequest.builder()<br>            .schemaId(schemaId(schemaName))<br>            .schemaDefinition(schemaDefinition)<br>            .build()<br>        );<br><br>        if (response.status() == SchemaVersionStatus.FAILURE) {<br>            log.error(&quot;Failed to upload schema definition for schema: {}\nDefinition: {}\nAWS Response: {}&quot;,<br>                schemaName, schemaDefinition, response);<br>            return;<br>        }<br><br>        if (existingVersion.equals(response.versionNumber())) {<br>            log.info(&quot;No changes detected for schema: {}&quot;, schemaName);<br>        } else {<br>            log.info(&quot;Updated schema: {} | New Version: {} | Status: {}&quot;,<br>                schemaName, response.versionNumber(), response.status());<br>        }<br>    }<br><br>    private Long getSchemaVersion(String schemaName) {<br>        try {<br>            return client.getSchema(GetSchemaRequest.builder()<br>                    .schemaId(schemaId(schemaName))<br>                    .build())<br>                .latestSchemaVersion();<br>        } catch (EntityNotFoundException e) {<br>            log.info(&quot;Schema {} not found in registry {}.&quot;, schemaName, registryName);<br>            return null;<br>        }<br>    }<br><br>    private SchemaId schemaId(String schemaName) {<br>        return SchemaId.builder()<br>            .registryName(registryName)<br>            .schemaName(schemaName)<br>            .build();<br>    }<br>}</pre><h4>Step 3: Configure Kafka Producer with AWS Glue Schema Registry</h4><p>To send Avro messages with your schema, you need to configure your Kafka producer to use the <strong>AWS Glue Schema Registry</strong> serializer.</p><p>This ensures that the producer serializes messages using the schema stored in the registry.</p><p>Add the following configuration to your config class:</p><pre>  @Bean<br>  public AvroProductEventProducer avroProductEventProducer(KafkaTemplate&lt;String, Object&gt; avroKafkaTemplate,<br>      KafkaProperties kafkaProperties) {<br>    return new AvroProductEventProducer(avroKafkaTemplate, kafkaProperties.producer().topic()<br>    );<br>  }<br><br>  @Bean<br>  KafkaTemplate&lt;String, Object&gt; avroKafkaTemplate(KafkaProperties kafkaProperties, GlueProperties glueProperties) {<br>    var props = new HashMap&lt;String, Object&gt;();<br>    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.bootstrapServers());<br>    props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, false);<br>    props.put(AWSSchemaRegistryConstants.AWS_REGION, glueProperties.region());<br>    props.put(AWSSchemaRegistryConstants.DATA_FORMAT, &quot;AVRO&quot;);<br>    props.put(AWSSchemaRegistryConstants.SCHEMA_AUTO_REGISTRATION_SETTING, &quot;false&quot;);<br>    props.put(AWSSchemaRegistryConstants.REGISTRY_NAME, glueProperties.registryName());<br><br>    var producerFactory = new DefaultKafkaProducerFactory&lt;&gt;(props, StringSerializer::new, GlueSchemaRegistryKafkaSerializer::new);<br>    return new KafkaTemplate&lt;&gt;(producerFactory);<br>  }<br><br>  @Bean<br>  public GlueSchemaUploader schemaUploader(KafkaProperties kafkaProperties, GlueProperties glueProperties) {<br>    // Validate GlueProperties<br>    if (glueProperties.region() == null || glueProperties.registryName() == null) {<br>      throw new IllegalArgumentException(&quot;Glue region and registry name must be configured.&quot;);<br>    }<br><br>    var avroProductEvents = kafkaProperties.producer();<br>    log.info(&quot;Initializing GlueSchemaUploader for topic: {}&quot;, avroProductEvents.topic());<br><br>    // Create the GlueClient with proper region and credentials<br>    var glueClient = GlueClient.builder()<br>        .region(Region.of(glueProperties.region()))<br>        .credentialsProvider(WebIdentityTokenFileCredentialsProvider.create())<br>        .httpClient(UrlConnectionHttpClient.create()) // Default HTTP client<br>        .build();<br><br>    // Map topic names to Avro schema definitions<br>    var schemaMap = Map.of(<br>        avroProductEvents.topic(), ProductAvroDto.getClassSchema().toString()<br>    );<br><br>    log.info(&quot;Schema map initialized: {}&quot;, schemaMap);<br>    return new GlueSchemaUploader(glueClient, glueProperties.registryName(), schemaMap);<br>  }</pre><h4>Step 4: Sending Avro Messages to Kafka</h4><p>Once the schema is uploaded to AWS Glue and the producer is configured, you can start sending Avro-encoded messages to Kafka. The GlueSchemaRegistryKafkaSerializer will ensure that the message is serialized according to the schema you uploaded to the Glue Schema Registry.</p><p>In this guide, we learned about Avro and how to upload and manage Avro schemas using AWS Glue Schema Registry, and how to integrate them into your Kafka application.<br>For full code examples, check out the GitHub repository: <a href="https://github.com/minarashidi/spring-kafka-avro-demo.git">https://github.com/minarashidi/spring-kafka-avro-demo.git</a>.</p><p>I hope you found it helpful! Feel free to share your thoughts or ask questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7645b5b0610" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/mastering-kafka-with-avro-schemas-c7645b5b0610">Mastering Kafka with Avro Schemas</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kafka: Key Concepts and How It Works (Part 1)]]></title>
            <link>https://levelup.gitconnected.com/mastering-kafka-key-concepts-and-how-it-works-part-1-e38b0f7630d2?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/e38b0f7630d2</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Fri, 20 Dec 2024 21:28:42 GMT</pubDate>
            <atom:updated>2024-12-27T01:59:56.846Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/301/0*mfTj3QIZNPEA8b1T" /></figure><p>Imagine building a system where data flows seamlessly between different services in real-time, such as tracking orders in an online store or analyzing user activity on a website. You want it to be fast, reliable, scalable — and event-driven. That’s where Apache Kafka comes in.</p><p>Kafka is more than just a message broker — it’s a distributed event streaming platform that empowers <strong>event-driven architectures</strong> by handling massive amounts of real-time data and enabling loosely coupled systems. Beyond simply sending and receiving events, mastering Kafka means understanding core concepts like <strong>partitions</strong>, <strong>offsets</strong>, and <strong>consumer groups</strong>, as well as managing schema changes without any downtime.</p><p>In this guide, we’ll break it all down:</p><ul><li>How Kafka works and its core concepts.</li><li>The schema evolution for smooth updates with a deep dive into <strong>Avro schemas</strong>, including how to upload them to tools like <strong>AWS Glue</strong>. (will be covered in part 2)</li><li>A complete code example to get you started, with everything you need to run and test the application using Docker( follow the README on GitHub): <a href="https://github.com/minarashidi/spring-kafka-avro-demo.git">https://github.com/minarashidi/spring-kafka-avro-demo.git</a></li></ul><h3>Let’s Get Started with Kafka Architecture</h3><h4>What is Apache Kafka?</h4><p>Apache Kafka is an open-source platform that handles large streams of real-time data. It helps systems communicate by publishing and consuming events at scale, making it ideal for building data pipelines and real-time analytics. Kafka is designed to be highly reliable, scalable, and fast.</p><h4>What is an Event in Kafka?</h4><p>An <strong>event</strong> in Kafka is a message that represents something that happened in your system, like a user clicking a button or an order being placed. An event has two parts:</p><ul><li><strong>Notification</strong>: A timestamp or signal that tells you when the event occurred.</li><li><strong>State</strong>: The actual data about what happened, usually in formats like <strong>JSON</strong> or <strong>Avro</strong>. For example, a status update for an order.</li></ul><p>In short, an event is a record of what happened, along with the details, allowing systems to respond to real-time data.</p><h4>What is the Key in Kafka?</h4><p>Each Kafka event can have a <strong>key</strong>. This key helps Kafka decide how to store events in partitions, ensuring related events (e.g., all orders from the same customer) stay grouped together and maintain the correct order within a partition.</p><h4>Kafka is Not a Queue</h4><p>Unlike traditional message queues, Kafka isn’t just about sending messages from one point to another. Kafka <strong>stores</strong> and <strong>processes</strong> streams of events over time, allowing systems to replay or reprocess events whenever needed something simple queues can’t do.</p><h4>Why is Kafka So Fast?</h4><p>Kafka’s speed comes from a few key features:</p><ul><li><strong>Sequential Writes</strong>: Kafka writes data sequentially on disk, reducing disk seek times.</li><li><strong>Zero-Copy Technology</strong>: Kafka transfers data directly from disk to the network adapter, skipping memory for efficiency.</li><li><strong>Partitioning for Parallelism</strong>: Kafka partitions data, enabling distributed and parallel processing, which improves performance.</li></ul><h3>What Are Topics in Kafka?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4dPlFY67RpGviIWj.png" /></figure><p>In Kafka, a <strong>topic</strong> is like a channel or category where events are published and stored. It helps organize the flow of data, allowing producers to publish events and consumers to consume them. Think of topics as a way to organize different data streams.</p><p>Here’s how it works:</p><ul><li><strong>Producers</strong> publish events to specific topics.</li><li><strong>Consumers</strong> consume events from those topics and process the data.</li></ul><p>Events can be anything — transaction records, log entries, or event notifications.</p><p>For example, in an e-commerce platform:</p><ul><li>A topic called <strong>order-events</strong> might store events related to customer orders, like order placements, status updates, or cancellations.</li><li>A separate topic, like <strong>user-notifications</strong>, could be used to publish events to customers about the progress of their orders.</li></ul><p>By organizing data into topics, Kafka ensures that data is well-structured. Different systems or services can consume only the data they need, improving scalability, real-time processing, and making data management simpler.</p><h3>Understanding Partitions in Kafka</h3><p>Kafka uses <strong>partitions</strong> to break up a topic’s data into smaller, manageable pieces. These partitions are spread across multiple servers (called brokers) in a Kafka cluster. This design is crucial for Kafka’s ability to handle large amounts of data efficiently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4mgdeuK_y1xiP36C.png" /></figure><h4>Why Partitions Matter</h4><ol><li><strong>Scalability</strong>: By splitting a topic into partitions, Kafka can distribute the data across multiple brokers. This allows multiple consumers to process different partitions at the same time, improving performance and scalability.</li><li><strong>Fault Tolerance</strong>: Partitions can be replicated across different brokers. If one broker fails, a replica ensures no data is lost, and Kafka continues to run smoothly. Each partition has a <strong>leader broker</strong> responsible for handling reads and writes, while other brokers maintain replicas. If the leader fails, a replica automatically takes over.</li></ol><h4><strong>The Role of the Event Key</strong></h4><p>When publishing events to Kafka, an event key determines the partition where the event is stored. Using the same key for related events ensures they are sent to the same partition, maintaining their order within that partition.</p><h4><strong>How Event Keys Help with Ordering</strong></h4><p>Kafka guarantees the order of events only within a <strong>single</strong> partition. By using a consistent event key, such as a user ID for orders, related events are grouped in the same partition. This ensures they are processed in the correct sequence, as Kafka guarantees strict order within partitions.</p><p>Using event keys not only keeps related events ordered but also distributes data efficiently, enabling Kafka to handle large-scale data streams with high performance.</p><h3>What Are Producers?</h3><p>Producers are systems or applications that publish events to Kafka topics.</p><h3>What Are Consumers and Consumer Groups?</h3><p>In Kafka, <strong>consumers</strong> are systems or applications that consume events from topics. Once they receive the data, they can process it, store it in a database, or trigger actions in other systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CHHnD56vxS2MoTis" /></figure><h4>Here’s how it works:</h4><ul><li>When a producer publishes an event to a topic, it gets delivered to any consumer that is subscribed to that topic.</li><li>Consumers can consume events from any point (offset) in a partition. Normally, consumers consume events in order (from the earliest to the latest), but they can start from any offset and even consume events out of order if needed.</li><li>After processing events, a consumer typically <strong>commits</strong> its offset (records where it left off) so it can resume reading from that point if it restarts later.</li></ul><blockquote><strong>Each consumer belongs to a consumer group</strong>.</blockquote><p>A consumer group is a collection of consumers working together to process events. A consumer group helps with:</p><ul><li><strong>Fault tolerance</strong>: If one consumer fails, others in the group can take over.</li><li><strong>Scalability</strong>: If you have more consumers than partitions, the extra consumers will be <strong>idle</strong> (not doing any work). If you have fewer consumers than partitions, some consumers will take on multiple partitions.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vMV0yJLd86X-k73d" /></figure><h4>Kafka Consumer Offsets</h4><p>In Kafka, <strong>Consumer Offsets</strong> are markers that track the position of a consumer in a particular partition. Each event within a partition is assigned a unique number called the <strong>offset</strong>. Consumers use this offset to know which event they have already processed and where to continue from.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/0*cfQuZPqhXx7LU0E2.png" /></figure><h4><strong>Key Concepts:</strong></h4><ul><li><strong>Offsets are unique to a partition</strong>:<br>For example, an offset of 3 in Partition 0 points to a specific event, but the same offset (3) in Partition 1 points to a different event. Offsets are local to each partition, meaning they don’t represent the same data across partitions.</li><li><strong>Order is guaranteed only within a partition</strong>:<br>Kafka guarantees that the order of events will be maintained within a single partition. So, the first event will always have a lower offset than the second event, and so on. But across partitions, there is no ordering guarantee.</li><li><strong>Data retention</strong>:<br>Kafka keeps data in partitions for a limited time (the default is 7 days). After this time, the data is deleted, and consumers can no longer read it.</li><li><strong>Immutability</strong>:<br>Once data is written to a partition in Kafka, it cannot be changed. It is immutable.</li><li><strong>How producers determines Partitions Without a Key</strong>:<br>If a key is not provided, producers determines which partition to publish events to using round robin algorithm.</li></ul><h4>Understanding Kafka Event Consuming and Offset Management</h4><p>In this section, we’ll walk through configuring and implementing a Kafka consumer that consumes Product events from a Kafka topic. The consumer will manage event offsets manually, ensuring reliable processing and acknowledgment.</p><h4>Consumer Configuration</h4><p>To customize Kafka listener behavior, we define a ConcurrentKafkaListenerContainerFactory bean. This allows us to set properties such as deserializers, offset management, and error handling.</p><pre>  @Bean<br>  ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; kafkaListenerContainerFactory(KafkaProperties kafkaProperties) {<br>    var productEventsConsumer = kafkaProperties.consumer();<br>    return createKafkaConsumerFactory(kafkaProperties, productEventsConsumer.groupId(), productEventsConsumer.autoOffsetReset());<br>  }<br><br>  private ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; createKafkaConsumerFactory(KafkaProperties kafkaProperties,<br>      String groupId, String autoOffsetReset) {<br><br>    var props = new HashMap&lt;String, Object&gt;();<br>    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.bootstrapServers());<br>    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);<br>    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);<br>    props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);<br>    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);<br>    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);<br>    props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, List.of(CooperativeStickyAssignor.class));<br>//    configureSasl(kafkaProperties, props);<br><br>    var factory = new ConcurrentKafkaListenerContainerFactory&lt;String, String&gt;();<br>    factory.setConsumerFactory(new DefaultKafkaConsumerFactory&lt;&gt;(props, StringDeserializer::new, StringDeserializer::new));<br>    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);<br>    return factory;<br>  }</pre><p>By setting ENABLE_AUTO_COMMIT_CONFIG to false, we disable automatic offset commits. Instead, the acknowledgment is explicitly handled using the Acknowledgment interface. This ensures:</p><ul><li><strong>Reliability</strong>: Offsets are committed only after successful processing.</li><li><strong>Avoidance of Data Loss</strong>: Prevents Kafka from marking unprocessed events as consumed.</li></ul><h4>Consumer <strong>Implementation</strong></h4><p>The ProductConsumer class listens to Kafka topics and processes incoming events. It deserializes the payload, maps it to an entity, and inserts the data into the database if it doesn’t already exist.</p><pre>@Slf4j<br>@AllArgsConstructor<br>@Component<br>public class ProductConsumer {<br><br>  private final ObjectMapper objectMapper;<br>  private final ProductService productService;<br><br>  @KafkaListener(<br>      containerFactory = &quot;kafkaListenerContainerFactory&quot;,<br>      topics = &quot;${kafka.consumer.topic}&quot;,<br>      concurrency = &quot;${kafka.consumer.threads}&quot;,<br>      clientIdPrefix = &quot;product-${random.uuid}&quot;,<br>      autoStartup = &quot;${kafka.consumer.autostart}&quot;<br>  )<br>  public void onMessage(@Payload String payload, Acknowledgment acknowledgment) {<br>    try {<br>      // Deserialize the payload into ProductDto<br>      var productDto = objectMapper.readValue(payload, ProductDto.class);<br>      log.debug(&quot;Product event received: {}&quot;, productDto);<br><br>      // Map ProductDto to Product entity<br>      var product = mapToEntity(productDto);<br><br>      // Insert into the database or process further<br>      productService.insertIfNotExisting(product);<br>      log.info(&quot;Product event processed and inserted: {}&quot;, product);<br><br>      // Acknowledge the message after successful processing<br>      acknowledgment.acknowledge();<br>    } catch (Exception e) {<br>      log.warn(&quot;Unable to process product event: &quot; + payload, e);<br>    }<br>  }<br><br>  private Product mapToEntity(ProductDto productDto) {<br>    return Product.builder()<br>        .name(productDto.name())<br>        .price(productDto.price() != null ? productDto.price().doubleValue() : null)<br>        .currency(productDto.currency() != null ? productDto.currency().toString() : null)<br>        .category(productDto.category())<br>        .subCategory(productDto.subCategory())<br>        .createdAt(productDto.createdAt())<br>        .updatedAt(productDto.updatedAt())<br>        .build();<br>  }<br>}</pre><h4><strong>Complete Flow</strong></h4><ol><li><strong>Receive Events:</strong> Events are consumed from the Kafka topic.</li><li><strong>Deserialize</strong>: The payload is deserialized into a ProductDto using Jackson.</li><li><strong>Map</strong>: Convert the ProductDto to a Product entity for database operations.</li><li><strong>Insert</strong>: Save the data if it doesn’t already exist.</li><li><strong>Acknowledge</strong>: Commit the offset only after the event is successfully processed.</li></ol><p><strong>Why Manual Offset Management? </strong>Manual offset management gives you finer control over the acknowledgment process, ensuring that no event is marked as processed until it has been successfully handled. This can be useful in scenarios where event processing involves multiple steps or external dependencies.</p><h3>Understanding Offset Reset Behavior</h3><p>This configuration controls what happens when the Kafka consumer starts consuming from a partition and has no valid offset to begin reading from. Add this property in your Kafka consumer configuration:</p><pre>props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, &quot;earliest&quot;);</pre><p>This AUTO_OFFSET_RESET_CONFIG setting defines how the consumer behaves when:</p><ul><li><strong>No initial offset is found</strong> (e.g., this is the first time the consumer is running for this topic-partition).</li><li><strong>Offsets are invalid</strong> (e.g., they were deleted due to Kafka’s retention policy).</li></ul><h4>Possible Values</h4><ul><li>&quot;earliest&quot;: The consumer will start reading from the <strong>oldest available event</strong> in the partition if no offset is found.</li><li>&quot;latest&quot;: The consumer will start reading from the <strong>most recent event</strong> if no offset is found.</li><li>&quot;none&quot;: The consumer will throw an error if no offset is found for a topic partition.</li></ul><h3>Understanding Partition Assignment Strategy</h3><p>This configuration helps for minimizing disruptions during rebalancing:</p><p>props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, List.of(CooperativeStickyAssignor.class));</p><h4><strong><em>What is Kafka rebalancing and How Kafka Handles it in Consumer Groups?</em></strong></h4><p>Kafka rebalancing occurs when the distribution of partitions among consumers in a consumer group changes such as when:</p><ul><li>A new consumer joins.</li><li>A consumer leaves.</li><li>The number of partitions changes.</li></ul><h4>Why Rebalancing is Important</h4><p>Rebalancing ensures <strong>balanced workloads</strong> across consumers. Without it, some consumers may become overloaded while others stay idle, leading to inefficiencies.</p><h4>How Rebalancing Works</h4><ol><li><strong>Consumer Group</strong>: Kafka assigns partitions to consumers in the group.</li><li><strong>Rebalance Trigger</strong>: Rebalancing is triggered when a consumer joins or leaves, or when partitions are added, and Kafka redistributes the partitions.</li><li><strong>Rebalancing Process</strong>: During rebalancing, consumers temporarily stop consuming events to handle new partition assignments.</li></ol><p>For example, if there are 3 consumers (C1, C2, C3) and 6 partitions (P1 to P6), each consumer initially reads from 2 partitions. If a new consumer (C4) joins, Kafka redistributes the partitions so that each consumer gets a fair share.</p><h4>Partition Assignment Strategy</h4><p>By default, Kafka uses the <strong>RangeAssignor</strong> strategy for partition assignment. This strategy does not support <strong>cooperative rebalancing</strong>, meaning that when a consumer joins, leaves, or restarts, Kafka performs a <strong>full rebalance</strong>. This causes a temporary pause in event processing while partitions are reassigned.</p><h4>Recommended Strategy: CooperativeStickyAssignor</h4><p>To reduce disruptions during consumer restarts or deployments, consider using the <strong>CooperativeStickyAssignor</strong> strategy. This strategy allows for <strong>cooperative rebalancing</strong>, where only the necessary partitions are reassigned incrementally, reducing the impact of rebalancing and keeping event processing smoother.</p><h3><strong>How Does Kafka Producer Handle Idempotency?</strong></h3><p>When idempotency is enabled in Producer, Kafka ensures that even if the same event is published multiple times (e.g., due to retries or network issues), only one event is written to the topic. This is done by Kafka using the <strong>Producer ID</strong> and <strong>sequence numbers</strong> to uniquely identify and deduplicate events.</p><p>Idempotency is managed automatically by Kafka, but it must be enabled in the producer configuration. To do this, you need to:</p><ul><li>Set acks to all (to ensure full acknowledgment from all replicas).</li><li>Set enable.idempotence to true (to enable idempotency).</li></ul><pre>Properties props = new Properties();<br>props.put(&quot;bootstrap.servers&quot;, &quot;localhost:9092&quot;);<br>props.put(&quot;key.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);<br>props.put(&quot;value.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);<br>props.put(&quot;acks&quot;, &quot;all&quot;); // Ensure full acknowledgment<br>props.put(&quot;enable.idempotence&quot;, &quot;true&quot;); // Enable idempotency</pre><p>Idempotency in Kafka is <strong>mainly a producer feature</strong>, ensuring that a event is written only once, even if published multiple times due to retries. However, <strong>consumers can’t handle idempotency directly</strong>, but you can manage duplicate processing by using these strategies:</p><ol><li><strong>Track Processed Events</strong>:<br>Consumers can track which events they’ve already processed by storing unique identifiers (like eventId) in a database or cache (e.g., Redis). Before processing an event, check if it’s been processed. If so, skip it.</li><li><strong>Idempotent Processing</strong>:<br>You can make your consumer logic <strong>idempotent</strong>. For example, if saving to a database, use upserts (update or insert) to avoid duplicates.</li></ol><p>In Summary, Kafka handles idempotency for producers, ensuring only one write per event. Consumers must manage their own logic to handle potential event re-processing (duplicates).</p><h4>Some Use Cases of Apache Kafka</h4><p>Apache Kafka is a powerful tool for handling real-time data streams. Here are some key ways it’s used:</p><ol><li><strong>Event Streaming</strong><br>Kafka is perfect for processing real-time events, like tracking user activity (clicks, purchases) or collecting data from IoT sensors. It allows systems to react instantly, making it ideal for event-driven applications.</li><li><strong>Log Aggregation</strong><br>Kafka collects logs from various services, making it easier to monitor and troubleshoot distributed apps and Microservices. It stores logs efficiently and helps analyze them in real time.</li><li><strong>Data Ingestion</strong><br>Kafka ingests large volumes of data from sources like databases or sensors and sends it to storage or analytics platforms. It helps build fast, reliable data pipelines.</li><li><strong>Real-Time Analytics</strong><br>Kafka enables businesses to process data as it arrives. For example, it can detect fraud in financial transactions or analyze customer behavior live, ensuring quick, data-driven decisions.</li><li><strong>Microservice Communication</strong><br>Kafka facilitates asynchronous communication between Microservices, allowing them to work independently. It helps manage high traffic, handle failures, and ensures scalability and resilience.</li></ol><p>In this article, we’ve explored the key concepts of Kafka and its architecture.</p><blockquote>In the next article, we’ll dive into Avro schemas with Kafka, schema registries, and their benefits, including how they help establish data contracts between Microservices and why these contracts matter.</blockquote><p>For full code examples, check out the GitHub repository: <a href="https://github.com/minarashidi/spring-kafka-avro-demo.git">https://github.com/minarashidi/spring-kafka-avro-demo.git</a></p><p>Here are some useful links to explore:</p><ul><li><a href="https://kafka.apache.org/quickstart">Apache Kafka</a></li><li><a href="https://www.youtube.com/@Confluent">Confluent</a></li></ul><p><a href="https://conduktor.io/"><strong>Conduktor</strong></a><strong>: </strong>a user interface for Apache Kafka:</p><p><a href="https://conduktor.io/">Conduktor - Scale Data Streaming with Security and Control</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e38b0f7630d2" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/mastering-kafka-key-concepts-and-how-it-works-part-1-e38b0f7630d2">Mastering Kafka: Key Concepts and How It Works (Part 1)</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Run and Use Open Source Models on Your Computer]]></title>
            <link>https://levelup.gitconnected.com/how-to-run-and-use-open-source-models-on-your-computer-b1e040b6922a?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1e040b6922a</guid>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[llama-3]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Mon, 26 Aug 2024 21:23:50 GMT</pubDate>
            <atom:updated>2024-09-04T09:41:42.259Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Run and Use Open Source Models Locally</h3><h4>Spring AI — Run Meta’s Llama 3.1 Locally with Ollama 🦙 | Hands-on Guide</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AcsrNhm6nV0kmAeF.jpg" /></figure><p>In this article, I’ll show you how to get started with Ollama and run open-source Large Language Models (LLMs) locally. We’ll use Meta’s latest model, Llama 3.1. Finally, I’ll demonstrate how to integrate this model into your Spring application using Spring AI.</p><p>While Python is commonly seen as the go-to language for AI and machine learning, Java offers robust alternatives through tools like Spring AI. Using Spring AI, you can work with large language models (LLMs) locally in Java.</p><h3>What is Ollama</h3><p>Ollama is an open-source platform that allows you to run large language models (LLMs) directly on your computer, ensuring privacy and data security. It supports various models, including Llama 3, Mistral, and Gemma, and works on Linux, macOS, and Windows. With Ollama, you can perform tasks like text generation, language translation, and creative content creation without needing a server.</p><p>The famous models that Ollama support:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*exCk1icaLDoLTfVJZtiVcQ.png" /></figure><p><strong>Benefits of Using Ollama and LLaMA 3.1:</strong></p><ul><li><strong>Local Installation</strong>: Download and run the model locally on your computer.</li><li><strong>Privacy and Security</strong>: No need to worry about privacy and data security — your data stays on your machine.</li><li><strong>Simplicity</strong>: Use Ollama to easily run the model directly from your terminal.</li><li><strong>Cost:</strong> It’s open-source, so you can use it without any licensing fees. This means you can use it freely without the associated costs.</li></ul><h4>How to install and run Ollama and Llama 3.1 locally</h4><p>Keep in mind that performance will depend on your computer’s power. Unlike OpenAI’s models, which run on their powerful servers, running LLMs locally with Ollama might be a bit slower. You’ll also need to install and launch the app on your machine.</p><p><strong>Step1: Download Ollama</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hcKzXY8SOTjKdAr1OisRdw.png" /></figure><p>And then go ahead and install it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-fMsAomXRkK9zC9x-PxCNQ.png" /></figure><p>You can use your Mac terminal to run any model from <a href="https://ollama.com/library">Ollama’s library</a>. Here, I’ll be using llama3.1.</p><h4>Step2: Run Llama3</h4><p>In your terminal, run ollama run llama3.1. For more details, check out the information <a href="https://ollama.com/library/llama3.1">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3ITqLxjNbKO7ccV7ovmYbA.png" /></figure><p>Once it’s running, you can run any prompt without needing an internet connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AbM-FJrQJIuHyYCU2v6cpg.png" /></figure><p>As you see, your data stays on your own machine, which means better privacy. But since it’s running locally, it might be slower because of your system’s hardware limits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/0*-0K_UcE5yw66Cq-d.png" /></figure><p>Now let’s create a Spring Boot application that interacts with the Ollama platform and exposes a REST API for adding prompts, you can follow these steps:</p><h4>1. Set Up the Spring Boot Project</h4><p>Start by creating a new Spring Boot project using Spring Initializer: <a href="https://start.spring.io/">start.spring.initializer</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NVG6kGWhW8T60dDw-BcE8g.png" /></figure><p>Choose the latest <strong>Spring Boot Version </strong>stable version (as of this writing, 3.3.3)</p><h4><strong>Add dependencies</strong>:</h4><ul><li><strong>Spring Web</strong>: To create RESTful web services.</li><li><strong>Ollama: </strong>To run various LLMs locally.</li></ul><h4>Generate the Project</h4><p>Once you’ve added the dependencies, click Generate to download the project zip file. Extract it and open it in your favorite IDE (e.g., IntelliJ IDEA, etc.).</p><h4>In pom.xml</h4><p>We also need to change spring-ai version:</p><pre>&lt;!-- change M1 to SNAPSHOT --&gt;<br>&lt;spring-ai.version&gt;1.0.0-SNAPSHOT&lt;/spring-ai.version&gt;<br>&lt;!-- Add this into repositories --&gt;<br>&lt;repository&gt;<br>   &lt;id&gt;spring-snapshots&lt;/id&gt;<br>   &lt;name&gt;Spring Snapshots&lt;/name&gt;<br>   &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;<br>   &lt;releases&gt;<br>     &lt;enabled&gt;false&lt;/enabled&gt;<br>   &lt;/releases&gt;<br>&lt;/repository&gt;</pre><h4>Running Ollama and Setting Up the Llama Model</h4><p>Now that you’ve set up the pom.xml, let’s run Ollama on your local machine and set up the llama model. Earlier, I explained how to do this without Docker, but now let’s use Docker which makes it much easier.</p><p><strong>Follow these steps to set up Ollama with Docker:</strong></p><ol><li><strong>Find the Docker Image</strong>: Visit the <a href="https://github.com/ollama/ollama?tab=readme-ov-file#docker">Ollama Docker setup guide</a> for details.</li><li><strong>Create a </strong><strong>docker-compose.yml File</strong>: Use the following configuration:</li></ol><pre>version: &#39;3.8&#39;<br>services:<br>  ollama:<br>    hostname: ollama<br>    container_name: ollama<br>    image: ollama/ollama<br>    ports:<br>    - &quot;11434:11434&quot;</pre><p>To run Ollama using Docker Compose, execute the following command:</p><pre>docker-compose -f docker-compose.yml up</pre><p>This will start the Ollama container.</p><p><strong>Verify the Image: </strong>You can check if the Ollama image is available by listing your Docker images:</p><pre>➜  ollama-demo docker images                          <br>REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE<br>ollama/ollama               latest    e3c06ee96fcf   7 days ago     584MB</pre><p>Now we have started our Ollama platform, now we need to run the model llama3.1 inside the platform/container.</p><p><strong>Run the Llama3 Model</strong>:<br>Once Ollama is running, execute the following command to start the llama model inside the container:</p><pre>docker exec -it ollama ollama run llama3</pre><p>This command initiates the llama3 model within the Ollama platform.</p><p>With the model running, you can now send prompts and receive responses through your API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p1PiYbGnIO0K79U5rBwUSw.png" /></figure><p>To configure the connection to the Ollama platform, use the following properties in yourapplication.yml file:</p><pre>spring:<br>  ai:<br>    ollama:<br>      base-url: http://localhost:11434/ # URL where Ollama is up and running<br>      chat:<br>        model: llama3 # The model to be used<br>  application:<br>    name: ollama-demo<br><br>server:<br>  port: 9191</pre><h4>Create a Controller to Expose the REST API</h4><p>Now, implement a simple REST API to interact with Ollama.</p><pre>package dev.mina.ollama.controller;<br><br>import dev.mina.ollama.service.LlamaAiService;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RequestParam;<br>import org.springframework.web.bind.annotation.RestController;<br><br><br>@RestController<br>public class AIController {<br><br>  private final LlamaAiService aiService;<br><br>  public AIController(LlamaAiService aiService) {<br>    this.aiService = aiService;<br>  }<br><br>  @GetMapping(&quot;/api/v1/generate&quot;)<br>  public String generate(@RequestParam(value = &quot;prompt&quot;) String prompt) {<br>    return aiService.generateResult(prompt);<br>  }<br>}</pre><h4>Create a service to interact with the Llama model</h4><p>This service focuses on sending the prompt to the Llama model and returning the response.</p><pre>package dev.mina.ollama.service;<br><br>import org.springframework.ai.chat.model.ChatResponse;<br>import org.springframework.ai.chat.prompt.Prompt;<br>import org.springframework.ai.ollama.OllamaChatModel;<br>import org.springframework.ai.ollama.api.OllamaModel;<br>import org.springframework.ai.ollama.api.OllamaOptions;<br>import org.springframework.stereotype.Service;<br><br><br>@Service<br>public class LlamaAiService {<br><br>  private final OllamaChatModel chatModel;<br><br>  public LlamaAiService(OllamaChatModel chatModel) {<br>    this.chatModel = chatModel;<br>  }<br><br>  public String generateResult(String prompt) {<br>    ChatResponse response = chatModel.call(<br>        new Prompt(<br>            prompt,<br>            OllamaOptions.builder()<br>                .withModel(OllamaModel.LLAMA3)<br>                .build()<br>        ));<br>    return response.getResult().getOutput().getContent();<br>  }<br><br>}</pre><h4>Run the Application</h4><p>You can now make a POST request to <a href="http://localhost:9191/api/v1/generate?prompt=your-desired-prompt,">http://localhost:9191/api/v1/generate?prompt=your-desired-prompt</a> , replacing your-desired-prompt with the actual prompt you want to send.</p><p>To Test the Endpoint, you can use a tool like Postman to send the POST request.</p><p>The application will forward this prompt to the Ollama platform, and the response from the Ollama platform will be returned to the client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ylks3pOLw9A9xZoiRAK4Bg.png" /></figure><p><strong>You can find the source code here: </strong><a href="https://github.com/minarashidi/ollama-demo">https://github.com/minarashidi/ollama-demo</a></p><p>I hope this post helped you to gain better understanding of Ollama and how to communicate with LLMs within Java and Spring .</p><p>Feel free to share your thoughts and feedbacks.</p><p>Thank you for reading!</p><p><a href="https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat.html">Ollama Chat</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1e040b6922a" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/how-to-run-and-use-open-source-models-on-your-computer-b1e040b6922a">How to Run and Use Open Source Models on Your Computer</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Essential Startup Terms and Phrases]]></title>
            <link>https://medium.com/@minadev/essential-startup-terms-and-phrases-0e0d2fc5984d?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/0e0d2fc5984d</guid>
            <category><![CDATA[entrepreneurship]]></category>
            <category><![CDATA[entrepreneur]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[business]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Wed, 07 Aug 2024 07:04:01 GMT</pubDate>
            <atom:updated>2024-08-07T07:06:33.232Z</atom:updated>
            <content:encoded><![CDATA[<p>Thinking about starting a business or just curious about startups? This blog will explain what a startup is, the different types of startups, and key stages of growth. We’ll also cover important startup terms that every founder should know. Let’s get started!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*rSaqKnOrHkLISHiq.jpg" /></figure><p><strong>What is a Startup?</strong></p><p>It’s a small company that’s just starting out, trying to create something fresh and exciting. It’s often focused on developing a new product or service, and it aims to grow rapidly. They are usually less than five years old and seek funding to help them expand.</p><p><strong>Startups can be categorized by:</strong></p><p><strong>Industry or Focus:</strong><br>This classification is based on the field or area a startup operates in:</p><ul><li><strong>Tech Startups:</strong> Innovate in technology or software.<br><em>Example:</em> A company developing a new app or software.</li><li><strong>Social Startups:</strong> Aim to solve social or environmental issues.<br><em>Example:</em> A startup working on sustainable packaging.</li><li><strong>E-commerce Startups:</strong> Sell products or services online.<br><em>Example:</em> An online store for handmade crafts.</li><li><strong>Healthcare Startups:</strong> Focus on health-related technologies or services.<br><em>Example:</em> Huma, which specializes in remote patient monitoring and digital health technology.</li><li><strong>Fintech Startups:</strong> Innovate in financial services or products.<br><em>Example:</em> Brite, which provides instant bank payments through open banking.</li></ul><p><strong>Business Model and Growth Potential:</strong><br>This classification is based on the nature of the business model and how the startup is structured for growth:</p><ul><li><strong>Small Business Startups:</strong> Focus on local markets and aim for steady, moderate growth.<br><em>Example:</em> A local bakery or a small consultancy firm.</li><li><strong>Buyable Startups:</strong> Created with the intention of being acquired by a larger company.<br><em>Example:</em> A tech startup developing a niche product that complements a larger tech company’s offerings.</li><li><strong>Scalable Startups:</strong> Designed to grow rapidly and can scale up without a proportional increase in costs.<br><em>Example:</em> A software platform that supports a growing number of users without significant additional investment.</li><li><strong>Offshoot Startups:</strong> Spin-offs from existing companies or organizations, often with a specific focus or new approach.<br><em>Example:</em> A new company formed from a department within a large corporation, exploring a different market.</li><li><strong>Social Startups:</strong> Similar to the first category, these startups focus on creating social or environmental impact.<br><em>Example:</em> A startup addressing issues like climate change or social inequality.</li></ul><p>The first classification focuses on the industry or area of impact, while the second looks at the business model and growth strategy. Both perspectives offer valuable insights into the diverse world of startups.</p><p><strong>Key Pillars of a Successful Startup</strong></p><p>To build a successful startup, focus on these key pillars:</p><ul><li><strong>Product or Service:</strong> What you offer to customers must be valuable and well-developed.</li><li><strong>Team:</strong> Your team should be skilled and work well together. They are crucial for driving the startup’s success.</li><li><strong>Business Model:</strong> This outlines how your startup will make money and remain sustainable.</li><li><strong>Market:</strong> Understand your target customers and know your competition.</li></ul><p>Each pillar is essential for a startup’s success. While the product or service is often the most critical, balancing attention across all these areas is key for long-term growth.</p><p><strong>Key Startup Growth Stages</strong></p><ol><li><strong>Seed Stage:</strong> The startup is in its early phase, focusing on developing the idea and securing initial funding. This stage often includes creating the <strong>Minimum Viable Product (MVP)</strong> — a basic version of the product to test the concept and gather early feedback.</li><li><strong>Early Stage:</strong> The product or service is developed, including the MVP, and is tested with real users. The startup seeks additional funding and begins to build its customer base.</li><li><strong>Growth Stage:</strong> The startup starts expanding operations and market reach. The focus is on scaling the business and increasing revenue.</li><li><strong>Expansion Stage:</strong> The startup is established and looks to enter new markets or add new products. It may seek additional funding or partnerships.</li><li><strong>Maturity Stage:</strong> The startup is now a stable company with steady revenue. It may consider going public or exploring acquisition opportunities.</li></ol><p>By recognizing these stages and the importance of the MVP, startups can have a better plan and manage their growth.</p><h4><strong><em>Here are some popular startup terms you should know:</em></strong></h4><h3><strong><em>1. Startup Fundamentals:</em></strong></h3><h3><strong>Bootstrapping:</strong></h3><p>Starting a business using personal finances without external funding or investment.</p><p><strong>Bootstrapping</strong> offers several benefits:</p><ol><li><strong>Full Control and Ownership: </strong>you have your<strong> </strong>complete ownership and decision-making power.</li><li><strong>No Investor Pressure</strong>: you are independent and no need for external investors or their influence.</li><li><strong>Retain Equity</strong>: Keep all ownership and rewards.</li></ol><h3>Pre-seed/Seed:</h3><p>What are <strong>Pre-seed and Seed</strong> in startup?</p><ul><li><strong>Pre-seed</strong> funding is the initial capital used to develop an idea or concept into a basic business setup. It usually covers early research, prototyping, and initial costs. This funding usually comes from the founders, close family, friends, or early angel investors.</li><li><strong>Seed</strong> funding is the next stage, used to develop and launch a product, typically an MVP, and to start growing the business. Seed funding often comes from angel investors or early-stage venture capital firms and is usually larger than pre-seed funding.</li></ul><h3>Angel Investor:</h3><p>An individual who provides funding to early-stage startups in exchange for equity (ownership). Angel investors use their own personal money to invest and often offer valuable advice, mentorship, and connections to help the startup grow.</p><p><strong>Example:</strong> An angel investor might invest $50,000 in a new tech startup in return for a percentage of ownership in the company.</p><h3>VC (Venture Capital):</h3><p>This refers to the funding/money provided by venture capitalists to invest in early-stage startups or businesses with high growth potential.</p><p>Venture capitalists are individuals or firms that invest their money in exchange for equity (ownership) in the company.</p><p>They support startups to help them grow, aiming for significant returns on their investment. <strong>If you don’t need this type of funding, it’s best not to pursue it.</strong></p><h3>Crowdfunding:</h3><p>This is a method for raising small amounts of money from a large number, usually through online platforms. It allows startups to gather funds from a broad audience rather than relying on a few large investors.</p><h3>Series Funding:</h3><p>It refers to different rounds of investment a startup goes through as it grows. Each round is labeled with a letter (e.g., Series A, Series B, Series C).</p><ul><li><strong>Series A:</strong> Typically used to scale the business after the initial seed funding.</li><li><strong>Series B:</strong> Used for expanding the company’s market presence, enhancing operations, and accelerating business growth.</li><li><strong>Series C:</strong> Often used for major growth initiatives or preparing for an IPO.</li></ul><p><strong>Example:</strong> A startup might raise $5 million in Series A funding to enhance its product and $10 million in Series B to expand its market presence.</p><h3>Bridge Loan:</h3><p>A bridge loan is a short-term loan used to provide immediate funding to a startup until it secures more permanent or long-term financing. It serves as a temporary solution to cover expenses or gaps until long-term funds are available. Essentially, it helps “bridge” the gap between immediate financial needs and future funding.</p><h3>Convertible Note:</h3><p>It is a way for startups to get quick funding. It’s like a loan that can turn into company shares later. Instead of paying back the loan with money, the startup gives the investor a share of the company in the future.</p><p><strong>Example:</strong> Imagine you give a startup $50,000 through a convertible note. Later, when the startup raises more money, your $50,000 can be converted into ownership shares of the company. This means you might own a part of the company instead of getting the $50,000 back in cash.</p><p>The Key Differences between bridge loan and convertible note:</p><ul><li><strong>Repayment Method</strong>: Convertible notes convert into equity, while bridge loans are repaid in cash.</li><li><strong>Purpose</strong>: Convertible notes are often used in early-stage funding, while bridge loans are used to cover short-term gaps in funding.</li></ul><h3>2. Business Models and Metrics:</h3><h3><strong>Equity</strong></h3><p>Equity refers to ownership in a company. It represents the portion of the company that founders, employees, or investors own, typically in exchange for their contributions or investments.</p><p>In everyday conversation, people often use <strong>stake</strong> and <strong>share</strong>, which are related but slightly different:</p><ul><li><strong>Share</strong>: A unit of ownership in a company. If you own shares, you have a portion of the company’s equity.</li><li><strong>Stake</strong>: Your overall ownership interest in the company, usually expressed as a percentage. It shows how much of the company you own in total.</li></ul><p>For example, if a startup has 1,000 shares of stock and you own 100 shares, you have 10% equity in the company. This means you own 10% of the company and would benefit proportionally from its success. If the startup grows and becomes valuable, your 10% equity could increase significantly in value.</p><h3><strong>Pitch deck</strong></h3><p>It’s a brief presentation used to showcase a startup or business idea to potential investors, partners, or clients. It typically includes key information such as the problem being solved, the solution or product, the market opportunity, business model, financial projections, and the team. The goal is to persuade the audience to invest in or support the business.</p><p>Basically is a fancy term for an investor presentation.</p><h3>Burn rate:</h3><p>It is simply how fast a company is spending its money.</p><ul><li><strong>Gross burn rate</strong>: The total amount of money a company spends in a month, not accounting for any incoming revenue. It shows how quickly the company is using its cash before considering any income.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T7UN95X7if5gE_mrNI5TTg.png" /></figure><ul><li><strong>Net burn rate</strong>: The amount of money a company loses each month after accounting for revenue. It shows the actual cash outflow.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i5bArJ6O9Lzsl2vWjZfGgQ.png" /></figure><p>Lets elaborate it in this scenario:</p><ul><li>A startup has an initial cash balance of $200,000.</li><li>After 6 months, the remaining cash balance is $140,000.</li><li>During those 6 months, the startup has generated $50,000 in revenue.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-uIsfmyp_fdZ7aJfAcV6Ow.png" /></figure><p>The <strong>gross burn rate</strong> is $10,000 per month, meaning the company is spending $10,000 each month.</p><p>The <strong>net burn rate</strong> is $1,667 per month, meaning after accounting for the revenue, the company is actually losing $1,667 per month.</p><blockquote>A <strong>positive net burn rate</strong> means the company is losing money.</blockquote><blockquote>A <strong>negative net burn rate</strong> means the company is earning more than it’s spending, indicating profitability.</blockquote><h3>Runway:</h3><p>The amount of time a startup can continue operating before it runs out of money. It’s calculated based on the current cash balance and the monthly burn rate.</p><h3>Cash flow</h3><p>This is the money coming in (like revenue from sales) and the money going out (like expenses for rent, salaries, and supplies).</p><p>For example, if a startup earns $10,000 from sales in a month but spends $8,000 on expenses, it has a positive cash flow of $2,000. This means the business has more money coming in than going out, which is healthy. However, if the startup earns $10,000 but spends $12,000, it has a negative cash flow of -$2,000, indicating it’s spending more than it earns, which could be a problem if it continues.</p><h3>Customer Acquisition Cost(CAC)</h3><p>Customer Acquisition Cost (CAC) is the total amount of money a startup spends to get a new customer. This includes costs like marketing, advertising, and sales efforts.</p><p>For example, if a startup spends $1,000 on marketing in a month and gains 10 new customers, the CAC would be $100 per customer. This means it costs the startup $100 to acquire each new customer.</p><h3>Lifetime Value (LTV)</h3><p>It represents the value of a customer. It is the total revenue a startup expects to earn from a customer over their entire relationship with the company.</p><p><strong>Example:</strong></p><p>If a customer spends $50 per month on your product and stays with your company for 2 years, their LTV would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hF05ZEOKlqdzkzUn3hO0Gw.png" /></figure><p>So, the Lifetime Value of this customer is $1,200. This helps a startup understand how much they can afford to spend on acquiring customers and how valuable each customer is over time.</p><h3>Churn rate:</h3><p>The percentage of customers who stop using a product or service over a specific period.</p><p><strong>Example</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YJU5XzT-gvPbB--eCPnMaQ.png" /></figure><p>If a startup has 100 customers at the start of the month and 5 customers leave by the end of the month, the churn rate is 5%.</p><h3>3. Product Development:</h3><h3>Alpha Test</h3><p>The first testing phase where a new product or feature is tested internally within the company to find and fix bugs to see whether a product meets business requirements and functions as expected.</p><h3>Beta Test</h3><p>The second testing phase where the product or feature is released to a select group of external users to gather feedback and identify any remaining issues before the official launch.</p><h3>Lean:</h3><p>From what we have learned from project management it’s crucial to listen to the client, address issues in product development, and providing clear guidance on what actions to take and avoid to make sure the project is going the right way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eqGOjfr34iy5wPAg.jpg" /></figure><p>And <strong>Lean</strong> is a startup approach focused on building products quickly with minimal resources, testing ideas with real customers, and using feedback to make improvements. The goal is to reduce waste and iterate rapidly to find a successful business model.</p><blockquote><em>A must-do is to use </em><strong><em>Lean Agile</em></strong><em>, which combines Lean principles (minimizing waste and maximizing value) with Agile practices (iterative development and continuous improvement). This approach focuses on making small, incremental improvements quickly while efficiently using resources and responding to feedback.</em></blockquote><h3>Pivot</h3><p>This is when a business changes its direction or strategy to better fit market needs. For example, Twitter started as a podcasting platform called Odeo. When Apple launched iTunes with podcasting, Odeo’s founders pivoted to a simple messaging platform where users could share short updates, which became Twitter, now one of the most popular social media platforms.</p><h3>4. Growth and Scaling:</h3><h3>Scalability:</h3><p>It’s the ability of a company to grow and manage increased demand without compromising performance or revenue growth.</p><p>If a software company can easily add new users without a major increase in costs or needing to redesign the system, it’s considered scalable. For instance, a cloud-based service that can handle thousands of users with minimal extra cost is scalable.</p><h3>Growth hacking:</h3><p>It’s a marketing strategy used by startups to rapidly grow and scale their business with limited resources. It involves using creative, low-cost methods and data-driven techniques to achieve significant business growth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xO0aD3akx440kyRW.jpeg" /></figure><p><strong>Example:</strong> A startup could use referral programs to encourage existing users to invite friends, rapidly increasing their user base without expensive marketing campaigns.</p><h3><strong>5. Funding and Financial Strategies:</strong></h3><h3>Freemium:</h3><p>It is a business model where a company offers basic services for free but charges for premium features or advanced functionalities.</p><p><strong>Example:</strong> A software app might let users access basic features at no cost but require a subscription for advanced tools or additional storage.</p><p>Crowdfunding</p><h3>IPO(Initial public offering):</h3><p>It is when a company offers its shares to the public for the first time, allowing anyone to buy a stake in the company. This is often done to raise funds for growth and expand the business.</p><p><strong>Example:</strong> A startup that has been privately owned decides to go public, offering shares on the stock market so investors can buy ownership in the company.</p><h3>SaaS (Software as a Service):</h3><p>It is a model where you access and use software over the internet instead of installing it on your computer.</p><ul><li><strong>Free SaaS:</strong> Google Drive offers free cloud storage and document editing.</li><li><strong>Paid SaaS:</strong> Salesforce is a customer relationship management (CRM) platform that requires a subscription fee. Google Workspace is another example, offering tools like Gmail, Google Drive, and Google Docs for businesses, also on a subscription basis.</li></ul><p>In both cases, the provider handles software updates and maintenance.</p><h3>Accelerator:</h3><p>An <strong>Accelerator</strong>, also known as a <strong><em>seed accelerator</em></strong>, is a business program designed to accelerate your startup’s growth. It supports early-stage, growth-driven companies through education, mentorship, and financing over <strong><em>a fixed period of time.</em></strong></p><p>Accelerators typically select startups with promising <strong>MVPs</strong> and strong founders.</p><p>To be accepted in accelerator program, you need to have a <strong>validated MVP</strong> with either paying or free customers.</p><blockquote><strong><em>MVP</em></strong><em> (Minimum Viable Product) should be “validated” means it has been tested in the market and received feedback from real users, whether they are paying or free customers.</em></blockquote><p>Here are 4 institutions to support startups: <strong>Incubators, Angle Investors, Accelerators and Hybrids</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*74T5CUIGnNHxw4u6.png" /></figure><h3>Incubator:</h3><p><strong>A</strong> <strong>business incubator</strong> is a program that helps very early-stage startups by providing mentorship, investor connections, and essential resources to get them off the ground. These programs focus on helping startups develop an MVP and create a plan to bring it to market. Startups with an MVP or launched product are typically better suited for accelerators rather than incubators.</p><p>Incubators also offer access to technical resources, logistical support, and shared office space. Programs can last from a few months to a few years, aiming to equip startups with the tools and knowledge to become self-sufficient.</p><p>Some incubators operate as non-profits, while others offer seed capital and support in exchange for equity in the companies they help.</p><h3><strong>Exit:</strong></h3><p>An exit strategy is a plan that allows founders and investors to sell their shares and potentially earn a return on their investment.</p><p>This usually happens through:</p><ul><li><strong>Acquisition:</strong> Another company buys the startup.</li><li><strong>Initial Public Offering (IPO):</strong> The startup becomes publicly traded by selling shares on a stock exchange.</li></ul><p>It outlines how they will exit their investment in the startup.</p><h3>Know Your Numbers (KYN)</h3><p>As we’ve explored various startup types, growth stages, and key terms, it’s essential to highlight one critical aspect: knowing your metrics. This principle is known as “<strong>Know Your Numbers</strong>” (KYN) which is the golden rule for any startup founder.</p><p><strong>Why KYN Matters:</strong></p><ul><li><strong>Informed Decision-Making:</strong> Metrics such as Customer Acquisition Cost (CAC), Lifetime Value (LTV), and Burn Rate are key to making informed decisions.</li><li><strong>Tracking Growth:</strong> Understanding these numbers allows you to monitor your startup’s performance and make necessary adjustments.</li><li><strong>Attracting Investors:</strong> Clear metrics can make your startup more attractive to potential investors.</li></ul><p>In summary, while understanding startup terminology and stages provides the foundational knowledge, knowing your metrics helps you manage and grow your startup effectively. Make sure to focus on your key metrics to set your business up for success.</p><p>I hope this post has provided you with a better understanding of the startup world and its complexities.</p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0e0d2fc5984d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring AI: Communicating with LLMs and Parsing Responses]]></title>
            <link>https://levelup.gitconnected.com/spring-ai-communicating-with-llms-and-parsing-responses-92e25066df86?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/92e25066df86</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[spring-ai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Sat, 18 May 2024 20:12:47 GMT</pubDate>
            <atom:updated>2024-09-04T09:41:36.789Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Comprehensive Guide with Java 21, Spring AI, and OpenAI</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*_sA7LL9NjQwq7OFNyy5L2w.jpeg" /></figure><p><strong><em>Mastering Communication with Large Language Models: Overcoming Challenges with Spring AI</em></strong></p><p>Communicating with Large Language Models (LLMs) presents several challenges, including parsing responses, integrating our own data, augmenting the context that we are sending (RAG), and more.</p><p>Spring AI, within the Spring Framework,<strong> </strong>provides API and let you create Java-based AI applications with less complexity. It addresses some of these issues by offering tools and frameworks that facilitate seamless communication with LLMs.</p><p>Spring AI supports various AI models, including OpenAI, Azure OpenAI, Amazon Bedrock (Anthropic, Llama, Cohere, Titan, Jurassic2), HuggingFace, and Google VertexAI (PaLM2, Gemini) and more.</p><p>In this project, I will use the OpenAI GPT( Chat Generative Pre-trained Transformer) model. To begin, you’ll need to sign up for an account and generate an API key. Keep in mind that this is not free; it comes at a cost, which varies depending on the model’s capabilities. You can review the pricing details on the OpenAI website for each model. The pricing is based on tokens, but what exactly are tokens?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z6QQpAmj0AyXTGmSOn5ffQ.png" /></figure><h4>Tokens</h4><p>LLMs(large language models) process text using tokens, which are pieces of words, not strictly bound by word boundaries, and can include sub-words and spaces. Each token represents about 4 characters or ¾ of a word. For instance, the phrase “Learning is fun.” consists of 4 tokens and 16 characters. Tokens are the currency of the LLMs, with limits on their usage and varying pricing by model.</p><p>You can use the OpenAPI tokenizer to see how it’s counted:</p><p><a href="https://platform.openai.com/tokenizer">https://platform.openai.com/tokenizer</a></p><blockquote>It’s important to note that the exact tokenization process varies between models. Newer models like GPT-3.5 and GPT-4 use a different tokenizer than previous models, and will produce different tokens for the same input text.</blockquote><p>Now I will create a Spring Boot application to show how to use Spring AI for interacting with LLMs. This includes parsing the output, creating prompts, and providing a comprehensive, step-by-step explanation.</p><h3>Project setup</h3><p><em>Start by creating a new Spring Boot project using Spring Initializr, selecting the following dependencies. Import the project into your preferred IDE.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qWGTnssKM63mqMN1hCQm3w.png" /></figure><ul><li>Spring web for building Restful APIs</li><li>Spring AI support for ChatGPT</li></ul><p>To communicate with OpenAI, we must first define the configuration properties in the application.properties file. Paste the API key you generated earlier into this file.</p><pre>spring.application.name=chat-model-demo<br>spring.ai.openai.api-key=${OPENAI_API_KEY} // define it as an environment variable<br>spring.ai.ollama.chat.model=gpt-3.5-turboprop // the name of the chat model that you are using</pre><p>Next, we need to create a REST controller class to expose an API. When this API is called, it will invoke OpenAI with our question (prompt).</p><p>In order to interact with the OpenAI model, we need to inject the ChatClient interface, which contains two methods:</p><pre>@FunctionalInterface<br>public interface ChatClient extends ModelClient&lt;Prompt, ChatResponse&gt; {<br><br> default String call(String message) {<br>  Prompt prompt = new Prompt(new UserMessage(message));<br>  Generation generation = call(prompt).getResult();<br>  return (generation != null) ? generation.getOutput().getContent() : &quot;&quot;;<br> }<br> @Override<br> ChatResponse call(Prompt prompt);</pre><p>The first method takes message and returns String, while the second one takes a Prompt and returns a ChatResponse.</p><h4>Now let’s expose an API to tell a story:</h4><pre>package dev.mina.chatmodeldemo;<br><br>import org.springframework.ai.chat.ChatClient;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RequestParam;<br>import org.springframework.web.bind.annotation.RestController;<br><br>@RestController<br>public class StoryController {<br><br>  private final ChatClient chatClient;<br><br>  public StoryController(ChatClient chatClient) {<br>    this.chatClient = chatClient;<br>  }<br><br>  @GetMapping(&quot;/story&quot;)<br>  public String generateStory(@RequestParam(value = &quot;message&quot;, defaultValue = &quot;Tell me a short funny story&quot;) String message) {<br>    return chatClient.call(message);<br>  }<br>}</pre><p>Now if we run the application and call the api , using <strong>HTTPie</strong>, a powerful and easy-to-use command-line HTTP client, like thishttp :8080/story it will return an string response, containing a funny story.</p><p>Let’s explore the second method of ChatClient interface, create and pass prompts, but what is prompt: <em>A prompt is the input or question that we are sending the LLM.</em></p><p>If we look at the Prompt class it has different constructors which creates a new UserMessage which is an implementation of the AbstractMessage.</p><p>Here are the implementations of AbstractMessage:</p><ul><li>AssistantMessage.java</li><li>ChatMessage.java</li><li>FunctionMessage.java</li><li>SystemMessage.java</li><li>UserMessage.java</li></ul><p>we also need to learn about different roles when creating a prompt:</p><h4>System Role:</h4><p>Guides the AI’s behavior and response style by setting rules before the conversation starts. It’s like giving instructions to the AI.</p><h4>User Role:</h4><p>Represents the user’s input, forming the basis of the AI’s response.</p><h4>Assistant Role:</h4><p>The AI’s response to the user’s input, crucial for maintaining conversation flow by tracking the AI’s previous responses (its ‘Assistant Role’ messages).</p><h4>Function Role:</h4><p>Performs specific tasks or operations during the conversation, beyond just talking like performing calculations or fetching data.</p><p>find full details about prompt here: <a href="https://docs.spring.io/spring-ai/reference/api/prompt.html">https://docs.spring.io/spring-ai/reference/api/prompt.html</a></p><p>Now, let’s create an API demonstrating the use of prompts.</p><h4>Expose an API to find the most read stories by genre:</h4><pre>@GetMapping(&quot;/stories/must-read&quot;)<br>  public String getMustReadStories(@RequestParam(value = &quot;genre&quot;, defaultValue = &quot;bedtime&quot;) String genre) {<br>    String message = &quot;&quot;&quot;<br>        Give me the top 10 must-read {genre} stories.<br>        &quot;&quot;&quot;;<br>    PromptTemplate promptTemplate = new PromptTemplate(genre);<br>    Prompt prompt = promptTemplate.create(Map.of(&quot;genre&quot;, genre));<br>    return chatClient.call(prompt).getResult().getOutput().getContent();<br>  }</pre><p>Run the application and make the API call: http :8080/most-read</p><p>It will return the response string containing the 10 most read adventure stories.</p><pre>1. &quot;Goodnight Moon&quot; by Margaret Wise Brown<br>2. &quot;Where the Wild Things Are&quot; by Maurice Sendak<br>3. &quot;The Tale of Peter Rabbit&quot; by Beatrix Potter<br>4. &quot;Winnie-the-Pooh&quot; by A.A. Milne<br>5. &quot;The Very Hungry Caterpillar&quot; by Eric Carle<br>6. &quot;Guess How Much I Love You&quot; by Sam McBratney<br>7. &quot;Corduroy&quot; by Don Freeman<br>8. &quot;Love You Forever&quot; by Robert Munsch<br>9. &quot;Bedtime for Frances&quot; by Russell Hoban<br>10. &quot;Harold and the Purple Crayon&quot; by Crockett Johnson</pre><p>One thing to note is we need to avoid hardcoding messages. Instead, we can create prompt templates under the resources folder. Simply create a folder named “prompts” under resources, and place all the string templates there. For example: /resources/prompts/story.st. Inside, put the string template. Give me the top 10 must-read {genre} stories.</p><pre>@RestController<br>public class StoryController {<br><br>  private final ChatClient chatClient;<br>  @Value(&quot;classpath:/prompts/story.st&quot;)<br>  private Resource storyPromptResource;<br><br>  public StoryController(ChatClient chatClient) {<br>    this.chatClient = chatClient;<br>  }<br><br>  @GetMapping(&quot;/story&quot;)<br>  public String generateStory(@RequestParam(value = &quot;message&quot;, defaultValue = &quot;Tell me a short funny story&quot;) String message) {<br>    return chatClient.call(message);<br>  }<br><br>  @GetMapping(&quot;/stories/most-read&quot;)<br>  public String getMostReadStories(@RequestParam(value = &quot;genre&quot;, defaultValue = &quot;bedtime&quot;) String genre) {<br>    PromptTemplate promptTemplate = new PromptTemplate(storyPromptResource);<br>    Prompt prompt = promptTemplate.create(Map.of(&quot;genre&quot;, genre));<br>    return chatClient.call(prompt).getResult().getOutput().getContent();<br>  }<br>}</pre><h3>Output Parsing</h3><p>As you see the response from LLM is always String, but what if we want something else like list of Strings? <strong><em>how we can handle the responses received from LLMs and parse them into objects within our system.</em></strong></p><p>Spring AI offers the OutputParser, which currently comes with three implementations:</p><pre>Certainly! You can present this information in a table format like this:<br><br>| OutputParser      | Description                                                                                                                                                     |<br>|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|<br>| BeanOutputParser  | Converts the LLM output to a specific Java bean instance. |<br>| ListOutputParser  | Converts the LLM output into a List instance.<br>| MapOutputParser   | Converts the LLM output into a java.util.Map&lt;String, Object&gt; instance.</pre><h3>ListOutputParser</h3><p>It specifies that the prompt should request LLM output in a list format, where values are separated by commas. Later, Spring AI uses Jackson to parse these comma-separated values into a List.</p><p>Now let’s explore the ListOutputParser, expose an API to return the list of 10 most popular stories in Science genre.</p><pre>@GetMapping(&quot;/stories/popular&quot;)<br>public List&lt;String&gt; getPopularStories(@RequestParam(value = &quot;genre&quot;, defaultValue = &quot;Science Fiction&quot;) String genre) {<br>  var message = &quot;&quot;&quot;<br>        Give me a list of 10 most popular stories for the genre: {genre}. If you don&#39;t know the answer, just say &quot;I don&#39;t know&quot; {format}<br>        &quot;&quot;&quot;;<br>  ListOutputParser outputParser = new ListOutputParser(new DefaultConversionService());<br><br>  PromptTemplate promptTemplate = new PromptTemplate(message, Map.of(&quot;genre&quot;, genre, &quot;format&quot;, outputParser.getFormat()));<br>  Prompt prompt = promptTemplate.create();<br><br>  ChatResponse response = chatClient.call(prompt);<br>  return outputParser.parse(response.getResult().getOutput().getContent());<br>}</pre><p>Then, make a request to http :8080/stories/popular. The response will look like this:</p><pre>[<br>  &quot;Dune&quot;,<br>  &quot;The Hitchhiker&#39;s Guide to the Galaxy&quot;,<br>  &quot;Neuromancer&quot;,<br>  &quot;Foundation&quot;,<br>  &quot;1984&quot;,<br>  &quot;Brave New World&quot;,<br>  &quot;Snow Crash&quot;,<br>  &quot;Ender&#39;s Game&quot;,<br>  &quot;The War of the Worlds&quot;,<br>  &quot;The Martian&quot;MapOutputParser&quot;<br>]</pre><h3>MapOutputParser</h3><p>It uses a pre-configured MappingJackson2MessageConverter to convert the LLM output into a java.util.Map&lt;String, Object&gt; instance.</p><p>Expose an API to return a list of stories for the author, including their genres.</p><pre>@GetMapping(&quot;/stories/author&quot;)<br>public Map&lt;String, Object&gt; getAuthorsStories(@RequestParam(value = &quot;author&quot;, defaultValue = &quot;Jill McDonald&quot;) String author) {<br>  var message = &quot;&quot;&quot;<br>        Give me a list of stories for the author {author}, including their genres. If you don&#39;t know the answer, just say &quot;I don&#39;t know&quot; {format}<br>        &quot;&quot;&quot;;<br>  MapOutputParser outputParser = new MapOutputParser();<br><br>  PromptTemplate promptTemplate = new PromptTemplate(message, Map.of(&quot;author&quot;, author, &quot;format&quot;, outputParser.getFormat()));<br>  Prompt prompt = promptTemplate.create();<br><br>  ChatResponse response = chatClient.call(prompt);<br>  return outputParser.parse(response.getResult().getOutput().getContent());<br>}</pre><p>Calling http :8080/stories/author will return a map of the author to her stories.</p><h3>BeanOutputParser</h3><p>BeanOutputParser helps in parsing and transforming the LLM output into the desired type.</p><p>Expose an API to return a random story for an author.</p><pre>public record Story(String author, String title, String genre) {<br><br>}<br><br>@GetMapping(&quot;/stories/random-by-author&quot;)<br>public Story getRandomStoryByAuthor(@RequestParam(value = &quot;author&quot;, defaultValue = &quot;J.K Rowling&quot;) String author) {<br>  var message = &quot;&quot;&quot;<br>        Give me a story for the author {author}. If you don&#39;t know the answer, just say &quot;I don&#39;t know&quot; {format}<br>        &quot;&quot;&quot;;<br>  var outputParser = new BeanOutputParser&lt;&gt;(Story.class);<br><br>  PromptTemplate promptTemplate = new PromptTemplate(message, Map.of(&quot;author&quot;, author, &quot;format&quot;, outputParser.getFormat()));<br>  Prompt prompt = promptTemplate.create();<br><br>  ChatResponse response = chatClient.call(prompt);<br>  return outputParser.parse(response.getResult().getOutput().getContent());<br>}</pre><p>Calling http :8080/stories/random-by-author will return a story object with author, title, and genre.</p><p><strong>You can find the source code here: </strong><a href="https://github.com/minarashidi/chat-model-demo">https://github.com/minarashidi/chat-model-demo</a></p><p>I hope this post helped you to gain better understanding of Spring AI and how to communicate with LLMs within java.</p><p>Feel free to share your thoughts and feedbacks.</p><p>Thank you for reading!</p><h3>Reference</h3><p><a href="https://spring.io/projects/spring-ai">Spring AI</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92e25066df86" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/spring-ai-communicating-with-llms-and-parsing-responses-92e25066df86">Spring AI: Communicating with LLMs and Parsing Responses</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Retrieval-Augmented Generation (RAG)]]></title>
            <link>https://blog.gopenai.com/retrieval-augmented-generation-rag-585aa903d6bd?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/585aa903d6bd</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[retrieval-augmented]]></category>
            <category><![CDATA[vector-database]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Wed, 06 Mar 2024 00:54:53 GMT</pubDate>
            <atom:updated>2024-03-07T18:02:29.708Z</atom:updated>
            <content:encoded><![CDATA[<p><em>In this blog post, I will discuss some of the challenges we face when working with large language models (LLMs) and how to use Retrieval-Augmented Generation (RAG) framework to address these issues to retrieve more accurate and up-to-date data.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MI9WDgzoOGAH4bOnAwBKEw.jpeg" /></figure><h3><strong>What is Retrieval-Augmented Generation, or RAG?</strong></h3><p>RAG is a framework that combines large language models with retrieval mechanisms to provide relevant knowledge to the LLM to generate text.</p><ul><li>Retrieval-based methods: These involve getting information from external sources like databases or articles.</li><li>Generative models: generating coherent and contextually relevant text.</li></ul><p>Let’s explore the concept of Generation. The term Generation refers to large language models (LLMs) that generate text in response to a user query(Prompt).</p><p><strong>What is a Prompt?</strong> In the context of language models, a prompt refers to the text we provide to the model when making a request or asking a question. The response is the text generated by the model in reply to your prompt.</p><p>For instance, when you inquire with ChatGPT:</p><ul><li><em>What is the tallest building in the world?</em></li></ul><p>The prompt is the question you asked, and the response is the answer generated by the model. In this case, the response would be:</p><ul><li>The Burj Khalifa in Dubai is the world’s tallest building, standing at 828 meters (2,717 feet).</li></ul><p><strong>What are the challenges with LLM?</strong></p><p>Now let’s understand the problem we might have with LLMs and how to solve them, if we ask this question to a large language model(LLM):</p><ul><li><em>what is the best-selling book this year?</em></li></ul><p>We may receive a response indicating the lack of available information.</p><p>The reason the LLM doesn’t provide the answer is that it hasn’t been trained on.</p><p>Here’s another business-related question:</p><ul><li><em>Did we onboard any new merchants last quarter?</em></li></ul><p>LLM does not have this information and cannot answer this question.</p><p><em>In general, we have two major challenges when interacting with large language models:</em></p><ol><li>Lack of a definitive <strong>source</strong>.</li><li>Information being <strong>outdated</strong>.</li></ol><p><strong>How would a large language model respond to this query?</strong></p><p>Rather than relying solely on the LLM’s knowledge, we can add a content store, which could be documents, policies, or other relevant information. By leveraging the RAG framework, we first retrieve relevant content, then combine it with the user’s query before generating an answer.</p><p>The prompt now consists of three main components:</p><ol><li>An instruction to pay attention to.</li><li>The retrieved content from the content store.</li><li>The user’s original question.</li></ol><p>Using this approach, the LLM can now provide an answer along with evidence supporting its response. This enables users to better understand the reasoning behind the LLM’s answer.</p><p><em>Therefore our goal is to augment LLM so that it can respond with information that it has not yet been trained on.</em></p><p>How can we do that at a high level? here’s how large language models are used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*an57uC-9rmLnbwkx9ummEw.jpeg" /></figure><p>When a user asks a question, it is sent to a large language model (LLM) which then provides an answer. Although this method works well, it has some limitations. One limitation is that the LLM only knows what it has been trained on, and its knowledge is limited to information available up to its training cutoff date. Continuously training LLMs with the latest real-time data can be costly and is not always practical.</p><p>Large language models (LLMs) cannot access your private or business-specific data.</p><h4><strong>How to use RAG framework to address this issue?</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*I9snkL6xNayZGhnYmepWwg.png" /></figure><p><strong>Preparing Proprietary Data</strong></p><p>proprietary data is converted to vector embeddings by an embedding model and stored in a vector database for later use.</p><p><strong>Retrieving Relevant Data</strong></p><p>When a user asks a question, it fetches relevant data from the vector database based on the user’s query and then a prompt is created which is made up of the original user question and the retrieved contextual data.</p><p><strong>Generating a Response</strong></p><p>This prompt is then sent to the LLM, which uses it to generate a response. The result is a relevant and contextual answer that takes the proprietary data into account.</p><p><strong>Vector embeddings</strong> are compact numerical representations of data, essentially arrays of numbers. The reason we convert data into vectors is because machines find it easier to work with numbers. The numbers in these vectors represent different dimensions of the source data.</p><p><strong>Vector database</strong> indexes and stores Vector embeddings for fast retrieval and similarity search.</p><p>I hope this blog was useful, Please leave your feedback, comments, and improvements.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=585aa903d6bd" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/retrieval-augmented-generation-rag-585aa903d6bd">Retrieval-Augmented Generation (RAG)</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring AWS Lambda and SnapStart]]></title>
            <link>https://awstip.com/exploring-aws-lambda-and-snapstart-36d61a3d6c18?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/36d61a3d6c18</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Fri, 26 Jan 2024 21:41:57 GMT</pubDate>
            <atom:updated>2024-02-05T17:40:05.726Z</atom:updated>
            <content:encoded><![CDATA[<h4>A detailed example with Java 21 and Spring Cloud</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PyALc0moj9Ic_Y5ZdA9-lA.jpeg" /></figure><p>AWS Lambda is a powerful and flexible service for building and running serverless applications. With Lambda, you can run your code without the need to provision or manage servers.</p><p><em>Serverless does not mean there are no servers, it means you just don’t manage and provision servers.</em></p><h3>AWS Lambda vs. Amazon EC2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LHOsIUCZSmHEE6X8QVeqNw.png" /></figure><p>Both Amazon EC2 and Lambda serve the same purpose for any application to get services as microservice from AWS Api-gateway. However, several attributes should be considered before choosing between these two options to align with your application’s requirements.</p><pre>|                     | Amazon EC2                              | Amazon Lambda                             |<br>|---------------------|-----------------------------------------|-------------------------------------------|<br>| Description         | Virtual Servers in the Cloud            | Virtual functions — no servers to manage! |<br>| Limitations         | Limited by RAM and CPU                  | Limited by time — short executions        |<br>| Execution Model     | Continuously running                    | Run on-demand                             |<br>| Scaling             | Scaling means adding or removing servers| Scaling is automated!                     |</pre><h3><strong>Some benefits of using Lambda:</strong></h3><h4>1. Lambda is cost-effective with simple pricing:</h4><ul><li><em>only pay per request and compute time</em></li><li><em>Free tier of 1,000,000 AWS Lambda requests and 400,000 GBs of compute time</em></li></ul><h4><strong>2. Lambda support many programming languages:</strong></h4><ul><li>like: Node.js (JavaScript) — Python — Java — C# (.NET Core) — Golang — C# — Ruby — Custom Runtime API</li></ul><blockquote>Lambda supports multiple languages through the use of <em>runtimes</em>. A runtime provides a language-specific environment that relays invocation events, context information, and responses between Lambda and the function. You can use runtimes that Lambda provides, or build your own.</blockquote><h4>3. Lambda is well-integrated with the entire AWS services.</h4><p>As an example, you can create a Lambda function and configure a trigger for an Amazon Simple Storage Service (Amazon S3) bucket.</p><p>Every time that you add an object to your Amazon S3 bucket, your function runs and outputs the object type to Amazon CloudWatch Logs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*4MKdiWoiADCTyPDpBKgVUQ.png" /></figure><h3>Using a Lambda function to access an Amazon RDS database</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mxd6qa46s_Zzsy0M7NX7PQ.png" /></figure><p>If you use a Lambda function to write data to an Amazon Relational Database Service (Amazon RDS) database, it may open excessive connections during periods of high load. In such cases, it’s recommended to use RDS Proxy for more efficient connection management.</p><p><strong>RDS Proxy:</strong></p><p>• Improve scalability by pooling and sharing DB connections</p><p>• Improve availability by reducing by 66% the failover time and preserving connections</p><p>• Improve security by enforcing IAM authentication and storing credentials in Secrets Manager</p><p><em>The Lambda function must be deployed in your VPC, because RDS Proxy is never publicly accessible</em></p><h3>Invoking an AWS Lambda function from an RDS</h3><p>Only Supported for RDS for PostgreSQL and Aurora MySQL</p><p>With that, we can access and process data events from the database. For example, whenever a user registers, we can invoke a lambda function from RDS to send an email.</p><p><em>Notes: There is another service named </em><em>RDS Event Notifications which tells information about the DB instance itself (created, stopped, start, …) not the data events.</em></p><h3>What is AWS Lambda SnapStart and How it improves startup performance?</h3><p>AWS Lambda SnapStart was introduced by AWS on Nov 29, 2022, to reduce the cold start times of Lambda and this performance optimization can significantly improve the startup time for applications.</p><p>This feature improves performance up to 10x at no extra cost for Java 11 and above at no extra cost, and with minimal or no code changes.</p><p>SnapStart supports <strong>only</strong> Java 11 and later Java versions.</p><blockquote>AWS states that Lambda SnapStart is ideal for synchronous APIs, interactive microservices, or data processing applications.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*hzN--vXTiq21HOeQ7Fitdg.png" /></figure><p>Before going deep dive into this, let’s explore how Lambda execution works and understand the phases of execution:</p><p><em>Lambda execution contains two phases: cold start and warm start</em></p><blockquote>“cold start” occurs when a function is invoked for the first time or after a long pause, involving resource provisioning, environment setup and loading the function’s code.</blockquote><blockquote>“Warm start” happens in subsequent invocations, reusing the initialized function for faster execution.</blockquote><p>Cold start time is the largest contributor to startup latency. This can cause degraded user experience for interactive workloads.</p><p><strong>AWS Lambda SnapStart</strong> can significantly reduce cold starts at no additional cost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*33c2nYOjGz7-8tP0CsO7Og.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PyALc0moj9Ic_Y5ZdA9-lA.jpeg" /></figure><p>Now, let’s create our Spring Boot application to handle Lambda invocation requests. We will test it locally and then deploy it to AWS. I’ll provide a detailed step-by-step explanation of how to test it on AWS as well.</p><p>Here you can find the source code: <a href="https://github.com/minarashidi/lambda-snapstart">https://github.com/minarashidi/lambda-snapstart</a></p><h3>Getting Started</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tcfq8oAoo0GYpNOKK3wBWw.png" /></figure><h3>Creating Spring Boot Application With Spring Cloud Function</h3><p>Spring Cloud function provides an AWS adapter which takes a Spring Cloud Function app and converts it to a form that can run in AWS Lambda.</p><p>It makes writing AWS Lambda handlers easy. We just need to create a java.util.Function as a bean to handle incoming requests and return a response.</p><p>Here I have defined one handler bean which checks if the given string is a palindrome.</p><pre>@SpringBootApplication<br>public class Main {<br><br>  public static void main(String[] args) {<br>    SpringApplication.run(Main.class, args);<br>  }<br><br>  @Bean<br>  public Function&lt;String, Boolean&gt; isPalindrome() {<br><br>    /*<br>      Checks if the given string is a palindrome.<br>      A palindrome is a word, phrase, number, or sequence of characters that reads the same forward and backward.<br>      Examples include &quot;level,&quot; &quot;radar,&quot; &quot;12321,&quot; and &quot;madam.&quot;<br><br>      @param input The string to be checked for palindrome.<br>      @return True if the string is a palindrome, false otherwise.<br>     */<br>    return input -&gt; {<br>      if (input == null) {<br>        return false;<br>      }<br>      // Remove non-alphanumeric characters and spaces<br>      String cleanedInput = input.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;).toLowerCase();<br>      String reversed = new StringBuilder(cleanedInput).reverse().toString();<br>      return cleanedInput.equalsIgnoreCase(reversed);<br>    };<br>  }<br>}</pre><p>Here is an artifact that helps us in building an AWS-specific artifact, which can be then uploaded to the AWS console.</p><pre>&lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-cloud-function-adapter-aws&lt;/artifactId&gt;<br> &lt;/dependency&gt;</pre><p>Make sure you have all these dependencies and plugging in pom.xm:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;<br>  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br>  &lt;parent&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>    &lt;version&gt;3.2.2&lt;/version&gt;<br>    &lt;relativePath/&gt;<br>  &lt;/parent&gt;<br>  &lt;groupId&gt;com.mina&lt;/groupId&gt;<br>  &lt;artifactId&gt;lambda-snapstart&lt;/artifactId&gt;<br>  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;<br>  &lt;name&gt;lambda-snapstart&lt;/name&gt;<br>  &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;<br>  &lt;properties&gt;<br>    &lt;java.version&gt;21&lt;/java.version&gt;<br>    &lt;spring-cloud.version&gt;2023.0.0&lt;/spring-cloud.version&gt;<br>  &lt;/properties&gt;<br><br>  &lt;dependencies&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-cloud-function-web&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-cloud-function-adapter-aws&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;<br>      &lt;scope&gt;test&lt;/scope&gt;<br>    &lt;/dependency&gt;<br>  &lt;/dependencies&gt;<br>  &lt;dependencyManagement&gt;<br>    &lt;dependencies&gt;<br>      &lt;dependency&gt;<br>        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>        &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;<br>        &lt;version&gt;${spring-cloud.version}&lt;/version&gt;<br>        &lt;type&gt;pom&lt;/type&gt;<br>        &lt;scope&gt;import&lt;/scope&gt;<br>      &lt;/dependency&gt;<br>    &lt;/dependencies&gt;<br>  &lt;/dependencyManagement&gt;<br><br>  &lt;build&gt;<br>    &lt;plugins&gt;<br>      &lt;plugin&gt;<br>        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;<br>        &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;<br>        &lt;configuration&gt;<br>          &lt;skip&gt;true&lt;/skip&gt;<br>        &lt;/configuration&gt;<br>      &lt;/plugin&gt;<br>      &lt;plugin&gt;<br>        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>        &lt;dependencies&gt;<br>          &lt;dependency&gt;<br>            &lt;groupId&gt;org.springframework.boot.experimental&lt;/groupId&gt;<br>            &lt;artifactId&gt;spring-boot-thin-layout&lt;/artifactId&gt;<br>            &lt;version&gt;1.0.31.RELEASE&lt;/version&gt;<br>          &lt;/dependency&gt;<br>        &lt;/dependencies&gt;<br>      &lt;/plugin&gt;<br>      &lt;plugin&gt;<br>        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;<br>        &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;<br>        &lt;version&gt;3.5.1&lt;/version&gt;<br>        &lt;configuration&gt;<br>          &lt;createDependencyReducedPom&gt;false&lt;/createDependencyReducedPom&gt;<br>          &lt;shadedArtifactAttached&gt;true&lt;/shadedArtifactAttached&gt;<br>          &lt;shadedClassifierName&gt;aws&lt;/shadedClassifierName&gt;<br>        &lt;/configuration&gt;<br>      &lt;/plugin&gt;<br>    &lt;/plugins&gt;<br>  &lt;/build&gt;<br>&lt;/project&gt;</pre><p>Now you should be able to run the application locally:</p><ul><li>Go to the browser and call the function name with and input strings:</li><li><a href="http://localhost:8080/isPalindrome/deified">http://localhost:8080/isPalindrome/deified</a></li><li>Then you will get the result (true or false) indicating whether the given string is a palindrome.</li></ul><p>Now, we need to build the project. Under the target folder, you will find lambda-snapstart-0.0.1-SNAPSHOT-aws.jar, which is AWS-specific. We will use this JAR file later for the upload process.</p><p><strong>In the AWS console, create a function as follows:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s8G54g_fb0AXOwUI3QDlgw.png" /></figure><p>Then upload the artifact:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Yqpkwoh_mCPEbH2uBKeZg.png" /></figure><p>In Edit runtime settings, add this handler:</p><p>org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxVYpZ5rC4zKeD4WMfl9iA.png" /></figure><p>Going to test it with SnapStart enabled first, later on we will enable SnapStart and compare them.</p><p><strong>Test without SnapStart:</strong></p><p>Provide an input example: “level”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wRFnPHNGjIESI6JhSvFVKA.png" /></figure><p>As you can see, it took more than 3.5 seconds.</p><h3><strong>Now Activating SnapStart</strong></h3><p>As of this writing, SnapStart can be configured on any Lambda with Java 21, Java 17 and Java 11 managed runtimes. SnapStart can be activated on new and existing functions using the AWS Console, AWS CLI or AWS Lambda API.</p><blockquote>An important note is when activating Lambda SnapStart is that, once enabled, you must publish a new Lambda version before invoking and leveraging SnapStart benefits. SnapStart can be used only on a published Lambda function version or alias that points to a version, as opposed to the $latest version.</blockquote><blockquote>To benefit from Lambda SnapStart, the services invoking the Lambda function must reference the specific version of the Lambda function where SnapStart is enabled.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l-x-W_-hJPT4D6b48ONDlA.png" /></figure><p>Then publish a new version:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PwfhZzMboNPz4oBjFFVQag.png" /></figure><p>Be patient, It will takes some minutes to be created, then test it with an input example: “level”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0YKNI6S79VM73KJiZmQbHw.png" /></figure><p>As you can see it has only restore duration which is less that a second with not init duration.</p><p><strong>You can find the source code here: </strong><a href="https://github.com/minarashidi/lambda-snapstart">https://github.com/minarashidi/lambda-snapstart</a></p><p>I hope this post has helped you gain a better understanding of AWS Lambda, Lambda SnapStart, and how to effectively use them.</p><p>I’m eager to hear your thoughts and comments. Feel free to share your insights or ask any questions you may have.</p><p>Thank you for reading!</p><ul><li><a href="https://docs.aws.amazon.com/lambda/latest/dg/welcome.html">What is AWS Lambda?</a></li><li><a href="https://docs.spring.io/spring-cloud-function/reference/adapters/aws-intro.html">AWS Lambda</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36d61a3d6c18" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/exploring-aws-lambda-and-snapstart-36d61a3d6c18">Exploring AWS Lambda and SnapStart</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authentication and Authorization with Spring Security]]></title>
            <link>https://medium.com/@minadev/authentication-and-authorization-with-spring-security-bf22e985f2cb?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf22e985f2cb</guid>
            <category><![CDATA[spring-security]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Tue, 19 Dec 2023 21:40:57 GMT</pubDate>
            <atom:updated>2024-01-20T20:06:14.464Z</atom:updated>
            <content:encoded><![CDATA[<p>I will explain various authentication and authorization mechanisms, highlighting their pros and cons. Additionally, I’ll explore JWT and Spring Security. In the end, I will guide you through a detailed example implementation of using JSON Web Tokens (JWT) in a Spring Boot 3.2 application with Spring Security 6.2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*84_-wKZd2il6RSGeVStfVQ.png" /></figure><h3>Authentication Mechanisms:</h3><p>We have two authentication mechanisms: stateful and stateless.</p><p><strong>Stateful Authentication (Cookie/Session Based Authentication):</strong></p><p>This is the default and traditional method for handling user authentication. In this approach, the backend is responsible for creating, storing the session ID, and verifying the user’s identity.</p><p>Here is how it works: The server creates a session ID upon a user’s login request, storing it in either a database or an in-memory cache on the server. This session ID is then stored on a cookie in the user’s browser. With each subsequent request, the server receives the cookie containing the session ID and validates the user’s identity by comparing it with the corresponding session information stored on the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OMPhyKgYaOXaK09FhF7T6Q.png" /></figure><p><strong><em>Stateful disadvantages:</em></strong></p><p><strong>Scalability</strong>: This approach might have challenges in highly scalable systems, as it requires server-side storage for session data.</p><p><strong>Complexity</strong>: Implementing and managing session data, can add complexity to the system.</p><h3>Stateless Authentication:</h3><p>Stateless authentication using tokens (e.g., JWT) that are gaining popularity, especially in modern Microservices and distributed systems.</p><p><strong>Token-Based Authentication:</strong><br> Issuing a token (e.g., JWT) upon successful authentication.<br>The token is sent to the server with each request for authorization.<br>It is Stateless and scalable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oNKiEL61XIwLgBcqwSHeiA.png" /></figure><h3>JSON Web Token (JWT)</h3><p>JWT represents claims between two parties. JWT has a compact format, making them easy to transform over the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*A5s50vnA-3HXEky67qHwPA.png" /></figure><p>It consists of three parts:</p><ul><li><strong>Header</strong>: Specifies the JWT encoding and signing algorithm, with properties { “alg”: “HS256”, “typ”: “JWT” } where alg is the algorithm that is used to encrypt the JWT.</li><li><strong>Payload</strong>: Contains the data(Claims) to be sent as JSON property–value pairs within the claims.</li><li><strong>Signature</strong>: This is created by encrypting, with the algorithm specified in the header: (i) the base64Url-encoded header, (ii) base64Url-encoded payload, and (iii) a secret (or a private key):</li></ul><pre>HMACSHA256(<br>  base64UrlEncode(header) + &quot;.&quot; + <br>  base64UrlEncode(payload), <br>  secret|privateKey)</pre><p>Using HMAC SHA256 algorithm, the signature will be created in the above way and separated by dots. For more details, refer to the jwt.io website.</p><h3><em>Where and why should we use JWT?</em></h3><p><strong>Authentication</strong>: JWTs can be used for user authentication. Once a user logs in, a JWT is generated on the server and sent to the client. The client includes this token in the header of subsequent requests to authenticate the user.</p><p><strong>Authorization</strong>: JWTs can also be used to convey information about the user’s permissions and roles. The server can include this information in the payload of the JWT, and the client can use it for authorization purposes.</p><p><strong>Stateless</strong>: JWTs are often used in <strong>stateless authentication mechanisms</strong>. The server does not need to store the user’s session data, making it scalable, efficient and less complex.</p><p><strong>Expiration</strong>: JWTs can have an expiration time, after which they are no longer considered valid. This helps enhance security by limiting the window of opportunity for an attacker to use a stolen token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*b4FC7XFxPZgSUys4rx9cSw.png" /></figure><h3>Spring Security</h3><p>Spring Security is a powerful framework that focuses on providing both authentication and authorization to Java applications, also addressing common security vulnerabilities like CSRF (cross-site request forgery) and CORS (Cross-origin resource sharing).</p><p>Whenever we expose REST endpoints in Spring Boot, all the incoming requests are first received by the DispatcherServlet. The DispatcherServlet is the front and responsible to dispatch incoming HttpRequests to the correct handlers.</p><p>Adding spring security, enables us with the security filter chain to process requests and perform security-related tasks. We can customize this filter chain by adding or modifying filters based on our requirements.</p><p>This ensures that any incoming request will go through these filters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vURtu8XnUCsqnwiOw_s0Ag.png" /></figure><p>Here is a list of filters in the filter chain:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G5zKXrwAgaUinsDv_i-JOg.png" /></figure><p>The order is important, because they are dependent of each other.</p><p>We can also add a custom filter between two other filters in the chain.</p><p>Here is the diagram of the flow with step-by-step explanation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RrRf3-3clS1azO_gS2XKBQ.png" /></figure><ul><li><strong>Authentication : </strong>Filters like UsernamePasswordAuthenticationFilter handle the extraction of username/password from the HTTP request and create an authentication token.</li><li><strong>AuthenticationManager : </strong>Receives requests from filters and delegates the validation of user details to the available authentication providers. It manages one or multiple authentication providers.</li><li><strong>AuthenticationProvider : </strong>Is responsible for validating user details.</li><li><strong>UserDetailsManager/UserDetailsService : </strong>loads user details and match the user with the provided input.</li></ul><p>I will now create a Spring Boot application to handle user registration and authentication via REST APIs using JWT and Spring Security, providing a detailed step-by-step explanation.</p><p>I will expose Restful APIs for user registration and login. Client can make a request with their credentials (email + password) and when they are successfully authenticated the service will return a JWT.</p><p>Here you can find the source code: <a href="https://github.com/minarashidi/authentication">https://github.com/minarashidi/authentication</a></p><h3>Project setup</h3><p><em>Start by creating a new Spring Boot project using Spring Initializr, selecting the following dependencies. Import the project into your preferred IDE.</em></p><p>Here is the pom.xml file:</p><pre>    &lt;!-- Webmvc/Security --&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br><br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br><br>    &lt;!-- DB --&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br><br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.postgresql&lt;/groupId&gt;<br>      &lt;artifactId&gt;postgresql&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br><br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.liquibase&lt;/groupId&gt;<br>      &lt;artifactId&gt;liquibase-core&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br><br>    &lt;!-- JWT --&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>      &lt;artifactId&gt;jjwt-api&lt;/artifactId&gt;<br>      &lt;version&gt;${jwt.version}&lt;/version&gt;<br>    &lt;/dependency&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>      &lt;artifactId&gt;jjwt-impl&lt;/artifactId&gt;<br>      &lt;version&gt;${jwt.version}&lt;/version&gt;<br>      &lt;scope&gt;runtime&lt;/scope&gt;<br>    &lt;/dependency&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>      &lt;artifactId&gt;jjwt-jackson&lt;/artifactId&gt;<br>      &lt;version&gt;${jwt.version}&lt;/version&gt;<br>      &lt;scope&gt;runtime&lt;/scope&gt;<br>    &lt;/dependency&gt;</pre><ul><li>Spring web(<em>spring-boot-starter-web</em>) for building Restful APIs</li><li>Spring Security for handling user authentication and authorization</li><li>JWT to use token-based authentication</li><li>Spring JDBC to interact with database</li><li>PostgreSQL Driver, we are going to be using the Postgres database.</li></ul><h3><strong>Signup API</strong></h3><p>We need to expose a signup endpoint to register user, first I create a signup request class:</p><pre><br>// Dto object with basic validation<br>public record SignupRequest(<br>    @NotBlank(message = &quot;Name cannot be blank&quot;)<br>    String name,<br><br>    @Email(message = &quot;Invalid email format&quot;)<br>    @NotBlank(message = &quot;Email cannot be blank&quot;)<br>    String email,<br><br>    @NotBlank(message = &quot;Password cannot be blank&quot;)<br>    @Size(min = 6, max = 20, message = &quot;Password must be between 6 and 20 characters&quot;)<br>    String password) {<br><br>}</pre><p>I added basic validation on login request properties using spring-boot-starter-validation dependency.</p><p>By adding spring-boot-starter-validation dependency, we do not need to do any extra configuration and it will automatically add the Hibernate Validator dependency to your pom.xml file and configure the validation for you.</p><p><em>A quick note: hibernate-validator is entirely separate from the persistence aspects of Hibernate. So, by adding it as a dependency, we’re not adding these persistence aspects into the project.</em></p><p>Here is the controller:</p><pre>@PostMapping(&quot;/signup&quot;)<br>  public ResponseEntity&lt;Void&gt; signup(@Valid @RequestBody SignupRequest requestDto) {<br>    userService.signup(requestDto);<br>    return ResponseEntity.status(HttpStatus.CREATED).build();<br>  }</pre><p>In the service, we first need to check if the user does not exist, then encrypt the password before saving it to the database.</p><pre> package com.mina.authentication.service;<br><br>import com.mina.authentication.controller.dto.SignupRequest;<br>import com.mina.authentication.domain.User;<br>import com.mina.authentication.exceptions.DuplicateException;<br>import com.mina.authentication.repository.UserRepository;<br>import java.util.Optional;<br>import org.springframework.security.crypto.password.PasswordEncoder;<br>import org.springframework.stereotype.Service;<br>import org.springframework.transaction.annotation.Transactional;<br><br>@Service<br>@Transactional(readOnly = true)<br>public class UserService {<br><br>  private final UserRepository repository;<br>  private final PasswordEncoder passwordEncoder;<br><br>  public UserService(UserRepository repository, PasswordEncoder passwordEncoder) {<br>    this.repository = repository;<br>    this.passwordEncoder = passwordEncoder;<br>  }<br><br>  @Transactional<br>  public void signup(SignupRequest request) {<br>    String email = request.email();<br>    Optional&lt;User&gt; existingUser = repository.findByEmail(email);<br>    if (existingUser.isPresent()) {<br>      throw new DuplicateException(String.format(&quot;User with the email address &#39;%s&#39; already exists.&quot;, email));<br>    }<br><br>    String hashedPassword = passwordEncoder.encode(request.password());<br>    User user = new User(request.name(), email, hashedPassword);<br>    repository.add(user);<br>  }<br><br>}</pre><p>And in the repository, I use JDBC Client in Spring Boot 3.2 which is Fluent API to store user in DB.</p><pre>@Repository<br>public class UserRepository {<br><br>  private static final String INSERT = &quot;INSERT INTO authentication.user (name, email, password) VALUES(:name, :email, :password)&quot;;<br>  private static final String FIND_BY_EMAIL = &quot;SELECT * FROM authentication.user WHERE email = :email&quot;;<br><br>  private final JdbcClient jdbcClient;<br><br>  public UserRepository(JdbcClient jdbcClient) {<br>    this.jdbcClient = jdbcClient;<br>  }<br><br>  public void add(User user) {<br>    long affected = jdbcClient.sql(INSERT)<br>        .param(&quot;name&quot;, user.name())<br>        .param(&quot;email&quot;, user.email())<br>        .param(&quot;password&quot;, user.password())<br>        .update();<br><br>    Assert.isTrue(affected == 1, &quot;Could not add user.&quot;);<br>  }<br><br>  public Optional&lt;User&gt; findByEmail(String email) {<br>    return jdbcClient.sql(FIND_BY_EMAIL)<br>        .param(&quot;email&quot;, email)<br>        .query(User.class)<br>        .optional();<br>  }<br>}</pre><h3>Login API</h3><p>Now the login API which takes the email and password as login request and returns JWT token as a response.</p><p>In the controller:</p><pre>@PostMapping(value = &quot;/login&quot;)<br>  public ResponseEntity&lt;LoginResponse&gt; login(@Valid @RequestBody LoginRequest request) {<br>    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.email(), request.password()));<br>    String token = JwtHelper.generateToken(request.email());<br>    return ResponseEntity.ok(new LoginResponse(request.email(), token));<br>  }</pre><p>We also need to have a helper class to generate and resolve JWT token.</p><pre>package com.mina.authentication.helper;<br><br>import com.mina.authentication.exceptions.AccessDeniedException;<br>import io.jsonwebtoken.Claims;<br>import io.jsonwebtoken.ExpiredJwtException;<br>import io.jsonwebtoken.Jwts;<br>import io.jsonwebtoken.SignatureAlgorithm;<br>import io.jsonwebtoken.security.Keys;<br>import io.jsonwebtoken.security.SignatureException;<br>import java.security.Key;<br>import java.time.Instant;<br>import java.time.temporal.ChronoUnit;<br>import java.util.Date;<br>import org.springframework.security.core.userdetails.UserDetails;<br><br>public class JwtHelper {<br><br>  private static final Key SECRET_KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256);<br>  private static final int MINUTES = 60;<br><br>  public static String generateToken(String email) {<br>    var now = Instant.now();<br>    return Jwts.builder()<br>        .subject(email)<br>        .issuedAt(Date.from(now))<br>        .expiration(Date.from(now.plus(MINUTES, ChronoUnit.MINUTES)))<br>        .signWith(SignatureAlgorithm.HS256, SECRET_KEY)<br>        .compact();<br>  }<br><br>  public static String extractUsername(String token) {<br>    return getTokenBody(token).getSubject();<br>  }<br><br>  public static Boolean validateToken(String token, UserDetails userDetails) {<br>    final String username = extractUsername(token);<br>    return username.equals(userDetails.getUsername()) &amp;&amp; !isTokenExpired(token);<br>  }<br><br>  private static Claims getTokenBody(String token) {<br>    try {<br>      return Jwts<br>          .parser()<br>          .setSigningKey(SECRET_KEY)<br>          .build()<br>          .parseSignedClaims(token)<br>          .getPayload();<br>    } catch (SignatureException | ExpiredJwtException e) { // Invalid signature or expired token<br>      throw new AccessDeniedException(&quot;Access denied: &quot; + e.getMessage());<br>    }<br>  }<br><br>  private static boolean isTokenExpired(String token) {<br>    Claims claims = getTokenBody(token);<br>    return claims.getExpiration().before(new Date());<br>  }<br>}</pre><p><em>generateToken(String email) : we need to create a </em><strong><em>Claims</em></strong><em> object from user data and build a JWT token. The </em><strong><em>Claims </em></strong><em>object is used as the jwt payload.</em></p><h3>UserDetailsService &amp; UserRepository</h3><p>Spring security uses an interface called <strong>UserDetailsService</strong> to load user details and match the user with the input.</p><p>We create a custom user details service<strong> </strong>which<strong> </strong>implements <strong>UserDetailsService</strong> and override the <strong>loadUserByUsername()</strong> method, which returns an UserDetails object.</p><pre>@Service<br>public class UserDetailsServiceImpl implements UserDetailsService {<br><br>  private final UserRepository repository;<br><br>  public UserDetailsServiceImpl(UserRepository repository) {<br>    this.repository = repository;<br>  }<br><br>  @Override<br>  public UserDetails loadUserByUsername(String email) {<br><br>    User user = repository.findByEmail(email).orElseThrow(() -&gt;<br>        new NotFoundException(String.format(&quot;User does not exist, email: %s&quot;, email)));<br><br>    return org.springframework.security.core.userdetails.User.builder()<br>        .username(user.email())<br>        .password(user.password())<br>        .build();<br>  }<br>}</pre><p>Spring security will internally call this method with user provided email, and then match the password from <strong>UserDetails</strong> object with user provided password.</p><h3><strong>Adding Spring Security Config</strong></h3><p>We need to create a security config class to define permissions and annotate it with <strong>@EnableWebSecurity @Configuration </strong>These annotations tells spring security to use our custom security configuration instead of default.</p><p>We need to create a Bean of <strong>SecurityFilterChain</strong> which implements our custom filter logic.</p><pre>package com.mojang.authentication.config;<br><br>import com.mojang.authentication.service.UserDetailsServiceImpl;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br>import org.springframework.http.HttpMethod;<br>import org.springframework.security.authentication.AuthenticationManager;<br>import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;<br>import org.springframework.security.config.annotation.web.builders.HttpSecurity;<br>import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;<br>import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;<br>import org.springframework.security.config.http.SessionCreationPolicy;<br>import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;<br>import org.springframework.security.crypto.password.PasswordEncoder;<br>import org.springframework.security.web.SecurityFilterChain;<br>import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;<br><br>@Configuration<br>@EnableWebSecurity<br>public class SecurityConfig {<br><br>  private final UserDetailsServiceImpl userDetailsService;<br><br>  public SecurityConfig(UserDetailsServiceImpl userDetailsService) {<br>    this.userDetailsService = userDetailsService;<br>  }<br><br>  @Bean<br>  public PasswordEncoder passwordEncoder() {<br>    return new BCryptPasswordEncoder();<br>  }<br><br>  @Bean<br>  public SecurityFilterChain filterChain(HttpSecurity http, AuthenticationManager authenticationManager) throws Exception {<br>    return http<br>        .cors(AbstractHttpConfigurer::disable)<br>        .csrf(AbstractHttpConfigurer::disable)<br>        .sessionManagement(session -&gt; session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))<br>//        Set permissions on endpoints<br>        .authorizeHttpRequests(auth -&gt; auth<br>//            our public endpoints<br>            .requestMatchers(HttpMethod.POST, &quot;/api/auth/signup/**&quot;).permitAll()<br>            .requestMatchers(HttpMethod.POST, &quot;/api/auth/login/**&quot;).permitAll()<br>            .requestMatchers(HttpMethod.GET, &quot;/authentication-docs/**&quot;).permitAll()<br>//            our private endpoints<br>            .anyRequest().authenticated())<br>        .authenticationManager(authenticationManager)<br>        .build();<br>  }<br><br>  @Bean<br>  public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {<br>    AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);<br>    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());<br>    return authenticationManagerBuilder.build();<br>  }<br>}<br><br>// CORS(Cross-origin resource sharing) is just to avoid if you run javascript across different domains like if you execute JS on http://testpage.com and access http://anotherpage.com<br>// CSRF(Cross-Site Request Forgery)</pre><p>SessionCreationPolicy.STATELESS is used to specify that Spring Security does not create or access a session.</p><p>/api/auth/signup/** This line tells spring security to permit all request without any authentication that starts with this url.</p><p>I use Bcrypt to hash passwords. In practice, we don’t store plain text passwords into database.</p><pre>@Bean<br>  public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {<br>    AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);<br>    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());<br>    return authenticationManagerBuilder.build();<br>  }</pre><p>We need to create a Bean of <strong>AuthenticationManager</strong> which will be used to authenticate the user. We passed our <strong>CustomUserDetailsService</strong> object &amp; password encoding object to <strong>AuthenticationManager.</strong></p><p>If you need to ckeck if the user is authenticated:</p><pre>authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.email(), request.password()));</pre><p>This authenticates the user with email &amp; password. The <strong>authenticationManager.authenticate()</strong> method will internally call <strong>loadUserByUsername()</strong> method from our<strong> CustomUserDetailsService</strong> class. Then it will match the password from <strong>userDetailsService</strong> with the password found from <strong>LoginRequest</strong>. This method will throw exception if the authentication is not successful.</p><p>Now build &amp; run the application. Send a POST request from Postman to /api/auth/signup api.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DH-Xu2FNIP7E4UKsFybLAA.png" /></figure><p>Then login:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kRWAxJDu0EkTxcII2djb7w.png" /></figure><p>Now we have successfully created a JWT token and sent back to user. Now if we want to access any other resource, we will get a <strong>403 Forbidden </strong>error which means the request is not authenticated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AgrDrhoQZi8cMLakTh5D8Q.png" /></figure><h3>Adding JwtAuthorizationFilter</h3><p>We need to add a JWT authorization filter for each request. This filter will block all requests that don’t have JWT token in the request header.</p><pre>private final JwtAuthFilter jwtAuthFilter;<br><br>  public SecurityConfig(UserDetailsServiceImpl userDetailsService, JwtAuthFilter jwtAuthFilter) {<br>    this.userDetailsService = userDetailsService;<br>    this.jwtAuthFilter = jwtAuthFilter;<br>  }<br><br>@Bean<br>  public SecurityFilterChain filterChain(HttpSecurity http, AuthenticationManager authenticationManager) throws Exception {<br>    return http<br>        .cors(AbstractHttpConfigurer::disable)<br>        .csrf(AbstractHttpConfigurer::disable)<br>        .sessionManagement(session -&gt; session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))<br>        .authorizeHttpRequests(auth -&gt; auth<br>            .requestMatchers(HttpMethod.POST, &quot;/api/auth/signup/**&quot;).permitAll()<br>            .requestMatchers(HttpMethod.POST, &quot;/api/auth/login/**&quot;).permitAll()<br>            .requestMatchers(HttpMethod.GET, &quot;/authentication-docs/**&quot;).permitAll()<br>            .anyRequest().authenticated())<br>        .authenticationManager(authenticationManager)<br><br>//        Add JWT token filter<br>        .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class)<br>        .build();<br>  }</pre><p>Please note that we added the JwtTokenFilter before the Spring Security internal UsernamePasswordAuthenticationFilter. We’re doing this because we need access to the user identity at this point to perform authentication/authorization.</p><p><strong>UsernamePasswordAuthenticationFilter</strong>:<em> Tries to find a username/password request parameter from POST login API and if found, create a UsernamePasswordAuthenticationToken from a username and password then tries to authenticate the user.</em></p><p>To implement JwtAuthFilter we need to extend OncePerRequestFilter since we need every request to be authenticated before going through spring security filter.</p><pre>package com.mina.authentication.config;<br>import com.fasterxml.jackson.databind.ObjectMapper;<br>import com.mojang.authentication.controller.dto.ApiErrorResponse;<br>import com.mojang.authentication.exceptions.AccessDeniedException;<br>import com.mojang.authentication.helper.JwtHelper;<br>import com.mojang.authentication.service.UserDetailsServiceImpl;<br>import jakarta.servlet.FilterChain;<br>import jakarta.servlet.ServletException;<br>import jakarta.servlet.http.HttpServletRequest;<br>import jakarta.servlet.http.HttpServletResponse;<br>import java.io.IOException;<br>import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;<br>import org.springframework.security.core.context.SecurityContextHolder;<br>import org.springframework.security.core.userdetails.UserDetails;<br>import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;<br>import org.springframework.stereotype.Component;<br>import org.springframework.web.filter.OncePerRequestFilter;<br><br>@Component<br>public class JwtAuthFilter extends OncePerRequestFilter {<br><br>  private final UserDetailsServiceImpl userDetailsService;<br>  private final ObjectMapper objectMapper;<br><br>  public JwtAuthFilter(UserDetailsServiceImpl userDetailsService, ObjectMapper objectMapper) {<br>    this.userDetailsService = userDetailsService;<br>    this.objectMapper = objectMapper;<br>  }<br><br>  @Override<br>  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)<br>      throws ServletException, IOException {<br>    try {<br>      String authHeader = request.getHeader(&quot;Authorization&quot;);<br>      String token = null;<br>      String username = null;<br>      if (authHeader != null &amp;&amp; authHeader.startsWith(&quot;Bearer &quot;)) {<br>        token = authHeader.substring(7);<br>        username = JwtHelper.extractUsername(token);<br>      }<br>//      If the accessToken is null. It will pass the request to next filter in the chain.<br>//      Any login and signup requests will not have jwt token in their header, therefore they will be passed to next filter chain.<br>      if (token == null) {<br>        filterChain.doFilter(request, response);<br>        return;<br>      }<br>//       If any accessToken is present, then it will validate the token and then authenticate the request in security context<br>      if (username != null &amp;&amp; SecurityContextHolder.getContext().getAuthentication() == null) {<br>        UserDetails userDetails = userDetailsService.loadUserByUsername(username);<br>        if (JwtHelper.validateToken(token, userDetails)) {<br>          UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, null);<br>          authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));<br>          SecurityContextHolder.getContext().setAuthentication(authenticationToken);<br>        }<br>      }<br><br>      filterChain.doFilter(request, response);<br>    } catch (AccessDeniedException e) {<br>      ApiErrorResponse errorResponse = new ApiErrorResponse(HttpServletResponse.SC_FORBIDDEN, e.getMessage());<br>      response.setStatus(HttpServletResponse.SC_FORBIDDEN);<br>      response.getWriter().write(toJson(errorResponse));<br>    }<br>  }<br><br>  private String toJson(ApiErrorResponse response) {<br>    try {<br>      return objectMapper.writeValueAsString(response);<br>    } catch (Exception e) {<br>      return &quot;&quot;; // Return an empty string if serialization fails<br>    }<br>  }<br>}</pre><p>Now to access resources, include an Authorization header</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaBUwujV1EFdQ7BsBOKBLg.png" /></figure><p><strong>You can find the source code here: </strong><a href="https://github.com/minarashidi/authentication">https://github.com/minarashidi/authentication</a></p><p>I hope this post helped you to gain better understanding of the different authentication mechanisms and how to work with spring security.</p><p>I’m eager to hear your thoughts and comments. Feel free to share your insights or ask any questions you may have.</p><p>Thank you for reading!</p><ul><li><a href="https://docs.spring.io/spring-security/reference/">Spring Security</a></li><li><a href="https://jwt.io/introduction">JSON Web Token Introduction - jwt.io</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf22e985f2cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Structure Code]]></title>
            <link>https://medium.com/@minadev/how-to-structure-our-code-b5e7b1c32c21?source=rss-d69531287448------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5e7b1c32c21</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[architectural-design]]></category>
            <dc:creator><![CDATA[Mina]]></dc:creator>
            <pubDate>Sat, 02 Dec 2023 21:59:19 GMT</pubDate>
            <atom:updated>2023-12-03T18:37:55.336Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Software design best practices, discovering package by layer, package by feature, and hexagonal architecture/ports and adapters.</em></p><p>In this blog post, I will explore how to structure our code and discuss best practices, covering three different approaches: package by layer, package by feature, and hexagonal architecture/ports and adapters with their pros and cons.</p><p>Before exploring different approaches for structuring code, we need to understand fundamental software design principles:</p><ul><li><strong>Cohesion</strong>: Refers to the degree to which classes within a module are related to one another.</li><li><strong>Coupling</strong>: Refers to the degree of dependence between different modules.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*2Q8ye2K8hYRtNOMHWxoOBQ.png" /></figure><ul><li><strong>Modularity</strong>: Refers to the extent to which a software system is divided into separate and independent modules. Each module encapsulates a specific set of functionalities, and designed to work independently while interacting with each other through well-defined interfaces.</li><li><strong>Abstraction</strong>: Hiding implementation details and only exposing essential functionalities through interfaces.</li><li><strong>Separation of Concerns: </strong>Having distinct sections, each addressing a specific concern.</li><li><strong>Encapsulation: </strong>Bundle data and methods into a single module or class to hide internal details.</li></ul><p><strong>Let’s have a closer look at cohesion and coupling?</strong></p><p><em>Cohesion describes how </em><strong><em>focused</em></strong><em> a piece of software is. It is very related to the Single Responsibility Principle.</em></p><ul><li><strong>High cohesion</strong> means that classes within a module are closely related and share a common, well-defined purpose.</li><li><strong>Low cohesion</strong> means classes within a module are loosely related, lack a clear purpose with unrelated responsibilities.</li></ul><p>A best practice to follow is to aim for <strong>high cohesion and loose coupling</strong> between modules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6DpCfIP-T09sZBfxu8G2DA.png" /></figure><p>Loose coupling is considered to be a sign of a well-structured computer system and a good design, and when combined with high cohesion, it results in high readability and maintainability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*WVS54T-5rqEvL7xf1AeADA.png" /></figure><p>Now, let’s explore different ways of structuring our code. First, I’ll cover package by layer, followed by package by feature, comparing the two. Afterwards, we’ll explore the port and adapter pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fmBZBkeP4RAmM9DwFoynyQ.png" /></figure><h3>Package by Layer</h3><p>It represents a project structure where classes are organized into multiple layers, each responsible for a specific set of functionalities.</p><pre>src<br>├── main<br>│   ├── java<br>│   │   └── com<br>│   │       └── app<br>│   │           ├── service <br>│   │           │   └── UserService.java<br>│   │           │   └── OrderService.java<br>│   │           │   └── ProductService.java<br>│   │           ├── domain   <br>│   │           │   └── User.java<br>│   │           │   └── Order.java<br>│   │           │   └── Product.java <br>│   │           ├── repository   <br>│   │           │   └── UserRepository.java<br>│   │           │   └── OrderRepository.java<br>│   │           │   └── ProductRepository.java<br>│   │           ├── controller     <br>│   │           │   └── UserController.java<br>│   │           │   └── OrderController.java<br>│   │           │   └── ProductController.java</pre><p>The typical layers include:</p><ol><li><strong>Presentation Layer:</strong> This layer is responsible for handling user interactions and presenting information to the users. It often includes components related to user interfaces, controllers, and views.</li><li><strong>Service Layer:</strong> This layer contains the business logic and provides the data needed by presentation layer.</li><li><strong>Domain package: </strong>This package contains domain entities.</li><li><strong>Data Access Layer: </strong>This layer deals with the persistence and retrieval of data to/from database.</li><li><strong>Infrastructure package:</strong> This package provides services that support the operation of the application. It may include components for logging, configuration, security, and other cross-cutting concerns.</li></ol><p><em>Here are some disadvantages of using </em><strong><em>Package by Layer</em></strong><em>:</em></p><ul><li>Low cohesion: unrelated classes are grouped into the same package.</li><li>High coupling</li><li>Poor Encapsulation: Most classes are public so we cannot make classes as package private, since they are needed in other layers.</li><li>Low Modularity: Since each package contains classes related to a particular layer, it is difficult to break down the code into a Microservice later on.</li><li>Poor Maintainability: Since classes are scattered across packages, it is difficult to find the class you are looking for.</li><li>It promotes Database Driven Design rather than Domain Driven Design.</li></ul><h3><strong>Package By Feature</strong></h3><p>It represents a structure where code is organized based on features or functionalities rather than layers. In this approach, each package represents a distinct and independent feature.</p><p>The goal is to group together all the components (such as controllers, services, repositories and domain classes) related to a particular feature in a single package.</p><pre>src<br>├── main<br>│   ├── java<br>│   │   └── com<br>│   │       └── app<br>│   │           ├── user<br>│   │           │   ├── UserController.java<br>│   │           │   ├── UserService.java<br>│   │           │   └── UserRepository.java<br>│   │           ├── order<br>│   │           │   ├── OrderController.java<br>│   │           │   ├── OrderService.java<br>│   │           │   └── OrderRepository.java<br>│   │           ├── product<br>│   │           │   ├── ProductController.java<br>│   │           │   ├── ProductService.java<br>│   │           │   └── ProductRepository.java</pre><p>Some of the benefits of using this structure:</p><ul><li>High cohesion</li><li>Low coupling</li><li>Strong encapsulation: Allows some classes to set their access modifier to package-private instead of public.</li><li>High Modularity: Since each package contains classes related to a particular feature, it is easy to break code down into a Microservice later on.</li><li>Maintainability: Reduces the need to navigate between packages since all classes needed for a feature are in the same package.</li><li>Promotes Domain Driven Design</li></ul><h3>Ports and Adapters Pattern (Hexagonal Architecture)</h3><p>Hexagonal Architecture also known as Ports and Adapters is a software architectural pattern introduced by Dr. Alistair Cockburn in an article he wrote in 2005.</p><p><a href="https://alistair.cockburn.us/hexagonal-architecture/">Hexagonal architecture</a></p><p>The pattern promotes the isolation/separation of concerns by keeping the core business logic independent of external details and not tightly coupled to external dependencies such as databases, user interfaces, or external services.</p><p>This makes it easier to test, maintain, and evolve the system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*wF_VJcN5dpwCQB_sx1z2_w.png" /></figure><p>In this pattern:</p><ol><li><strong>Domain / Core:</strong><br>Represents the application’s business logic or domain(the heart of the application).</li><li><strong>Ports:<br></strong>Ports are interfaces defined by the core that allow interaction with external components. These can include interfaces for services, repositories, or any external dependency.</li><li><strong>Adapters:<br></strong>Adapters are implementations of the ports. They connect the core application to external components such as databases, user interfaces, and external services. Adapters can be specific to different technologies or protocols.</li><li><strong>Primary actors:<br></strong>Users of the system such as a webhook, a UI request, or a test script.</li><li><strong>Secondary actors:<br></strong>Used by the application, these services are either a <em>Repository</em> (for example, a database) or a <em>Recipient</em> (such as a message queue).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*AUdml5HCo92xf8WgHkZfRA.gif" /></figure><p><strong>Hexagonal Shape: <br></strong>The hexagonal shape symbolizes the idea that the core application is at the center, surrounded by adapters. This shape represents the clear separation between the core and its external dependencies.</p><p>The top level package structure should look like this:</p><pre>src/main/<br>  java<br>    mina<br>      dev<br>        &lt;servicename&gt;<br>          adapters<br>          config<br>          core<br>          &lt;ServiceApplication&gt;.java</pre><p>The root package should only contain packages: core, adapters and config.</p><ul><li>The core package contains all the domain logic of the service. It may contain sub packages.</li><li>Ports should be located in the core package: The ports are just interfaces declared by the core to be called or implemented by the adapters.</li><li>The adapters package contains all adapter implementation code. It may contain sub packages to organize adapter code either per individual adapter or by technology.</li><li>The config package contains configuration classes used to connect the different components together.</li></ul><p><strong>Package dependency rules:</strong></p><ul><li>The root package may depend on all other packages.</li><li>The config package may depend on core and adapters.</li><li>The adapters may depend on core but not on config.</li><li>Core may not depend on any of the other packages.</li></ul><p>You can find more details and example in my github repository: <a href="https://github.com/minarashidi/transfer-service-hexagonal-architecture/tree/main/src/main/java/com/rashidi/transferservice">https://github.com/minarashidi/transfer-service-hexagonal-architecture/tree/main/src/main/java/com/rashidi/transferservice</a></p><p>I hope this post helped you to gain better understanding of the different code structures.</p><p>I’m eager to hear your thoughts and comments. Feel free to share your insights or ask any questions you may have. Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5e7b1c32c21" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>