0

My service is writing to clickhouse. I want to implement a cache which will help me with batching data for CH + it will store the data when CH is unavailable so i dont lose the data. I chose redis sorted set for that purpose, which uses row timestamp as a score and serialized row(including timestamp) as a member. I write to cache first with ZAdd and periodically read first n members from cache with ZRevRange(top n recent rows) to send the data to CH, and, after successful write, i clear those members from cache with ZRem.

The problem i'm facing is that i can't manage TTL properly in that case. TTL is set for the whole set(key), but i need it per-member, so when updating TTL with Expire method i dont overwrite TTL for the old data. I know this method supports additional options, like nx xx gt lt, but i don't see how they can help me with my issue.

I can switch to other data structure, which supports per-key TTL, but then i lose a possibility to easily read top n most recent rows (which is important because in case CH becomes available after a long period i need to write the most recent rows instead of the oldest, in other words i need a LIFO behavior).

Can you help me adjust my current solution, or maybe propose another?

3
  • maybe redis is not best choice here and kafka or redpanda will works better Commented Jul 5 at 16:19
  • maybe you're right, currently im trying to give redis another chance with their streams data structure, it might work because it works as lifo but also supports trim Commented Jul 6 at 17:23
  • You should switch to Redis Streams as it is better suited for this type of use case (a very common one, I must add) without losing what you want to accomplish. You can even leverage the command XREVRANGE to retrieve the recent N entries. Commented Jul 25 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.