Realtime Leaderboard Demo with Redis (ACRE, Write-Behind), .NET 6, Azure SQL with Azure Functions
Summary
We based this project from our Basic Leaderboard project to show how you can acess Azure Cache for Redis using .NET 6 and added a Write-Behind pattern to Azure SQL. This pattern can be implemented using RedisGears or Azure Functions, however RedisGears is not available in ACRE at this time.
We decided to implement the Write-Behind pattern using an Azure Function that reads the key change sent through a Redis Stream. It is using a pulling mechanism but we are looking forward to implement it using an event-driven approach.
Features
- Listens to Key Space Notifications to add changes to the stream
- Use StackExchange.Redis to access ACRE
- Use Azure Function to sync the updates to Azure SQL db using a Write-Behind pattern
Architecture
Prerequisites
- VS Code or Visual Studio
- .Net 6
- OSX or Windows
- Azure SQL
- Configuration steps here
- Azure Cache for Redis Enterprise
- Configuration steps here
Installation
Azure SQL
Run SQL Script to create table:
Copy the contents of the script named "CreateCompanyTable.sql". The file is located inside the SQL folder located in the Solution Items folder.
Open the query editor of your preferred database tool (Azure Data Studio or SQL Server Management Studio) and paste the SQL script copied during Step 1.
Run the script to create the table.
Front End
If you need to run the front end by itself:
- Go to the ClientApp folder
cd BasicRedisLeaderboardDemoDotNetCore
cd ClientApp
code .
- Install node modules
npm install
- Run front end
npm run serve
Quickstart
- Clone the git repository
git clone https://github.com/Redislabs-Solution-Architects/acre-sql-demo
Open it with your Visual Studio Code or Visual Studio
Update App Settings to: include actual connection to Redis, Azure SQL and configure the application:
RedisHost = "Redis server URI"
RedisPort = "Redis port"
RedisPassword = "Password to the server"
IsACRE = "True if using Azure Cache for Redis Enterprise"
AllowAdmin = "True if need to run certain commands"
DeleteAllKeysOnLoad = "True if need to delete all keys during load"
LoadInitialData = "True if running the application for the first time and want to load test data"
UseReadThrough = "True to use the Read Through pattern"
UseWriteBehind = "True to use the Write Behind pattern"
ReadThroughFunctionBaseUrl = "Url of the Read Through Function"
Update local.settings.json for the SQLSweeperFunction
- Replace "--SECRET--" with the real connection strings for Azure SQL and Redis
"ConnectionStrings": { "SQLConnectionString": "--SECRET--", "RedisConnectionString": "--SECRET--" }
Update local.settings.json for the ReaderFunction
- Replace "--SECRET--" with the real values
"ReaderFunctionSettings:RedisHost": "--SECRET--", "ReaderFunctionSettings:RedisPort": "10000", "ReaderFunctionSettings:RedisPassword": "--SECRET--", "ReaderFunctionSettings:IsACRE": "true", "ReaderFunctionSettings:SQLConnectionString": "--SECRET--"
Run backend
dotnet run
Run Azure Function (Write Behind)
- You can try the Write Behind pattern by setting "true" to the "UseWriteBehind" configuration variable inside the appsettings.json. If so, you need to run the Write Behind Function by:
cd SQLSweeperFunction func start
Run Azure Function (Read Through)
- You can try the Read Through pattern by setting "true" to the "UseReadThrough" configuration variable inside the appsettings.json. If so, you need to run the Read Through function by:
cd ReaderFunction func start
Note: Static content runs automatically with the backend part. In case you need to run it separately, please see README in the client folder.