allBlogsList

Salesforce Headless Commerce Scalability Through Platform Events

Introduction

Can Salesforce process over 50,000 orders per hour? Yes it can and here is how we have done it. Salesforce runs a multi-tenant SaaS architecture. One tenant cannot take over and drive too much traffic resulting in negatively impacting all other tenants. Anyone working with Salesforce APIs is quickly reminded of the API limits and guardrails. Even if those limits and guardrails were not an issue, there are situations where the execution of the code within Salesforce itself may take time, and in a high pressure situation any millisecond delay can impact the performance of the system as a whole. Commerce implementations need to handle requests or responses extremely fast under the best of circumstances in order to drive revenue. Add to that the possibility that sustained high traffic can cause a bottleneck and data insertion hotspots as multiple processes attempt to lock and update the same page within a table in the underlying database, resulting in a scalability nightmare. The following blog outlines one possible solution that has worked well for us.

Setting The Stage

In recent months we have been working on a headless commerce implementation that receives orders from an external website and processes them in a Black Friday-type event. Black Friday is actually a tame description for the traffic we have seen. The recent Taylor Swift/LiveNation meltdown is probably a better description for the spikes we saw. The system must handle an extreme number of orders in a very short time. 

To make matters even more complicated, the orders move through several back-end provisioning and enhancement steps before they are considered complete and available to the buyer.

The provisioning is coordinated via a Kafka (Confluent) based middle tier which provides the “connective tissue” between several heterogeneous systems as well as the web front-end. Once provisioning has been completed by the various participants, the order passes to Salesforce for enhancement and final confirmation. As you can imagine, simply by reading this description, the roundtrip takes several seconds longer than shoppers are willing to wait.

The Participants

  • A web front end – current popular implementations include Vercel-based sites, but in truth, it can be any web as long as it is fast and scales.
  • Kafka (Confluence) -  Kafka began its life at LinkedIn where it helped to massively scale operations. Confluence is a commercial version of this tool that comes with an additional value – for example, the Salesforce Connector used for our project.
  • Salesforce OMS provides order management and can handle subsequent fulfillment, subscription, and user profile management if desired.
  • An ERP system

The following sequence diagram outlines the process.  The most important aspects of the execution are highlighted.

Kafka Headless Commerce

  • Step 1 - Notice the immediate response to the shopper's request to submit a cart.
  • Step 2 - Kafka orchestrates (queues) the execution of numerous backend steps.
  • Step 3 - Kafka has subscribed to Salesforce Platform Events and submits the order
  • Step 4 - Even within Salesforce, the order submission entails additional business code which takes time. Once complete the code fires a platform event which is caught by Kafka.
  • Step 5 - The user is notified of the completed order by email.

The Secret Sauce

Platform Events

By using platform events, businesses can automate and streamline their order processing, resulting in faster turnaround times and improved customer satisfaction.

In Salesforce, platform events are triggered using Apex code. Here is an example of how to raise a platform event after the order items of a given order receive additional data:

public class OrderItemHandler { public void processOrderItems(Id orderId)
{
   List<OrderItem> orderItems = [SELECT Id, Name, Quantity FROM OrderItem WHERE    
   OrderId = :orderId];

   for (OrderItem item : orderItems)
   {
     // Update the order item with additional data
        item.Quantity = item.Quantity + 1;     
        update item;

     // Raise the platform event
        OrderItemUpdatedEvent event = new OrderItemUpdatedEvent();
        event.orderId = orderId;
        event.orderItemId = item.Id;
        event.quantity = item.Quantity;
        event.timestamp = DateTime.now();

        EventBus.publish(event); 
       }
     }
   }

In this example, the processOrderItems method takes the ID of an order as a parameter and retrieves the order items for that order. It then loops through the order items, updating each one with additional data and raising a platform event for each updated item. The platform event, called OrderItemUpdatedEvent, contains information about the order, the updated order item, and the new quantity. This information can be used by other components of the Salesforce application to respond to the update in real time.

Kafka/Confluent

For background, Apache Kafka is a popular open-source messaging system that can be used to stream data between different applications and services. In addition, Confluent is a commercial version of Kafka that brings its own value adds, for example, its own Salesforce connectors. This allows for a quick connection and integration between Kafka/Confluent and Salesforce.  Kafka/Confluent can subscribe to Salesforce platform events and stream the data to a specific Kafka topic. Here is an example of how that code might look in Kafka / Confluent.

// Create a new Kafka Salesforce connector
KafkaSalesforceConnector connector = new KafkaSalesforceConnector();

// Set up the connector to use the appropriate Salesforce credentials connector.setUsername("my_username"); connector.setPassword("my_password"); connector.setLoginUrl("https://login.salesforce.com");

// Set up the connector to subscribe to the OrderItemUpdatedEvent platform event connector.setEventName("OrderItemUpdatedEvent");

// Set up the connector to send the data from the platform event to the "order-updates" topic in Kafka
connector.setTopic("order-updates");

// Start the connector
connector.start();

Conclusion

Platform events are a type of event-driven messaging system that allows different components of a Salesforce application to communicate with each other in real time. This means that when one part of the application performs an action, such as creating a new order, it can instantly trigger a response from other parts of the application. This can be particularly useful for processing a large numbers of orders in a short amount of time.

In our example, a business receives a large number of orders through its online store. With platform events, the process doesn’t have to wait until all participants are done. Instead, the submittal can respond instantly to the customer and free up their UI.  Later as Kafka and Platform Events are used to complete the order,  the system can automatically trigger another response such as sending a confirmation email to the customer, updating the inventory levels, and adding the order to the fulfillment queue. By separating the taking of an order from the enhancement and fulfillment steps, orders are received more quickly and efficiently

Platform events are also useful for integrating Salesforce with other systems and applications. For example, if a business uses a separate fulfillment system, platform events can be used to send information about new orders from Salesforce to the fulfillment system in real-time, allowing the orders to be processed and shipped more quickly.

Finally, platform events are scalable and can handle large volumes of data. As a business grows and receives more orders, Salesforce can handle the increased load without slowing down or crashing.

Platform events coupled with an asynchronous commerce process are a powerful tool for businesses that want to increase the number of order transactions.