Redis

Redis Endpoint Documentation

Redis API Documentation - Comprehensive Command CoverageCopied!

OverviewCopied!

Redis endpoints provide a comprehensive interface for interacting with Redis databases, offering access to the full range of Redis data structures and commands. This API allows you to leverage Redis' in-memory data structure store for high-performance operations including caching, real-time analytics, message brokering, and more.

Key FeaturesCopied!

  • Complete Redis Command Support: Access to all native Redis commands and data structures

  • Data Structure Operations: Full operations on strings, lists, sets, sorted sets, hashes, bitmaps, HyperLogLogs, and geospatial indexes

  • Pub/Sub Messaging: Support for publishing and subscribing to channels

  • Transaction Management: Ability to execute commands in atomic transactions

  • Lua Scripting: Support for executing Lua scripts

  • Module Support: Access to Redis modules like RediSearch, RedisJSON, Redis Bloom, RedisTimeSeries, and RedisAI

  • Cluster and Replication: Commands for Redis Cluster and replication management

  • ACL Management: Access control list capabilities for security management

  • Vector Operations: Support for vector similarity search operations

  • Time Series: Operations for time series data management

  • TopK and TDigest: Statistical operations for data analysis

API Request StructureCopied!

Redis API requests follow this general structure:

{
  "kind": "redis",
  "type": "[command_name]",
  "[additional_parameters]": "[values]"
}

OperationsCopied!

String Operations

SET

Sets the string value of a key.

{
  "kind": "redis",
  "type": "SET",
  "key": "user:1",
  "value": "John Doe",
  "rule": "NX",  // Optional: NX (only set if key does not exist), XX (only set if key exists)
  "options": {   // Optional
    "EX": 3600      // Expire in seconds
  }
}
GET

Gets the string value of a key.

{
  "kind": "redis",
  "type": "GET",
  "key": "user:1"
}
INCR/DECR

Increments or decrements the numeric value of a key.

{
  "kind": "redis",
  "type": "INCR",
  "key": "counter"
}
SETEX

Sets string value with expiration in seconds.

{
  "kind": "redis",
  "type": "SETEX",
  "key": "session:token",
  "seconds": 3600,
  "value": "ABC123XYZ"
}
SETNX

Sets string value only if key does not exist.

{
  "kind": "redis",
  "type": "SETNX",
  "key": "lock:process",
  "value": "1"
}
STRLEN

Gets the length of the string value at key.

{
  "kind": "redis",
  "type": "STRLEN",
  "key": "user:name"
}
SUBSTR

Gets a substring of the string value stored at key.

{
  "kind": "redis",
  "type": "SUBSTR",
  "key": "message",
  "start": 0,
  "end": 10
}

Hash Operations

HSET

Sets field in a hash stored at key to value.

{
  "kind": "redis",
  "type": "HSET",
  "key": "user:1",
  "fields": [
    {"field": "name", "value": "John Doe"},
    {"field": "email", "value": "john@example.com"}
  ]
}
HGET

Gets the value of a hash field.

{
  "kind": "redis",
  "type": "HGET",
  "key": "user:1",
  "field": "name"
}
HGETALL

Gets all the fields and values in a hash.

{
  "kind": "redis",
  "type": "HGETALL",
  "key": "user:1"
}

List Operations

LPUSH/RPUSH

Inserts elements at the beginning/end of a list.

{
  "kind": "redis",
  "type": "LPUSH",
  "key": "tasks",
  "element": ["task1", "task2", "task3"]
}
LRANGE

Gets a range of elements from a list.

{
  "kind": "redis",
  "type": "LRANGE",
  "key": "tasks",
  "start": 0,
  "stop": -1  // -1 means the last element
}

Set Operations

SADD

Adds members to a set.

{
  "kind": "redis",
  "type": "SADD",
  "key": "tags",
  "members": ["tag1", "tag2", "tag3"]
}
SMEMBERS

Gets all members in a set.

{
  "kind": "redis",
  "type": "SMEMBERS",
  "key": "tags"
}
SISMEMBER

Determines if a value is a member of a set.

{
  "kind": "redis",
  "type": "SISMEMBER",
  "key": "tags",
  "member": "tag1"
}
SMISMEMBER

Determines if multiple values are members of a set.

{
  "kind": "redis",
  "type": "SMISMEMBER",
  "key": "tags",
  "member": ["tag1", "tag2", "tag3"]
}
SINTER

Returns the intersection of multiple sets.

{
  "kind": "redis",
  "type": "SINTER",
  "keys": ["set1", "set2", "set3"]
}
SINTERSTORE

Stores the intersection of multiple sets in a destination key.

{
  "kind": "redis",
  "type": "SINTERSTORE",
  "destination": "result",
  "keys": ["set1", "set2", "set3"]
}
SUNION

Returns the union of multiple sets.

{
  "kind": "redis",
  "type": "SUNION",
  "keys": ["set1", "set2", "set3"]
}
SUNIONSTORE

Stores the union of multiple sets in a destination key.

{
  "kind": "redis",
  "type": "SUNIONSTORE",
  "destination": "result",
  "keys": ["set1", "set2", "set3"]
}
SPOP

Removes and returns random members from a set.

{
  "kind": "redis",
  "type": "SPOP",
  "key": "lottery",
  "count": 3 // Optional: number of members to pop
}
SRANDMEMBER

Returns random members from a set.

{
  "kind": "redis",
  "type": "SRANDMEMBER",
  "key": "users",
  "count": 5 // Optional: number of members to return
}

Sorted Set Operations

ZADD

Adds members to a sorted set.

{
  "kind": "redis",
  "type": "ZADD",
  "key": "leaderboard",
  "scores": [
    {"score": 100, "member": "player1"},
    {"score": 85, "member": "player2"},
    {"score": 95, "member": "player3"}
  ]
}
ZRANGE

Returns a range of members from a sorted set.

{
  "kind": "redis",
  "type": "ZRANGE",
  "key": "leaderboard",
  "start": 0,
  "stop": 2,
  "with_scores": true
}
ZRANK

Returns the rank of a member in a sorted set.

{
  "kind": "redis",
  "type": "ZRANK",
  "key": "leaderboard",
  "member": "player1",
  "with_scores": false  // Optional: return score along with rank
}
ZREVRANK

Returns the reverse rank of a member in a sorted set.

{
  "kind": "redis",
  "type": "ZREVRANK",
  "key": "leaderboard",
  "member": "player1",
  "with_score": false  // Optional: return score along with rank
}
ZINCRBY

Increments the score of a member in a sorted set.

{
  "kind": "redis",
  "type": "ZINCRBY",
  "key": "leaderboard",
  "increment": 10,
  "member": "player1"
}
ZCARD

Returns the number of members in a sorted set.

{
  "kind": "redis",
  "type": "ZCARD",
  "key": "leaderboard"
}
ZSCORE

Returns the score of a member in a sorted set.

{
  "kind": "redis",
  "type": "ZSCORE",
  "key": "leaderboard",
  "member": "player1"
}
ZINTER

Returns the intersection of multiple sorted sets.

{
  "kind": "redis",
  "type": "ZINTER",
  "numkeys": 2,
  "keys": ["leaderboard1", "leaderboard2"],
  "weights": [1, 2],  // Optional: weights for each sorted set
  "aggregate": "SUM",  // Optional: SUM, MIN, MAX
  "with_scores": true  // Optional: include scores in result
}

Stream Operations

XADD

Adds an entry to a stream.

{
  "kind": "redis",
  "type": "XADD",
  "key": "mystream",
  "id": {
    "ID": "*"  // Auto-generate ID ("*") or specify an ID
  },
  "entries": [
    {"field": "sensor-id", "value": "1234"},
    {"field": "temperature", "value": "19.8"}
  ]
}
XREAD

Reads entries from streams.

{
  "kind": "redis",
  "type": "XREAD",
  "count": 10,  // Optional: maximum number of entries to return
  "block": 5000,  // Optional: block for milliseconds if no entries
  "keys": ["stream1", "stream2"],
  "ids": ["0-0", "0-0"]  // Starting IDs for each stream
}
XRANGE

Returns a range of entries from a stream.

{
  "kind": "redis",
  "type": "XRANGE",
  "key": "mystream",
  "start": "-",  // Start from the beginning
  "end": "+",    // End at the last entry
  "count": 10    // Optional: maximum number of entries to return
}
XGROUP CREATE

Creates a consumer group for a stream.

{
  "kind": "redis",
  "type": "XGROUP CREATE",
  "key": "mystream",
  "group": "mygroup",
  "id": {
    "ID": "$"  // Start from the last entry
  },
  "mk_stream": true  // Optional: create the stream if it doesn't exist
}
XREADGROUP

Reads entries from a stream as part of a consumer group.

{
  "kind": "redis",
  "type": "XREADGROUP",
  "group": "mygroup",
  "consumer": "consumer1",
  "count": 10,  // Optional: maximum number of entries to return
  "block": 5000,  // Optional: block for milliseconds if no entries
  "no_ack": false,  // Optional: do not require acknowledgment
  "keys": ["stream1", "stream2"],
  "ids": [">", ">"]  // Starting IDs for each stream (">" for new entries)
}

Pub/Sub Operations

PUBLISH

Publishes a message to a channel.

{
  "kind": "redis",
  "type": "PUBLISH",
  "channel": "notifications",
  "message": "New user registered"
}
SUBSCRIBE

Subscribes to channels.

{
  "kind": "redis",
  "type": "SUBSCRIBE",
  "channels": ["notifications", "system-alerts"]
}
UNSUBSCRIBE

Unsubscribes from channels.

{
  "kind": "redis",
  "type": "UNSUBSCRIBE",
  "channel": ["notifications", "system-alerts"]
}

Geo Operations

GEOADD

Adds geospatial items to a key.

{
  "kind": "redis",
  "type": "GEOADD",
  "key": "locations",
  "position": [
    {"longitude": -122.4194, "latitude": 37.7749, "member": "San Francisco"},
    {"longitude": -74.0060, "latitude": 40.7128, "member": "New York"}
  ]
}
GEODIST

Gets the distance between two members.

{
  "kind": "redis",
  "type": "GEODIST",
  "key": "locations",
  "member1": "San Francisco",
  "member2": "New York",
  "unit": "km"  // Options: m, km, mi, ft
}

Time Series Operations (RedisTimeSeries Module)

TS.CREATE

Creates a new time series.

{
  "kind": "redis",
  "type": "TS.CREATE",
  "key": "temperature:sensor1",
  "retention": 86400,  // Optional: retention period in seconds
  "labels": [
    {"label": "sensor", "value": "temperature"},
    {"label": "location", "value": "room1"}
  ]
}
TS.ADD

Adds a sample to a time series.

{
  "kind": "redis",
  "type": "TS.ADD",
  "key": "temperature:sensor1",
  "timestamp": "*",  // Auto-generate timestamp ("*") or specify timestamp
  "value": 24.5
}
TS.RANGE

Returns a range of samples from a time series.

{
  "kind": "redis",
  "type": "TS.RANGE",
  "key": "temperature:sensor1",
  "from_timestamp": 1577836800,
  "to_timestamp": 1609459200,
  "count": 1000  // Optional: maximum number of samples to return
}
TS.MRANGE

Returns a range of samples from multiple time series.

{
  "kind": "redis",
  "type": "TS.MRANGE",
  "from_timestamp": 1577836800,
  "to_timestamp": 1609459200,
  "filter": "sensor=temperature",
  "count": 1000,  // Optional: maximum number of samples per time series
  "align": {
    "aggregator": "avg",
    "bucket_duration": 3600
  }
}

Vector Operations (RedisAI Module)

VADD

Adds a vector to a collection.

{
  "kind": "redis",
  "type": "VADD",
  "key": "product_embeddings",
  "value": {
    "VALUES": [0.1, 0.2, 0.3, 0.4, 0.5]
  },
  "vector": 768,  // Dimension of the vector
  "element": "product:123"
}
VSIM

Performs similarity search against vectors.

{
  "kind": "redis",
  "type": "VSIM",
  "key": "product_embeddings",
  "value": {
    "VALUES": [0.1, 0.2, 0.3, 0.4, 0.5]
  },
  "element": {
    "Vector": 10  // Number of nearest neighbors to return
  },
  "with_scores": true
}

TopK Operations (Redis Bloom Module)

TOPK.RESERVE

Creates a new TopK filter.

{
  "kind": "redis",
  "type": "TOPK.RESERVE",
  "key": "popular_items",
  "topk": 10,  // Number of top items to track
  "params": {
    "width": 1000,
    "depth": 7,
    "decay": 0.9
  }
}
TOPK.ADD

Adds items to a TopK filter.

{
  "kind": "redis",
  "type": "TOPK.ADD",
  "key": "popular_items",
  "items": ["item1", "item2", "item3"]
}
TOPK.LIST

Lists items in a TopK filter.

{
  "kind": "redis",
  "type": "TOPK.LIST",
  "key": "popular_items",
  "with_count": true  // Optional: include estimated counts
}

TDigest Operations (RedisBloom Module)

TDIGEST.CREATE

Creates a new TDigest sketch.

{
  "kind": "redis",
  "type": "TDIGEST.CREATE",
  "key": "latency_stats",
  "compression": 100  // Optional: compression parameter
}
TDIGEST.ADD

Adds values to a TDigest sketch.

{
  "kind": "redis",
  "type": "TDIGEST.ADD",
  "key": "latency_stats",
  "value": [10.2, 15.1, 12.7, 9.4, 11.2]
}
TDIGEST.QUANTILE

Returns the value at the given quantile.

{
  "kind": "redis",
  "type": "TDIGEST.QUANTILE",
  "key": "latency_stats",
  "quantile": [0.5, 0.9, 0.99]  // Multiple quantiles to calculate
}

JSON Operations (RedisJSON Module)

JSON.SET

Sets the JSON value at path in key.

{
  "kind": "redis",
  "type": "JSON.SET",
  "key": "user:1",
  "path": ".",
  "value": "{\"name\":\"John Doe\",\"email\":\"john@example.com\",\"age\":30}"
}
JSON.GET

Gets the JSON value at path in key.

{
  "kind": "redis",
  "type": "JSON.GET",
  "key": "user:1",
  "path": "."
}

Search Operations (RediSearch Module)

FT.CREATE

Creates a search index.

{
  "kind": "redis",
  "type": "FT.CREATE",
  "index": "users",
  "schema": {
    "fields": [
      {"name": "name", "type": "TEXT", "weight": 1.0},
      {"name": "email", "type": "TEXT", "sortable": true},
      {"name": "age", "type": "NUMERIC", "sortable": true}
    ]
  }
}
FT.SEARCH

Searches the index.

{
  "kind": "redis",
  "type": "FT.SEARCH",
  "index": "users",
  "query": "@name:John",
  "limit": {"offset": 0, "num": 10}
}

Bloom Filter Operations (Redis Bloom Module)

BF.ADD

Adds an item to a Bloom filter.

{
  "kind": "redis",
  "type": "BF.ADD",
  "key": "emailFilter",
  "item": "john@example.com"
}
BF.EXISTS

Checks if an item exists in a Bloom filter.

{
  "kind": "redis",
  "type": "BF.EXISTS",
  "key": "emailFilter",
  "item": "john@example.com"
}

Transaction Operations

MULTI

Marks the start of a transaction block.

{
  "kind": "redis",
  "type": "MULTI"
}
EXEC

Executes all commands issued after MULTI.

{
  "kind": "redis",
  "type": "EXEC"
}
WATCH

Watches keys for changes during a transaction.

{
  "kind": "redis",
  "type": "WATCH",
  "keys": ["key1", "key2"]
}
UNWATCH

Forgets all watched keys.

{
  "kind": "redis",
  "type": "UNWATCH"
}

Key Management Operations

SELECT

Selects the logical database having the specified zero-based numeric index.

{
  "kind": "redis",
  "type": "SELECT",
  "index": 1  // Database index
}
TTL

Returns the remaining time to live of a key that has a timeout.

{
  "kind": "redis",
  "type": "TTL",
  "key": "session:token"
}
TYPE

Returns the string representation of the type of the value stored at key.

{
  "kind": "redis",
  "type": "TYPE",
  "key": "mykey"
}
UNLINK

Asynchronously deletes specified keys (non-blocking).

{
  "kind": "redis",
  "type": "UNLINK",
  "keys": ["key1", "key2", "key3"]
}

Server Management

INFO

Gets information and statistics about the server.

{
  "kind": "redis",
  "type": "INFO",
  "section": ["server", "clients", "memory"]  // Optional specific sections
}
TIME

Returns the server time.

{
  "kind": "redis",
  "type": "TIME"
}
SLOWLOG

Access the Redis slow queries log.

{
  "kind": "redis",
  "type": "SLOWLOG GET",
  "count": 10  // Optional: number of entries to return
}

Replication Management

SLAVEOF

Makes the server a replica of another instance, or promotes it to master.

{
  "kind": "redis",
  "type": "SLAVEOF",
  "set": {
    "HP": {
      "host": "master.example.com",
      "port": 6379
    }
  }
}

To promote to master:

{
  "kind": "redis",
  "type": "SLAVEOF",
  "set": "NOONE"
}
WAIT

Blocks until the specified number of replicas have processed the write.

{
  "kind": "redis",
  "type": "WAIT",
  "num_replicas": 2,
  "timeout": 1000  // Milliseconds
}

ACL Management

ACL LIST

Lists the current ACL rules.

{
  "kind": "redis",
  "type": "ACL LIST"
}
ACL SETUSER

Creates or modifies an ACL user.

{
  "kind": "redis",
  "type": "ACL SETUSER",
  "username": "appuser",
  "rules": [
    "On",                   // Enable the user
    {"AddPass": "password"},
    "AllCommands",          // Allow all commands
    "AllKeys"               // Allow access to all keys
  ]
}

Best PracticesCopied!

Performance Optimization

  1. Connection Pooling:

    • Implement connection pooling to reuse connections

    • Avoid creating a new connection for each operation

    • Set appropriate connection pool size based on workload

  2. Pipelining:

    • Use pipelining to send multiple commands at once

    • Reduce round-trip latency by batching operations

    • Group related operations when possible

  3. Data Structure Selection:

    • Choose appropriate Redis data structures for your use case

    • Use specialized commands like HINCRBY, ZADD, etc., for better performance

    • Leverage Redis' sorted sets for leaderboards and priority queues

  4. Caching Strategy:

    • Set appropriate TTL (Time-To-Live) for cached data

    • Use EXPIRE or include expiration in SET operations

    • Implement cache invalidation strategies

Memory Management

  1. Data Size Optimization:

    • Keep keys and values as small as possible

    • Use hash structures for objects with many fields

    • Monitor memory usage with INFO MEMORY command

  2. Eviction Policies:

    • Configure appropriate eviction policies (e.g., LRU, LFU)

    • Set maxmemory limit based on available resources

    • Monitor eviction metrics regularly

Security

  1. Authentication and Authorization:

    • Use strong passwords for Redis AUTH

    • Implement ACL (Access Control List) for fine-grained permissions

    • Enable TLS for encrypted connections

  2. Network Security:

    • Run Redis behind a firewall

    • Bind to specific interfaces rather than 0.0.0.0

    • Consider using Redis in protected mode

Error Handling

  1. Connection Issues:

    • Implement automatic reconnection mechanisms

    • Add exponential backoff for retry attempts

    • Monitor connection health with heartbeats

  2. Command Failures:

    • Handle Redis errors gracefully

    • Add fallback mechanisms for critical operations

    • Log errors with appropriate context

Common Use CasesCopied!

  • Caching frequently accessed data

  • Session storage for web applications

  • Leaderboards and real-time analytics

  • Message brokering with Pub/Sub

  • Rate limiting and request throttling

  • Geospatial applications

  • Full-text search with RediSearch

  • Real-time filtering with Bloom filters

  • Distributed locks and synchronization

  • Job queues and task management

  • Vector similarity search for AI/ML applications

  • Time series data for metrics and monitoring

  • Statistical analysis with TDigest and TopK

See the official Redis Documentation