Recently I have been researching about Event Driven Architecture for use with Kafka.
Coming from using regular microservices and communicating between them directly with something like RPC, I am struggling to understand how to perform the same kind of operations through an Event Driven system.
I'd like to use an example to understand how an operation would be done in orchestrated system compared to event driven where you have users
and products
microservices.
Example: An ecommerce store, where a user can have 10 products on a free plan, otherwise unlimited products on a paid plan.
How it would be done in regular orchestrated services:
const users = // grpc connection to external users microservice
function createProduct(userId, newProduct) {
let productsCount = await query("SELECT COUNT(*) FROM products WHERE user_id = " + userId);
let userIsPaid = await users.getUserIsPaid(userId);
if productsCount >= 10 && !userIsPaid {
// Error: cannot make new product because user is on free plan
return;
}
// Go ahead and insert the new product
}
How could this logic be done in an Event Driven system?
I am aware that the products microservice has the option to store a copy of the data it needs, such as user_plan
, but I'd like to avoid this as I'm concerned with data being out of sync and inconsistent.
This particular example would be exactly the same even if your system uses integration events. Messages/events are used to notify other services that something important happened.
In this sample you just want to read data and do some business logic based on it. You can read data from local database or remote service, this is design choice and both options have pros and cons. Messages/Events are not used to read data.
Microsoft has a good book about microservices architecture. There are several chapters, like this one about events.