{
  "schema_version": 2,
  "id": "integrate/spring-framework-cache/streams",
  "title": "Process Redis streams with Spring",
  "url": "https://redis.io/docs/latest/integrate/spring-framework-cache/streams/",
  "summary": "Add entries to a Redis stream and consume them with a consumer group in a Spring application.",
  "tags": [
    "docs",
    "integrate",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "client"
  ],
  "last_updated": "2026-11-17T15:08:03Z",
  "page_type": "content",
  "content_hash": "2ab4bcd886ffe64561c644a19b5163f32e97f21dba3de437c75e8c75bec1a626",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Spring Data Redis supports Redis [streams](https://redis.io/docs/latest/develop/data-types/streams)\nthrough the template (`opsForStream()`) and through a stream message listener container\nthat reads entries in the background. See the\n[streaming use case](https://redis.io/docs/latest/develop/use-cases/streaming) for when to\nchoose a stream."
    },
    {
      "id": "add-entries",
      "title": "Add entries",
      "role": "content",
      "text": "Use `StreamRecords` to build a record and add it to the stream with the template:\n\n[code example]"
    },
    {
      "id": "consume-with-a-consumer-group",
      "title": "Consume with a consumer group",
      "role": "content",
      "text": "A consumer group shares the entries of a stream between several consumers, and tracks\nwhich entries each consumer has processed. The following configuration creates the\ncontainer, creates the group, and registers a listener that acknowledges each entry\nafter processing it:\n\n[code example]\n\nThe `createGroup` call creates the stream if it doesn't exist, and the container must\nfind the group before it starts reading. `ReadOffset.lastConsumed()` asks for entries\nthat no consumer in the group has received yet."
    },
    {
      "id": "find-and-claim-pending-entries",
      "title": "Find and claim pending entries",
      "role": "content",
      "text": "An entry stays in the group's pending list until a consumer acknowledges it. If a\nconsumer stops before acknowledging, its entries stay pending. List them with\n`pending()` and take them over with `claim()`:\n\n[code example]\n\nThe `claim` call transfers only the entries that have been idle for at least the\nduration you give, so a consumer that is still working isn't interrupted.\n\nFor a reactive alternative to the listener container, see\n[Redis Streams](https://docs.spring.io/spring-data/redis/reference/redis/redis-streams.html)\nin the Spring Data Redis documentation."
    }
  ],
  "examples": [
    {
      "id": "add-entries-ex0",
      "language": "java",
      "code": "template.opsForStream().add(\n        StreamRecords.string(Map.of(\"sku\", \"a1\")).withStreamKey(\"orders\"));",
      "section_id": "add-entries"
    },
    {
      "id": "consume-with-a-consumer-group-ex0",
      "language": "java",
      "code": "@Configuration\npublic class StreamsConfig {\n    static final String STREAM = \"orders\";\n    static final String GROUP = \"order-workers\";\n\n    @Bean(initMethod = \"start\", destroyMethod = \"stop\")\n    StreamMessageListenerContainer<String, MapRecord<String, String, String>> streamContainer(\n            RedisConnectionFactory connectionFactory) {\n        var options = StreamMessageListenerContainerOptions.builder()\n                .pollTimeout(Duration.ofSeconds(1))\n                .build();\n        return StreamMessageListenerContainer.create(connectionFactory, options);\n    }\n\n    @Bean\n    Subscription orderSubscription(\n            StreamMessageListenerContainer<String, MapRecord<String, String, String>> container,\n            StringRedisTemplate template) {\n        // Creates the stream if needed. Throws if the group already exists.\n        try {\n            template.opsForStream().createGroup(STREAM, ReadOffset.from(\"0\"), GROUP);\n        } catch (Exception e) {\n            // The group already exists.\n        }\n\n        return container.receive(\n                Consumer.from(GROUP, \"worker-1\"),\n                StreamOffset.create(STREAM, ReadOffset.lastConsumed()),\n                message -> {\n                    process(message.getValue());\n                    template.opsForStream().acknowledge(STREAM, GROUP, message.getId());\n                });\n    }\n}",
      "section_id": "consume-with-a-consumer-group"
    },
    {
      "id": "find-and-claim-pending-entries-ex0",
      "language": "java",
      "code": "PendingMessages pending = template.opsForStream()\n        .pending(STREAM, GROUP, Range.unbounded(), 10);\n\nfor (PendingMessage entry : pending) {\n    List<MapRecord<String, Object, Object>> claimed = template.opsForStream()\n            .claim(STREAM, GROUP, \"rescuer\", Duration.ofMinutes(1), entry.getId());\n    // Process the claimed entries, then acknowledge them.\n}",
      "section_id": "find-and-claim-pending-entries"
    }
  ]
}
