Basic Redis Leaderboard Demo .Net 6 with Write-Behind
July 6, 2023 ยท View on GitHub
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. At the moment, our preferred way to implement this pattern is to use RedisGears, but is not availble 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
- Use Azure Function to use Read-Through pattern
- Ability to configure Cache-Aside pattern
- Ability to configure Real-Time Ingestion pattern with RDI
Architecture
Write-Behind & Read-Through

Cache-Aside

Real-Time Ingestion

Prerequisites
- VS Code or Visual Studio
- .Net 6
- OSX or Windows
- Azure SQL
- Configuration steps here
- Azure Cache for Redis Enterprise
- Configuration steps here
- If using Real Time Ingestion with RDI use version v0.104.0 when creating the RDI CLI Pod. RDI started support of creating sorted sets starting this version. Please use the Job yaml file located here
Installation
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" UseCacheAside = "True to use the Cache Aside pattern" UsePreFetch = "True to use Real Time Ingestion" IsSSL = "True if Redis configure with SSL" ReadThroughFunctionBaseUrl = "Url of the Read Through Function" -
Run the EF Core command
dotnet ef database update -
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.
Setup Pre-Fetch (Optional)
To setup pre-fetch you need to configure RDI either through AKS or VMs.
1. Deploy AKS Cluster
Login to your subcription via Azure CLI:
az login
Create a cluster:
az aks create \
--resource-group <resource-group-name> \
--name <cluster-name> \
--enable-managed-identity \
--node-count 4 \
--generate-ssh-keys \
--node-vm-size Standard_D4s_v3
Get cluster credentials:
az aks get-credentials --resource-group <resource-group-name> --name <cluster-name>
2. Deploy Redis on AKS
Follow the steps here to deploy Redis on AKS
3. Deploy RDI on AKS
Follow the steps here to deploy RDI on AKS