Design Microservices by DDD

Agenda

  • Microservices

  • RESTful

  • Domain Driven Design (DDD)

  • Example

What’s Microservices

Microservices are a software development technique—a variant of the service-oriented architecture (SOA) architectural style that structures an application as a collection of loosely coupled services.
— wikipedia

Monolith vs. SOA vs. Microservices

Diagram

Monolith

Diagram

SOA

Diagram

Microservices

Diagram

RESTFul

Representational State Transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services.
— wikipedia

Richardson Maturity Model

Richardson Maturity Model

Level 0: The Swamp of POX

The starting point for the model is using HTTP as a transport system for remote interactions, but without using any of the mechanisms of the web.
Richardson Maturity Model
— Martin Fowler

Level 0: The Swamp of POX

  1. Use one or a few URL endpoint to receive all request

  2. Send all request by one HTTP method, mostly POST

  3. Always response HTTP status 200 regardless of sucess or fail

  4. Contain verbs, likes action or command code, in request body

  5. Contain status in response body, likes SUCESS, FAIL, etc.

  6. The structures of request body and response body are action/command specific

Level 0: The Swamp of POX

POST /api/level0/operation HTTP/1.1
Content-Type: application/json;charset=UTF-8
Accept: application/json
Host: localhost:8080
Content-Length: 86

{
  "command" : "createProduct",
  "parameters" : {
    "title" : "Test product 123"
  }
}

Level 0: The Swamp of POX

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 164

{
  "status" : "SUCCESS",
  "message" : "",
  "parameters" : {
    "id" : 3,
    "title" : "Test product 123",
    "createdAt" : "Sun Mar 11 15:43:11 CST 2018"
  }
}

Level 1: Resources

The first step towards the Glory of Rest in the RMM is to introduce resources. So now rather than making all our requests to a singular service endpoint, we now start talking to individual resources.
Richardson Maturity Model
— Martin Fowler

Level 1: Resources

  1. Use one or a few URL endpoint to receive all request Expose multiple URL endpoints, each one only responds to requests of individual resource

  2. Send all request by one HTTP method, mostly POST

  3. Always response HTTP status 200 regardless of sucess or fail

  4. Contain verbs, likes action or command code, in request body

  5. Contain status in response body, likes SUCESS, FAIL, etc.

  6. The structures of request body and response body are action/command specific Each URL endpoint only accept fixed structure request payload and response fixed structure payload

  7. Define resources which used in many reqeuest/response paylaod

Level 1: Resources

POST /api/level1/product HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:8080
Content-Length: 117

{
  "command" : "create",
  "data" : {
    "id" : null,
    "title" : "Test product 123",
    "createdAt" : null
  }
}

Level 1

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 160

{
  "status" : "SUCCESS",
  "message" : null,
  "data" : {
    "id" : 5,
    "title" : "Test product 123",
    "createdAt" : "2018-03-11T07:43:11.840+0000"
  }
}

Level 2: HTTP Verbs

Level 2 moves away from this, using the HTTP verbs as closely as possible to how they are used in HTTP itself.
Richardson Maturity Model
— Martin Fowler

Level 2: HTTP Verbs

  1. Expose multiple URL endpoints, each one only responds to requests of individual resource

  2. Send all request by one HTTP method, mostly POST

  3. Always response HTTP status 200 regardless of sucess or fail

  4. Contain verbs, likes action or command code, in request body

  5. Contain status in response body, likes SUCESS, FAIL, etc.

  6. Each URL endpoint only accept fixed structure request payload and response fixed structure payload

  7. Define resources which used in many reqeuest/response paylaod

  8. Use HTTP Verbs to represent action

  9. Use HTTP Status to represent result

Level 2: HTTP Verbs

POST /api/level2/product HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:8080
Content-Length: 73

{
  "id" : null,
  "title" : "New product title",
  "createdAt" : null
}

Level 2

HTTP/1.1 201 Created
Content-Type: application/json;charset=UTF-8
Content-Length: 97

{
  "id" : 21,
  "title" : "New product title",
  "createdAt" : "2018-04-30T12:02:08.667+0000"
}

Level 3: Hypermedia Controls

Diagram

Level 3: Hypermedia Controls

HTTP/1.1 201 Created
Content-Length: 589
Content-Type: application/json;charset=UTF-8
Location: http://localhost:8080/products/5d958ccadf86bd1cd947dbcc

{
  "title" : "New Product",
  "tags" : [ "Electronics", "Mobile" ],
  "createdAt" : "2019-10-03T05:53:14.592+0000",
  "updatedAt" : "2019-10-03T05:53:14.592+0000",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc"
    },
    "product" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc"
    },
    "images" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc/images"
    },
    "variants" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc/variants"
    }
  }
}