"Turning the database inside out with Apache Samza" by Martin Kleppmann
(Command and Query Responsibility Segregation)
We use this pattern in heaps of places with a payroll product.
Employee ID | Payslip | Leave balance | Deleted? |
---|---|---|---|
1 | 7 | 10 | true |
1 | 8 | 12 | true |
1 | 9 | 14 | true |
1 | 9 | 15 | false |
Was the edit to payslip 9 correct or incorrect?
Employee ID | Payslip | Debit | Credit | Running Total |
---|---|---|---|---|
1 | 1 | 0 | 2 | 10 |
1 | 2 | 0 | 2 | 12 |
1 | 3 | 0 | 2 | 14 |
1 | 3 | 2 | 3 | 15 |
Now we can see the logic behind the changes
Shift ID | Approved Start | Approved End | Processed? |
---|---|---|---|
1 | 9am | 5pm | false |
Set to true when the shift has been processed
We start processing the job
Fetch the shift details from the db
Edit is made
Processed flag is marked as true and the edit is not processed
Log ID | Shift ID | Approved Start | Approved End | Processed? |
---|---|---|---|---|
1 |
1 | 9am | 5pm | true |
2 | 1 | 10am | 5pm | false |
3 | 1 | 9:30am | 5pm | false |
We could process each change sequentially but that might be unnecessary
Log ID |
---|
1 |
2 |
3 |
Grab the latest version which is unprocessed and process the latest version
Log ID |
---|
4 |
5 |
Log ID | Processed? |
---|---|
1 | true |
2 | true |
3 | true |
Repeat when more changes come in
James Harvey
Chief Software Architect @ foundU