---
id: cache--redis-write
title: "Redis write"
summary: "Writes one key to a Redis cache with an expiry: the whole input as JSON, or a text from an expression. Use it to fill a cache, for idempotency (write only if missing) and to share values with C# scripts that use StackExchange.Redis or `IDistributedCache`."
audience: user
keywords: "input, written, exists, connectionstring, storagemode, instancename, connecttimeoutms, synctimeoutms, cacertificate, clientcertificate, key, value, ttl, slidingttl, condition"
---
# Redis write

Writes one key to a Redis cache with an expiry: the whole input as JSON, or a text from an expression. Use it to fill a cache, for idempotency (write only if missing) and to share values with C# scripts that use StackExchange.Redis or `IDistributedCache`.

## Pins

| Pin | Kind | Types | Description |
|---|---|---|---|
| Input | input | any | The record to store (empty `Value`), and what the expressions read. |
| Written | continue | same as input | The unchanged input, after the value was stored. |
| Exists | no data | same as input | `Condition` `1` only: the unchanged input, when the key already existed and nothing was written. |

## Configuration

The configuration is shared with Redis read and Redis delete.

| Field | Type | Default | Description |
|---|---|---|---|
| `ConnectionString` | string (max 2048) | | Required. StackExchange.Redis configuration string, e.g. `redis1:6379,ssl=true,password=...,defaultDatabase=0`. |
| `StorageMode` | dropdown: `0` String, `1` DistributedCache | `0` | `0`: a plain Redis string (`SET`). `1`: the `IDistributedCache` hash format (`data`, `absexp`, `sldexp`). |
| `InstanceName` | string (max 255) | | Visible with `StorageMode` `1`. Prefix in front of every key, the `RedisCacheOptions.InstanceName` of the scripts. |
| `ConnectTimeoutMs` | integer (min 1) | `5000` | Connect timeout in milliseconds. |
| `SyncTimeoutMs` | integer (min 1) | `5000` | Timeout of one operation in milliseconds. |
| `CaCertificate` | certificate name | | CA that signed the server certificate (`ssl=true`, private CA). Empty: OS trust store. |
| `ClientCertificate` | certificate name | | Client certificate with private key for mutual TLS. |
| `Key` | string (max 1024) | | Required. The key, usually an expression: `order:{data.OrderId}`. |
| `Value` | text | | Text to store, usually an expression such as `{data.Token}`. Empty: the whole input as JSON. |
| `Ttl` | integer (min 0) | `3600` | Absolute time to live in seconds. `0`: no expiry. |
| `SlidingTtl` | integer (min 0) | `0` | Visible with `StorageMode` `1`. Sliding expiration in seconds: the entry expires when not read for this long. `0`: none. |
| `Condition` | dropdown: `0` Always, `1` Only if missing | `0` | Visible with `StorageMode` `0`. `1`: `SET NX`, writes only when the key doesn't exist; otherwise the record goes to `Exists`. |

## Behaviour

- `Key` and `Value` are evaluated for every record: `{data.X}` is the input, `{meta.X}` the metadata, `{config.X}` the flow configuration. An empty key fails that record. `Ttl` and `SlidingTtl` are fixed numbers (an expression setting `=Package.Setting` works, a per-record expression doesn't).
- Empty `Value`: the input is stored as JSON written by `ViaJsonSerializer` (default options, UTF-8), byte for byte what a script writes with `ViaJsonSerializer.Serialize`. A `Value` is stored as is (UTF-8 text).
- The connection is opened at flow start and shared by all Redis components on the Host with the same connection string, timeouts and certificates. A wrong connection string, a missing certificate, an unreachable server or `Condition` `1` with `StorageMode` `1` stops the flow at start.
- A timeout or lost connection fails that record (recoverable error); the next record is tried again.

## Notes

- Keys without expiry (`Ttl` `0`) stay until deleted and fill a large cache; choose it deliberately.
- `Condition` `1` gives claim-once and idempotency without a lock: the first record writes, the others go to `Exists`. `IDistributedCache` has no such call, so it isn't available in DistributedCache mode.
- To store part of the input as JSON, shape it with the data mapper first and leave `Value` empty.
- Keep the password in an expression setting: `"ConnectionString": "=Secrets.RedisConnection"`.

## Example

Process each order once, across Hosts:

```json
{ "ConnectionString": "=Secrets.RedisConnection", "Key": "processed:{data.OrderId}", "Value": "1", "Ttl": 86400, "Condition": 1 }
```

Kafka consumer → Json deserializer → **Redis write** ─ Written → process the order; Exists → Log (duplicate)
