Event Driven Architecture

Why Big Tech Companies Use Event-Driven Architecture .?
Event-Driven Architecture (EDA) is a pattern where events—such as user actions or system changes—trigger other processes, enabling components to function independently while responding to those events. This approach creates scalable, flexible systems that can easily adapt to evolving business needs. Major companies like Netflix, Amazon, and Uber use EDA to manage high-performance, distributed applications.
Creating a event class
import EventEmitter from "events";
class AnalyticsEventEmitter extends EventEmitter {}
const analyticsEventEmitter = new AnalyticsEventEmitter();
export { analyticsEventEmitter };
Registering the events
analyticsEventEmitter.on("PRODUCT_ADD_TO_CART", async (variantId, count) => {
try {
const productId = await getProductIdFromVariant(admin, variantId);
let storedAnalytics = await getProductMetafield(admin, productId);
storedAnalytics = {
...storedAnalytics,
count: (storedAnalytics?.count || 0) + count,
};
await updateMetafield(
admin,
productId,
"Analytics",
"product",
storedAnalytics,
);
} catch (error) {
console.error("Error in PRODUCT_ADD_TO_CART handler:", error);
}
});
Triggering the event
export const action = async ({ request }: ActionFunctionArgs) => {
const analytics = await request.json();
if (!analytics?.productVariantId)
return { success: false, message: "No analytics provided" };
analyticsEventEmitter.emit(
"PRODUCT_ADD_TO_CART",
analytics.productVariantId,
analytics.count,
);
return { success: true, };
};
Solving a Shopify App Issue Using Event-Driven Architecture:
In my current project, I'm working with a Shopify app built with Remix (React) and Node.js. I encountered an issue when attempting to update a metafield on Shopify. The update requires admin access, but since the data is being sent directly from the frontend, I didn’t have the necessary credentials to perform this action.
Here's how Event-Driven Architecture played a critical role in solving this challenge:
Frontend Issue: The route handling the metafield update was on the frontend, and as expected, I did not have admin access to make changes directly.
Event Emission: Instead of trying to handle the logic directly on the frontend, I emitted an event whenever the frontend needed to trigger an update. This event essentially served as a signal that something needed to happen in the backend.
Event Listener with Admin Access: On the backend, specifically in the app index page, where I have the necessary admin access, I registered the event. The backend was able to listen for the event, and once triggered, it proceeded to update the metafield with the correct admin privileges.
By using this approach, I could perform the necessary admin-level operations securely without exposing sensitive credentials in the frontend. This allowed the app to remain both functional and scalable.
Subscribe to my newsletter
Read articles from Prachanda Rana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
