When you need to update some thing in a distributed
system
and you have multiple services to update, you need
distributed transactions.
If you were using a single SQL database, you could do some
thing like the following. To synchronize changes to the
order
system, delivery system and email
system, you group your
changes into a single local transaction.
BEGIN TRANSACTION
update users set last_use = date.now()
insert into orders (user_id, product, qty) values (20,
"Schlongs", 10) returning id
insert into deliveries (user_id, order_number, address)
values
(20, 10, "120 pemwinkle drive, birmingham, uk")
COMMIT
In a microservice based system, you don't have the luxury of
using a single database to run a transaction within because
you're connecting from different servers where each has its
own database.
I propose using Internet Relay Chat to coordinate the lock
on tables
initiator: g393 LOCK users orders deliveries
initiator: g393 update users set last_use = date.now()
userservice: g393 ok
initiator: g393 insert into orders (user_id, product, qty)
values
(20, "Schlongs", 10) returning id
orderservice: g393 ok
initiator: insert into deliveries (user_id, order_number,
address) values (20, 10, "120 pemwinkle drive, birmingham,
uk")
deliveriesservice: g393 ok
initiator g393 COMMIT
userservice: g393 done
orderservice: g393 done
deliveriesservice: g393 done