Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

service fabric - is stateful service single instance per partition

I am trying to digest the Service fabric architectural patterns and its best practices.

use case:

I define a stateful service with 26 partitions, and in each partition I am storing words that are with the same first letter.

  • 1) Does this means that I actually have 26 instances of my stateful service?
  • 2) When outside of the stateful service, i.e in the caller - I am constructing a URI for my service fabric client, specifying the partition ID I want the client to operate on. Does this mean that once I am in the context of the stateful service (i.e service client instantiaded and called the stateful service) - I cannot reference other partitions?
  • 3) Is it true to say that a stateful service is a unit of work that needs to know which partition to operate on, and cannot make a decision on its own? Here I am referring to the many examples where inside the RunAsync method of a stateful service, there are calls to the underlying reliable store, for example, the code taken from this post:

    protected override async Task RunAsync(CancellationToken cancelServicePartitionReplica)
    {
    var myDictionary = await   this.StateManager.GetOrAddAsync<IReliableDictionary<string, int>> ("myDictionary");
    
    var partition = base.ServicePartition.PartitionInfo.Id;
    byte append = partition.ToByteArray()[0];
    
    while (!cancelServicePartitionReplica.IsCancellationRequested)
    {
    
    // Create a transaction to perform operations on data within this partition's replica.
    using (var tx = this.StateManager.CreateTransaction())
    {
        var result = await myDictionary.TryGetValueAsync(tx, "A");
    
        await myDictionary.AddOrUpdateAsync(tx, "A", 0, (k, v) => v + append);
        ServiceEventSource.Current.ServiceMessage(this,
            $"Append {append}: {(result.HasValue ? result.Value : -1)}");
        await tx.CommitAsync();
    }
    
    // Pause for 1 second before continue processing.
        await Task.Delay(TimeSpan.FromSeconds(3),  cancelServicePartitionReplica);
        }
    }
    

So, probably my statement 3) is wrong - A stateful service may call its internal storage without someone (a service client) to call it externaly and to supply information for the exact partition. But then, how the code above decides into which partition to put its data? And most importantly, how to later query that data via a service client which should provide an exact partition ID?

like image 989
akrsmv Avatar asked Sep 15 '25 10:09

akrsmv


1 Answers

Stateful service 'instances' are actually replicas. You configure how many replicas you have for every partition (for performance, scaling, high availability & disaster recovery). Only one replica (primary) does writes. All replicas (secondaries and primary) may be used for reads. A replica contains a shard of your data set. Data in partition 1 is not shared with partition 2.

Clients calling Stateful services need to decide for themselves with which partition they want to communicate. Services can only read/write in their own partition (directly).

More info here.

like image 94
LoekD Avatar answered Sep 18 '25 10:09

LoekD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!