h5bp / Front-end-Developer-Interview-Questions

A list of helpful front-end related questions you can use to interview potential candidates, test yourself or completely ignore.

Home Page:https://h5bp.org/Front-end-Developer-Interview-Questions/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asp.Net Core with redis implementation gives 502 Error for high traffic

tsdev1 opened this issue · comments

In Our application, we are having heavy traffic of users and it is around 2000 request per second.
We have created application in Asp.Net core and used dapper. We are using redis cache manager for distibuted caching purpose.

When we hosted this site and checked it for few (20 or 30) request per second, it was working fine. But when we are hitting more than around 50 requests per second, site is giving
502 - Web server received an invalid response while acting as a gateway or proxy server.

We changed redis caching to memory cache and then it started to work fine for all 2000 requests per second. We are using redis version 3.2.100
So, here using redis we are not able to run this site for more requests and getting 502 error with heavy traffic.

Code written for Redis Cache :

using Common;
using Common.DependencyInjection;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Text;

namespace Service.Caching
{
[TransientDependency(ServiceType = typeof(ICacheManager))]
public class RedisCacheManager : ICacheManager
{
    private readonly string _redisHost;
    private readonly int _redisPort;
    private readonly string _redisPassword;
    private readonly ConfigurationOptions _configurationOptions;
   
    public RedisCacheManager()
    {
        _redisHost = ConfigItems.RedisHost;
        _redisPassword = ConfigItems.RedisPassword;
        _redisPort = ConfigItems.RedisPort;

        _configurationOptions = new ConfigurationOptions();
        configurationOptions.EndPoints.Add(_redisHost, redisPort);
        _configurationOptions.Ssl = false;
        //_configurationOptions.Password = _redisPassword;
        _configurationOptions.AbortOnConnectFail = false;
        _configurationOptions.SyncTimeout = int.MaxValue;
        _configurationOptions.DefaultVersion = new Version(2, 8, 8);
        _configurationOptions.WriteBuffer = 10000000;
        _configurationOptions.KeepAlive = 180;
    }

    /// <summary>
    /// Gets or sets the value associated with the specified key.
    /// </summary>
    /// <typeparam name="T">Type</typeparam>
    /// <param name="key">The key of the value to get.</param>
    /// <returns>
    /// The value associated with the specified key.
    /// </returns>
    public T Get<T>(string key)
    {
        using (var connection = ConnectionMultiplexer.Connect(_configurationOptions))
        {
            var db = connection.GetDatabase(-1);
            //return (T)(object)db.StringGet(key);

            return (T)ConvertToObject<T>(db.StringGet(key));
        }
    }

    /// <summary>
    /// Adds the specified key and object to the cache.
    /// </summary>
    /// <param name="key">key</param>
    /// <param name="data">Data</param>
    /// <param name="cacheTime">Cache time</param>
    public void Set(string key, object data, int cacheTime)
    {
        if (data == null)
            return;

        DateTime expireDate;
        if (cacheTime == 99)
            expireDate = DateTime.Now + TimeSpan.FromSeconds(30);
        else
            expireDate = DateTime.Now + TimeSpan.FromMinutes(cacheTime);

       var value = (RedisValue)ConvertToByteArray(data);

        using (var connection = ConnectionMultiplexer.Connect(_configurationOptions))
        {
            var db = connection.GetDatabase(-1);
            db.StringSet(key, value, new TimeSpan(expireDate.Ticks));
        }
    }

    /// <summary>
    /// Gets a value indicating whether the value associated with the specified key is cached
    /// </summary>
    /// <param name="key">key</param>
    /// <returns>
    /// Result
    /// </returns>
    public bool IsSet(string key)
    {            
        using (var connection = ConnectionMultiplexer.Connect(_configurationOptions))
        {
            var db = connection.GetDatabase(-1);
            return db.KeyExists(key);
        }
    }

    /// <summary>
    /// Removes the value with the specified key from the cache
    /// </summary>
    /// <param name="key">/key</param>
    public void Remove(string key)
    {
        using (var connection = ConnectionMultiplexer.Connect(_configurationOptions))
        {
            var db = connection.GetDatabase(-1);
            db.KeyDelete(key);
        }
    }

    /// <summary>
    /// Removes items by pattern
    /// </summary>
    /// <param name="pattern">pattern</param>
    public void RemoveByPattern(string pattern)
    {            
        using (var connection = ConnectionMultiplexer.Connect(_configurationOptions))
        {
            var server = connection.GetServer(_redisHost, _redisPort);
            var keysToRemove = server.Keys(pattern: "*" + pattern + "*");//-1, pattern);
            foreach (var key in keysToRemove)
                Remove(key);
        }
    }

    /// <summary>
    /// Clear all cache data
    /// </summary>
    public void Clear()
    {
        using (var connection = ConnectionMultiplexer.Connect(_configurationOptions))
        {
            var server = connection.GetServer(_redisHost, _redisPort);
            //var keysToRemove = server.Keys(-1);
            var keysToRemove = server.Keys();
            foreach (var key in keysToRemove)
                Remove(key);
        }
    }

    /// <summary>
    /// Converts to byte array.
    /// </summary>
    /// <param name="data">The data.</param>
    /// <returns>System.Byte[].</returns>
    private byte[] ConvertToByteArray(object data)
    {
        if (data == null)
            return null;

        string serialize = JsonConvert.SerializeObject(data);
        return Encoding.UTF8.GetBytes(serialize);
    }

    /// <summary>
    /// Converts to object.
    /// </summary>
    /// <param name="data">The data.</param>
    /// <returns>System.Object.</returns>
    private T ConvertToObject<T>(byte[] data)
    {
        try
        {
            return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(data));
        }
        catch (Exception ex)
        {
            return (T)Activator.CreateInstance(typeof(T));
        }
    }
}
}

This is not the appropriate place to ask this question. Please close and ask somewhere better, like Stack Overflow or Server Fault.