Previous Next JavaScript must be enabled to correctly display this content

## Setting Durability Guarantees

To set a durability guarantee, use the `Durability` class. When you do this, you must provide three pieces of information:

- The acknowledgment policy.

- A synchronization policy at the master node.

- A synchronization policy at the replica nodes.

The combination of policies that you use is driven by how sensitive your application might be to potential data loss, and by your write performance requirements.

For example, the fastest possible write performance can be achieved through a durability policy that requires:

- No acknowledgments.

- NO_SYNC at the master.

- NO_SYNC at the replicas.

However, this durability policy also leaves your data with the greatest risk of loss due to application or machine failure between the time the operation returns and the time when the data is written to stable storage.

On the other hand, if you want the highest possible durability guarantee, you can use:

- All replicas must acknowledge the write operation.

- SYNC at the master.

- SYNC at the replicas.

Of course, this also results in the slowest possible write performance.

Most commonly, durability policies attempt to strike a balance between write performance and data durability guarantees. For example:

- Simple majority (> 50%) of replicas must acknowledge the write.

- SYNC at the master.

- NO_SYNC at the replicas.

Note that you can set a default durability policy for your `KVStore` handle, but you can also override the policy on a per-operation basis for those situations where some of your data need not be as durable (or needs to be MORE durable) than the default.

For example, suppose you want an intermediate durability policy for most of your data, but sometimes you have transient or easily re-created data whose durability really is not very important. Then you would do something like this:

First, set the default durability policy for the `KVStore` handle:

    package kvstore.basicExample;

    import oracle.kv.Durability;
    import oracle.kv.KVStore;
    import oracle.kv.KVStoreConfig;
    import oracle.kv.KVStoreFactory;

    ...

    KVStoreConfig kconfig = new KVStoreConfig("exampleStore",
        "node1.example.org:5088, node2.example.org:4129");

    Durability defaultDurability =
        new Durability(Durability.SyncPolicy.SYNC,    // Master sync
                       Durability.SyncPolicy.NO_SYNC, // Replica sync
                       Durability.ReplicaAckPolicy.SIMPLE_MAJORITY);
    kconfig.setDurability(defaultDurability);

    KVStore kvstore = KVStoreFactory.getStore(kconfig);

In another part of your code, for some unusual write operations, you might then want to relax the durability guarantee so as to speed up the write performance for those specific write operations:

    package kvstore.basicExample;

    ...

    import oracle.kv.Durability;
    import oracle.kv.DurabilityException;
    import oracle.kv.KVStore;
    import oracle.kv.table.Row;
    import oracle.kv.table.Table;
    import oracle.kv.table.TableAPI;

    ...

    TableAPI tableH = kvstore.getTableAPI();

    // The name you give to getTable() must be identical
    // to the name that you gave the table when you created
    // the table using the CREATE TABLE DDL statement.
    Table myTable = tableH.getTable("myTable");

    // Get a Row instance
    Row row = myTable.createRow();

    // Now put all of the cells in the row.

    row.put("item", "Bolts");
    row.put("description", "Hex head, stainless");
    row.put("count", 5);
    row.put("percentage", 0.2173913);

    // Construct a durability policy
    Durability durability =
        new Durability(Durability.SyncPolicy.NO_SYNC, // Master sync
                       Durability.SyncPolicy.NO_SYNC, // Replica sync
                       Durability.ReplicaAckPolicy.NONE);

    // Construct a WriteOptions object using the durability policy.
    WriteOptions wo = new WriteOptions(durability, 0, null);

    // Now write the table to the store using the durability policy
    // defined, above.
    tableH.put(row, null, wo);