Skip to content

Sequences

We can detect duplicates, gaps and missing messages over a reliable ordered delivery medium such as Aeron by using sequences.

Creating the sequence on the sending side is as simple as:

1
2
3
4
5
6
7
long sequence = 0;
...
void send(byte[] message)
{
    sequence = sequence + 1;
    send(message, sequence);
}

On the receiver, the sequence can be interpretted :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void receive(byte[] message, long sequence)
{
    //check it is at least as large as the previously received sequence
    if (sequence <= lastSequence)
    {
        //out of order or duplicate
        handleUnexpectedMessageAt(sequence);
    }
    else
    {
        if (lastSequence + 1 == sequence)
        {
            //sequence is as expected, safe to process
            process(message);
        } 
        else //sequence > lastSequence + 1
        {
            //there have been missing messages
            handleMissingMessagesSince(lastSequence);
        }
    }
    //update lastSequence as needed
}

Sequences can support verification of correct message flow between two processes. In Aeron this would be expected only if the two processes were communicating on the same channel and stream. You could use the position in a stream in place of the sequence, although this will not help in cases of a process being down or in several other expected failure scenarios. This is why the Aeron team recommend that you use your own order and gap detection techniques within an Aeron channel and stream.