This repository contains the sample code for the NATS Data Series blog posts. Each post has its own directory which includes a Taskfile that can be used to run the code.
We rely on a few things to be installed on your system to run the code. The following is a list of tools we expect to be available:
- Task for running the code and making your life easier
- NATS cli for running the server and interacting with it
- Go for building the code
Make sure you have these installed before running the code.
To run the server, you can use the following command:
task server
This will start a server on your system that will be used as part of the examples. It will also create a nats-cli context you can use to interact with the server.
run nats context select rethink
to select it
Hop into the Data Series Slack Channel and share your thoughts. I would love
The first post in the series sets the stage for the rest of the series. It is a brief recap of how we got to where we are today and why we need to rethink the way we build systems.
This post explores how to use NATS to build a system of record that can be used to store and retrieve data from a distributed system.
- Start the server if it hasn't been started yet
- Run
task rethink:setup
to build the code and create the JetStream stream and KV store - Run
task rethink:listen:low_stock
to start listening for low stock events - Run the next few commands in their own terminal windows
- Run
task rethink:run:low_stock_detector
to start detecting low stock items - Run
task rethink:run:stock_updater
to start updating the stock levels onstock.sold
andstock.replenished
events - Run
task rethink:run:stock_event_generator
to start generating stock events
- Run
The stock_event_generator
will send change events and print out the updates it made:
Replenished 53 Pears (54 left)
Replenished 14 Grapes (15 left)
Sold 27 Pears (27 left)
Sold 60 Oranges (8 left)
...
The stock_updater
will update the stock based on the stock.sold
and stock.replenished
events:
Received stock sold event: {"product_name":"Grapes","quantity":1}
Received stock replenished event: {"product_name":"Grapes","quantity":63}
Received stock replenished event: {"product_name":"Oranges","quantity":34}
Received stock sold event: {"product_name":"Apples","quantity":4}
Received stock sold event: {"product_name":"Pears","quantity":4}
...
The low_stock_detector
will look at the stock levels and send an event when the stock is low. It will not
output anything but send a message stock.low
message
The listener
will listen for the stock.low
events and print out the message it received:
[#182] Received on "product.Pears"
myorg_type: stock.low
myorg_format: application/json
myorg_msg_id: mBZvheMnM10QcgS9ZPCguH
{"product_name":"Pears"}
[#183] Received on "product.Pears"
myorg_format: application/json
myorg_msg_id: A3WBvfAGUqwQCnZPeHBU8x
myorg_type: stock.low
{"product_name":"Pears"}
- Each of the tasks can be stopped by pressing
ctrl+c
. Just keep the server running for the time being - Run
task rethink:teardown
to remove the JetStream stream and KV store - Stop the server by pressing
ctrl+c
- Run
task clean
to remove any lingering server files