Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Domain-Driven Data at SunshinePHP

Domain-Driven Data at SunshinePHP

There are many types of databases and data analysis tools from which to choose today. Should you use a relational database? How about a key-value store? Maybe a document database? Or is a graph database the right fit for your project? What about polyglot persistence? Help! Applying principles from Domain-Driven Design such as strategic design and bounded contexts, this session will help you choose and apply the right data layer for your application's model or models. We'll explore traditional relational databases, graph databases, document databases, key/value stores, polyglot persistence, CQRS, event sourcing, and data layers for microservices.

Bradley Holt

February 09, 2018
Tweet

More Decks by Bradley Holt

Other Decks in Programming

Transcript

  1. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Domain-Driven Data

    Bradley Holt
    Developer Advocate
    Senior Software Engineer

    View Slide

  2. View Slide

  3. View Slide

  4. A fully-managed
    database-as-a-service
    (DBaaS) based on
    Apache CouchDB with
    additional full text and
    geospatial search
    capabilities
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation

    View Slide

  5. Open for Data Dome (outside) by Bradley Holt.

    View Slide

  6. Brent Spiner with Zoltar from Big by Bradley Holt, on Twitter .
    Big Data

    View Slide

  7. IBM
    Cloud
    Data
    Services
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation

    View Slide

  8. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    A Brief History of Data

    View Slide

  9. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    The Relational Database
    order
    *order_id
    customer_id
    date
    customer
    line_item
    *customer_id
    email_address
    name
    *order_id
    *item_id
    price
    quantity
    database 2 by Tim Morgan, on Flickr (CC BY 2.0).

    View Slide

  10. Relational databases
    guarantee atomicity,
    consistency,
    isolation, and
    durability (ACID)
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Hard Disk by Jeff Kubina, on Flickr (CC BY-SA 2.0).

    View Slide

  11. The ACID guarantees
    provided by relational
    databases were (and
    often still are) critical
    for systems of record
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    IBM 360 Announcement center by Robert Nix, on Flickr (CC BY 2.0).

    View Slide

  12. The introduction of the
    Web brought new
    types of applications
    with different
    constraints than
    systems of record
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Dialing Up Web History by Mike Licht, on Flickr (CC BY 2.0).

    View Slide

  13. The introduction of
    mobile apps added to
    the growing number of
    systems of
    engagement
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Instagram and other Social Media Apps by Jason Howie, on Flickr (CC BY 2.0).

    View Slide

  14. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Changing Constraints

    View Slide

  15. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Always On
    Dynamo, un siècle de lumière et de mouvement dans l'art, 1913 – 2013 by Yann Caradec, on Flickr (CC BY-SA 2.0).

    View Slide

  16. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Big Data
    World travel and communications recorded on Twitter by Eric Fischer, on Flickr (CC BY 2.0).

    View Slide

  17. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    The CAP Theorem
    Consistency Availability
    Partition
    Tolerance

    View Slide

  18. Horizontal scaling
    through the addition of
    commodity hardware
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Server grill with blue light by David Precious, on Flickr (CC BY 2.0).

    View Slide

  19. Every node in an
    eventually consistent
    distributed system will
    eventually arrive at a
    consistent state if
    given no new updates
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Spider Web by Alden Chadwick, on Flickr (CC BY 2.0).

    View Slide

  20. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Enter “Not only SQL” (NoSQL)

    View Slide

  21. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    key-value
    graph
    document
    …more
    database by Tim Morgan, on Flickr (CC BY 2.0).

    View Slide

  22. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation

    View Slide

  23. Key-value stores
    provide opaque data
    accessed through
    unique keys
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Keys for the Stanley Hotel by Mike Silva, on Flickr (CC BY 2.0).

    View Slide

  24. Document databases
    are a variation on key-
    value stores with
    strictly defined values
    (e.g. JSON objects)
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    paper by malik, on Flickr (CC BY-ND 2.0).

    View Slide

  25. Graph databases
    provide nodes and
    properties of nodes
    connected via edges
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Edinburgh Road Network analysis by Steven Kay, on Flickr (CC BY-ND 2.0).

    View Slide

  26. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Domain-Driven Design (DDD)

    View Slide

  27. View Slide

  28. Domain-Driven Design
    is a collaboration
    between domain
    experts and software
    practitioners
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    IMG_2619 by Jason Pelletier, on Flickr (CC BY-SA 2.0).

    View Slide

  29. Complexity is in the
    domain, not in the
    technology
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Sounds_of_Complexity11.jpg by Enzo Varriale, on Flickr (CC BY-ND 2.0).

    View Slide

  30. Models are tools used
    to solve problems
    within the domain
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    model by MaZzuk, on Flickr (CC BY 2.0).

    View Slide

  31. Don't confuse models
    with reality—the map
    is not the territory
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    taking the subway to find the rents by Eli Duke, on Flickr (CC BY-SA 2.0).

    View Slide

  32. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Building Blocks of DDD and
    Domain Object Life Cycle

    View Slide

  33. Entities are defined by
    their identity
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    DSC_3407 by Mad House Photography, on Flickr (CC BY 2.0).

    View Slide

  34. Value objects encode
    attributes that
    describe things
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    red numbers by DaveBleasdale, on Flickr (CC BY 2.0).

    View Slide

  35. Aggregates group
    related entities to
    minimize complexity
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Social graph by Dmitry Grigoriev, on Flickr (CC BY 2.0).

    View Slide

  36. Repositories provides
    the illusion of in-
    memory data stores
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Catalog. by Adam Mayer, on Flickr (CC BY-SA 2.0).

    View Slide

  37. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Domain Layer
    Order Aggregate
    «interface»
    OrderRepository
    + insertOrder(order:Order)
    + updateOrder(order:Order)
    + findOrderById(id:int) : Order
    + recentOrders([limit:int]) : Order[0..*]
    Customer

    Infrastructure Layer
    LineItem

    Order
    - id : int
    - customer : Customer
    - date : Date
    - lineItems : LineItem[1..*]
    + total() : Money
    InMemoryOrderRepository
    + insertOrder(order:Order)
    + updateOrder(order:Order)
    + findOrderById(id:int) : Order
    + recentOrders(limit:int) : Order[0..*]
    RelationalMapperOrderRepository
    + insertOrder(order:Order)
    + updateOrder(order:Order)
    + findOrderById(id:int) : Order
    + recentOrders(limit:int) : Order[0..*]

    View Slide

  38. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Choosing the Right Data Layer

    View Slide

  39. A repository cannot
    completely abstract
    the constraints of
    your data store
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Lina Bo Bardi, SESC Pompéia by paulisson miura, on Flickr (CC BY 2.0).

    View Slide

  40. Object-oriented
    programming and
    relational databases
    use different models,
    resuting in the
    object-relational
    impedance mismatch
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Financial District Classical Building Reflection Distortion, San Francisco, California, USA by Wonderlane, on Flickr (CC BY 2.0).

    View Slide

  41. “This is the world of NoSQL to me,
    that we can choose a tool that fits
    well with the problem we’re trying to
    solve.”
    Eric Evans (author of Domain-Driven Design)
    Software Engineering Radio Episode 226:
    Eric Evans on Domain-Driven Design at 10 Years, 2015
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    http://www.se-radio.net/2015/05/se-radio-episode-226-eric-evans-on-domain-driven-design-at-10-years/

    View Slide

  42. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Strategic Design

    View Slide

  43. Bounded contexts
    allow different models
    to be used within
    different domain
    contexts
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    rectangles by Dean Hochman, on Flickr (CC BY 2.0).

    View Slide

  44. Each bounded context
    should have its own
    data layer, and should
    not directly access a
    data layer belonging to
    a different bounded
    context
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Hexagons by Henry Burrows, on Flickr (CC BY-SA 2.0).

    View Slide

  45. A data layer may be a
    database, or it can be
    a data system
    consisting of multiple
    databases—
    sometimes called
    polyglot persistence
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    FLOWERS Mixed Boquet by kazandrew, on Flickr (CC BY-SA 2.0).

    View Slide

  46. Each bounded context
    may be represented
    as a microservice or a
    cluster of
    microservices
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Rainforest Biome by BMiz, on Flickr .

    View Slide

  47. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Catalog
    Document Database
    Key/Value Store
    Graph Database
    Full Text Search
    Shopping
    Cart
    Document Database
    Key/Value Store
    Orders Relational Database
    Big Data Analytics

    View Slide

  48. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Alternative Architectures

    View Slide

  49. Rather than update an
    entity in place,
    Command Query
    Responsibility
    Segregation (CQRS)
    provides separate
    read and write models
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    rectangles-10 by Karen Cropper, on Flickr (CC BY 2.0).

    View Slide

  50. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Domain Layer
    Read Model
    «interface»
    OrderQueryHandler
    + findOrderById(id:int) : OrderDetails
    + recentOrders([limit:int]) : OrderSummary[0..*]
    Write Model
    OrderDetails
    + getId() : int
    + getCustomer() : Customer
    + getDate() : Date
    + getLineItems() : LineItem[1..*]
    + getTotal() : Money
    «interface»
    OrderCommandHandler
    + handle(command:CreateOrder)
    + handle(command:AddLineItem)
    CreateOrder
    - customer : Customer
    - date : Date
    - lineItems : LineItem[1..*]
    OrderSummary
    + getId() : int
    + getDate() : Date
    + getTotal() : Money
    AddLineItem
    - orderId : int
    - lineItem : LineItem

    View Slide

  51. Event sourcing is an
    architecture in which
    application state is
    computed from a
    sequence of events
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Rusty Chain by veggiefrog, on Flickr (CC BY 2.0).

    View Slide

  52. IBM
    Cloud
    Data
    Services
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation

    View Slide

  53. Thank you
    IBM Cloud / February 9, 2018 / © 2018 IBM Corporation
    Bradley Holt
    Developer Advocate
    Senior Software Engineer

    ibm.com

    View Slide

  54. IBM Cloud / February 9, 2018 / © 2018 IBM Corporation

    View Slide