Open Closed

Redis skip initial on Start-up #6377


User avatar
0
NH-Support created

I have an Application that uses the ABP Commercial Template,

in the appsettings.json i have a Redis server configured,

The current situation is when the application can't find the Redis server it will crash what i want is: when the application first start-up, if the Redis server is not available for some reason i want to skip it and continue to run and fetch Data from the SQL DB

Later on when the application try to fetch data it will check for Redis server availability if found it will fetch data from it, if not it will continue to the SQL DB Is that doable ?


5 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think you shouldn't stop the application from crashing if Redis server is not available

    In distributed and microservice applications, distributed caching is necessary. It's like SQLServer. Imagine if the SQLServer is unavailable, will you choose to keep the application running?

    You can check this: https://support.abp.io/QA/Questions/6344/Why-do-we-need-to-install-Redis-When-is-it-used-Why-Redis-is-needed

  • User Avatar
    0
    NH-Support created

    Hi,

    I think you shouldn't stop the application from crashing if Redis server is not available

    In distributed and microservice applications, distributed caching is necessary. It's like SQLServer. Imagine if the SQLServer is unavailable, will you choose to keep the application running?

    You can check this: https://support.abp.io/QA/Questions/6344/Why-do-we-need-to-install-Redis-When-is-it-used-Why-Redis-is-needed

    Hi liangshiwei

    I already have a Redis cluster, But my point is that Redis is add-on for the application for optimization and therefore the application should consider it as optional,

    let's say that for whatever reason the Redis server is not available or can't accept connection in that case can i skip it and get the data i want from my SQL DB server

    because right now my application just crashes if redis is not there

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Ok, Although I don't recommend you to do this, it should be possible.

    For example(this just is an idea):

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IDistributedCache))]
    public class MyRedisCache : IDistributedCache, ICacheSupportsMultipleItems
    {
        private readonly AbpRedisCache _cache;
    
        public MyRedisCache(AbpRedisCache cache)
        {
            _cache = cache;
        }
    
        public byte[]? Get(string key)
        {
            if (IsRedisAvailable())
            {
                return _cache.Get(key);
            }
    
            return null;
        }
        
        protected virtual bool IsRedisAvailable()
        {
            // check redis connection
            return true;
        }
        
        .....
    }
    
    
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        .....
        context.Services.AddSingleton<AbpRedisCache>();
    }
    
  • User Avatar
    0
    NH-Support created

    Hi,

    Ok, Although I don't recommend you to do this, it should be possible.

    For example(this just is an idea):

    [Dependency(ReplaceServices = true)] 
    [ExposeServices(typeof(IDistributedCache))] 
    public class MyRedisCache : IDistributedCache, ICacheSupportsMultipleItems 
    { 
        private readonly AbpRedisCache _cache; 
     
        public MyRedisCache(AbpRedisCache cache) 
        { 
            _cache = cache; 
        } 
     
        public byte[]? Get(string key) 
        { 
            if (IsRedisAvailable()) 
            { 
                return _cache.Get(key); 
            } 
     
            return null; 
        } 
         
        protected virtual bool IsRedisAvailable() 
        { 
            // check redis connection 
            return true; 
        } 
         
        ..... 
    } 
     
     
    public override void ConfigureServices(ServiceConfigurationContext context) 
    { 
        ..... 
        context.Services.AddSingleton<AbpRedisCache>(); 
    } 
    

    hi

    thanks for quick response

    I believe this check is performed after the initial start-up, in my case the app is not starting at all

    on start application if Redis connection string is not available / URL not correct, how can i skip it and continue the start-up

    because right now the application crashes if the Redis URL not available ? and the Try/Catch Solution didn't worked also

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can try:

    ConnectionMultiplexer.Connect(configuration["Redis:Configuration"], configure =>
    {
        configure.AbortOnConnectFail = false;
    });
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09