Unleashing the Power of Quarkus: Building a Lightning-Fast eCommerce API from Scratch

Abhishek Ranjan
Javarevisited
Published in
4 min readMay 26, 2023

Hello there, Today, we’re going to learn how to build a basic eCommerce API using Quarkus. If you’re new to Quarkus, it’s a Kubernetes-native Java stack that’s tailor-made for Java virtual machines (JVMs) and native compilation. It’s super fast, and you’re going to love it!

To explain our architecture, we’ll use a tool called Mermaid, which helps us create beautiful diagrams using simple text definitions.

Why Quarkus ?

Main Advantages of Quarkus

  1. Kubernetes-Native: Quarkus has been designed with containerization and orchestration in mind. It’s lightweight and handles port adjustments easily, which makes it perfect for Kubernetes and other cloud-based environments.
  2. Superfast Startup Times and Lower Runtime: With Quarkus, you get lightning-fast startup times and lower runtime memory consumption, which is essential for functions-as-a-service and microservices architectures where services need to start and stop quickly.
  3. Unified Configuration: All configurations are consolidated into one file, which eliminates the confusion of multiple configurations and makes it easier for developers.
  4. Live Coding: This is my personal favorite! Quarkus’ live coding feature, enabled by running your projects in the Quarkus Dev mode, increases developer productivity. You can make changes to your codebase and immediately see their impact without needing to manually restart your server.
  5. Imperative and Reactive: Quarkus unifies the imperative programming model with reactive principles, allowing you to choose the best tool for the task at hand without compromising on the architectural vision.
  6. Native and JVM Modes: Quarkus applications can be run in JVM mode and native mode. The native mode compiles the application into a native executable, which has a quicker startup time and lower runtime memory overhead, which can be a huge advantage for serverless applications and microservices.

Quarkus Performance Benchmarks

As for benchmarks, well, they can vary based on a multitude of factors, including the nature of the application, server configuration, etc. But to give you a rough idea, here are some numbers from a study done by RedHat:

  1. Startup Time: A simple REST application on Quarkus can start in less than 0.8 seconds, while a traditional cloud-native stack might take several seconds.
  2. Memory Consumption: The same application consumes 74 MB of heap, as opposed to 130 MB on a traditional cloud-native stack.
  3. Native Compilation: If we compile the application into a native image, the startup time goes down to an astounding 0.016 seconds, and it consumes just 13 MB of memory.

Remember, these numbers come from a specific use case and might not reflect the exact performance you get with your application.

But in general, Quarkus represents a significant step forward for enterprise Java. Its blend of ahead-of-time (AOT) and just-in-time (JIT) compilation, combined with its modern, flexible infrastructure, makes it an attractive option for microservices, serverless, and other cloud-native applications.

Preparation

Before we start, let’s make sure you have the necessary tools:

  1. JDK 11 or later
  2. Maven 3.6.2 or later
  3. Docker (for PostgreSQL)
  4. Your favourite IDE

Setting Up Quarkus Project

Firstly, we’ll create our project. Navigate to the directory where you want your project to reside and execute the following command:

mvn io.quarkus:quarkus-maven-plugin:2.7.1.Final:create \
-DprojectGroupId=org.example \
-DprojectArtifactId=ecommerce-api \
-DclassName="org.example.ProductResource" \
-Dpath="/products"

Great, now you have your Quarkus project set up!

Database Setup

Next, we’ll set up our PostgreSQL database with Docker:

docker run --name ecommerce-db -e POSTGRES_PASSWORD=password -e POSTGRES_DB=ecommerce -p 5432:5432 -d postgres

Creating Entity

Now let’s create the Product entity. In src/main/java/org/example, create a new class Product.java:

package org.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
@Entity
public class Product {
@Id
@GeneratedValue
private Long id;
@NotBlank
private String name;
@NotNull
private BigDecimal price;
// getters and setters
}

Creating Repository

Next, we’ll create a ProductRepository class:

package org.example;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class ProductRepository implements PanacheRepository<Product> {
}

Setting Up the REST API

Our REST API will allow users to create, read, update, and delete products.

package org.example;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ProductResource {
@Inject
ProductRepository productRepository;
@GET
public List<Product> getAll() {
return productRepository.listAll();
}
@POST
@Transactional
public Product create(Product product) {
productRepository.persist(product);
return product;
}
// similarly add methods for PUT and DELETE
}

Configuring DataSource

Before we start, we need to configure the data source. Open the application.properties file and add the following:

quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
quarkus.datasource.password=password
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/ecommerce
quarkus.hibernate-orm.database.generation=update

Diagram of Our Setup

Let’s illustrate the structure of our simple e-commerce application :

Conclusion

And there you have it! You’ve just created a simple e-commerce API with Quarkus. The API interacts with a PostgreSQL database to manage products. You can run the application using the ./mvnw compile quarkus:dev command.

Remember, this is a very basic implementation. In a real-world scenario, you’ll probably need user management, categories, product images, shopping carts, and payment system integration.

But this is a great starting point. As you’ve seen, Quarkus makes it super easy to get a RESTful API up and running. So go forth and build something amazing!

🔗 Connect with me on LinkedIn!

I hope you found this article helpful! If you’re interested in learning more and staying up-to-date with my latest insights and articles, don’t hesitate to connect with me on LinkedIn.

Let’s grow our networks, engage in meaningful discussions, and share our experiences in the world of software development and beyond. Looking forward to connecting with you! 😊

Follow me on LinkedIn ➡️

--

--