# How Does the SOW Work?

AMPS SOW topics persist the most recent update for each message, in the same way that a relational database stores the current state of each record. For performance, AMPS SOW topics store the full content of the message verbatim rather than storing a deserialized or "shredded" version of the message.

Each distinct record in a SOW topic is identified by a _SOW key_. AMPS treats the _SOW key_ for a SOW topic the same way a relational database uses the primary key for a table: each distinct _SOW key_ value is a unique message.

There are several ways to create a SOW key for a message. Each topic defines one of the following strategies:

* Most applications specify that AMPS will calculate a SOW key based on the content of the message. The configuration of the topic specifies the field, or fields, to be used for the key.
* A topic can also be configured to require that a publisher provide a SOW key for each message when publishing the message to AMPS. This is less commonly used than determining the key based on the message content, however, since this strategy does not require any explicit configuration, AMPS will default to this strategy for identifying messages if no other strategy is specified.
* AMPS also supports the ability for custom SOW key generation logic to be defined in an AMPS module, which will be invoked to generate the SOW key for each message.

Although the SOW key is derived from the content of the message in many cases, the SOW key itself is metadata, distinct from the content of the message. Each record in a SOW topic has a distinct SOW key, which is stored with the record.

For example, the diagram below shows how AMPS computes the SOW key for a topic named ORDERS with a key definition of `/orderId`. For each publish to the topic, AMPS uses the value of the key fields (in this case, simply `/orderId`) to compute a `SowKey`, then uses that `SowKey` to insert or update the appropriate record.

<figure><picture><source srcset="https://82018141-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFQiXqIcXoyOqwDbFf6Nl%2Fuploads%2Fgit-blob-f64ef0ca5130869c87b98514b70497d78b75988e%2Fhow-sow-works-dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://82018141-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFQiXqIcXoyOqwDbFf6Nl%2Fuploads%2Fgit-blob-c3fa8dddb368b0d230431b5e4eac4328182603f8%2Fhow-sow-works-light.svg?alt=media" alt="Diagram showing a SOW key calculation based on a field in the message"></picture><figcaption></figcaption></figure>
