|
| 1 | +--- |
| 2 | +sidebarTitle: Ephemerals |
| 3 | +--- |
| 4 | + |
| 5 | +import {Tabs} from 'nextra/components' |
| 6 | + |
| 7 | +# Ephemeral records |
| 8 | + |
| 9 | +Oxia records can be optionally marked as *ephemerals*. |
| 10 | + |
| 11 | +Ephemeral records have their lifecycle tied to a particular client instance, and they |
| 12 | +are automatically deleted when the client instance is closed. |
| 13 | +These records are also deleted if the client cannot communicate with the Oxia |
| 14 | +service for some extended amount of time, and the session between the client and |
| 15 | +the service "expires". |
| 16 | + |
| 17 | +Application can control the session behavior by setting the session timeout |
| 18 | +appropriately with [WithSessionTimeout] option when creating the client instance. |
| 19 | + |
| 20 | +Oxia can partition the data in different namespaces. Application can use namespaces to isolate different use cases |
| 21 | +and also have more visibility in the metrics, which are going to be labeled with the namespace as well. |
| 22 | + |
| 23 | +Each namespace has its own independent key-space and set of shards. |
| 24 | + |
| 25 | +## Use cases |
| 26 | + |
| 27 | +Ephemeral records can be used to implement many system coordination tasks, such as: |
| 28 | + |
| 29 | + 1. Service discovery |
| 30 | + 2. Leader election |
| 31 | + 3. Distributed resource locks |
| 32 | + |
| 33 | +## Using ephemeral records |
| 34 | + |
| 35 | +When creating/modifying a record, applications can specify the "ephemeral" option: |
| 36 | + |
| 37 | +<Tabs items={['Go', 'Java']}> |
| 38 | + <Tabs.Tab> |
| 39 | + ```go |
| 40 | + insertedKey, res, err := client.Put(context.Background(), |
| 41 | + "my-key", []byte("my-value"), |
| 42 | + oxia.Ephemeral()) |
| 43 | + ``` |
| 44 | + </Tabs.Tab> |
| 45 | + <Tabs.Tab> |
| 46 | + ```java |
| 47 | + PutResult pr = client.put("my-key", "my-value".getBytes(), |
| 48 | + Set.of(PutOption.AsEphemeralRecord)); |
| 49 | + ``` |
| 50 | + </Tabs.Tab> |
| 51 | +</Tabs> |
| 52 | + |
| 53 | + |
| 54 | +## Oxia session management |
| 55 | + |
| 56 | +An ephemeral record is tied to a particular session. Oxia client SDK will automatically |
| 57 | +create a session the first time there is an attempt to write ephemeral records. |
| 58 | + |
| 59 | +The session is internally managed by the client SDK, which will also take care of the |
| 60 | +heartbeat communications with the server. |
| 61 | + |
| 62 | +A session is deleted when: |
| 63 | + |
| 64 | + 1. Client instance is gracefully closed, deleting the session |
| 65 | + 2. Client crashed, causing the Oxia server to expire the session |
| 66 | + 3. Client is partitioned from Oxia, causing the Oxia server to expire the session |
| 67 | + |
| 68 | +When a session is deleted or expired, all the ephemeral records tied to that session |
| 69 | +are automatically deleted. |
| 70 | + |
| 71 | +If a client is subscribed to the notifications, it will receive the corresponding events. |
| 72 | + |
| 73 | +In case of network partitioning, the client SDK is internally proceeding to create a |
| 74 | +new session. |
| 75 | + |
| 76 | +### Session ID and client identity |
| 77 | + |
| 78 | +A client can optionally specify a client identifier that will be tied to the session. |
| 79 | + |
| 80 | +<Tabs items={['Go', 'Java']}> |
| 81 | + <Tabs.Tab> |
| 82 | + ```go |
| 83 | + client, err := oxia.NewClient("localhost:6648", oxia.WithIdentity("my-client-identity")) |
| 84 | + ``` |
| 85 | + </Tabs.Tab> |
| 86 | + <Tabs.Tab> |
| 87 | + ```java |
| 88 | + SyncOxiaClient client = OxiaClientBuilder.create("localhost:6648") |
| 89 | + .clientIdentifier("my-client-identifier") |
| 90 | + .syncClient(); |
| 91 | + ``` |
| 92 | + </Tabs.Tab> |
| 93 | +</Tabs> |
| 94 | + |
| 95 | +The client identity is stored as part of the version object, for ephemeral records, and will |
| 96 | +allow applications to detect ephemeral records that were written by the same logical client |
| 97 | +instance before a crash. |
| 98 | + |
| 99 | +```json |
| 100 | +{ |
| 101 | + "key": "my-key", |
| 102 | + "version_id": 2145, |
| 103 | + "created_timestamp": "2025-06-22T10:21:00.107-07:00", |
| 104 | + "modified_timestamp": "2025-06-22T10:21:00.107-07:00", |
| 105 | + "modifications_count": 0, |
| 106 | + "ephemeral": true, |
| 107 | + "session_id": 1677, |
| 108 | + "client_identity": "my-client-identity" |
| 109 | +} |
| 110 | +``` |
0 commit comments