Develop application with Azure Redis cache in local

11 Feb 2023

Azure Redis Cache offers distributed, in-memory cache for faster data access for applications that run from multiple machines but depend on shared data. It avoids repeated data fetch from slow external sources like APIs or databases by storing it on the super-fast, low latency, in-memory cache source.

Problem

When multiple developers are working from their local machines connecting to a shared Redis cache, it can lead to strange data fetch-related errors. e.g., one of the developers may suddenly decide to flush all keys to test his feature or introduce a structural change to the cached data. This dependency requires the developers to repeatedly communicate with others before any cache-related changes or flush operation which causes unnecessary productivity hurdles. 

Other Azure resources like Azure Files, Blobs, Table, Queues, CosmosDB, etc., are provided with local emulators that address such issues. If you are expecting a similar local emulator for Azure Cache - you'll be disappointed to see there is none provided by Microsoft. Also, you can see Microsoft's official stance in the following document;

https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-development-faq#is-there-a-local-emulator-for-azure-cache-for-redis-

Microsoft has suggested an alternative option in the above FAQ page using MSOpenTech version of redis-server.exe. But that project seems pretty old, unmaintained - probably has multiple outstanding security & functionality bugs that keep piling-up as it remains Outdated. 

If you are happy with the above warnings and want a quick way to run Redis server locally, then MsOpenTech redis server exe might be your preferred solution. But if you are part of organisation that requires the development tools to be regularly updated with security patches and tested, then you won't be allowed to run that old executable file.

Another option would be to shell out additional cost for purchasing other commercial services for offline development seats, that would solve the problem.
https://stackoverflow.com/a/10525215 

If you are not ready with the above options, then read through the rest of the article to run local in a recommended and cost-effective way.

Solution

Redis is an Open-Source project that is built to run on Linux machines. Hold on! I'm not recommending you to switch from windows-based IDE tool chain to Linux 😄

Windows offers WSL (Windows Subsystem for Linux) that allows you to run Linux packages within a Windows machine. It is much smaller in size and achieves virtualization with less-overhead compared to typical full-fledged Virtual machines image. WSL, WSLg is enough for most of our development needs that involve running Linux based packages/tools. 

Let me take through quick steps in setting it up in your local windows machine

1. Search for Turn Windows feature on or off and select the following highlighted features in the dialog

enable wsl2 dependent features on

2. Open command prompt in administrator mode, and run following script to make sure dependent packages are installed and updated to latest WSL version by running 

wsl --install
wsl --update

Above command will install the latest default Ubuntu within your WSL environment.  You can also check for other distributions, install them, and set them as default later.

 3. Now search and launch the Ubuntu terminal from the windows start

4. Try to ping some external domain from the Ubuntu terminal using ping www.google.com.

If you see an issue resolving the specified domain or see unknown host.
Then find "/etc/resolv.conf" and specify the nameserver for DNS resolution as 1.1.1.1 (Cloudflare) or 8.8.8.8 (Google). This should address the DNS issue.

Warning: Don't try to modify any files within WSL from File explorer, as this can corrupt the underlying LSXX filesystem. So, edit the file from the editors within Ubuntu terminal.

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false

nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4

5. Now, it's time to setup our Linux based Redis server package. Run following script from the Ubuntu terminal

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

The above script just adds a package source verified by the keyring, downloads the package, and finally installs it.

6. You can start the Redis server by running following

sudo service redis-server start

7. Verify the Redis installation by launching the "redis-cli" from the Ubuntu terminal.
Type "Ping" and on enter it should echo "Pong"

That's all, now you can configure the local redis(127.0.0.1:6379) in your application connection strings. 

Happy Hacking!!! ✨

Reference

Install Redis on Windows | Redis