If we continue with the analogy of the log file, one obvious way is to mimic what we normally do with the Unix command tail -f, that is, we may start to listen in order to get the new messages that are appended to the stream. However, while appending data to a stream is quite obvious, the way streams can be queried in order to extract data is not so obvious. Now we are finally able to append entries in our stream via XADD. To do this, use the syntax below: > XADD somestream 0-* baz qux In this case, the sequence portion of the ID will be automatically generated. If you're running Redis 7 or later, you can also provide an explicit ID consisting of the milliseconds part only. (error) ERR The ID specified in XADD is equal or smaller than the target stream top item Note that in this case, the minimum ID is 0-1 and that the command will not accept an ID equal or smaller than a previous one: > XADD somestream 0-1 foo bar If for some reason the user needs incremental IDs that are not related to time but are actually associated to another external system ID, as previously mentioned, the XADD command can take an explicit ID instead of the * wildcard ID that triggers auto-generation, like in the following examples: > XADD somestream 0-1 field value We will see this soon while covering the XRANGE command. Because the ID is related to the time the entry is generated, this gives the ability to query for time ranges basically for free. The reason is that Redis streams support range queries by ID. The format of such IDs may look strange at first, and the gentle reader may wonder why the time is part of the ID. Since the sequence number is 64 bit wide, in practical terms there is no limit to the number of entries that can be generated within the same millisecond. The sequence number is used for entries created in the same millisecond. The milliseconds time part is actually the local time in the local Redis node generating the stream ID, however if the current milliseconds time happens to be smaller than the previous entry time, then the previous entry time is used instead, so if a clock jumps backward the monotonically incrementing ID property still holds. The entry ID returned by the XADD command, and identifying univocally each entry inside a given stream, is composed of two parts:. It is possible to get the number of items inside a Stream just using the XLEN command: > XLEN mystream Returning back at our XADD example, after the key name and ID, the next arguments are the field-value pairs composing our stream entry. The fact that each Stream entry has an ID is another similarity with log files, where line numbers, or the byte offset inside the file, can be used in order to identify a given entry. Auto-generation of IDs by the server is almost always what you want, and the reasons for specifying an ID explicitly are very rare. Every new ID will be monotonically increasing, so in more simple terms, every new entry added will have a higher ID compared to all the past entries. However, in this case, we passed * because we want the server to generate a new ID for us. It gets as its first argument the key name mystream, the second argument is the entry ID that identifies every entry inside a stream. The above call to the XADD command adds an entry sensor-id: 1234, temperature: 19.8 to the stream at key mystream, using an auto-generated entry ID, which is the one returned by the command, specifically 1518951480106-0. The fundamental write command, called XADD, appends a new entry to the specified stream.Įach stream entry consists of one or more field-value pairs, somewhat like a record or a Redis hash: > XADD mystream * sensor-id 1234 temperature 19.8 Streams are an append-only data structure. These include random access in O(1) time and complex consumption strategies, such as consumer groups. Streams model a log data structure but also implement several operations to overcome some of the limits of a typical append-only log. The Redis stream data type was introduced in Redis 5.0. For a more comprehensive tutorial, read on. If you're new to streams, see the Redis Streams introduction. A comprehensive tutorial on Redis streams
0 Comments
Leave a Reply. |