$30 off During Our Annual Pro Sale. View Details »

Mastering Java Persistence Best Practices for Cloud-Native Applications and Modernization

Mastering Java Persistence Best Practices for Cloud-Native Applications and Modernization

Join us for an insightful presentation as we explore the world of Java persistence in the context of cloud-native applications and modernization. Discover the fundamental principles, challenges, and strategies. From uncovering database patterns to harnessing the power of cloud-native technologies, this session will provide valuable insights for developers, engineers, and architects seeking to optimize their application's persistence layer. Take advantage of this opportunity to better understand how to create maintainable architectures, tackle data integration challenges, and embark on successful modernization journeys. Whether you're new to the concept or looking to enhance your expertise, this presentation is your guide to mastering Java persistence for the future of software development.

Otavio Santana

November 10, 2023
Tweet

More Decks by Otavio Santana

Other Decks in Technology

Transcript

  1. Mastering Java Persistence
    Best Practices for Cloud-Native
    Applications and Modernization

    View Slide

  2. Endures knowledge
    Throughout humanity
    Temples, Caves as database
    Why do we need
    databases anyway?

    View Slide

  3. Database evolutions

    View Slide

  4. Why Do Modern
    Applications
    Need Data Storage?
    The new opportunities
    The current opportunity
    It's where the state is

    View Slide

  5. Maturity Model
    Database flavors
    Paradigms
    Persistence landscape
    State of affairs
    https://survey.stackoverflow.co/2023/

    View Slide

  6. Development lifecycle very
    often starts with immature
    data structures
    Changing a database type in
    existing applications is
    complex and expensive.
    Over time, maintenance of
    data and schema evolution
    gets challenging
    First thing we do
    Persistence landscape
    Change Maintenance
    State of affairs
    Evolutionary Data

    View Slide

  7. The rules of Software Architecture
    Everything in Software Architecture
    is a trade-off
    Why is more important than How

    View Slide

  8. Business Isolation Performance
    The success at Software Architecture

    View Slide

  9. SQL Database
    Database solutions
    Polyglot Persistence
    key value
    key
    key
    key
    value
    value
    value
    Wide-Column Graph Document Key Value
    NoSQL Database

    View Slide

  10. Apollo
    Key-value
    Structure
    Ares
    Love Beauty
    War
    Sun
    Aphrodite
    Key Value

    View Slide

  11. Use Case Description
    Caching Speeds up data retrieval by storing frequently accessed data in
    memory, reducing the need to fetch it from the backend.
    Session Management Stores and manages user sessions, enhancing application
    performance and scalability.
    Real-time Analytics Powers real-time dashboards and analytics by swiftly processing and
    aggregating data.
    Pub/Sub Messaging Facilitates real-time communication between components through
    publish-subscribe messaging patterns.
    Redis
    Use cases

    View Slide

  12. Simplify Schema
    Identify Key Patterns
    Use Contextual Keys
    Batching Operations
    Cache-friendly Design
    Avoid Complex Joins
    Beware of
    Over-Normalization
    Avoid Bloated Values
    Limit Indexes
    Redis
    Do and Don'ts

    View Slide

  13. Redis
    Clusters

    View Slide

  14. Apollo
    Aphrodite
    Ares
    Kratos
    Duty
    Dead Gods
    Love, happy
    Sun
    War
    13
    Color
    Sword
    Row-key Columns
    Wide-Column
    Structure
    Duty
    Duty
    weapon

    View Slide

  15. Apache Cassandra
    User Cases
    Use Case Description
    Online Retail
    Managing e-commerce platforms, handling product catalogs, user profiles,
    and transaction records.
    Social Media Analytics
    Monitoring and analyzing social media interactions, tracking trends, and user
    engagement.
    Real-Time Analytics
    Enabling quick analysis of data streams for instant insights, critical in financial
    and logistics sectors.
    Logging and Monitoring
    Centralized storage and analysis of logs and monitoring data from applications
    and servers.

    View Slide

  16. Apache Cassandra
    Modeling tips
    Denormalize When Necessary
    Design for Query Patterns
    Choose Optimal Column Families
    Utilize Secondary Indexes
    Compression and Bloom Filters
    Avoid Over-Denormalization
    Limit Column Count
    Avoid Overusing Secondary Indexes

    View Slide

  17. 1
    2
    3
    4
    5
    6
    7
    8
    9
    11
    12
    10
    Client
    R1
    R2
    R3
    1
    2
    3
    4
    5
    6
    7
    8
    9
    11
    12
    10
    R4
    R5
    R6
    DC1 DC2
    Apache Cassandra
    Cluster

    View Slide

  18. {
    "name":"Diana",
    "duty":[
    "Hunt",
    "Moon",
    "Nature"
    ],
    "siblings":{
    "Apollo":"brother"
    }
    }
    Document
    Structure

    View Slide

  19. MongoDB
    User Cases
    Use Case Description
    Content Management
    Store and manage diverse content types like articles, images, and videos with
    schema flexibility.
    Catalog Management
    Efficiently organize and categorize products or items with changing attributes
    and metadata.
    Mobile Applications
    Provide offline capabilities and sync data seamlessly once online, enhancing
    user experience.
    Social Media Platforms
    Facilitate rapid storage and retrieval of user-generated content, profiles, and
    social interactions.

    View Slide

  20. MongoDB
    Modeling tips
    Leverage Embedded Documents
    Design Around Use Cases
    Employ Indexes Judiciously
    Normalize When Logical
    Stay Flexible with Arrays
    Avoid Over-Embedding
    Steer Clear of Monolithic Documents
    Don’t Over-Index
    Beware of the One-Size-Fits-All Approach

    View Slide

  21. MongoDB
    Cluster

    View Slide

  22. MongoDB
    Cluster

    View Slide

  23. Apollo Ares
    Kratos
    was killed by was killed by
    killed killed
    Graph
    Structure

    View Slide

  24. Neo4J
    Modeling tips
    Avoid Over-Reliance on Joins
    Steer Clear of Over-Connecting Nodes
    Don’t Overcomplicate Graph Design
    Beware of Property Index Overuse
    Embrace Relationship-Driven Design
    Craft Efficient Traversal Paths
    Use Labels and Types Wisely
    Leverage Indexing for Nodes
    Utilize Property Indexes

    View Slide

  25. Neo4J
    User Cases
    Use Case Description
    Social Networks Modeling user profiles, friendships, and interactions for effective social
    networking platforms.
    Recommendation
    Engines
    Powering personalized recommendations by analyzing connections and
    preferences.
    Knowledge Graphs Organizing and querying complex relationships in fields like healthcare,
    finance, and research.
    Fraud Detection Uncovering hidden patterns and connections indicative of fraudulent
    activities.
    Network Analysis Analyzing intricate relationships in data networks, such as transportation
    and communication.

    View Slide

  26. Neo4J
    Leader
    Follower
    Follower
    Read Replica
    Read Replica

    View Slide

  27. Scalability
    Flexibility
    key-value
    Column
    Document
    Graph
    Time-series
    Scalability vs Flexibility
    Query and speed

    View Slide

  28. Application vs. Database

    View Slide

  29. Application
    (Object Oriented Language)
    Mismatch
    Database
    (Relational)
    Object
    Challenges in a database land
    Different paradigms: Apps x DBMS
    Tables

    View Slide

  30. Mapping
    Mismatch
    1
    *
    Addresses
    Inheritance
    Polymorphism
    Encapsulation
    Types Normalization
    Denormalization
    Structure
    Application Database
    key value
    key
    key
    key
    value
    value
    value
    wide-Column Graph
    Document Key Value
    Challenges in a database land
    Different paradigms: Apps x DBMS

    View Slide

  31. Driver
    Data Mapper
    DAO
    Database integration
    Data Access Patterns
    Handling Data Integration in Java
    Active Record
    Repository
    DTO

    View Slide

  32. Driver
    Database integration
    Data Mapping
    Database Application
    Data Mapper
    Active Record
    Repository
    DAO
    Data-oriented
    Programing
    Object Oriented
    Programming

    View Slide

  33. Database-oriented Programming
    4 Principles
    Database
    Data-oriented
    Programing
    1. Separating code (behavior) from data.
    2. Representing data with generic data structures.
    3. Treating data as immutable.
    4. Separating data schema from data
    representation.

    View Slide

  34. Object Oriented Programming
    Principles
    Application
    Object Oriented
    Programming
    1. Expose behavior and Hide data
    2. Abstraction
    3. Polymorphism

    View Slide

  35. Driver
    Data Mapping
    try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS){
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(QUERY);) {
    // Extract data from result set
    while (rs.next()) {
    // Retrieve by column name
    System.out.print("ID: " + rs.getInt("id"));
    System.out.print(",name: " + rs.getInt("name"));
    System.out.print(",birthday: " + rs.getString("birthday"));
    System.out.println(",city: " + rs.getString("city"));
    System.out.println(",street: " + rs.getString("street"));
    …}
    }
    Complex relation
    w/ business logic
    Code flexibility

    View Slide

  36. Data Mapper
    Data Mapping
    @Entity
    public class Person {
    @Id @GeneratedValue(strategy = AUTO) Long id;
    String name;
    LocalDate birthday;
    @ManyToOne List address;

    }
    impedance mismatch
    centralize mapper
    responsibility
    public class PersonRowMapper implements RowMapper {
    @Override
    public Person mapRow(ResultSet rs, int rowNum) throws
    SQLException {
    Person person = new Person();
    person.setId(rs.getInt("ID"));
    return person;
    }
    }

    View Slide

  37. Data Access Object
    Data Mapping
    public interface PersonDAO {
    List getAll();
    void update(Person person);
    void delete(Person person);
    void insert(Person person);
    }
    Centralize Data
    operations
    impedance mismatch

    View Slide

  38. Active Record
    Data Mapping
    @Entity
    public class Person extends PanacheEntity {
    public String name;
    public LocalDate birthday;
    public List addresses;
    }
    Person person =...;
    // persist it
    person.persist();
    List people = Person.listAll();
    // finding a specific person by ID
    person = Person.findById(personId);
    SOLID breaking
    Higher domain's
    responsibility

    View Slide

  39. Repository
    Data Mapping
    Far from database
    Domain oriented
    @Entity
    public class Person {
    private @Id Long id;
    private @Column String name;
    private @Column LocalDate birthday;
    private @ManyToOne List addresses;
    }
    public interface PersonRepository extends {}
    Person person =...;
    // persist it
    repository.save(person);
    List people = repository.findAll();
    // finding a specific person by ID
    person = repository.findById(personId);

    View Slide

  40. Database integration
    Data Mapping
    Client
    Database Database
    Client
    Mapper
    DAO
    Repository
    Data mapping
    and conversion

    View Slide

  41. Flexibility vs Complexity, Use with Caution
    Database Layers

    View Slide

  42. DTO
    Entity
    Resource
    DTO
    Flexibility vs Complexity, Use with Caution
    Database

    View Slide

  43. Database metrics
    Biz. transactions
    Oversized /
    downsized
    Invalid/stale
    connections
    Apps on await state
    Fixed cache size
    No cache usage
    Consistency
    impacts w/
    distributed cache
    Complex mapping
    Auto-generated
    schemas
    On-prem x Cloud
    Many NoSQL types
    SQL x
    NoSQL x NewSQL
    Eager x Lazy
    loading
    N+1 Problem
    Hard to change
    db types
    Persistence
    Config.
    Data storage
    Data
    Manipulation
    Cache Conn. Pool
    Framework
    What's next?

    View Slide

  44. books.a4j.dev/mastering-persistence
    Expand your Knowledge
    Deliver high-performance Java solutions
    Get your copy today!
    @otaviojava @kvarel4

    View Slide