Настройка потока
Созданный на странице Ваш первый поток ORDERS не ограничен и сохраняет всё. Для обучения это допустимо, для эксплуатации — нет.
Без ограничений он заполнит диск и может остановить сервер.
Предел определяет заполнение, политика Discard — что выбросить: новую запись или старейшую.
Ограничения
Стандартная политика Limits сохраняет до достижения одного из пределов:
- MaxAge — максимальный возраст. Семь дней удаляют сообщение примерно через неделю; удобно для недавних событий.
- MaxBytes — объём хранения. Один гигабайт ограничивает размер независимо от возраста и защищает сервер.
- MaxMsgs — число сообщений. При миллионе следующая запись требует вытеснения; удобно, если важен счётчик.
Все действуют независимо и одновременно: срабатывает первый достигнутый. Задайте нужные, остальные оставьте неограниченными.
MaxAge удаляет по часам, MaxMsgs — по количеству:
Ограничьте ORDERS
Задайте семь дней и гигабайт через nats stream edit. Поток меняется на месте с сохранением данных, укладывающихся в пределы.
- CLI
- JavaScript/TypeScript
- Go
- Python
- Java
- Rust
- C#/.NET
- C
#!/bin/bash
# Cap the ORDERS stream so it cannot grow without bound.
#
# `nats stream edit` changes an existing stream in place. The three
# messages already stored stay where they are; only the configuration
# changes. The command prints a diff of what will change and asks for
# confirmation before applying it.
#
# --max-age sets MaxAge: the oldest a message may get. 7d means a
# message is removed roughly seven days after it was stored.
# --max-bytes sets MaxBytes: the most disk the stream may occupy on
# disk. 1GiB caps it at one gibibyte (about a gigabyte); the stream
# never grows past it. nats reports the cap back as "1.0 GiB".
#
# MaxMsgs is left unset, so message count stays unlimited. Discard
# policy is left at its default (Old), so when a limit is hit the
# server drops the oldest messages to make room.
nats stream edit ORDERS \
--max-age=7d \
--max-bytes=1GiB
// Cap ORDERS with a seven-day age limit and a 1 GiB byte ceiling. streams.update
// takes just the fields you're changing and leaves the rest, and the stored
// messages, in place. max_age is in nanoseconds.
const jsm = await jetstreamManager(nc);
await jsm.streams.update("ORDERS", {
max_age: 7 * 24 * 60 * 60 * 1_000_000_000, // 7 days
max_bytes: 1024 * 1024 * 1024, // 1 GiB
});
console.log("ORDERS capped at 7d age and 1 GiB");
// Cap ORDERS so it can't grow without bound. Fetch the current config, add
// a seven-day age limit and a 1 GiB byte ceiling, and update the stream in
// place. Editing limits leaves the messages already stored alone.
s, err := js.Stream(ctx, "ORDERS")
if err != nil {
panic(err)
}
cfg := s.CachedInfo().Config
cfg.MaxAge = 7 * 24 * time.Hour
cfg.MaxBytes = 1 << 30 // 1 GiB
if _, err := js.UpdateStream(ctx, cfg); err != nil {
panic(err)
}
fmt.Println("ORDERS capped at 7d age and 1 GiB")
# Cap ORDERS with a seven-day age limit and a 1 GiB byte ceiling. Fetch the
# current config, set the limits, and update the stream in place; the stored
# messages stay put. max_age is in seconds.
info = await js.stream_info("ORDERS")
config = info.config
config.max_age = 7 * 24 * 3600 # 7 days
config.max_bytes = 1024 * 1024 * 1024 # 1 GiB
await js.update_stream(config)
print("ORDERS capped at 7d age and 1 GiB")
// Cap ORDERS with a seven-day age limit and a 1 GiB byte ceiling.
// Build from the current config so other fields, and the stored
// messages, are left in place, then update the stream.
StreamInfo si = jsm.getStreamInfo("ORDERS");
StreamConfiguration cfg = StreamConfiguration.builder(si.getConfiguration())
.maxAge(Duration.ofDays(7))
.maxBytes(1L << 30) // 1 GiB
.build();
jsm.updateStream(cfg);
System.out.println("ORDERS capped at 7d age and 1 GiB");
// Cap ORDERS so it can't grow without bound. Fetch the current config, add
// a seven-day age limit and a 1 GiB byte ceiling, and update the stream in
// place. Editing limits leaves the messages already stored alone.
let mut stream = js.get_stream("ORDERS").await?;
let mut config = stream.info().await?.config.clone();
config.max_age = Duration::from_secs(7 * 24 * 3600);
config.max_bytes = 1 << 30; // 1 GiB
js.update_stream(config).await?;
println!("ORDERS capped at 7d age and 1 GiB");
// Cap ORDERS with a seven-day age limit and a 1 GiB byte ceiling. Fetch
// the current config, set the limits, and update the stream in place;
// the stored messages stay put.
var stream = await js.GetStreamAsync("ORDERS");
var config = stream.Info.Config;
config.MaxAge = TimeSpan.FromDays(7);
config.MaxBytes = 1L << 30; // 1 GiB
await js.UpdateStreamAsync(config);
output.WriteLine("ORDERS capped at 7d age and 1 GiB");
// Cap ORDERS so it can't grow without bound. Fetch the current
// config, add a seven-day age limit and a 1 GiB byte ceiling, and
// update the stream in place. Editing limits leaves the messages
// already stored alone.
s = js_GetStreamInfo(&si, js, "ORDERS", NULL, &jerr);
if (s == NATS_OK)
{
si->Config->MaxAge = (int64_t) 7 * 24 * 60 * 60 * 1000000000LL; // 7 days in ns
si->Config->MaxBytes = 1024 * 1024 * 1024; // 1 GiB
s = js_UpdateStream(NULL, js, si->Config, NULL, &jerr);
}
if (s == NATS_OK)
printf("ORDERS capped at 7d age and 1 GiB\n");
Команда показывает изменения и запрашивает подтверждение. Затем проверьте:
nats stream info ORDERS
Вместо unlimited появятся пределы:
Options:
Retention: Limits
Acknowledgments: true
Discard Policy: Old
Duplicate Window: 2m0s
Limits:
Maximum Messages: unlimited
Maximum Per Subject: unlimited
Maximum Bytes: 1.0 GiB
Maximum Age: 7d0h0m0s
Maximum Message Size: unlimited
Maximum Consumers: unlimited
Maximum Messages остаётся unlimited. Maximum Message Size ограничивает отдельное сообщение, не весь поток, и тоже не задан. Теперь поток не превышает гигабайт и не хранит старше недели.
Политика Discard
При новой записи за пределами выбирается одно из двух поведений.
Discard Old, стандартное, удаляет старейшие сообщения ради новых. Публикация успешна, самая новая запись сохраняется.
Discard New отклоняет новую публикацию при MaxMsgs, MaxBytes или соответствующем ограничении темы, сохраняя прежние данные. MaxAge независимо удаляет по времени при любой политике. Первый достигнутый предел действует.
Для живого ORDERS нужна недавняя неделя, поэтому оставим Discard Old: при нехватке места важнее сегодняшний заказ.
Discard New подходит, если старые данные нельзя терять и лучше замедлить издателя. Он обязан обрабатывать отказы.
Ограничения относятся к потоку
Удалённое пределом сообщение исчезает для всех потребителей.
Позиция shipping и фильтр analytics не защищают от удаления. Если читатель отстал до истечения возраста, данные потеряны. Далее рассмотрим влияние политики хранения.
Ограничения отдельных тем
MaxMsgsPerSubject может хранить, например, последние сто сообщений каждой темы под orders.>, независимо от общего количества.
Все ограничения — в Конфигурации потока. Здесь используются MaxAge, MaxBytes и Discard.
Типичные ошибки
Две основные ловушки:
Discard Old удаляет без предупреждения издателя. Успешная запись может вытеснить старый заказ. Для сохранения истории выбирайте Discard New с ошибками maximum bytes exceeded или maximum messages exceeded. Но MaxAge продолжает действовать: поднимите или уберите и его.
- CLI
- JavaScript/TypeScript
- Go
- Python
- Java
- Rust
- C#/.NET
- C
#!/bin/bash
# Switch ORDERS from Discard Old to Discard New so a full stream pushes
# backpressure to the publisher instead of silently dropping old orders.
#
# Under Discard Old (the default) a publish that exceeds a limit always
# succeeds, because the server drops the oldest message to make room and
# tells the publisher nothing. If you need to keep history, that silent
# drop is data loss you never see.
#
# --discard new flips the trade: when a limit is hit, the server rejects
# the new message and the publish fails with "maximum bytes exceeded" (or
# "maximum messages exceeded"). The publisher now feels the limit.
# Force the full condition so you can see the rejection. Discard New never
# drops messages that are already stored, so switching ORDERS to it and
# capping it at one message leaves the orders from earlier pages in place and
# puts the stream instantly over its limit -- both in one edit.
nats stream edit ORDERS --discard new --max-msgs 1
# ORDERS is now full. Under Discard New this publish fails instead of
# succeeding silently, so the publisher can retry, alert, or shed load:
nats pub orders.created \
'{"order_id":"ord_8w2k","customer":"acme-co","total_cents":4200,"ts":"2026-05-22T10:14:22Z"}'
# -> nats: maximum messages exceeded
# Put ORDERS back the way it was: Discard Old and no message-count cap. The
# 7-day age and 1 GiB byte limits set earlier stay in place.
nats stream edit ORDERS --discard old --max-msgs -1
// Switch ORDERS to Discard New. Discard New never drops stored messages, so
// capping it at one message leaves the existing orders in place and puts the
// stream instantly over its limit; the next publish is rejected.
const jsm = await jetstreamManager(nc);
await jsm.streams.update("ORDERS", { discard: DiscardPolicy.New, max_msgs: 1 });
// This publish hits the full stream and rejects with "maximum messages
// exceeded" instead of succeeding silently. Handle it in the publisher.
const js = jetstream(nc);
try {
await js.publish("orders.created", JSON.stringify({ order_id: "ord_8w2k" }));
} catch (err) {
console.log(`publish rejected: ${(err as Error).message}`);
}
// Put ORDERS back: Discard Old, no message cap (age and byte limits stay).
await jsm.streams.update("ORDERS", { discard: DiscardPolicy.Old, max_msgs: -1 });
// Switch ORDERS to Discard New. Discard New never drops messages already
// stored, so capping it at one message leaves the existing orders in place
// and puts the stream instantly over its limit. The next publish is
// rejected rather than evicting an older order.
s, err := js.Stream(ctx, "ORDERS")
if err != nil {
panic(err)
}
cfg := s.CachedInfo().Config
cfg.Discard = jetstream.DiscardNew
cfg.MaxMsgs = 1
if _, err := js.UpdateStream(ctx, cfg); err != nil {
panic(err)
}
// This publish hits the full stream and fails with "maximum messages
// exceeded" instead of succeeding silently. Handle it in the publisher.
if _, err := js.Publish(ctx, "orders.created", []byte(`{"order_id":"ord_8w2k"}`)); err != nil {
fmt.Printf("publish rejected: %v\n", err)
}
// Put ORDERS back: Discard Old, no message cap (age and byte limits stay).
cfg.Discard = jetstream.DiscardOld
cfg.MaxMsgs = -1
if _, err := js.UpdateStream(ctx, cfg); err != nil {
panic(err)
}
# Switch ORDERS to Discard New. Discard New never drops stored messages, so
# capping it at one message leaves the existing orders in place and puts the
# stream instantly over its limit; the next publish is rejected.
info = await js.stream_info("ORDERS")
config = info.config
config.discard = DiscardPolicy.NEW
config.max_msgs = 1
await js.update_stream(config)
# This publish hits the full stream and is rejected instead of succeeding
# silently. Handle it in the publisher.
try:
await js.publish("orders.created", b'{"order_id":"ord_8w2k"}')
except APIError as err:
print(f"publish rejected: {err.description}")
# Put ORDERS back: Discard Old, no message cap (age and byte limits stay).
config.discard = DiscardPolicy.OLD
config.max_msgs = -1
await js.update_stream(config)
// Switch ORDERS to Discard New. Discard New never drops stored
// messages, so capping it at one message leaves the existing orders
// in place and puts the stream instantly over its limit; the next
// publish is rejected.
StreamInfo si = jsm.getStreamInfo("ORDERS");
jsm.updateStream(StreamConfiguration.builder(si.getConfiguration())
.discardPolicy(DiscardPolicy.New)
.maxMessages(1)
.build());
// This publish hits the full stream and is rejected instead of
// succeeding silently. Handle it in the publisher.
try {
js.publish("orders.created", "{\"order_id\":\"ord_8w2k\"}".getBytes());
}
catch (JetStreamApiException e) {
System.out.println("publish rejected: " + e.getMessage());
}
// Put ORDERS back: Discard Old, no message cap (age and byte limits
// stay).
jsm.updateStream(StreamConfiguration.builder(si.getConfiguration())
.discardPolicy(DiscardPolicy.Old)
.maxMessages(-1)
.build());
// Switch ORDERS to Discard New. Discard New never drops messages already
// stored, so capping it at one message leaves the existing orders in place
// and puts the stream instantly over its limit. The next publish is
// rejected rather than evicting an older order.
let mut stream = js.get_stream("ORDERS").await?;
let mut config = stream.info().await?.config.clone();
config.discard = DiscardPolicy::New;
config.max_messages = 1;
js.update_stream(config.clone()).await?;
// This publish hits the full stream; the ack returns an error instead of
// the publish succeeding silently. Handle it in the publisher.
let ack = js
.publish("orders.created", r#"{"order_id":"ord_8w2k"}"#.into())
.await?;
if let Err(err) = ack.await {
println!("publish rejected: {err}");
}
// Put ORDERS back: Discard Old, no message cap (age and byte limits stay).
config.discard = DiscardPolicy::Old;
config.max_messages = -1;
js.update_stream(config).await?;
// Switch ORDERS to Discard New. Discard New never drops stored messages,
// so capping it at one message leaves the existing orders in place and
// puts the stream instantly over its limit; the next publish is rejected.
var stream = await js.GetStreamAsync("ORDERS");
var config = stream.Info.Config;
config.Discard = StreamConfigDiscard.New;
config.MaxMsgs = 1;
await js.UpdateStreamAsync(config);
// This publish hits the full stream and is rejected instead of
// succeeding silently. Handle it in the publisher.
try
{
var ack = await js.PublishAsync<Order>(subject: "orders.created", data: new Order(OrderId: "ord_5k1m"));
// PublishAsync returns the server's answer; EnsureSuccess turns a
// rejection into an exception instead of leaving it unchecked.
ack.EnsureSuccess();
}
catch (NatsJSApiException e)
{
output.WriteLine($"publish rejected: {e.Message}");
rejected = true;
}
// Put ORDERS back: Discard Old, no message cap (age and byte limits stay).
config.Discard = StreamConfigDiscard.Old;
config.MaxMsgs = -1;
await js.UpdateStreamAsync(config);
// Switch ORDERS to Discard New. Discard New never drops messages
// already stored, so capping it at one message leaves the existing
// orders in place and puts the stream instantly over its limit. The
// next publish is rejected rather than evicting an older order.
const char *order = "{\"order_id\":\"ord_8w2k\"}";
s = js_GetStreamInfo(&si, js, "ORDERS", NULL, &jerr);
if (s == NATS_OK)
{
si->Config->Discard = js_DiscardNew;
si->Config->MaxMsgs = 1;
s = js_UpdateStream(NULL, js, si->Config, NULL, &jerr);
}
// This publish hits the full stream and fails with "maximum messages
// exceeded" instead of succeeding silently. Handle it in the
// publisher.
if (s == NATS_OK)
{
if (js_Publish(NULL, js, "orders.created", order, (int) strlen(order), NULL, &jerr) != NATS_OK)
printf("publish rejected: %s\n", nats_GetLastError(NULL));
}
// Put ORDERS back: Discard Old, no message cap (age and byte limits
// stay).
if (s == NATS_OK)
{
si->Config->Discard = js_DiscardOld;
si->Config->MaxMsgs = -1;
s = js_UpdateStream(NULL, js, si->Config, NULL, &jerr);
}
Семидневный MaxAge не гарантирует неделю истории: при всплеске MaxBytes может удалить данные нескольких часов. Рассчитывайте объём по пиковой, не средней нагрузке.
Общие пределы не балансируют темы. Поток orders.created может вытеснить orders.shipped. Для отдельной квоты задайте MaxMsgsPerSubject:
- CLI
- JavaScript/TypeScript
- Go
- Python
- Java
- Rust
- C#/.NET
- C
#!/bin/bash
# Add a per-subject ceiling to ORDERS so one noisy subject cannot evict
# another subject's messages.
#
# MaxMsgs, MaxBytes, and MaxAge all measure the stream as a whole,
# across every subject under orders.>. If orders.created floods in, its
# messages count toward the same ceiling as orders.shipped, and Discard
# Old can drop a shipped order to make room for a created one.
#
# --max-msgs-per-subject sets MaxMsgsPerSubject: a separate ceiling
# applied to each individual subject. Set it to 100000 and every subject
# keeps its own most-recent 100000 messages, independent of how loud its
# neighbors are.
nats stream edit ORDERS --max-msgs-per-subject=100000
# Read the stream back; the per-subject limit now appears alongside the
# whole-stream limits.
nats stream info ORDERS
// Add a per-subject ceiling so one noisy subject can't evict another's
// messages. max_msgs_per_subject keeps the most recent N messages for every
// subject independently, alongside the whole-stream limits.
const jsm = await jetstreamManager(nc);
await jsm.streams.update("ORDERS", { max_msgs_per_subject: 100000 });
console.log("ORDERS now keeps 100000 messages per subject");
// Add a per-subject ceiling so one noisy subject can't evict another's
// messages. MaxMsgsPerSubject keeps the most recent N messages for every
// subject independently, alongside the whole-stream limits.
s, err := js.Stream(ctx, "ORDERS")
if err != nil {
panic(err)
}
cfg := s.CachedInfo().Config
cfg.MaxMsgsPerSubject = 100000
if _, err := js.UpdateStream(ctx, cfg); err != nil {
panic(err)
}
fmt.Println("ORDERS now keeps 100000 messages per subject")
# Add a per-subject ceiling so one noisy subject can't evict another's
# messages. max_msgs_per_subject keeps the most recent N messages for every
# subject independently, alongside the whole-stream limits.
info = await js.stream_info("ORDERS")
config = info.config
config.max_msgs_per_subject = 100000
await js.update_stream(config)
print("ORDERS now keeps 100000 messages per subject")
// Add a per-subject ceiling so one noisy subject can't evict
// another's messages. maxMessagesPerSubject keeps the most recent N
// messages for every subject independently, alongside the
// whole-stream limits.
StreamInfo si = jsm.getStreamInfo("ORDERS");
StreamConfiguration cfg = StreamConfiguration.builder(si.getConfiguration())
.maxMessagesPerSubject(100000)
.build();
jsm.updateStream(cfg);
System.out.println("ORDERS now keeps 100000 messages per subject");
// Add a per-subject ceiling so one noisy subject can't evict another's
// messages. max_messages_per_subject keeps the most recent N messages for
// every subject independently, alongside the whole-stream limits.
let mut stream = js.get_stream("ORDERS").await?;
let mut config = stream.info().await?.config.clone();
config.max_messages_per_subject = 100_000;
js.update_stream(config).await?;
println!("ORDERS now keeps 100000 messages per subject");
// Add a per-subject ceiling so one noisy subject can't evict another's
// messages. MaxMsgsPerSubject keeps the most recent N messages for every
// subject independently, alongside the whole-stream limits.
var stream = await js.GetStreamAsync("ORDERS");
var config = stream.Info.Config;
config.MaxMsgsPerSubject = 100000;
await js.UpdateStreamAsync(config);
output.WriteLine("ORDERS now keeps 100000 messages per subject");
// Add a per-subject ceiling so one noisy subject can't evict
// another's messages. MaxMsgsPerSubject keeps the most recent N
// messages for every subject independently, alongside the
// whole-stream limits.
s = js_GetStreamInfo(&si, js, "ORDERS", NULL, &jerr);
if (s == NATS_OK)
{
si->Config->MaxMsgsPerSubject = 100000;
s = js_UpdateStream(NULL, js, si->Config, NULL, &jerr);
}
if (s == NATS_OK)
printf("ORDERS now keeps 100000 messages per subject\n");
При Discard Old удаляется старейшее своей темы. Сам Discard New это не меняет: для отказа по заполненной теме дополнительно нужен DiscardNewPerSubject, поле discard_new_per_subject. Тогда возвращается maximum messages per subject exceeded наряду с общими ошибками. См. Конфигурацию потока.
Что уже готово
Теперь есть:
- ORDERS с MaxAge семь дней и MaxBytes 1 ГиБ.
- Прежние сообщения, укладывающиеся в пределы.
- Discard Old для вытеснения старейших, а не новых.
Что дальше
Пределы настроены для Limits. Теперь выберем саму политику хранения: Limits, Interest или WorkQueue.
См. также
- Конфигурация потока — типы, диапазоны и значения ограничений.