For some flows, I need to store an object like that in Redis:
ObjectA: {id: X, name: 'a', age: 'b', surname: 'c', city: 'abc', state: 'cdb'}
And in a specific flow that has a huge throughput, I just need to check if the ObjectA
with the given id
already exists in cache. So, I just need the id
.
Considering the overhead to read and write more data, what is better?
- Create another cache with just the object
id
and keep duplicated data in Redis, once the cache that keeps the entire object already has theid
information as well. - Or use the same cache that stores the entire object for this flow that just needs the object id.
2
Answers
For high-speed scenarios where you only need to check if the id exists and don’t need the full object, storing just the id in a separate cache is usually better. It makes checking faster.
If memory is not a big concern and you want to keep things simple, you can stick with storing everything in the same cache, but it will be slower.
So, in most cases with a high-throughput flow, create a separate cache for just the id. This will make the system faster because you only need to read the small id data instead of the whole object.
I assume you store this value as a Redis string? You can use a JSON key instead and query just for the value of the id field.
If your data is flat you can use a Hash as well.