Part 2 – Web Farm Example (Docker, Redis Cache, Haproxy, .Net Core Web API)

In this article, I will add redis cache to web farm project. To do this I added redis configurations to docker-compose.yml. Latest version of the file is looking like following.

version: '2'

services:

    ali_webapi:
        image: ali/webapi
        environment:
            - ASPNETCORE_ENVIRONMENT=Development
        ports:
            - "9901:80"
           
    ali_redis:  
        image: ali/redis
        ports:
            - "6379:6379"

And I am changing up.ps1 script file and adding a script to up redis cache container.

docker rm $(docker ps -aq)
docker-compose up -d ali_redis
sleep 3
docker-compose up -d ali_webapi

I added redis up script before web api script. If there are configurations to store cache data at the web api project startup time, we should run the redis first to don’t miss any cache data.

Finally, I am adding some code in web api project to use redis cache to store cache data. Before this change we need to download redis cache extensitons. Nuget packages that we should download;

Microsoft.Extensions.Caching.Redis
StackExchange.Redis.StrongName

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
 
    services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "localhost";
        options.InstanceName = "redisInstance";
    });
}

And now, we can run project on docker with powershell scripts like in previous article. I am running powershell scripts in order (first, build-all.ps1 and second up.ps1) in location WebFarmExample > docker_config.

Here is the powershell commands that I run.

powershell scripts to run docker

Now, let’s check if redis cache is actually work. To understand if redis is realy running, I will call the actions in controller that are managing the cache keys. For example, I will call action that set cache key and second I will call action to get cache key. And result:

call api to set cache

We are getting above errors, because redis is using different ip and this ip is assigned by docker automatically. To see redis ip you can run docker inspect container_id command.

Docker command to get container ip

Docker command to get container ip

There are two way to fix this. First is hardcoded; we can add this ip to redis configurations in web api startup class.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
 
    services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "172.21.0.2";
        options.InstanceName = "redisInstance";
    });
}

After this change, I am running build and up scripts again. (I forgot: before run build and up scripts we should run this script to stop docker containers docker-compose down -v –rmi local and also you can create a down.ps1 script and you can add this command to in down.ps1 file)

And, now I am testing web api project again and result;

set cache data

get cache data

There an elegant way to fix this error that I mentioned above. Final version of Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
 
    services.AddDistributedRedisCache(options =>
    {
        options.Configuration = Dns.GetHostAddressesAsync("dockerconfig_ali_redis_1").Result.FirstOrDefault().ToString();
        options.InstanceName = "redisInstance";
    });
}

dockerconfig_ali_redis_1 is the name of redis container. After this changing, web api project will resolve the dns of machine that is deployed on.

In this article, I finished the adding redis step. Next step; I will add the haproxy and finish the article.

Source code of the project finished version: https://github.com/alirizaadiyahsi/WebFarmExample

Part 1 – Web Farm Example (Docker, Redis Cache, Haproxy, .Net Core Web API)

Part 3 – Web Farm Example (Docker, Redis Cache, Haproxy, .Net Core Web API)

#net-core, #docker, #haproxy, #redis-cache, #web-api, #web-farm