Microservice architecture patterns
Alejandro Vidal Rodriguez
Director of Engineer @TribalScale
www.tribalscale.ae
we are hiring!
Tech lead position at company that delivers food to people!
toys
At foodToGo they been developing their system in a big single solution.
yallaToyz
The Monolith
Microservices vs Monolith
simple to develop
easy to make radical changes to the application
straightforward to test
straightforward to deploy
easy to scale more instances
IPC speed
Pros
Cons
Pros
Cons
| one to one | one to many | |
|---|---|---|
| synchronous | Request/Response | - |
| asynchronous | Request/notification | Publish/subscribe |
Contract definition
API design first
Semantic versioning
Message formatting
JSON
XML
Protocol buffers
Avro
Text based
Binary based
REST
Pros
Cons
Messaging
Messaging
Message broker
Message broker
Pros
Cons
If you have to call 5 systems with an SLA of 99% uptime, what is this aggregated SLA?
99 * 99 * 99 * 99 * 99 / 10000000000 = 95%
begin transaction
UPDATE ProductStock SET quantity = quantity - 1
WHERE id = $productId field > 0
INSERT INTO Purchases (ItemId, ProdName, Quantity, Price, Total)
VALUES (1234, 'Teddy Bear', 1, 200, 200);
commit transactionbegin catch
raiserror('Not enough teddy bears!')
rollback transaction
end catchMaintain data consistency across services using a sequence of local transactions that are coordinated using asynchronous messaging.
| Service | Transaction | Compensating tx |
|---|---|---|
| Order Service | createOrder() | rejectOrder() |
| Stock service | verifyStock() | - |
| Fullfillment Service | createTicket() | rejectTicket() |
| Payment Service | chargeAmount() | refundAmount() / - |
| Fullfillment Service | approveTicket() | - |
| Order Service | approveOrder() | - |
Compensating transactions to achieve rollbacks
No central coordinator
participants 'simply' subscribe to each other’s events and respond accordingly
Pros
Cons
define an orchestrator class whose sole responsibility is to tell the saga participants what to do
Pros
Cons
Semantic lock: ORDER_PENDING -> ORDER_APPROVED
Conmutative operations: credit() <---> debit()
Re read value: Check if data has change -> restart
Version: if the data has a higher version -> discard
By value: high risk? -> don't use sagas, use distributed tx
Problems
• Reliably publishes domain events
• Preserves the history of aggregates
• Mostly avoids the O/R impedance mismatch problem
• Provides developers with a time machine
• Different programming model that has a learning curve
• Complexity of a messaging-based application
• Evolving events can be tricky
• Deleting data is tricky
• Querying the event store is challenging
Pros
Cons
Your data used to be in one place, one simple query gets your data.
Can I get my previous orders given a keyword?
How many red teddy bears did I bought last year?
Monolith
Microservices
Even simple queries get tricky:
findAvailableToyStores()
has the service the geospatial capablitity?
Its owned by the StoreService or by the PurchaseService?
Capability
Ownership
• Simple and intuitive
• Effective if can be paralelized
• Increased overhead
• Risk of reduced availability
• Lack of transactional data consistency
Pros
Cons
• Enables efficient impl of queries in a ms architecture
• Enables efficient impl of a diverse queries
• Makes querying possible in an event sourcing application
• Improves separation of concerns
• More complex architecture
• Dealing with the replication lag
• More expensive
Pros
Cons