imperugo / StackExchange.Redis.Extensions

MIT License
601 stars 179 forks source link

Uncontrolled growth of keys when using tags #560

Closed MacMcDell closed 1 year ago

MacMcDell commented 1 year ago

Describe the bug I am adding to my redis collection via the following convention:

         var tag = string.Concat(_cachePrefix, entity.SensorId);
                var key = String.Concat(_cachePrefix, entity.Id);
                var expiration = DateTime.UtcNow.AddMinutes(_ttlMinutes);

                await _redisDatabase.AddAsync<RawSensorData>(key, entity, expiration, When.Always, CommandFlags.None,
                    new HashSet<string>() { tag });

This works and the keys are expiring after 30 minutes except they are not leaving the cache :O
I now have over 450 keys in each of my 38000 keys.. this is killing the server. image

How can I get rid of these items under the keys? I have attempted DEL manually and the key is removed from the collection BUT the item is still in the list, and the data call is choking the server.

I am getting the data back (where it is choking I assume) back this

                var keys = await _redisDatabase.SetMembersAsync<string>(tagKey, CommandFlags.None).ConfigureAwait(false);
                var hashKeys = new HashSet<string>();
                foreach (var key in keys)
                    hashKeys.Add(key);

                var redisResultWithPossibleNulls = await _redisDatabase.GetAllAsync<RawSensorData>(hashKeys, 
                CommandFlags.None).ConfigureAwait(false);

                var nullKeys = redisResultWithPossibleNulls.Where(x => x.Value is null).ToList();
                var keysToRemove = nullKeys.Select(x => x.Key).ToArray();

                ///hopefully remove all the null items
                var removeResult = await _redisDatabase.RemoveAllAsync(keysToRemove);

Server

redis_version:6.0.14 redis_mode:standalone os:Windows
arch_bits:64 multiplexing_api:winsock_IOCP run_id:ec79002a67094a2cfe88b8f98b29f4b5b186c89d uptime_in_seconds:22649 uptime_in_days:0 hz:10

Clients

connected_clients:182 maxclients:5000 client_recent_max_input_buffer:14362 client_recent_max_output_buffer:41056 client_total_writes_outstanding:0 client_total_sent_bytes_outstanding:0 blocked_clients:0 tracking_clients:0 clients_in_timeout_table:0

Memory

used_memory:1907701808 used_memory_human:1.78G used_memory_rss:2006663168 used_memory_rss_human:1.87G used_memory_peak:1908163480 used_memory_peak_human:1.78G used_memory_peak_perc:99.98% used_memory_overhead:159772332 used_memory_startup:363432 used_memory_dataset:1747929476 used_memory_dataset_perc:91.64% used_memory_lua:32768 maxmemory:6100000000 maxmemory_reservation:1279000000 maxfragmentationmemory_reservation:636000000 maxmemory_desired_reservation:1279000000 maxfragmentationmemory_desired_reservation:636000000 maxmemory_human:5.68G maxmemory_policy:volatile-lru mem_allocator:jemalloc-4.0.3

Stats

total_connections_received:259625 total_commands_processed:50696903 instantaneous_ops_per_sec:2302 bytes_received_per_sec:6822126 bytes_sent_per_sec:9805761 bytes_received_per_sec_human:6.51M bytes_sent_per_sec_human:9.35M rejected_connections:0 expired_keys:6220471 evicted_keys:0 keyspace_hits:107021739 keyspace_misses:2085652028 pubsub_channels:2 pubsub_patterns:0 total_oom_messages:0

Replication

role:master

CPU

used_cpu_sys:937.625000 used_cpu_user:6376.828125 used_cpu_avg_ms_per_sec:418 server_load:37.34 event_wait:564 event_no_wait:2725 event_wait_count:513 event_no_wait_count:950

Cluster

cluster_enabled:0 cluster_myself_name:

Keyspace

db0:keys=623548,expires=549717,avg_ttl=943043

Expected behavior I expected once a key was expired to get removed from the tag and smembers

Screenshots / StackTrace image

What am I doing wrong???

MacMcDell commented 1 year ago

part of this issue was/is that the extensions, when you use the tags feauture, creates a tag:{{youritemKey}} you must ensure you have created the key to reference correctly for cleanup. Also I was having big issues with the fact it would serialize and deserialize the strings when writing to the cache.