StackExchange / StackExchange/StackExchange.Redis

Question: Efficient Cache Invalidation Strategy

Open
#2,825 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.2k
Forks
1.6k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

Hello, I am new to Redis and trying to improve my understanding so forgive my ignorance.

Problem:
I am seeing many chain SCAN calls in Azure App Insights that hold up the request and looking to find a much more efficient method to find and bulk delete keys with the same prefix.

Example problem:
In this case each SCAN is very fast but we see each scan taking >1ms across regions.
image

Implementation:

  1. Setting a key for example {account123}:attribute1:attribute2
connection.GetDatabase().StringSetAsync(key, data, cacheTime, When.Always, flags: CommandFlags.FireAndForget);
  1. Bulk removal for an account prefix where prefix is {account123}. We do not expect a high amount of records being returned around 5 - 20.
var pageSize = 50;
var database = connection.GetDatabase();

foreach (var endpoint in database.Multiplexer.GetEndPoints())
{
     var server = database.Multiplexer.GetServer(endpoint);
     var results = server.KeysAsync(pattern: $"{prefix}:*", pageSize: pageSize);

     await foreach (var pageResults in results.Segment(pageSize))
     {
         await database.KeyDeleteAsync(pageResults);
      }
}

How could I improve this solution to avoid waiting on the SCAN to complete? I am looking at HSET but in this case I need to set an expiry per name/value.

Thanks in advance.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are identified. Start by reviewing the Redis SCAN usage through server.KeysAsync, the page iteration, and database.KeyDeleteAsync, along with the HSET alternative mentioned in the question. Done should mean turning the performance question into a specific, testable repository change or documented recommendation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, redis
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.