Skip to content

[NEW] Expiration options on JSON.SET #1709

Open

Description

@alberttwong

The problem/use-case that the feature addresses

Batch and streaming pipelines (Spark, Flink, ETL jobs) often write each entity as one JSON document, such as a user profile, a product record or a session, and give the record a lifetime:

  • A table that is rebuilt every night writes each document with a TTL a little longer than the rebuild interval. A document that drops out of the next run then expires on its own, without a separate cleanup job or a destructive delete-and-reload.
  • Session and activity documents use a sliding expiry, where every update pushes the whole document's expiry forward.

These writers need three things:

  1. The document expires as a unit, through a TTL on the key.
  2. The value and its TTL are written atomically. Writers are distributed and get retried, so a task can die between two commands. A document written without its TTL never expires, and nothing points to it afterwards, so it becomes a slow memory leak.
  3. One command per document. Writes are pipelined, often thousands of documents per round trip.

JSON.SET has no expiration option, so no single command meets all three:

  • JSON.SET then EXPIRE, pipelined, is not atomic. If the client fails between the two commands, the key is left with no TTL.
  • MULTI / JSON.SET / EXPIRE / EXEC on each key is atomic, but it turns one logical write into four commands, with a transaction for every key inside every pipeline.

Description of the feature

Add optional expiration arguments to JSON.SET, mirroring SET. They apply to the key when the write succeeds:

JSON.SET key path value [NX | XX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]

The same options on JSON.MSET would cover batch writes, the way MSETEX does for strings.

Alternatives you've considered

  • MULTI/EXEC per key: works, but adds overhead to every write.
  • Lua scripts or Redis Functions: these need scripts or libraries loaded on the server, plus scripting ACL permissions, just to get an atomic write-with-TTL.
  • Storing the document as a string with SET … EX: this loses partial updates and Query Engine indexing, which are the reasons to use JSON in the first place.

Additional information

Strings already have this (SET … EX, and MSETEX since Redis 8.4). For hashes, the same gap is tracked in redis/redis#2905: HSETEX gained field TTLs in Redis 8.0, but not a TTL on the key.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions