Need help with Redis / NoSQL conceptualization

I think I understand all the commands well to use Redis, but it's hard for me to figure out how best to use it. I am developing a client notification system that will notify them according to their preferred method (Email, SNMP, Syslog) when there is an alarm on any of their schemes.

So, I get the device name and port. I need to associate this with one customer, and then associate this customer with the delivery method. With relational db, this would probably look something like this:

Device name: Los_Angeles Port: 11 SELECT Customer_ID, Customer_name from device_info where device_port = 'Los_Angeles:11' SELECT Customer_protocol, SNMP_destination, Syslog_destination from CUSTOMER where Customer_ID = <customer_id from above> 

(A very simplified example).

I see how to do this programmatically with a hash of lists or hashes. But I think I'm having problems with Redis, because these more complex data structures are not available to me (as far as I know). So, how do I associate several pieces of information with one key? I can come up with several ways that I can do this, but they all seem to include a few steps, and I would like some of the current Redis programmers to say that this is the best way to do this.

+7
source share
1 answer

You are right that only simple data structures are available with Redis, and they cannot be compiled by value (for example, you could use documentation like CouchDB or MongoDB). However, you can create data structures by reference, and this is a very common pattern.

For example, the elements contained in a set may be keys for other objects (lists, hash tables, other sets, etc.). Try applying this to your example.

To model the relationship between clients and device + ports, you can use sets containing client identifiers. To store customer information, one hash table for each client is in order.

Here are the clients:

 hmset c:1 name Smith protocol tcp snmp_dest 127.0.0.1 syslog_dest 127.0.0.2 hmset c:2 name Jackson protocol udp snmp_dest 127.0.0.1 syslog_dest 127.0.0.2 hmset c:3 name Davis protocol tcp snmp_dest 127.0.0.3 syslog_dest 127.0.0.4 

The keys for these entries are: c: ID

Let me link two of them to the device and port:

 sadd d:Los_Angeles:11 2 3 

The key to this set is d: device: port. The prefix c: and d: is just a convention. One set per device / port must be created. This client can belong to several sets (and therefore is connected to several devices / ports).

Now, to find customers with the delivery methods that came with this device / port, we just need to get the contents of the kit.

 smembers d:Los_Angeles:11 1) "2" 2) "3" 

then the corresponding client information can be obtained by pipelining several hgetall commands:

 hgetall c:2 hgetall c:3 1) "name" 2) "Jackson" 3) "protocol" 4) "udp" 5) "snmp_dest" 6) "127.0.0.1" 7) "syslog_dest" 8) "127.0.0.2" 1) "name" 2) "Davis" 3) "protocol" 4) "tcp" 5) "snmp_dest" 6) "127.0.0.3" 7) "syslog_dest" 8) "127.0.0.4" 

Do not be afraid of the number of teams. They are very fast, and most Redis clients have the ability to pipelining requests, so only a minimal number of hits is required. Just using one smemen and several hgetall, the problem can be solved with just two callbacks.

Now you can optimize a little further thanks to the ubiquitous SORT team. This is probably the most complex command in Redis, and it can be used to save back and forth.

 sort d:Los_Angeles:11 by nosort get c:*->name get c:*->protocol get c:*->snmp_dest get c:*->syslog_dest 1) "Jackson" 2) "udp" 3) "127.0.0.1" 4) "127.0.0.2" 5) "Davis" 6) "tcp" 7) "127.0.0.3" 8) "127.0.0.4" 

In one command, it retrieves the contents of a set of devices / ports and retrieves relevant client information.

This example was trivial, but more generally, although you can represent complex data structures using Redis, it is not immediate. You need to think carefully about the model both in terms of structure and data access (i.e., during development, stick to your data and your use cases).

+10
source

All Articles