concept scalability in category .net

appears as: scalability, scalability
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

For performance and scalability reasons, in a concurrent application it’s useful to combine a lazy evaluation that can be executed on demand using an independent thread. The Lazy initializer Lazy<T> can be utilized to implement a useful pattern to instantiate objects that require asynchronous operations. Let’s consider the class Person that was used in the previous section. If the first and second name fields are loaded from a database, you can apply a type Lazy<Task<Person>> to defer the I/O computation. It’s interesting that between Task<T> and Lazy<T> there’s a commonality: both evaluate a given expression exactly once.

Listing 2.29 Lazy asynchronous operation to initialize the Person object
Lazy<Task<Person>> person =
 new Lazy<Task<Person>>(async () =>   #1  
    {
        using (var cmd = new SqlCommand(cmdText, conn))
        using (var reader = await cmd.ExecuteReaderAsync())
        {
            if (await reader.ReadAsync())
            {
                string firstName = reader["first_name"].ToString();
                string lastName = reader["last_name"].ToString();
                return new Person(firstName, lastName);
            }
        }
        throw new Exception("Failed to fetch Person");
    });

async Task<Person> FetchPerson()
{
 return await person.Value;   #2  
}

#1   Shows the asynchronous lambda constructor for the Lazy type
#2   Materialized asynchronously the Lazy type

In this example, the delegate returns a Task<Person>, which asynchronously determines the value once and returns the value to all callers. These are the kind of designs that ultimately improve the scalability of your program. In the example, this feature implements asynchronous operations using the async-await keywords (introduced in C# 5.0). Chapter 8 covers in detail the topics of asynchronicity and scalability.

This is a useful design that can improve scalability and parallelism in your program. But there’s a subtle risk. Because the lambda expression is asynchronous, it can be executed on any thread that calls Value, and the expression will run within the context. A better solution is to wrap the expression in an underlying Task, which will force the asynchronous execution on a thread-pool thread. This listing shows the preferred pattern.

Functional programming facilitates the implementation of a stateless server (figure 14.1), which is an important asset for building scalability when architecting large web applications required to handle a huge number of request concurrently, such as social networks or e-commerce sites. A program is stateless when the operations (such as functions, methods, and procedures) aren’t sensitive to the state of the computation. Consequently, all the data used in an operation is passed as inputs to the operation, and all the data used by the operations invoked is passed back as outputs. A stateless design never stores application or user data for later computational needs. The stateless design eases concurrency, because it’s easy for each stage of the application to run on a different thread. The stateless design is the key that makes the design able to scale out perfectly according to Amdahl’s Law.

Figure 14.1 Server with state (stateful) compared to a server without state (stateless). The stateful server must keep the state between requests, which limits the scalability of the system, requiring more resources to run. The stateless server can auto-scale because there’s no sharing of state. Before stateless servers, there can be a load balancer that distributes the incoming requests, which can be routed to any machine without worrying about hitting a particular server.

c14-01.png
NHibernate in Action

This is an excerpt from Manning's book NHibernate in Action.

The database isolates the effects of concurrent database transactions. It should appear to the application that each transaction is the only transaction currently accessing the database (even when it isn’t). Isolation is expensive. The database must allocate significant resources to each transaction for the duration of the transaction. In particular, as we’ve discussed, many databases lock rows that have been read or updated by a transaction, preventing access by any other transaction, until the first transaction completes. In highly concurrent systems with hundreds or thousands of updates per second, these locks can prevent scalability if they’re held for longer than absolutely necessary. For this reason, you shouldn’t hold the database transaction (or even the ADO.NET connection) open while waiting for user input. If a user takes a few minutes to enter data into a form while the database is locking resources, then other transactions may be blocked for that entire duration! All this, of course, also applies to an NHibernate ITransaction, because it’s an adaptor to the underlying database transaction mechanism.

The MSDN defines six design goals: availability, reliability, manageability, securability, performance, and scalability. We look at each of them with NHibernate in mind.