Dragonfly

Redis XADD in Python (Detailed Guide w/ Code Examples)

Use Case(s)

The XADD command in Redis is used with streams, a type of data structure that was introduced in Redis 5.0. Streams are designed to hold a list of messages, where each message is a dictionary-like collection of fields and values. The common use case for the XADD command is to push data to a Redis stream.

Code Examples

Example 1: Adding a single item to a stream.
CODE_BLOCK_PLACEHOLDER_0
In this example, we're creating a connection to a local Redis server using the redis.Redis() function. Then, we define a stream name and a dictionary message. The xadd method adds the message to the stream and returns the ID of the added message.

Example 2: Adding an item to a stream with a maximum length.
CODE_BLOCK_PLACEHOLDER_1
In this second example, we're adding an optional maxlen parameter which sets a limit to the number of items in the stream. If the limit is reached, older items will be removed.

Best Practices

Common Mistakes

FAQs

Was this content helpful?

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost