0

I'm starting a project using .NET and Microsoft Orleans, running on Kubernetes. The main reason I chose Orleans is that I have a system that deals with accounts. There can be millions of accounts, but only thousands will be active at any given time, with occasional peaks of tens of thousands. I need the system to scale during these peaks. I also need to ensure that only one operation can be performed on an account at a time to avoid concurrency issues.

In my system, there is only one type of Grain, called AccountGrain, which represents accounts and handles all operations that can be performed on them. This Grain has a private attribute with a list of objects that represent the account's state as a cache. Most operations use this cache (LINQ queries, adding objects, etc.), so performance is critical.

Most operations in this system work with accounts, even though they belong to different "domains." For example, I have a "Purchases" domain, an "Contract Status" domain, and a few others. Each domain has a library with its own REST API, but they all end up calling the AccountGrain. I'm concerned that the AccountGrain will end up with a huge number of functions from all these domains, making it unmanageable, violating the Single Responsibility Principle, and having too many lines of code.

I would like to separate these functions by domain or functionality group, ideally each in its own library (module), while still maintaining access to the cached state in the grain, access to timers/reminders, with the concurrency management of Orleans, etc. Being able to call other modules is a must (e.g., in the example below the Purchase method needs to call the Contract State module to create a contract, and then store the result in the grain's cache).

I tried to move grain's logic to classes and inject these classes into the grain. The problem with this approach is that I loose access to the grain. Sure I can pass the state as a parameter, but I loose access to timers, reminders, state management (read/write), and other functionalities implemented in Orleans' "Grain" base class.

I also though on creatin an interface wrapping these functionalities, but then I would need to create "Public methods", and calling these public methods from the grain itself are going to cause deadlocks.

Example:

public class AccountGrain : Grain
{
    // This is like a “cache” because most methods need to know the products an account has 
    private List<PurchasedProduct> _purchasedProductsCache { get; }

    // This is like a “cache” because most methods need to know the contracts an account has 
    private List<Contract> _activeContractsCache { get; }


    // This a very simplified example of the “purchasing” domain actions
    #region Purchasing Domain

    public Task Purchase(int productId)
    {
        // Do actions
        // Create a contract for the purchase (calling CreateContract(); see next methods) and update _purchasedProductsCache 
    }
    public Task CancelPurchase(int purchaseId) {
        // Do actions
        // Update _purchasedProductsCache and cancel a contract for the purchase (calling CreateContract(); see next methods)
    }

    #endregion


    // This a very simplified example of the “card processing” domain actions
    #region Card Processing Domain

    public Task ProcessCardTap(int cardId)
    {
        // Do actions
        // Some of these actions can be calling Purchase, CreateContract, etc.
    }

    public Task ProcessDelayedCardTap(int cardId) 
    {
        // Do actions
        // Some of these actions can be calling Purchase, CreateContract, etc.
    }

    #endregion

    // This a very simplified example of the “contract management” domain actions
    #region Contract Management
    
    public Task CreateContract(CreateContractRequest request)
    {
        // Do actions
        // Add new contract to _activeContractsCache
    }

    public Task CancelContract(int contractId)
    {
        // Do actions
        // Delete contract from _activeContractsCache
    }

    #endregion
}

Note: I originally asked this on StackOverflow, but it was suggested that this platform might be more appropriate.

3
  • You’re right to be concerned. This AccountGrain should not contain state or behavior for other domains. Why don’t you create grains for these other concepts, each with their own state and behavior? You can leverage Orleans’ streaming mechanism to easily share events between grains.
    – Rik D
    Commented Jul 7 at 18:45
  • 1
    There is nothing wrong in moving a question from one SE site to another. But when you do so, you need to delete the original question on first site before you post the same text again, otherwise it becomes a crosspost, which is not accepted by the community.
    – Doc Brown
    Commented Jul 7 at 18:47
  • Sorry, I didn't know crosspost rules. I've just deleted the other post. @RikD Almost all "concepts" work (read and even update) with the same state that is stored in Accounts as an in-memory variable. But the state must only be updated when the operation has finished. Any other operation mustn't begin until that operation is finished. If I split into more grains, how can I keep this behaviour? For performance reasons, I want to follow Microsoft's best practice: "Avoid chatty communication between grains".
    – Kilian
    Commented Jul 9 at 16:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.