3

In my Redis DB I want to save the following data:

{"id": "1", "data": "abc", "param": "p1"}
{"id": "2", "data": "def", "param": "p2"}

Currently I am saving each line as follows (using Jedis client in Java):

JEDIS.set(line.getId(),line.getLine());

So, the result is something like this:

"1", "{"id": "1", "data": "abc", "param": "p1"}"

GET 1
{"id": "1", "data": "abc", "param": "p1"}

Is it an efficient way to save this kind of data in Redis? Maybe it's better to convert each line to a Set or something?

4
  • Sure, it's an efficient way to save it. But how do you plan to access it? That's the most important thing in choosing the right data structure. Commented Aug 19, 2016 at 8:43
  • @KevinChristopherHenry: I am new to Redis, but my idea was to read Json strings from Redis by ID and then perform parsing from my code. However, I am not sure if there is any way to avoid this parsing and also to consume less memory. The problem is that now I am also consuming the extra-memory resources for saving the punctuation of Json strings (e.g. {, }, "). Commented Aug 19, 2016 at 8:47
  • It sounds like what you need is mongodb Commented Aug 19, 2016 at 8:57
  • @for_stack: I am forced to use Redis. In fact it's for caching. Commented Aug 19, 2016 at 9:13

2 Answers 2

6

If all you need to do is get and set blobs of data, then a straightforward use of GET and SET makes sense. If memory use is a concern you could use MessagePack instead of JSON.

If your usual patterns of access don't involve the entire object, an alternative is to use a Hash, since that would allow you to get and set fields individually, reducing the amount of data you need to transfer. (It wouldn't reduce the total amount of memory needed to store your data, though.)

Sign up to request clarification or add additional context in comments.

Comments

2

Hashes are perfect for what you need as you can access the values of individual fields directly using HSET and HGET, and they are very memory efficient and optimised for this purpose. http://redis.io/topics/memory-optimization#use-hashes-when-possible

Comments

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.