|
| 1 | +--- |
| 2 | +title: "State TTL Migration Compatibility" |
| 3 | +weight: 1 |
| 4 | +type: docs |
| 5 | +aliases: |
| 6 | + - /dev/stream/state/state_migration.html |
| 7 | + - /apis/streaming/state_migration.html |
| 8 | +--- |
| 9 | +<!-- |
| 10 | +Licensed to the Apache Software Foundation (ASF) under one |
| 11 | +or more contributor license agreements. See the NOTICE file |
| 12 | +distributed with this work for additional information |
| 13 | +regarding copyright ownership. The ASF licenses this file |
| 14 | +to you under the Apache License, Version 2.0 (the |
| 15 | +"License"); you may not use this file except in compliance |
| 16 | +with the License. You may obtain a copy of the License at |
| 17 | +
|
| 18 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | +
|
| 20 | +Unless required by applicable law or agreed to in writing, |
| 21 | +software distributed under the License is distributed on an |
| 22 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | +KIND, either express or implied. See the License for the |
| 24 | +specific language governing permissions and limitations |
| 25 | +under the License. |
| 26 | +--> |
| 27 | + |
| 28 | +# State TTL Migration Compatibility |
| 29 | + |
| 30 | +Starting with **Apache Flink 2.2.0**, the system supports seamless enabling or disabling of **State Time-to-Live (TTL)** for existing state. |
| 31 | +This enhancement removes prior limitations where a change in TTL configuration could cause a `StateMigrationException` during restore. |
| 32 | + |
| 33 | +## Version Overview |
| 34 | + |
| 35 | +| Flink Version | Change | |
| 36 | +| ------------- | ------------------------------------------------------------------------------- | |
| 37 | +| **2.0.0** | Introduced `TtlAwareSerializer` to support TTL/non-TTL serializer compatibility | |
| 38 | +| **2.1.0** | Added TTL migration support for **RocksDBKeyedStateBackend** | |
| 39 | +| **2.2.0** | Added TTL migration support for **HeapKeyedStateBackend** | |
| 40 | + |
| 41 | +> Full TTL state migration support across all major state backends is available from Flink **2.2.0** onwards. |
| 42 | +
|
| 43 | +## Motivation |
| 44 | + |
| 45 | +In earlier Flink versions, switching TTL on or off in a `StateDescriptor` resulted in incompatibility errors. |
| 46 | +This was because TTL-enabled state used a different serialization format than non-TTL state. |
| 47 | + |
| 48 | +## Compatibility Behavior |
| 49 | + |
| 50 | +With the changes introduced across versions 2.0.0 to 2.2.0: |
| 51 | + |
| 52 | +* Flink can now restore state created **without TTL** using a descriptor **with TTL enabled**. |
| 53 | +* Flink can also restore state created **with TTL** using a descriptor **without TTL enabled**. |
| 54 | + |
| 55 | +The serializers and state backends transparently handle the presence or absence of TTL metadata. |
| 56 | + |
| 57 | +## Supported Migration Scenarios |
| 58 | + |
| 59 | +| Migration Type | Available Since | Behavior | |
| 60 | +| -------------------------------------- | ----------------------------- | ------------------------------------------------------------------- | |
| 61 | +| Non-TTL state → TTL-enabled descriptor | 2.1.0 (RocksDB), 2.2.0 (Heap) | Previous state restored as non-expired. TTL applied on new updates/accesses. | |
| 62 | +| TTL state → Non-TTL descriptor | 2.1.0 (RocksDB), 2.2.0 (Heap) | TTL metadata is ignored. State becomes permanently visible. | |
| 63 | + |
| 64 | +## Implementation Details |
| 65 | + |
| 66 | +The compatibility is achieved through the following changes: |
| 67 | + |
| 68 | +* **TtlAwareSerializer** (Flink 2.0.0): Wraps user serializers to support reading/writing TTL and non-TTL formats. |
| 69 | +* **Backend migration logic**: |
| 70 | + * RocksDBKeyedStateBackend (Flink 2.1.0) |
| 71 | + * HeapKeyedStateBackend (Flink 2.2.0) |
| 72 | + |
| 73 | +These components check the metadata during restore and adapt accordingly. |
| 74 | + |
| 75 | +## Limitations |
| 76 | + |
| 77 | +* Changes to TTL **parameters** (e.g. expiration time, update behavior) are not always compatible. These may require serializer migration. |
| 78 | +* TTL is not applied retroactively. Existing entries restored from non-TTL state will only expire after their next access or update. |
| 79 | +* This compatibility assumes no other incompatible changes to the state serializer. |
| 80 | + |
| 81 | +## Example |
| 82 | + |
| 83 | +```java |
| 84 | +ValueStateDescriptor<String> descriptor = new ValueStateDescriptor<>("user-state", String.class); |
| 85 | +descriptor.enableTimeToLive(StateTtlConfig.newBuilder(Time.hours(1)).build()); |
| 86 | +``` |
| 87 | + |
| 88 | +If this descriptor replaces an earlier one without TTL, the state will be restored successfully in Flink 2.2.0+ and TTL will be enforced going forward. |
| 89 | + |
| 90 | +## Related Information |
| 91 | + |
| 92 | +* [Working with State]({{< ref "docs/dev/datastream/fault-tolerance/state" >}}) |
| 93 | +* [FLINK-32955 JIRA](https://issues.apache.org/jira/browse/FLINK-32955) |
| 94 | + |
| 95 | +## FAQ |
| 96 | + |
| 97 | +### Can I disable TTL after it was previously enabled? |
| 98 | + |
| 99 | +Yes. Flink will restore the values and ignore any TTL expiration metadata. |
| 100 | + |
| 101 | +### Is this supported in RocksDB and Heap backends? |
| 102 | + |
| 103 | +Yes, RocksDB since 2.1.0, Heap since 2.2.0. |
| 104 | + |
| 105 | +### Which Flink version fully supports TTL migration? |
| 106 | + |
| 107 | +Flink 2.2.0 is the first version where all necessary support is available. |
| 108 | + |
| 109 | +### Do I need to change anything in my savepoint? |
| 110 | + |
| 111 | +No. The migration is handled internally by Flink, provided serializers are otherwise compatible. |
| 112 | + |
| 113 | +{{< top >}} |
0 commit comments