title: Does NATS Jetstream provide message ordering by a key?
body: <p>I am new to NATS Jetstream and I have been reading their official documentation (<a href="https://docs.nats.io/jetstream/jetstream" rel="noreferrer">https://docs.nats.io/jetstream/jetstream</a>) to understand its concepts and compare it with Kafka. One of the major use cases I have, is to solve for message/event ordering based on a particular id (like a <code>partition key</code> in the Kafka world).</p>
  <p>For example, there are several update events coming for an <code>Order</code> entity and my system needs to consume the events for a particular <code>Order</code> in the same order. In this case, I would use the <code>order-id</code> as the partition key while publishing to the Kafka topic. How do I accomplish this in Jetstream?</p>
  <p>I have come across a de-duplication key (<code>Nats-Msg-Id</code>) in Jetstream, but I think this feature is more synonymous with topic compaction in Kafka. Am I right?</p>
  <p>Nevertheless, I have written the following code in Golang for publishing:</p>
  <pre><code>order = Order{
      OrderId: orderId,
      Status:  status,
  }
  orderJson, _ := json.Marshal(order)
  dedupKey := nats.MsgId(order.OrderId)
  _, err := js.Publish(subjectName, orderJson, dedupKey)
  </code></pre>
  <p>Am I doing this right? Will all orders for a particular orderId go to the same consumer within a consumer group in the Jetstream world, hence maintaining the sequence?</p>
  <h2>Edit 1</h2>
  <p>This is what I get from @tbeets' suggestion. For example, I have predefined 10 stream subjects like <code>ORDER.1</code>, <code>ORDER.2</code>,<code>ORDER.3</code> .... <code>ORDER.10</code></p>
  <p>On the publishing side, I can do an <code>order-id%10+1</code> to find the exact stream subject to which I would want to publish. So here, we have accomplished that all update events for the same orderId will go to the same stream subject every time.</p>
  <p>Now, on the subscriber side, I have 10 consumer groups (there are 10 consumers within each consumer group) and each consume from a particular stream subject, like <code>consumerGroup-1</code> consumes from <code>ORDER.1</code>, <code>consumerGroup-2</code> consumes from <code>ORDER.2</code> and so on...</p>
  <p>Say, 2 order update events came for <code>order-id</code> 111, which would get mapped to <code>ORDER.1</code> stream subject, and correspondingly <code>consumerGroup-1</code> will consume these 2 events. But within this consumerGroup, the 2 update events can go to different consumers and if one of the consumers is a bit busy or slow, then at an overall level, the order update events consumption maybe out-of-sync or out-of-order.</p>
  <p>Kafka solves this using the concept of partition key as consumers of a consumer group are allocated to a particular partition. Hence, all events for the same orderId, are consumed by the same consumer, hence, maintaining the sequence of order update event consumption. How do I solve this issue in Jetstream?</p>