Question

Supposed I have a key-value database, and I need to build a queue on top of it. How could I achieve this without getting a bad performance?

One idea might be to store the queue inside an array, and simply store the array using a fixed key. This is a quite simple implementation, but is very slow, as for every read or write access the complete array must be loaded / saved.

I could also implement a linked list, with random keys, and there is one fixed key which acts as starting point to element 1. Depending on if I prefer a fast read or a fast write access, I could let point the fixed element to the first or the last entry in the queue (so I have to travel it forward / backward).

Or, to proceed with that - I could also have two fixed pointers: One for the first, on for the last item.

Any other suggestions on how to do this effectively?

Was it helpful?

Solution 2

I think it depends on the kind of queue you want to implement, and no solution will be perfect because a key-value store is not the right data structure for this kind of task. There will be always some kind of hack involved.

For a simple first in first out queue you could use a few kev-value stores like the folliwing:

{
     oldestIndex:5,
     newestIndex:10
}

In this example there would be 6 items in the Queue (5,6,7,8,9,10). Item 0 to 4 are already done whereas there is no Item 11 or so for now. The producer worker would increment newestIndex and save his item under the key 11. The consumer takes the item under the key 5 and increments oldestIndex.

Note that this approach can lead to problems if you have multiple consumer/producers and if the queue is never empty so you cant reset the index.

But the multithreading problem is also true for linked lists etc.

OTHER TIPS

Initially, key-value structure is extremely similar to the original memory storage where the physical address in computer memory plays as the key. So any type of data structure could be modeled upon key-value storage surely, including linked list.

Originally, a linked list is a list of nodes including the index information of previous node or following node. Then the node it self should also be viewed as a sub key-value structure. With additional prefix to the key, the information in the node could be separately stored in a flat table of key-value pairs.

To proceed with that, special suffix to the key could also make it possible to get rid of redundant pointer information. This pretend list might look something like this:

pilot-last-index: 5
pilot-0: Rei Ayanami
pilot-1: Shinji Ikari
pilot-2: Soryu Asuka Langley
pilot-3: Touji Suzuhara
pilot-5: Makinami Mari

The corresponding algrithm is also imaginable, I think. If you could have a daemon thread for manipulation these keys, pilot-5 could be renamed as pilot-4 in the above example. Even though, it is not allowed to have additional thread in some special situation, the result of the queue it self is not affected. Just some overhead would exist for the break point in sequence.

However which of the two above should be applied is the problem of balance between the cost of storage space or the overhead of CPU time.

The thread safe is exactly a problem however an ancient problem. Just like the class implementing the interface of ConcurrentMap in JDK, Atomic operation on key-value data is also provided perfectly. There are similar methods featured in some key-value middleware, like memcached, as well, which could make you update key or value separately and thread safely. However these implementation is the algrithm problem rather than the key-value structure it self.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top