Redis Cache memory Status

Viewed 213

I need to create a page where I can show the Redis cache Memory status like How much memory it has been assign and how much it is using. Is there any way using spring boot we can get all this information?

2 Answers

Yes it is possible. I'm telling you how to do it using Redis-client: Jedis

After adding jedis dependency into your project you can define a bean like:

    @Bean
    public JedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory();            
        //in-case if you have a cluster
        //return new JedisConnectionFactory(new RedisClusterConfiguration(Arrays.asList("127.0.0.1:6379,127.0.0.1:6369".split(","))));
    }

And now you could use it like:

class DummyClass{
    @Autowired
    JedisConnectionFactory factory;

    void printServerInfo(){
      Properties info = factory.getConnection().info()
      System.out.println(info)
    }
}

This is a very basic example how you can get the server info using jedis in spring-boot. Here is an sample output, what info contains: Hope that meets all your info about server.

[  
   uptime_in_seconds:6,
   maxmemory_human:0   B,
   aof_last_cow_size:0,
   master_replid2:0000000000000000000000000000000000000000,
   mem_replication_backlog:0,
   aof_rewrite_scheduled:0,
   total_net_input_bytes:14,
   rss_overhead_ratio:1.36,
   hz:10,
   redis_build_id:5e26944   cbfa127d5,
   aof_last_bgrewrite_status:ok,
   multiplexing_api:epoll,
   client_recent_max_output_buffer:0,
   allocator_resident:3657728,
   mem_fragmentation_bytes:4156584,
   repl_backlog_first_byte_offset:0,
   redis_mode:standalone,
   redis_git_dirty:1,
   allocator_rss_bytes:2510848,
   repl_backlog_histlen:0,
   rss_overhead_bytes:1310720,
   total_system_memory:16679944192,
   loading:0,
   evicted_keys:0,
   cluster_enabled:0,
   redis_version:5.0   .3,
   repl_backlog_active:0,
   mem_aof_buffer:0,
   allocator_frag_bytes:160352,
   instantaneous_ops_per_sec:0,
   used_memory_human:834.11   K,
   role:master,
   maxmemory:0,
   used_memory_lua:37888,
   rdb_current_bgsave_time_sec:-1,
   used_memory_startup:790976,
   lazyfree_pending_objects:0,
   used_memory_dataset_perc:21.31   %,
   allocator_frag_ratio:1.16,
   arch_bits:64,
   mem_clients_normal:49694,
   expired_time_cap_reached_count:0,
   mem_fragmentation_ratio:6.12,
   aof_last_rewrite_time_sec:-1,
   master_replid:95   dcc7b8b38c04621f8cbc089ce6e13c82d3ca58,
   aof_rewrite_in_progress:0,
   config_file:,
   lru_clock:1356079,
   maxmemory_policy:noeviction,
   run_id:8   bfe7f1cebd97ff0dfb98436e76084db6c781010,
   latest_fork_usec:0,
   total_commands_processed:0,
   expired_keys:0,
   used_memory:854128,
   mem_clients_slaves:0,
   keyspace_misses:0,
   executable:/redis-server,
   used_memory_peak_human:834.11   K,
   keyspace_hits:0,
   rdb_last_cow_size:0,
   used_memory_overhead:840670,
   active_defrag_hits:0,
   tcp_port:6379,
   uptime_in_days:0,
   used_memory_peak_perc:105.21   %,
   blocked_clients:0,
   sync_partial_err:0,
   used_memory_scripts_human:0   B,
   aof_current_rewrite_time_sec:-1,
   aof_enabled:0,
   master_repl_offset:0,
   used_memory_dataset:13458,
   used_cpu_user:0.011536,
   rdb_last_bgsave_status:ok,
   atomicvar_api:atomic-builtin,
   allocator_rss_ratio:3.19,
   client_recent_max_input_buffer:4,
   aof_last_write_status:ok,
   mem_allocator:jemalloc-5.1.0,
   used_memory_scripts:0,
   used_memory_peak:854128,
   process_id:9682,
   used_cpu_sys:0.007885,
   repl_backlog_size:1048576,
   connected_slaves:0,
   gcc_version:7.3   .0,
   total_system_memory_human:15.53   G,
   sync_full:0,
   connected_clients:1,
   allocator_active:1146880,
   total_net_output_bytes:0,
   pubsub_channels:0,
   active_defrag_key_hits:0,
   rdb_changes_since_last_save:0,
   instantaneous_input_kbps:0.00,
   configured_hz:10,
   used_memory_rss_human:4.74   M,
   expired_stale_perc:0.00,
   active_defrag_misses:0,
   used_cpu_sys_children:0.000000,
   number_of_cached_scripts:0,
   sync_partial_ok:0,
   used_memory_lua_human:37.00   K,
   rdb_last_save_time:1561637161,
   pubsub_patterns:0,
   slave_expires_tracked_keys:0,
   redis_git_sha1:9101   cbde,
   used_memory_rss:4968448,
   rdb_last_bgsave_time_sec:-1,
   os:Linux 4.15.0-52-generic x86_64,
   mem_not_counted_for_evict:0,
   active_defrag_running:0,
   rejected_connections:0,
   active_defrag_key_misses:0,
   allocator_allocated:986528,
   instantaneous_output_kbps:0.00,
   second_repl_offset:-1,
   rdb_bgsave_in_progress:0,
   used_cpu_user_children:0.000000,
   total_connections_received:1,
   migrate_cached_sockets:0
]

Check out RedisClient.

It's a GUI which does the same thing you are trying to do. According to README they use Jedis. I didn't fully examine his code. But it looks like for redis related work, he used

redis.clients.jedis.Tuple
Related