2

I am looking for a queue algorithm that fulfills the following properties:

  1. Processes communicate using only a shared dictionary (key-value-store)
  2. Does not use any atomic operations other than load and store (no CAS, for example)
  3. Supports multiple producers
  4. Supports a single consumer
  5. Producers can die at any time and queue must remain operational
  6. The consumer can also die at any time and be restarted later, but there will never be more than one consumer-process running at a time

This is meant as a general question about a suitable algorithm, since I'd like to use it in a couple of different scenarios. But to help visualize the requirements, here is an example use-case:

  • I have a website with two pages: producer.html and consumer.html
  • producer.html can be opened in multiple tabs simultaneously
  • Each producer.html adds events to the queue
  • One copy of consumer.html is open and consumes these events (to aggregate and stream them to a webserver, for example)

If the multiple producer-tabs are opened by the user rather than the page, these tabs do not have references to each other available, so the usual communication methods (postMessage or calling directly into the other tab's JS code) are out. One of the ways they can still communicate with each other is via LocalStorage as suggested here: Javascript; communication between tabs/windows with same origin. But LocalStorage is not "thread-safe" as detailed here.

Note: There may be other ways to implement cross-tab communication in the browser (Flash, ...), but these are NOT the aim of this question as they won't translate to my other use-cases. This is really just an example use-case for the general queue algorithm that I am trying to find.

A couple more parameters:

  • The number of producers will never be very large (10s or 100s maybe), so the scaling of the number of reads and writes needed with respect to the number of producers is not really a concern.
  • I don't know before hand how many producers I might have and there is no immediately obvious way to assign a number or index to them. (Many mutex algorithms (Lamport's Bakery, Eisenberg&McGuire, Szymański's, ...) maintain an array of state for each process, which wouldn't necessarily be a natural approach here, although I do not want to exclude these approaches ex ante, if they can be implemented using the shared dictionary in some way...)
  • The algorithm should be 100% reliable. So, I'd like to avoid things like the delay in Lamport's first Fast Mutex algorithm (page 2 in the PDF) since I don't have any kind of real-time guarantees.
  • It would be very helpful if the queue was FIFO, but it's not strictly required.
  • The algorithm should not be encumbered by any patents, etc.

Update:

The Two-Lock Concurrent Queue Algorithm by Michael and Scott looks like it could work, but I would need two things to implement it:

  • A locking mechanism using the shared dictionary that can survive the crash of a lock-holder
  • A reliable way to allocate a new node (if I move the allocation into the locked section, I could just generate new random keys until I find one that's not in use yet, but there might be a better way?)

Update 2:

It seems, I wasn't being specific enough about the dictionary:

It's really nothing more than a trivial key-value-store. It provides the functions get(key) to read the value of a key, put(key, value) to change the value of a key, and delete(key) to remove a key. In some of my use-cases, I can also iterate over keys, but if possible, I'd like to avoid it for generality. Keys are arbitrary and the producers and consumers can create or calculate them as needed. The dictionary does not provide any facilities for automatically generating unique keys.

Examples are HTML LocalStorage, Google AppEngine's Datastore, a Java Map, a Python dictionary, or even a file-system with only a single directory (where the keys would be the file-names and the values the content of the files).

Community
  • 1
  • 1
Markus A.
  • 11,761
  • 8
  • 44
  • 102
  • could you describe the relationship of the dictionary and queue and their roles more in detail please? – Jason Hu Oct 14 '15 at 02:42
  • @HuStmpHrrr The dictionary is simply the only "shared memory" between the processes. Each process can read and write values that are identified by keys. The dictionary does not provide any concurrency management (like atomic increment-and-get or compare-and-set). So, the queue implementation must store all its data that needs to be shared with other processes in the dictionary in some clever way. Beyond that, there is no relationship between the two. I hope this helps. – Markus A. Oct 14 '15 at 02:59
  • MPSC queue implementation can be based on CAS or locking (I doesn't know other ways). Existing locking algorithms require either CAS or per-process variables, arranged in a *static* list. You have **neither CAS** support, **nor static list of processes**. It seems that what you want is simply impossible. – Tsyvarev Oct 14 '15 at 08:02
  • It is still not very clear. Can you implement the dictionary yourself? – Jason Hu Oct 14 '15 at 17:11
  • @Tsyvarev I'm pretty sure this has to be possible. I posted an example answer as proof-of-concept that I think *should* work, but might not be optimal. – Markus A. Oct 14 '15 at 17:38
  • @HuStmpHrrr I added an update to the question that might help even more. Also, I added an example answer that I came up with, which might shed even more light on things. – Markus A. Oct 14 '15 at 17:39
  • Now that it's 2021, are you using IndexedDB? – ADJenks Mar 11 '21 at 23:27

2 Answers2

1

After quite a bit of further reading and sleeping on things for a night, I came up with one way that should be able to accomplish what I need, but it might not be the most elegant:

The paper Wait-Free Algorithms for Fast, Long-Lived Renaming by Moir and Anderson generalizes Lamport's Fast Mutex Algorithm #2 (page 6 here) into the following building block (Figure 2):
      building block
When n processes enter this section of code, at most one of them will stop, at most n-1 will move right and at most n-1 will move down.

In Lamport's algorithm, stopping means the process acquired the lock, whereas moving right or left will simply send the process back to the beginning of this section of code. To release the lock, a process simply sets Y back to false. (Not quite correct, actually... See "Update" below...)

The big problem with this is that if any of the processes ever die while holding the lock (i.e. before releasing it), the block will simply stay locked forever.

Another problem is that every process needs to be assigned a unique process ID p.

The locked-forever problem can be fixed by borrowing an idea from Moir and Anderson, namely to send processes that end up moving right or down into a different building block rather than back to this one, leading to a structure like this (Figure 3 in the paper):
      enter image description here
Except that in this case, I won't be using this grid to assign process IDs as M&A did (although I could probably solve the problem of the unique values for p with this). Instead, every box in the grid will correspond to a very simple queue. If a process stops on a box, it acquired the tail-lock for the corresponding queue (e.g. as per the algorithm by Michael and Scott) and proceeds to enqueue a new element to that queue. Upon completion, it sets the Y value of the box back to false to allow other processes to use this queue. This way, if there is high contention or if processes die before releasing locks, new queues will be created dynamically as needed.

The consumer-process doesn't need to worry about locking the heads of the queues when dequeuing elements, since it's the only process to ever do so. So, it simply traverses the tree of boxes to find all queues and trivially helps itself to their contained elements. One thing to note is that while each individual queue will be FIFO, there is no synchronization between the queues, so the combined queue will not necessarily be FIFO.

If we now change the boolean Y to a time-stamp (or null/0 to indicate false), the consumer can also expire locks after some safe timeout to re-activate dead queues.

A note about implementation using the dictionary:

  • The shared variables X and Y can be entries in the dictionaries with key-names X_123 and Y_123, where 123 is the number of the box.
  • p can simply be any unique random string and will be stored as the value of key X_123.
  • The boolean or time-stamp is also simply stored as the value of key Y_123. The producer-processes interpret a missing entry for Y_123 as false or null/0.
  • The box-numbers 123 need to be calculated from the move-pattern. One way to do this would be to start with 1 in the top-left corner. If the process stops in that box, we're done. If not, the current number (starting with 1) is shifted left by 1 (i.e. multiplied by 2) and, if the process moved down, also incremented by 1. Smaller (and fewer) numbers can be calculated with a different numbering scheme (I still need to work it out), but this one should work.
  • The queues then consist of one entry with key H_123 that holds the index of the current head of the queue in its value and one entry with key T_123 that holds the index of the tail. Both default to 0 if they don't exist.
  • To enqueue an item into queue 123, the tail index is read from T_123 (let's say it yields 48) and an entry with key Q_123_48 is put into the dictionary with its value containing the enqueued item. After, T_123 is incremented by 1.
  • After the item is enqueued, the Y_123 entry is set back to false or null/0 (not deleted!)
  • To dequeue an item, the head index is read from H_123 (let's say it yields 39) and compared to the tail index T_123. If it is smaller, an item is available at Q_123_39, which is then read and deleted from the dictionary. After, H_123 is incremented by 1.
  • To traverse the box-tree, the consumer starts with the box in the top left corner. For each box (e.g. 123), if a key Y_123 exists in the dictionary (even if it contains values null/0 or false), the consumer dequeues items from the corresponding queue, and then recursively moves right and down to the adjacent boxes. If no key Y_123 exists, this box hasn't been used by any processes yet and doesn't need to be considered (and neither do the boxes below or to its right).

I haven't actually implemented this yet, but I'll do that next. I just wanted to post this already to see if it could inspire other approaches or if anyone can see anything wrong with this idea.

Update:

I just noticed one complication: It is possible that if two processes are trying to acquire the lock for a queue simultaneously, both will fail and move on to the next block. This will leave that queue locked forever as no-one will be left to set Y back to false or null/0.

This is the reason why the "Long-Lived Renaming" algorithm by M&A as well as Lamport's algorithm #2 use an array of Y-values in which every process has its own entry that it resets also if it moves on to another block. Y is then only considered false if all entries are false.

Since I don't know before-hand how many processes I will have, I could implement this only if the dictionary had some way of enumerating keys (the keys would then be Y_123_456 where 456 is the value of p for each process).

But, with rare contention and the above described timeout-mechanism for reactivating dead queues, the issue might lead to only a little bit of memory inefficiency, rather than a major problem.

Update 2:

A better way to label the boxes would be this pattern:
      enter image description here
If we call the total number of moves n (counting the move into the top left box also, i.e. n ≥ 1) and the number of moves to the right r, then the box-number can be calculated using

      box = (n × (n - 1))/2 + r

Markus A.
  • 11,761
  • 8
  • 44
  • 102
0

Just use a RDBMS. It's pretty simple in MS SQL, for PostgresSQL you'd have to use the RETURNING keyword and for MySQL you'd probably have to use triggers.

CREATE TABLE Q ([Key] BIGINT IDENTITY(1,1) PRIMARY KEY, [Message] NVARCHAR(4000))

INSERT INTO Q OUTPUT inserted.* VALUE(@message)

DELETE TOP(1) Q WITH (READPAST) OUTPUT deleted.*

If you were really hoping for an algorithmic solution, just use a ring buffer.

const int MAX_Q_SIZE = 20000000;
static string[] Q = new string[MAX_Q_SIZE];
static long ProducerID = 0;
static long ConsumerID = 0;
public static long Produce(string message) {
    long key = Interlocked.Increment(ref ProducerID);
    int idx = (int)(key % MAX_Q_SIZE);
    Q[idx] = message;
    return key;
}
public static string Consume() {
    long key = Interlocked.Increment(ref ConsumerID);
    int idx = (int)(key % MAX_Q_SIZE);
    string message = Q[idx];
    return message;
}
Louis Ricci
  • 19,594
  • 5
  • 43
  • 60
  • Unfortunately I really only have a dictionary without locking. Otherwise, of course, it would be trivial... – Markus A. Oct 14 '15 at 16:19
  • @MarkusA. - I think you need to explain what this "shared dictionary without locking" is. Is it a separate process? how do producers and consumers access it, send data to it, receive data from it? Do the producers generate their own keys or does the dictionary? Can the consumer iterate over the keys of the dictionary? Knowing the the API of the dictionary mandatory to solve a problem like this. – Louis Ricci Oct 14 '15 at 16:31
  • Good points. I added an update to the original question. Also, I added an example answer that might help. Sorry for being unclear. – Markus A. Oct 14 '15 at 17:36