SignalR Scaling with Azure SignalR Service

SignalR is the best way to enable real-time communication in modern .NET web applications. So let’s say you have built your first app with SignalR, deployed it to Azure, and everything is working great. After seeing a huge influx of new users, you decide to scale out your App Service to add another instance to handle the extra load. Shortly after, you get complaints from users that the real-time notifications aren’t working, and after debugging it with your team, you find that SignalR is working “about half the time.”
You come to the realization (hopefully, very quickly) that since SignalR runs within the server process, special care must be taken in environments that involve more than one running instance. There are a few different ways to handle a server farm / multiple instance web application with SignalR:

  1. Sticky Sessions. Referred to as “ARR Affinity” (Application Request Routing Affinity) in Azure App Service, this will keep users “stuck” to the same server instance in a multi-server environment. However, this adds constraints on the load balancing across the instances, and it also prevents you from notifying clients across multiple server instances.
  2. Redis backplane. SignalR can be configured to use Redis as a backplane to manage clients across multiple server instances. While you can accomplish everything you need to scale SignalR with Redis, it is a more complicated setup and it has its limitations when compared to the third, recommended approach:
  3. Azure SignalR Service. This is certainly the easiest option to configure and provides the most flexibility. With a few lines of code and setup in Azure, your app can seamlessly delegate the management of SignalR connections to the SignalR Service that will allow you to scale your app without the need to manage how SignalR is hosted.

Azure SignalR Service Configuration

The best part about using Azure SignalR Service is that it is very easy to configure and use. By default, Azure SignalR will use the default configuration key of Azure:SignalR:ConnectionString. You will get this ConnectionString value when you create a new Azure SignalR Service in the Azure Portal. The following ConfigureServices() code will enable SignalR Service in your app with the default ConnectionString key:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR()
            .AddAzureSignalR();
}

Even though it is only one extra line of code, there is a lot that happens automatically for you. When web clients connect to your existing SignalR Hub, they will be redirected to instead connect to the Azure SignalR Hub. And when your app sends messages to clients, it will automatically route those messages through the Azure SignalR Service.

Azure SignalR Service Cost

The main downside to Azure SignalR Service right now is the current pricing structure. There is a free tier that supports 20 connections per unit and 20,000 messages per day. This is sufficient for a small-scale app or for a development or testing environment, but the free tier connection limit will quickly become a problem with a moderate number of users. The next (and only other) tier is “Standard” which allows 1,000 connections per Unit, up to a maximum 100 Units, and pricing is based on the number of Units, with a minimum of 1 Unit. So while this can scale to a large number of concurrent connections, the lowest possible cost per month is close to $50/month (depending on your Azure region).
There have been calls for consumption-based SignalR Service pricing, which would hopefully lower the cost at the low end and make the pricing much more granular based on the number of concurrent users. It is good to know that David Fowler at Microsoft is aware of this, but no announcements yet on the timing or if this would be added:

Dynamic Configuration and Cost Sharing

When we use SignalR Service on our Trailhead projects, we often configure it in a way to do two things:

  1. Allow the application to use either in-process SignalR (for local development and testing) or the hosted SignalR Service, based on configuration, and
  2. Allow for a SignalR ApplicationName to be set

When you set the ApplicationName property in the AddAzureSignalR() constructor, it allows you to create isolated sets of users (or applications) that can all share the same SignalR Service. If you have multiple applications that need SignalR and each application needs more users than the free tier, but fewer than what the Standard tier provides, you can use the ApplicationName to partition them and share the cost of the Standard tier. And while it is possible to use the same SignalR Service for separate environments (development, testing, production), it is still recommended to use a separate SignalR Service for your production environment. Cost-sharing with ApplicationName still isn’t as good as having a consumption-based pricing model, but until that is available, this can possibly reduce your cost by sharing a single Azure SignalR Service across multiple hosted applications.
Here is the full configuration that will use SignalR in-process if no connection string is set in the configuration (for local testing), and it also allows an ApplicationName to be specified in configuration so you can have multiple applications use the same SignalR ConnectionString, but each set of application users are isolated according to the ApplicationName:

public void ConfigureServices(IServiceCollection services)
{
    // Get the SignalR ConnectionString to determine whether to run locally or with Azure SignalR Service
    var signalRConnString = Configuration["SignalRConnectionString"];
    if (string.IsNullOrEmpty(signalRConnString))
    {
        // Run SignalR in-process for local development
        services.AddSignalR();
    }
    else
    {
        // Use Azure SignalR Service with connection string and ApplicationName
        services.AddSignalR().AddAzureSignalR(opt =>
        {
            opt.ConnectionString = signalRConnString;
            opt.ApplicationName = Configuration["SignalRAppName"];
        });
    }
}

 
 

Picture of Josh Eastburn

Josh Eastburn

Josh Eastburn is an experienced software architect who brings over 20 years of experience in web and mobile technologies to our leadership team. Josh is a certified Telerik Progress Development Expert and enjoys working with the latest technologies. He specializes in using C#, .NET Core, JavaScript, HTML5, CSS, XAML, Xamarin, and SQL Server to deliver enterprise-scale applications. Josh’s background includes development, analysis, and project management across a variety of industries, such as financial services, education, and logistics.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.