diff --git a/docs/platform/how-to/state-management.md b/docs/platform/how-to/state-management.md new file mode 100644 index 00000000..b70573ef --- /dev/null +++ b/docs/platform/how-to/state-management.md @@ -0,0 +1,70 @@ +# State management + +When deploying a service, enabling state management enables the service to maintain its state between restarts by creating a storage area for data and files, ensuring persistence across service restarts. + +## Enabling state management + +Enable state management during service deployment if your code relies on state or expects files to persist across restarts: + +![state management](../images/how-to/state/state-management.png){width=80%} + +Enabling state creates a `state` folder that can be accessed by your code. + +!!! important + + Note that data stored in this area is retained between service restarts and is shared between all replicas of the service, but it is not available globally to other services. + +## Storing files in the state folder + +You can use this `state` folder as a place to download and store model and other files. + +!!! note + + While the `state` folder is not visible in the UI, it is still available for use. + +For example, the following code downloads a model file to the `state` folder. It subsequently loads the model file from the `state` folder when needed: + +``` python +import requests + +model = requests.get('https://acme.com/models/model1.dat') +f = open('./state/model1.dat', 'wb') +f.write(model) +f.close() +... +f = open('./state/model1.dat', 'rb') +... +``` + +The model can be modified at run time, and its state is preserved between service restarts. + +!!! note + + There is a 100GB storage limit for the state management storage area. + +## Using state from Quix Streams + +If you have enabled state management for a service, then you can also use the [state management features](https://quix.io/docs/client-library/state-management.html) of Quix Streams in that service. This enables you to store and retrieve data in various formats, and retain that state between service restarts. See the [documentation](https://quix.io/docs/client-library/state-management.html) for further details. + +## Running Quix Streams locally + +If you are running Quix Streams locally, rather than using it as part of a deployed service in the cloud, then the `state` folder is created automatically for you when you use state management features of the library. For example, if you ran the following code locally: + +``` python +from quixstreams import LocalFileStorage + +storage = LocalFileStorage() +storage.clear() + +storage.set("KEY1", 12.51) +storage.set("KEY2", "str") +storage.set("KEY3", True) +storage.set("KEY4", False) +``` + +Then you would see the `state` folder and its contents: + +``` +$ ls state +key1 key2 key3 key4 +``` diff --git a/docs/platform/images/how-to/state/state-management.png b/docs/platform/images/how-to/state/state-management.png new file mode 100644 index 00000000..98744cfd Binary files /dev/null and b/docs/platform/images/how-to/state/state-management.png differ diff --git a/mkdocs.yml b/mkdocs.yml index ef93fdf5..fa335a47 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -45,6 +45,7 @@ nav: - 'Add environment variables': 'platform/how-to/environment-variables.md' - 'Testing using Quix data store': 'platform/how-to/testing-data-store.md' - 'Configure deployments': 'platform/how-to/yaml-variables.md' + - 'State management': 'platform/how-to/state-management.md' - 'Tutorials': - 'platform/tutorials/index.md' - 'Computer vision':