Repository navigation
Description
(This issue was split off from #83, and represents the current direction of data tiering)
Problem Statement
Valkey stores data in DRAM, and a major adoption barrier is the cost of DRAM. Many applications exhibit behavior where only a small % of their data is frequently accessed while a much larger % of the their data is infrequently accessed. By using bulk storage (i.e. flash/SSD) to store the infrequently accessed data and keeping the hot data in DRAM, these applications are substantially cheaper while maintaining almost the same level of performance. Essentially, disks can be used to offer larger storage capacity at a lower cost.
AWS has developed a data tiering service which has been used extensively since November 2021. With data tiering enabled, we automatically and transparently tier data between DRAM and locally attached NVMe SSDs which serves as an extension to memory. SSDs provide slightly higher latencies for caching workloads than DRAM, but has a much lower cost of storage. This allows users to do cost optimization by maintaining a working set in memory while tiering infrequently accessed data to cheaper SSD storage. When using clusters with data tiering, customer can save over 60% per GB of capacity while having minimal performance impact on applications when frequently accessed keys are maintained in DRAM. We would like to contribute this functionality to open-source Valkey.
ElastiCache data tiering has a core engine layer that manages in-memory data and spills the least used data (e.g. via LRU/LFU) to SSD. Data on SSD is retrieved on demand - when a command needs a key which is not in memory, the client gets blocked until the key is brought back into memory. It also handles evictions when memory+SSD is full, and expiration of keys that reside in both memory and SSD. The second layer is a non-volatile storage manager which runs on storage media such as NVMe SSD devices that efficiently handles data storage and retrieval. This storage manager allows parallel disk I/O access and minimizes write amplification through batching and efficient data layouts, along with compaction of items on SSD.
Key Design Choices
1. Should we keep the key in memory?
In Valkey, each item stored in memory requires ~16B of metadata overhead for keys without TTL, and 24B-32B for keys with TTL, plus the key and value sizes depending on the layout. From past experience operating the product in AWS ElastiCache, we noticed the typical key is 40B-60B long. For items that are spilled to external storage, we considered both approaches of keeping the keys in memory and not doing that:
a) We can remove the item from the main hashtable and roll the item entirely into the external storage. This effectively frees up the key almost entirely from memory including the 16B - 32B of metadata and the key string in addition to the values. However, the downside is that overall system becomes complicated in multiple dimensions:
- When processing a command that reads/writes to a key, if the key is not present in memory, we canβt effectively determine whether the key is present in the overall system or not without submitting an read IO request to the storage layer. This can be especially expensive when the application is trying to warm up the cache as it will incur additional latency from looking up in the external storage for each of the keys.
- When trying to delete a key, if the key is not present in memory, we canβt effectively determine if the key is absent from the system or not without submitting a DEL request to the storage layer. This again can be expensive as deleting an item from the system often requires doing an IO to the external storage.
- For background jobs that iterate over the keyspace, we need to build a similar mechanism in the storage layer. Namely,
- For active expirations, we need to add the itemβs TTL information in the storage layer, and implement a job to iterate over the items and deleting the items that have expired.
- For evictions, we need to sample keys in the storage layer and evict keys based on the configured
maxmemory-policy - For slot migrations, we need to be able to iterate over the keys in the slot that resides in the storage layer and pass them over to the target node.
b) For simplicity, we can keep the key and its metadata in memory and only move the value of the item to external storage. This means that once an item is spilled, we can safely delete the value from memory, but need to keep the metadata and the key string in memory, which amounts to approximately 60B - 90B of data. The implication of this approach is that the size of the value need to be significantly larger than the key size to achieve sufficient amount of cost savings. In our case, the value size needs to be minimally 300B - 400B in order for the user to fully utilize the SSD. We built this solution and over time, we learned that there is a certain category of user applications dominated by small value sizes where the overall cost savings is limited. For those users, it is advisable to not use data tiering when the memory cost savings doesnβt justify the cost of additional latency to serve commands.
One other consequence of storing items with small value sizes is that the SSD space is unable to get effectively utilized, and eventually degrades into a situation where all the values are spilled to external storage and the memory is fully occupied with keys alone before SSD gets filled up. This causes items to be constantly thrashing in and out of memory leading to poor latency for all incoming commands. For data tiering to operate effectively, we want a certain amount of working set data (i.e. X% of keys) to reside in memory at all times. We achieved this by evicting items instead of spilling items to external storage when the % of items in memory drops below a certain threshold.
2. The level of object to tier
Aside from simple string objects, Valkey also supports a wide variety of composite objects types including list, set, sorted set, hash, etc where operations can happen on a sub-object level such as retrieving one element from a hash via HGET command. For Valkey, the actual cost of reading the extra data in such composite data types depends on the memory footprint and the memory bandwidth cost of the unused data. Let's say a typical NVMe SSD transfers data at ~3GB/s, then with a 128MB data item you have a data transfer time of > 40ms from the SSD. This means reading large keys from SSD into memory have significant latency penalties, and that latency delays reading in other keys to serve other commands.
We operated data tiering in AWS ElastiCache with a configurable limit where items with serialized size of > 128MB will not be spilled to SSD. Over time, we noticed that very few user applications generate large collection objects with serialized size of > 128MB. For those few cases, we typically advised them to break up a large collection object into several smaller objects to be more performance efficient. With this learning, we want to start by tiering at the entire object level instead of at a sub-object level as tiering at a sub-object level would incur significant complexity and can be future work.
3. Data Replication
During replication, we want the replica to have a similar key distribution in memory vs. on external storage as the primary. Otherwise, any failover would lead to sub-optimal performance on the new primary until the key distribution gets normalized. With a typical ~20GB of memory, this process of re-warming the working set in memory can take up to several minutes where the application would experience prolonged latency.
Keeping the data distribution similar during full sync replication can also benefit the typical user scenario when the working set data is similar between the primary and replica. For the few exceptional cases when the client traffic pattern on the replica has a different working set from the primary, the primary might try to update keys that are not in the read working set on the replica which can cause the replication commands to be delayed waiting on keys to get fetched into memory. In such cases, we allow the replica to buffer a bit more data in memory compared to the primary (e.g. ~40MB) to make room for those keys.
4. One Interesting Barrier to Usage
At times, we received questions from users on whether data tiering would be suitable for their workload (i.e. the average key sizes, value sizes, the volume of data, access patterns, etc) for which the answer is not always obvious. Often times people want to be able to migrate between non data tiering cluster and a data tiering enabled cluster, so that they can try out the solution with their workload and be able to easily migrate back if data tiering doesnβt fit their workload. This calls for the need to support a compatible RDB snapshot format. i.e.
- When a data tiering enabled replica replicates from a primary without data tiering, it loads a regular RDB snapshot and spills items to external storage if the memory limit is reached while loading.
- When a non data tiering enabled replica replicates from a data tiering enabled primary, the primary transfers over an RDB snapshot which can be loaded in memory only.
Solution Architecture
1. Basic tiering capability
Data tiering capability will be a separate module which can be loaded / unloaded based on user needs. We can also have the module loaded but not spill items based on a configuration. The overall system has 2 major components:
a) The Main Server Thread
The main thread in Valkey leverages module interface which allows asynchronous movement of data from memory to external tiered storage media based on the client traffic pattern.
- When the memory usage exceeds
maxmemory, the least used keys (as determined by a certain policy) start to get spilled from memory to the tiering engine (i.e. flash / SSD) in an asynchronous manner. Embedded items that fits within theserverObject(i.e. 2 cache lines) and items with a serialized size of > X MB (defaulted at 128MB) are not eligible to be spilled to external storage for safety and performance reasons. Please note the policy used to determine the keys to spill doesnβt work the same way as the eviction policy (i.e.maxmemory-policy). When a key gets spilled, we label theserverObjectwith some additional bits of information indicating its storage state to be no longer in memory, and free its value from memory. - When an incoming client command attempts to access a key that is currently not in memory as indicated by its storage state, the client gets blocked until the value gets fetched back into memory asynchronously. The client is then unblocked and resumes the execution of the command when the key arrives in memory.
- When client attempts to delete a key that is not in memory, we block the client and submit a request to the storage layer to delete the key on the external media. Once the deletion request completes, the client gets unblocked and deletes the key from memory.
b) The Tiering Engine
The tiering engine runs on a separate thread and is responsible to take keys from the main thread and handle IO to the external storage media in an efficient manner. Note that reading/writing to storage media doesnβt block the main thread.
- Writing the key to the storage media involves doing RDB serialization for the item data into a binary blob and storing it
- Reading the key from storage media into memory involves reading the binary blob and de-serializing the RDB data into the appropriate in-memory representation before handing it back to the main thread to be put back into the main hashtable.
- The tiering engine scans the log file on the external media and handles compaction of data if needed. This is namely meaningful for SSD storage where we want to reduce disk fragmentation.
- Moving data to/from external storage often requires multi-threading in order to fully utilize the resources.
2. Snapshotting
- When snapshotting the data, we iterate over the main hashtable, and write keys with values in memory to RDB file as usual. For keys with value on the external storage media, we read in the RDB serialized value stored on the external storage media and write the key and value into the same RDB file with one additional opcode indicating that the item resides on the external media. While fetching the item from SSD, we need to delay mutation operations on the key. The end result is a Valkey RDB snapshot file with similar format as the current RDB format. We can submit multiple keys simultaneously to SSD in order to reduce the performance overhead of going to SSD for a large data set.
- Loading snapshot into a data tiering enabled server:
- For keys in the RDB file with the marker indicating in memory, valkey server loads the keys into memory as usual.
- Once the memory usage exceeds
maxmemory, loading additional keys marked to be in RAM would cause the restore process to abort as usual. Loading keys marked to be on external storage would cause the key to be tiered. This ensures we end up with the same tiered key distributions as the source cluster.
- Loading snapshot into a non data tiering server: all keys gets loaded into memory. The load fails if
maxmemoryis exceeded. That way we can load a data tiering snapshot into a non data tiering valkey server with sufficient amount of memory.
3. Replication
Full sync replication process works as follows:
- The replica would request SYNC from the primary
- Primary takes a snapshot of both in-memory and tiered data set and generates an RDB file as described by the snapshotting process above, and sends down the RDB file to the replica
- The replica loads the RDB snapshot file as indicated above, which automatically stores items in memory and tiers other items to external storage.
- After the replica loads the RDB snapshot, it replays the incremental replication commands from the primary, which concludes the full sync.
Please note that the process of generating the RDB snapshot and loading the snapshot with items tiered to external storage can make the full sync process longer compared to pure in-memory mode.
4. Evictions
When both memory and tiered storage media are full, or the number of items with value that reside in memory drops beyond a certain %, items start to get evicted from the system based on the configured maxmemory-policy. Eviction happens by randomly sampling a set of keys, and picking the best candidate to evict based on the configured policy (i.e. volatile/allkeys LRU/LFU) as what is currently done. The main difference is that if the key is not in memory, then we need to submit a request to the storage layer to delete the key.
In the future if we are to spill keys into external storage, we would need to move similar eviction logic into the storage layer, which is responsible to iterate over the items on the external storage and picking the best candidate to evict based on LRU/LFU information. This would allow us to evict items both from memory and from external storage.
5. Expiration
Expired items (both in-memory and tiered) gets deleted from the system. Expiration of items on external storage will not require the element to be loaded from external storage back into memory. We will simply delete the data from the external storage and the key and metadata from memory.
In the future if we are to spill keys and metadata into external storage, we would need to move active expiration logic into the storage layer, which would regularly iterate over the items on the external storage and delete the items that have expired.
6. Slot migration
Slot migration is conceptually similar to full sync data replication, except it operates on keys in a particular slot. Here we need to transfer both in-memory data and the tiered data to the target node, and keeping a similar layout of in-memory and tiered data. As we iterate over the keys in the slot, we need to read in the values of the keys on external storage and transfer it to the target node. At the end of the slot migration, the items in the slot are asynchronously purged from the source node.
7. Possible future extensions / enhancements
- Performance improvement for tiering engine i.e. optimize SSD access to allow faster random read access from SSD.
- Multiple types of storage devices other than local SSD (e.g. various cloud storage solutions including DynamoDB, S3, or other database solutions etc), potentially with user policies on which layers to tier to
Activity
madolson commentedon Mar 6, 2026
@kronwerk FYI, since it's related to the work you started with external storage.
kronwerk commentedon Mar 7, 2026
@QuChen88 that's very good news indeed. could u pls give some expectations of yours about when this feature could be production-ready put to review and/or merged into Valkey? whether it would be days, weeks or months, if it's possible to estimate that now. that would be very helpful, thx
QuChen88 commentedon Mar 9, 2026
It definitely won't be days as we need to redo lots of our internal implementations to make it into a module. I am looking into the module APIs and hopefully get a proposal / PR in a few weeks time. The full production implementation will take longer.
poiuj commentedon Mar 12, 2026
@QuChen88 thank you for the proposal.
We need to be very careful with the assumptions here. The approach to keep all the keys in memory for simplicity blocks many workloads. As you correctly explained in the proposal. People see data tiering as a way to increase the memory without paying for expensive RAM. A storage layer can utilize SSD very effectively and provide very attractive performance characteristics.
madolson commentedon Mar 16, 2026
Some key conversations I would like to have in todays meeting:
madolson commentedon Mar 16, 2026
@QuChen88 Will go off and do some PoCs to help determine how the APIs might look like for storing values and keys and for just keys. He will also come back with performance numbers.
dvkashapov commentedon Mar 17, 2026
@QuChen88 Do you also want to take a look at #3296 proposed API for external key-value storage to avoid introducing double work in the core? I feel that AWS's solution probably makes same assumptions for integrating with Valkey core and community would benefit from collaborating on such big project.
QuChen88 commentedon Mar 17, 2026
Will take a look at that.
xdk-amz commentedon Apr 14, 2026
As you start on the PoC, I think it is important to be clear on all the decisions and assumptions we are making. I appreciate the
Key Design Choicessection but suggest that you expand on it as it could use more clarity and should continue to grow as the project continues.Each section should make clear:
For example, "Should we keep the key in memory?" does a good job answering "what decision", "what are the options", and "what tradeoffs". But it is not clear what the recommendation is or how difficult it would be to implement the alternative as a configuration in the future.
"The level of object to tier" could make "the decision" more clear and present more options. I read a general recommendation to limit the size of spilled items, but what about alternatives like sub-value spilling? We have hash-field expiration now in Valkey 9, why not hash-field spilling?
"Data Replication" does not seem to explore any decision other than the additional buffering on the replica, are there other options or considerations? This feels more like a requirement rather than a design choice/decision unless there is more to explore here.
"One Interesting Barrier to Usage" This also doesn't seem like a decision, calling for a compatible RDB snapshot feels like a requirement and not a design decision, and the title of this section could be named in a more descriptive way IE "RDB Snapshot".
I think there are other unmentioned decisions to be made which should continue to be explored in this section, like:
This approach has some issues in our experience and could likely be improved. What other metrics can we consider to measure the effective 'working set'? There could be space here to introduce metrics into core which could inform the value of data tiering for a user that would also benefit spilling mechanisms. Are there probabilistic data structures or algorithms we can use to measure 'working set' sizes? This is somewhat parallel to the 'hot key' problem, but more generalized to the entire dataset.
"Configuration" - What options should we give to the user in terms of tuning and configuration? One of the Valkey tenets is heuristics over configuration, but data tiering is difficult to generalize across a wide variety of key:value ratios, value and key size distributions, hardware profiles, etc. and will certainly benefit from some amount of configuration.
"Metrics" - Similar to "Working Set", but a more broadly-scoped look into what metrics exist already, what could be added to core, and what should be specific to the data tiering module. This can help inform a data-driven approach to the efficacy of data tiering across varying workloads beyond simple latency benchmarks of cold/hot key fetches. It is difficult for users to reason about the benefits of data tiering without a deep understanding of the underlying mechanics and anything we can do to close that gap will be very helpful.
QuChen88 commentedon Apr 14, 2026
In the POC, I am starting to experiment with the following (sorry for not calling it out explicitly earlier) -
dubey02 commentedon Jun 17, 2026
Update : We're actively tracking data tiering work for Valkey at our public fork: dubey02/valkey-data-tiering. POC code and design artifacts are posted there, and all updates are shared in this Slack channel: https://valkey-oss-developer.slack.com/archives/C094VB713S4. Interested folks are welcome to check it out and follow along.
gkorland commentedon Oct 25, 2026
Will the proposed tiering mechanism support custom data types defined by other modules.
Specifically:
dubey02 commentedon Oct 25, 2026
@gkorland , the current ongoing work on Data Tiering would support custom data types by other modules.
Will tiering support RedisModuleType values using their existing RDB serialization callbacks? Yes. A spilled value is written with the module's own rdb_save callback and read back with rdb_load, so modules need no new code. We verified this with valkey-json and valkey-bloom.
Can a module-defined value be transparently offloaded to SSD and restored when accessed? Yes. In V1, The key stays in memory, the value moves to SSD, and any command touching the key fetches the value back before it runs, so the module never knows. One limit for V1: modules that read keys outside their command arguments, from keyspace-event handlers, or from their own threads (valkey-search is the main example) are out of scope.
Will loading and deserialization be asynchronous, allowing Valkey to continue serving unrelated requests? Yes. Fetch and deserialize run on IO threads while only the one waiting client is blocked. The main thread keeps serving every other request.
Will the maximum tierable object size be configurable beyond the proposed 128 MB default? Yes. The 128 MB limit is a default and will be configurable. Raising it is not free, though. A large value must be serialized, written, read back, and deserialized in one piece, so very large items add noticeable latency to the client that touches them.