Split actor model logic into modules

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.

New contributor

Kilian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật