Stay organized with collectionsSave and categorize content based on your preferences.
This page explains how to store vectors in hashes. Hashes provide an efficient way to store vectors in Redis.
Data serialization
Before storing vectors in a Redis hash, vectors need to be converted into a format that Redis understands. It requires vector serialization into binary blobs where the size equals the data type's byte size (e.g., 4 for FLOAT32) multiplied by the vector's number of dimensions. A popular choice for numerical vectors is the PythonNumPy library:
Connect to Redis
Before storing the vector in a hash, establish a connection to your Redis instance using a client likeredis-py:
Store the vector in a hash
Redis hashes are like dictionaries, with key-value pairs. Use the RedisHSETcommand to store your serialized vector:
import numpy as np
import redis
# Sample vector
vector = np.array([1.2, 3.5, -0.8], dtype=np.float32) # 3-dimensional vector
# Serialize to a binary blob
serialized_vector = vector.tobytes()
redis_client = redis.cluster.RedisCluster(host='your_redis_host', port=6379)
redis_client.hset('vector_storage', 'vector_key', serialized_vector) # 'vector_key' is a unique identifier
For successful indexing, your vector data must adhere to the dimensions and data type set in the index schema.
Backfilling Indexes
Backfilling indexes may occur in one of the following scenarios:
Once an index is created, the backfilling procedure scans through Redis keyspace for entries that meet the index filter criteria.
Vector indexes and their data are persisted inRDB snapshots. When an RDB file is loaded, an automatic index backfilling process is triggered. This process actively detects and integrates any new or modified entries into the index since the RDB snapshot was created, maintaining index integrity and ensuring current results.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Indexing vectors\n\nThis page explains how to store vectors in hashes. Hashes provide an efficient way to store vectors in Redis.\n\nData serialization\n------------------\n\nBefore storing vectors in a Redis hash, vectors need to be converted into a format that Redis understands. It requires vector serialization into binary blobs where the size equals the data type's byte size (e.g., 4 for FLOAT32) multiplied by the vector's number of dimensions. A popular choice for numerical vectors is the Python [NumPy library](https://numpy.org/):\n\nConnect to Redis\n----------------\n\nBefore storing the vector in a hash, establish a connection to your Redis instance using a client like [redis-py](https://github.com/redis/redis-py):\n\nStore the vector in a hash\n--------------------------\n\nRedis hashes are like dictionaries, with key-value pairs. Use the Redis `HSET` command to store your serialized vector: \n\n```\nimport numpy as np\nimport redis\n\n# Sample vector\nvector = np.array([1.2, 3.5, -0.8], dtype=np.float32) # 3-dimensional vector\n\n# Serialize to a binary blob\nserialized_vector = vector.tobytes()\n\nredis_client = redis.cluster.RedisCluster(host='your_redis_host', port=6379)\n\nredis_client.hset('vector_storage', 'vector_key', serialized_vector) # 'vector_key' is a unique identifier\n```\n\n- For successful indexing, your vector data must adhere to the dimensions and data type set in the index schema.\n\nBackfilling Indexes\n-------------------\n\nBackfilling indexes may occur in one of the following scenarios:\n\n- Once an index is created, the backfilling procedure scans through Redis keyspace for entries that meet the index filter criteria.\n- Vector indexes and their data are persisted in [RDB snapshots](/memorystore/docs/cluster/about-rdb-snapshots). When an RDB file is loaded, an automatic index backfilling process is triggered. This process actively detects and integrates any new or modified entries into the index since the RDB snapshot was created, maintaining index integrity and ensuring current results."]]