DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns
  • High-Performance Java Serialization to Different Formats
  • The First Annual Recap From JPA Buddy
  • Beginners Guide for Web Scraping Using Selenium

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  • Agile’s Quarter-Century Crisis
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  1. DZone
  2. Coding
  3. Languages
  4. A Look at the Java Distributed In-Memory Data Model (Powered by Redis)

A Look at the Java Distributed In-Memory Data Model (Powered by Redis)

This article details Redisson's LiveObject concept, which can help model data for distributed applications.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Oct. 14, 16 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
23.8K Views

Join the DZone community and get the full member experience.

Join For Free

There are a lot of frameworks (Coherence, Terracota, Redisson, GridGain, Hazelcast, Infinispan, etc.) nowadays that offer Java the ability to access distributed in-memory utility data types such as Map, Cache, etc.

In reality, we are often required to work directly with business data models of an application. The complexity is usually way beyond those utility data types' ability to comprehend. That's why, despite having all these distributed in-memory utility data types at hand, people are stilling using JPA to persist their business data.

While changes in the distributed in-memory data model should be reflected in real-time, it should be able to achieve that without the need to serialize and deserialize the whole object.

The LiveObject concept offered by Redisson has solved this stiff challenge. A detailed description of it was made in a previous article. Now, let's take a look at a practical application.

Here is a typical business data model diagram:

Image title

Below is the Java implementation of that data model.

Customer object:

@REntity
public static class Customer {
    @RId(generator = UUIDGenerator.class)
    private String id;
    private List<Order> orders;
    private String name;
    private String address;
    private String phone;
    protected Customer() {
    }

    public Customer(String id) {
        super();
        this.id = id;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public void addOrder(Order order) {
        orders.add(order);
    }

    public List<Order> getOrders() {
        return orders;
    }

    public String getId() {
        return id;
    }
}


Order object:

@REntity
public static class Order {

    @RId(generator = LongGenerator.class)
    private Long id;

    private List<OrderDetail> orderDetails; 

    private Customer customer;

    private Date date;

    private Date shippedDate;

    private String shipName;
    private String shipAddress;
    private String shipPostalCode;

    protected Order() {
    }

    public Order(Customer customer) {
        super();
        this.customer = customer;
    }

    public List<OrderDetail> getOrderDetails() {
        return orderDetails;
    }

    public Customer getCustomer() {
        return customer;
    }

    public Long getId() {
        return id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getShippedDate() {
        return shippedDate;
    }

    public void setShippedDate(Date shippedDate) {
        this.shippedDate = shippedDate;
    }

    public String getShipName() {
        return shipName;
    }

    public void setShipName(String shipName) {
        this.shipName = shipName;
    }

    public String getShipAddress() {
        return shipAddress;
    }

    public void setShipAddress(String shipAddress) {
        this.shipAddress = shipAddress;
    }

    public String getShipPostalCode() {
        return shipPostalCode;
    }

    public void setShipPostalCode(String shipPostalCode) {
        this.shipPostalCode = shipPostalCode;
    }

}


OrderDetail object:

@REntity
public static class OrderDetail {

    @RId(generator = LongGenerator.class)
    private Long id;

    private Order order;

    private Product product;

    private BigDecimal price;
    private Integer quantity;
    private BigDecimal discount;

    protected OrderDetail() {
    }

    public OrderDetail(Order order, Product product) {
        super();
        this.order = order;
        this.product = product;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    public Long getId() {
        return id;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Product getProduct() {
        return product;
    }

}


Product object:

@REntity
public static class Product {

    @RId
    private Long id;
    private String name;

    private Map<String, Integer> itemName2Amount;

    private BigDecimal price;
    private Integer unitsInStock;

    protected Product() {
    }

    public Product(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setUnitsInStock(Integer unitsInStock) {
        this.unitsInStock = unitsInStock;
    }

   public Integer getUnitsInStock() {
        return unitsInStock;
    }

    public String getName() {
        return name;
    }

    public void addItem(String name, Integer amount) {
        getItemName2Amount().put(name, amount);
    }

    public Integer getItemAmount(String name) {
        return getItemName2Amount().get(name);
    }

    Map<String, Integer> getItemName2Amount() {
        return itemName2Amount;
    }
}

Please notice that we encapsulated itemName2Amount field made its getter visibility as protected. 

Here is an example of work with data model described above:

        RLiveObjectService liveObjectService = redisson.getLiveObjectService()

        Customer customer = new Customer("12");
        // customer object is becoming "live" object
        customer = liveObjectService.merge(customer);

        customer.setName("Alexander Pushkin");
        customer.setPhone("+7193127489123");
        customer.setAddress("Moscow, Tverskaya str");

        Product product = new Product(1L, "FoodBox");
        // product object is becoming "live" object
        product = liveObjectService.merge(product);

        product.addItem("apple", 1);
        product.addItem("banana", 12);
        product.setPrice(BigDecimal.valueOf(10));
        product.setUnitsInStock(12);

        Order order = new Order(customer);
        // order object is becoming "live" object
        order = liveObjectService.merge(order);

        order.setDate(new Date());
        order.setShipAddress("Moscow, Gasheka str");
        order.setShipName("James Bond");
        order.setShipPostalCode("141920");

        OrderDetail od = new OrderDetail(order, product);
        // OrderDetail object is becoming "live" object
        od = liveObjectService.merge(od);

        od.setPrice(BigDecimal.valueOf(9));
        od.setQuantity(1);

        order.getOrderDetails().add(od);
        customer.getOrders().add(order);

        // "live" object could be get on other JVM.
        Customer customer = liveObjectService.get(Customer.class, "12");
        for (Order order : customer.getOrders()) {
            for (OrderDetail orderDetail : order.getOrderDetails()) {
                 // ...
            }
            order.setPrice(BigDecimal.valueOf(12))
        }

        Product product = liveObjectService.get(Product.class, id);
        // ...

The merge, get, persist, attach methods of the RLiveObjectService object return a proxy-object which connected to Redis and that's what make it a liveobject. This proxy translates each getter/setter call to read/write Redis operation in real time.

As seen from the example above, each object has an id field marked by the RId annotation. The id value can come from two difference places: provided by users or generated by a generator (LongGenerator.class, UUIDGenerator.class or any other implementation) LongGenerator returns incremented value which is unique for each type of Object.

The RCascade annotation allows you to cascade RLiveObjectService operations (merge, persist, delete) for marked fields.

The pseudo code below illustrates the store of the described data model in Redis used by Redisson:

redis.hset "customer:12" "name" "Alexander Pushkin"  
redis.hset "customer:12" "phone" "+7193127489123"  
redis.hset "customer:12" "address" "Moscow, Tverskaya 1 str"  

redis.incr "product:long:generator"  

redis.hset "product:1:long" "name" "FoodBox"  
redis.hset "product:1:long" "price" 10  
redis.hset "product:1:long" "unitsInStock" 12  
redis.hset "product:1:long" "items" "org.javamodel.Product:product:1:long:items"  

redis.hset "product:1:long:items" "apple" 1  
redis.hset "product:1:long:items" "banana" 12  

redis.incr "order:long:generator"  

redis.hset "order:1:long" "customer" "org.javamodel.Customer:customer:12"  
redis.hset "order:1:long" "orderDetails" "org.redisson.RedissonList:orderDetails:order:1:long"  
redis.hset "order:1:long" "date" "144516271223"  
redis.hset "order:1:long" "shipAddress" "Moscow, Gasheka 9 str"  
redis.hset "order:1:long" "shipName" "James Bond"  
redis.hset "order:1:long" "shipPostalCode" "141920"  

redis.incr "orderdetail:long:generator"

redis.hset "orderdetail:1:long" "order" "org.javamodel.Order:order:1"  
redis.hset "orderdetail:1:long" "product" "org.javamodel.Product:product:1:long"  
redis.hset "orderdetail:1:long" "price" 9  
redis.hset "orderdetail:1:long" "quantity" 1  

redis.rpush "org.redisson.RedissonList:orderDetails:order:1:long" "orderdetail:1:long"  

redis.incr "orderdetail:long:generator"  

redis.hset "orderdetail:2:long" "order" "org.javamodel.Order:order:1"  
redis.hset "orderdetail:2:long" "product" "org.javamodel.Product:product:2:long"  
redis.hset "orderdetail:2:long" "price" 4  
redis.hset "orderdetail:2:long" "quantity" 2  

redis.rpush "org.redisson.RedissonList:orderDetails:order:1:long" "orderdetail:2:long"  

As you can see, all properties of objects are mapped directly to the Redis hash object. References to objects are stored as strings, which contains the type and id of this object. List and Map objects are mapped directly to Redis hash/list objects, too.

Conclusions

This approach greatly simplifies the work for creating a distributed business application with the help of Redisson's distributed in-memory Java data model. It also reduces the time of the round-trip for read/write data. It's worth noting that LiveObject conception allows us to interact with a Java data model of any complexity and even with a huge amount of fields.

Data model (GIS) Data (computing) Redis (company) Java (programming language) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns
  • High-Performance Java Serialization to Different Formats
  • The First Annual Recap From JPA Buddy
  • Beginners Guide for Web Scraping Using Selenium

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!