{
  "id": "redis-sorted-set-example",
  "title": "Write to a Redis sorted set",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/redis-sorted-set-example/",
  "summary": "",
  "tags": [
    "docs",
    "integrate",
    "rs",
    "rdi"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "page_type": "content",
  "content_hash": "2e49825a275227f22da0208e895c798b30ba0e63a28efd6f1be2078263de7b87",
  "sections": [
    {
      "id": "content",
      "title": "Content",
      "role": "content",
      "text": "In the example below, data is captured from the source table named `invoice` and is written to a Redis sorted set. The `connection` is an optional parameter that refers to the corresponding connection name defined in `config.yaml`. When\nyou specify the `data_type` parameter for the job, it overrides the system-wide setting `target_data_type` defined in `config.yaml`.\n\nWhen writing to sorted sets, you must provide two additional arguments, `member` and `score`. These specify the field names that will be used as a member and a score to add an element to a sorted set. In this case, the result will be a Redis sorted set named `invoices:sorted` based on the key expression and with an expiration of 100 seconds for each set member. If you don't supply an `expire` parameter, the keys will never expire.\n\n[code example]\n\nSince sorted sets in Redis are inherently sorted, you can easily get the top N invoices by total invoice amount using the command below (the range 0..9 gets the top 10 invoices):\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "content-ex0",
      "language": "yaml",
      "code": "name: Write invoices to sorted set by total\nsource:\n  schema: public\n  table: invoice\noutput:\n  - uses: redis.write\n    with:\n      connection: target\n      data_type: sorted_set\n      key:\n        expression: \"`invoices:sorted`\"\n        language: jmespath\n      args:\n        score: Total\n        member: InvoiceId\n      expire: 100",
      "section_id": "content"
    },
    {
      "id": "content-ex1",
      "language": "plaintext",
      "code": "ZREVRANGE invoices:sorted 0 9 WITHSCORES",
      "section_id": "content"
    }
  ]
}
