Scale your .NET application with Entity Framework and Neon Postgres Read Replicas
Learn how to scale .NET applications with Entity Framework's DbContext and Neon Postgres Read Replicas
Neon read replicas are independent read-only compute instances that perform read operations on the same data as your primary read-write compute. A key advantage of Neon's architecture is that adding a read replica to a Neon project doesn't require additional storage, making it an efficient scaling solution.
This guide demonstrates how to leverage Neon read replicas to efficiently scale .NET applications using Entity Framework Core. You'll learn how to configure your DbContext to work with read replicas, enabling you to optimize your database operations and improve overall application performance.
Prerequisites
-
A Neon account and a Project. If you don't have one, you can sign up for a Neon account and create a project by following the Getting Started guide.
-
Basic knowledge of .NET Core
-
Dotnet SDK installed on your local machine. You can download it from the official .NET website.
-
Dotnet Entity Framework Core CLI tools installed. You can install them by running the following command:
Build the Todo app
To demonstrate how to use Neon read replicas with Entity Framework Core, we'll build a simple Todo application that uses a Neon database. We'll then update the application to use a read replica for read operations, improving the application's performance and scalability. This is just a simple example to demonstrate the concept, and you can apply the same principles to more complex applications.
Part 1: Build the initial Todo app with a single database
Set up the project
Create a new .NET Core Web API project using the following commands:
Delete the WeatherForecast files
Delete the files WeatherForecast.cs
and Controllers/WeatherForecastController.cs
as we won't be using them:
Install required packages
Install Entity Framework Core Design and Npgsql packages:
Best Practice
Ensure you install package versions that match your .NET version. You can verify your .NET version at any time by running dotnet --version
.
Create the Todo model
Create a new file Models/Todo.cs
:
Create the database context
Create a new file Data/TodoDbContext.cs
:
appsettings.json
/ appsettings.Development.json
:
Update Add the connection string:
Create the TodoController
Create a new file Controllers/TodoController.cs
:
This controller defines CRUD operations (Create, Read, Update, Delete) for Todo items using HTTP requests. It uses TodoDbContext
to interact with the database.
Program.cs
with the following content:
Update This code configures the application to use Entity Framework Core with a PostgreSQL database. It registers TodoDbContext
with the application's services and sets up the database connection using the connection string from appsettings.json
/ appsettings.Development.json
.
Create Migrations
Run the following commands to create and apply the initial migration:
Run the application
Start the application:
Visit the Swagger UI at http://localhost:5001/swagger
to test the API.
Part 2: Use a read replica for read-only operations
Create a read replica on Neon
To create a read replica:
- In the Neon Console, select Branches.
- Select the branch where your database resides.
- Click Add Read Replica.
- On the Add new compute dialog, select Read replica as the Compute type.
- Specify the Compute size settings options. You can configure a Fixed Size compute with a specific amount of vCPU and RAM (the default) or enable autoscaling by configuring a minimum and maximum compute size. You can also configure the Suspend compute after inactivity setting, which is the amount of idle time after which your read replica compute is automatically suspended. The default setting is 5 minutes.
note
The compute size configuration determines the processing power of your database. More vCPU and memory means more processing power but also higher compute costs. For information about compute costs, see Billing metrics.
- When you finish making selections, click Create.
Your read replica compute is provisioned and appears on the Computes tab of the Branches page.
Navigate to the Dashboard page, select the branch where the read replica compute was provisioned, and set the compute option to Replica to obtain the read replica connection string:
Update the TodoDbContext
Modify Data/TodoDbContext.cs
to include separate read and write contexts:
Update Program.cs
Modify Program.cs
to include both read and write contexts:
appsettings.json
/ appsettings.Development.json
Update Add the read replica connection string:
Update the TodoController
Modify Controllers/TodoController.cs
to use separate read and write contexts:
Did you know?
You can use dotnet-ef migrations even with multiple db contexts. You can specify the context to use by passing the --context
option to the dotnet ef
command.
The Todo API is now set up to use separate read and write contexts, leveraging Neon's read replica feature. Read operations (GET
requests) will use the read replica, while write operations (POST
, PUT
, DELETE
) will use the primary database.
You can find the source code for the application described in this guide on GitHub.
Conclusion
This setup allows you to distribute your read load across one or more read replicas while ensuring that all write operations are performed on the primary database. Monitor your application's performance and adjust the number of read replicas as needed to handle your specific load requirements. With Neon, you can quickly scale out with as many read replicas as you need.
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.