event sourcing and cqrs from the trenches
play

Event sourcing and CQRS from the trenches SIDNEY SHEK ARCHITECT - PowerPoint PPT Presentation

Event sourcing and CQRS from the trenches SIDNEY SHEK ARCHITECT ATLASSIAN @SIDNEYSHEK Universe of Users users Id Name Username APIKey 1 Homer homer d0a 2 Bart bart f00 3 Maggie maggie baa Universe of Users


  1. Event sourcing and CQRS from the trenches SIDNEY SHEK • ARCHITECT • ATLASSIAN • @SIDNEYSHEK

  2. Universe of Users users Id Name Username APIKey 1 Homer homer d0a 2 Bart bart f00 3 Maggie maggie baa

  3. Universe of Users users Id Name Username APIKey 1 Homer homer d0a 2 Bart bart f00 3 Lisa Jr maggie baa

  4. Universe of Users users Id Name Username APIKey 1 Homer homers d0a 2 Bart bart f00 3 Lisa Jr maggie baa

  5. Universe of Users users Id Name Username APIKey 1 Homer homer d0a 2 Bart bart f00 3 Maggie maggie baa events Seq Event Time 123 SetUsername(3, Maggie) 0

  6. Universe of Users users Id Name Username APIKey 1 Homer homer d0a 2 Bart bart f00 3 Lisa Jr maggie baa events Seq Event Time 123 SetUsername(3, Maggie) 0 124 SetName(3, Lisa Jr) 10

  7. Universe of Users users Id Name Username APIKey 1 Homer homers d0a 2 Bart bart f00 3 Lisa Jr maggie baa events Seq Event Time 123 SetUsername(3, Maggie) 0 124 SetName(3, Lisa Jr) 10 125 SetUsername(1, homers) 15

  8. Universe of Users users Id Name Username APIKey 1 Homer homers d0a users_new 2 Bart bart f00 Id Name Derived 3 Lisa Jr maggie baa 1 Homer Homer1 2 Bart Bart2 3 Lisa Jr Lisa Jr3 events Seq Event Time 123 SetUsername(3, Maggie) 0 124 SetName(3, Lisa Jr) 10 125 SetUsername(1, homers) 15

  9. Universe of Users users Id Name Username APIKey 1 Homer homers d0a users_new 2 Bart bart f00 Id Name Derived 3 Lisa Jr maggie baa 1 Homer Homer1 2 Bart Bart2 3 Lisa Jr Lisa Jr3 events Seq Event Time 123 SetUsername(3, Maggie) 0 124 SetName(3, Lisa Jr) 10 125 SetUsername(1, homers) 15

  10. Our Identity System requirements

  11. Our Identity System requirements • Users, groups and memberships

  12. Our Identity System requirements • Users, groups and memberships • Searching for users

  13. Our Identity System requirements • Users, groups and memberships • Searching for users • Retrieve by email

  14. Our Identity System requirements • Users, groups and memberships • High volume low latency reads • Searching for users • Retrieve by email

  15. Our Identity System requirements • Users, groups and memberships • High volume low latency reads • Searching for users • Retrieve by email • Incremental synchronisation

  16. Our Identity System requirements • Users, groups and memberships • High volume low latency reads • Searching for users • Retrieve by email • Incremental synchronisation • Audit trails for changes

  17. Our Identity System requirements • Users, groups and memberships • High volume low latency reads • Searching for users • Retrieve by email • Highly available • Incremental synchronisation • Disaster recovery • Zero-downtime upgrades • Audit trails for changes

  18. Our Identity System requirements • Users, groups and memberships • High volume low latency reads • Searching for users • Retrieve by email • Highly available • Incremental synchronisation • Disaster recovery • Zero-downtime upgrades • Audit trails for changes • Testing with production-like data

  19. Evolving the architecture

  20. REST calls e.g. Add User Core app Services Query Query Query views SaveAPI views views EventStream DynamoDB

  21. Command Query Responsibility Segregation

  22. UI User User Search for users Users and groups Domain (bus. logic) Database

  23. UI User User Search for users Users and groups Query Command Query Query views (write logic) views views Database

  24. UI User User Search for users Users and groups Query Command Query Query views (write logic) views views ElasticSearch

  25. REST calls e.g. Add User Core app Services Query Query Query Query Commands views sync views views EventStream EventStream EventStream DynamoDB

  26. REST calls e.g. Add User Core app Services Query Query Query Query Commands views sync views views EventStream EventStream EventStream ElasticSearch Events Kinesis Lambda DynamoDB

  27. REST calls e.g. Add User Groups Services Query Query Query Query Commands views sync views views EventStream EventStream EventStream External Events Platform Events Users Event Kinesis Lambda Kinesis Txf DynamoDB

  28. Events as an API

  29. Insert / Update Delta vs ‘Set’ events UserNameSet(id, name) UserAdded(id, name, email1) UserEmailSet(id, email1) UserUpdated(id, email = Some (email)) UserEmailSet(id, email2) Fits nicely with CRUD + PATCH Encourages idempotent processing Assume insert before update Single code path for query sync Minimally sized events to avoid conflict

  30. Single stream Multiple streams Sharding for throughput Transactions and consistent data Better availability vs resolution consistency compromise

  31. Rules for splitting streams 1. Place independent events on different streams

  32. Rules for splitting streams 1. Place independent events on different streams 2. Split streams by event type and unique Id

  33. Rules for splitting streams 1. Place independent events on different streams 2. Split streams by event type and unique Id 3. Identify the ‘transactions’ you really need

  34. Rules for splitting streams 1. Place independent events on different streams 2. Split streams by event type and unique Id 3. Identify the ‘transactions’ you really need 4. Use hierarchical streams to maximise number of streams

  35. Rules for splitting streams 1. Place independent events on different streams 2. Split streams by event type and unique Id 3. Identify the ‘transactions’ you really need 4. Use hierarchical streams to maximise number of streams 5. Splitting and joining streams later is possible

  36. But… no guaranteed order between streams

  37. Query views get populated eventually

  38. Query views get populated eventually • A field should only be updated by a single event stream

  39. Query views get populated eventually • A field should only be updated by a single event stream • No foreign key constraints

  40. Query views get populated eventually • A field should only be updated by a single event stream • No foreign key constraints • In general, unique or data constraints ‘enforced’ on write

  41. Let go of transactions and consistency

  42. Why do we need transactions?

  43. Why do we need transactions? • Enforce business constraints e.g. uniqueness

  44. Why do we need transactions? • Enforce business constraints e.g. uniqueness • Guaranteed to see what I just wrote

  45. Write and Read Consistency

  46. But CAP theorem…

  47. CAP or PACELC?

  48. CAP or PACELC? During a network Partition , choose between Availability versus Consistency

  49. CAP or PACELC? During a network Partition , choose between Availability versus Consistency Else choose between Latency versus Consistency

  50. There is a middle ground…

  51. Check-and-Set Optional forced writes reads Potentially conflicting events on Query view must be at stream seq X same stream 1. Read at seq X 2. Run business rule 3. Stream must be at X to write Potential false positives Potential increased latency Smaller streams alleviate Do not use as default problems Enforce timed waits

  52. Tokens to emulate transactions and consistency User: homer (id 4) All Users: Seq 100 User 4: Seq 23

  53. Tokens to emulate transactions and consistency • Returned on read and write via ETag User: homer (id 4) All Users: Seq 100 User 4: Seq 23

  54. Tokens to emulate transactions and consistency • Returned on read and write via ETag User: homer (id 4) All Users: Seq 100 • Pass as request header for: User 4: Seq 23

  55. Tokens to emulate transactions and consistency • Returned on read and write via ETag User: homer (id 4) All Users: Seq 100 • Pass as request header for: User 4: Seq 23 • Condition write (‘transaction’)

  56. Tokens to emulate transactions and consistency • Returned on read and write via ETag User: homer (id 4) All Users: Seq 100 • Pass as request header for: User 4: Seq 23 • Condition write (‘transaction’) • Force query view update (‘consistency’)

  57. Tokens to emulate transactions and consistency • Returned on read and write via ETag User: homer (id 4) All Users: Seq 100 • Pass as request header for: User 4: Seq 23 • Condition write (‘transaction’) • Force query view update (‘consistency’) • Caching

  58. Using tokens to enforce state Core App Client

  59. Using tokens to enforce state Core App Client Create User A

  60. Using tokens to enforce state Core App Client Create User A Token TU1

  61. Using tokens to enforce state Core App Client Create User A Token TU1 Create Group B

  62. Using tokens to enforce state Core App Client Create User A Token TU1 Create Group B Token TG1

  63. Using tokens to enforce state Core App Client Create User A Token TU1 Create Group B Token TG1 Add User A to Group B (tokens TU1, TG1)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend