I have a 2D array with 5000 Rows and 2 Million Columns the value stored is just a boolean 0 or 1. What is the Redis data structure best suited to storing this use-case in Redis?
2 Answers
A straightforward approach would be to store the array as a string and use bit commands to manipulate it. That should lead to minimal memory use, and SETBIT and GETBIT are both O(1).
If the data could fit in the 512MB limit of a Redis string you could just use a single string. The size you've given is too large, though, so an alternative approach would be to store each row as a separate string.
5 Comments
You can make 5000 databases in a Redis instance, then save each row with the index of the columns which have the value of '1' instead of saving all of the 2 million columns. By doing this, maybe you can reduce the size of each row to half a million length. Then at the time of deserialization, you may build the initial value of array programmatically.
//example: row -0 - columns: 00101....1
save [2,4,...,1999999] // considering zero based indexing