github centrifugal/centrifugo v6.10.4

46 minutes ago

Centrifugo is an open-source scalable real-time messaging server. It instantly delivers messages to application online users connected over supported transports (WebSocket, HTTP-streaming, Server-Sent Events (SSE), GRPC, WebTransport). Centrifugo is built around channel subscriptions – so it's a user-facing PUB/SUB server.

Centrifugo is language-agnostic and can be used to build chat apps, live comments, multiplayer games, real-time data visualizations, collaborative tools, AI streaming responses, etc. in combination with any backend. It is well suited for modern architectures and allows decoupling the business logic from the real-time transport layer.

Official client SDKs are available for JavaScript (browser, Node.js, React Native), Dart/Flutter, Swift, Java, Python, Go, Rust, and .NET. In addition, Centrifugo supports a unidirectional approach for simple use cases with no SDK dependency.

For details, go to the Centrifugo documentation site. For runnable demos see centrifugal/examples.

What's changed

Improvements

  • Detection of Redis lost writes. Redis replication and persistence are asynchronous: after a failover to a replica which missed the latest writes, or a restart from an older RDB or AOF file, a channel's history could lose its latest publications while keeping its epoch. New publications then reused offsets clients had already seen, and clients skipped them as duplicates. Maps had the same problem with their change streams. With the new engine.redis.detect_lost_writes option, Centrifugo detects that a Redis server may have lost writes, and channels and maps stored on that server start a new epoch, so subscribers resync from the current state instead of silently missing publications. On Redis 7+ and Valkey it detects every restart and failover and needs no extra permissions. On Redis 6, DragonflyDB and AWS ElastiCache Serverless it detects lost writes in channels with subscribers. Off by default. Enable it on every Centrifugo node, and expect one resync of subscribers after every failover or restart of a Redis server, also one which lost nothing. Reported by @mattjcowan (#1319, #1332, centrifugal/centrifuge#686, centrifugal/centrifuge#693).
    • engine.redis.detect_lost_writes_with_info detects with the Redis INFO command instead: also on Redis 6 and DragonflyDB in all channels, and also a quick failback to the old primary. It needs the @dangerous ACL category and gives more resyncs without any loss – see the docs before enabling it.
    • Both options are also available for a separate Redis broker (broker.redis.*) and the Redis map broker (map_broker.redis.*).
    • At start, Centrifugo logs a redis potential lost writes detection line for every Redis shard, also with the options off: which detection is in use and what the server supports. Check it before enabling the options.
    • New metrics centrifugo_broker_redis_potential_lost_writes_detections and centrifugo_broker_redis_potential_lost_writes_epoch_resets.
    • Along with this work, Valkey, AWS ElastiCache (node-based clusters and Serverless) and Google Memorystore (for Redis, for Redis Cluster, for Valkey) were tested with history, maps, PUB/SUB delivery, failovers, maintenance events and resharding. See Redis compatible storages for the results and connection notes. KeyDB has not been updated for a long time, so we do not recommend it anymore.
  • New channel.history_meta_ttl_initial option: a shorter TTL for the stream and map meta of channels nobody published to yet. Subscribing to a channel with positioning or recovery, or requesting its history, creates its meta even without publications, and it was kept for history_meta_ttl (30 days by default). Where clients can choose channel names, subscriptions and history requests to many channels nobody publishes to left that many long-lived keys in Redis or rows in PostgreSQL; with the option (for example 5m) they don't. Live subscribers keep the meta while they stay subscribed, and the first publication switches it to the usual TTL. The cost: a client away longer than the option from a channel nobody published to can't recover what was published meanwhile, and is told to resync. It can be set per namespace (history_meta_ttl_initial) and per map (map.meta_ttl_initial), where -1 disables it for the namespace's channels. The value must be at least 3 × client.channel_position_check_delay: a shorter one is raised to it, with a warning on start. Off by default (#1332, #1335, centrifugal/centrifuge#687, centrifugal/centrifuge#702).
  • Kafka consumer: new start_offset option, earliest (default, as before) or latest. It applies to partitions without a committed offset in the consumer group – a new group, or a topic added to an existing one. With latest a new group starts from new records only, instead of sending everything still in retention to online clients in publication data mode. Thanks @thuy-le-kafi (#1321).
  • New startup_wait_timeout option of Redis configurations (engine, broker, presence manager, map broker, Redis Stream consumer): how long Centrifugo waits on start for Redis which is not reachable yet or is still loading its dataset, instead of exiting at the first failed attempt to connect. Not set by default. Centrifugo does not listen while it waits, so in Kubernetes a long timeout needs a startupProbe. Thanks @BeyondEvil (#1327, #1330, centrifugal/centrifuge#690).
  • Redis PUB/SUB: messages waiting to be processed on a node are now bounded by their size, not by count, and take memory only while they wait. Before, every PUB/SUB processor allocated a buffer of 4096 messages on start, which took memory even while idle, and held up to 4096 messages of any size. The new pub_sub_queue_max_size option (default 64 MB, for all PUB/SUB connections of a broker together) sets the limit for engine.redis, broker.redis and map_broker.redis. Bursts of small messages get more room than before; of large messages, fewer wait. Over the limit messages are dropped, as before when the buffer was full (#1338, centrifugal/centrifuge#699).
  • Server HTTP API: request bodies are limited by the new http_api.max_request_body_size option, 32 MB by default. A larger request gets HTTP 413. Before, a body of any size was read into memory. The admin web UI API uses the same default limit. Thanks @aloneangel45, reported by @GG-Feng (#1328, #1340).
  • JWKS: a response of the JWKS endpoint larger than 1 MB now fails the key fetch instead of being read into memory in full. Thanks @draarivpatel-ui, reported by @GG-Feng (#1329, #1339).
  • Bad requests are logged at warn level, not error: server API requests which fail validation or can't be decoded, and server API errors caused by the request (bad request, unknown channel, not found, unrecoverable position, conflict), as well as malformed requests to unidirectional transports and WebTransport. Internal errors stay at error level. Alerts which match these messages at error level no longer fire (#1334).
  • WebSocket: pings from a client are now limited to a burst of 10 and 1 per second after it, far above what keepalive needs. A client pinging faster is disconnected with close code 1008 (too many pings). Before, nothing bounded them, and each ping cost the server a write (centrifugal/centrifuge#684).
  • WebSocket: after a disconnect or a refused connect, Centrifugo reads at most 32 messages from the client while waiting for its close frame. A closing client still completes the closing handshake and sees the close code, but a client which keeps sending is closed at once instead of being read for up to 5 seconds. In a test with clients flooding commands and getting disconnected, server CPU dropped about a hundred times (centrifugal/centrifuge#683).
  • Unidirectional WebSocket: after connect, only replies to pings are accepted from the client, one per ping. Other messages disconnect the client with bad request. Before, every message a client sent was read into memory for the whole life of the connection (#1318).

Fixes

  • PostgreSQL stream and map brokers: recovery could report success while changes were lost. If a client subscribed to a channel nobody had published to, missed its first publications, and the channel's meta expired before it came back, it got recovered: true without them. Such channels now get an epoch on subscribe or history request, as with the memory and Redis engines, so the client is told to resync. (#1335).
    • The first subscription with positioning or recovery to such a channel, or the first history request for it, now writes to the primary, so it fails while the primary is down. Channels with publications are still read from replicas where configured.

    • Such a first read leaves a meta row for the channel. It is kept for history_meta_ttl (and at least for the stream retention), and for a persistent map forever. If clients subscribe to many channels nobody publishes to, use the new history_meta_ttl_initial to keep these rows short-lived.

    • PostgreSQL map broker (map subscriptions are experimental): the database schema goes to version 2, migrated by the first v6.10.4 node on start. Running nodes of older versions keep working on the migrated database but can't start again, so don't restart them during a rolling upgrade. With map_broker.postgres.skip_schema_init, first start one v6.10.4 node with the option disabled and a database user allowed to create functions, otherwise subscriptions to maps without publications fail with an internal error.

    • To roll back to an older version with the PostgreSQL map broker, stop all v6.10.4 nodes and run this SQL (for the default table_prefix, with another one replace cf in the names), then start the older version. Map data is kept, and the new SQL functions may stay:

      BEGIN;
      DELETE FROM cf_map_meta WHERE top_offset = 0;
      DELETE FROM cf_binary_map_meta WHERE top_offset = 0;
      UPDATE cf_map_schema_version SET schema_version = 1 WHERE id = 1;
      UPDATE cf_binary_map_schema_version SET schema_version = 1 WHERE id = 1;
      COMMIT;
  • PostgreSQL stream and map brokers: cleanup of expired channel meta could delete a meta which had just been renewed, which started a new epoch, and for maps also dropped their state (#1335).
  • PostgreSQL stream broker: delta compression did not work for live publications, subscribers which negotiated it always got full payloads. They now receive deltas, which saves bandwidth and costs nodes some CPU to compute them (#1336).
  • WebTransport did not work in browsers since v6.6.1: the HTTP/3 listener negotiated no h3 ALPN when WebTransport was enabled, and the handshake failed (Chrome: NO_APPLICATION_PROTOCOL). Also, the reply to a refused connect (for example 109 for an expired token or a 3500 disconnect for an invalid one) was usually lost, so clients could not tell a refused token from a broken transport and reconnected with the same token. Reported by @jbiscella (#1322, #1323).
  • Redis engine with a single address in front of a primary and a replica (a managed Redis or Valkey endpoint, a Kubernetes Service): after a failover which kept the old primary running as a replica, Centrifugo kept its connections to it, and every publication to a channel with history failed with READONLY (server API error code 100) until a restart, while /health stayed OK. Now a READONLY reply makes Centrifugo drop the shard's connections and connect to the address again, which leads to the new primary. Redis Cluster and Sentinel setups were not affected. Reported by @mattjcowan (#1320, centrifugal/centrifuge#686).
  • Redis Sentinel: connections to Sentinels had no connect timeout and no keep-alive. When a node's network was lost silently during a failover, the node could keep the old master's address after the network came back. If the old master was a replica by then, publications to channels without history were accepted there and never reached subscribers. Sentinel connections now use the connect_timeout and keep-alive of data connections – in the Redis engine, and in the Redis stream consumer, Redis queue and Redis PUB/SUB. Reported by @Zerebokep (#1325, #1326, centrifugal/centrifuge#685).
  • Redis map broker: a map whose stream meta was created again on the same node (after its TTL expired, or after lost writes were detected) could get the epoch it had before, with offsets starting from 0 again. A subscriber which came back with a position in the old stream was not told to resync and could recover wrong publications. A recreated meta now always gets a new epoch (centrifugal/centrifuge#703).
  • JWKS: a placeholder in the host of jwks_public_endpoint (as in https://{{tenant}}.example.com/...) failed at start with jwks: invalid url value or format. Thanks @Adham-Kiwan, reported by @mathieu-aix (#1341, #1343).
  • Redis Stream consumer: when a read's request or answer was lost on a connection which stayed open, the consumer could wait for it forever and stop consuming. Reads are now bounded in time. Also, entries Redis had already handed out in such a lost answer were left pending forever when the visibility timeout is 0; the consumer now reads them again (#1342).
  • Redis map broker: outside Redis Cluster, publishing and removing failed with NOPERM for a Redis ACL user restricted by key patterns (centrifugal/centrifuge#689).
  • History requests from clients used the global history_meta_ttl, not the one of the channel's namespace as subscriptions and server API history do. A request for the history of a channel nobody published to could so keep its meta for longer or shorter than the namespace sets (#1335, centrifugal/centrifuge#702).
  • A subscription which unsubscribed and subscribed again right after its stream state was found insufficient could be unsubscribed again with the insufficient state code, causing a needless resubscribe (centrifugal/centrifuge#688).
  • Unidirectional HTTP streaming: a connection closed by the server while it was still connecting (for example, a connect-time subscription found its stream state insufficient right away) could hang. The client got neither the connect reply nor the disconnect, and the connection stayed open (centrifugal/centrifuge#704).
  • A Centrifugo stopping right after start, for example on a restart by --watch-config-file, could leave a Redis connection open after its Redis shards were closed (centrifugal/centrifuge#694).
  • A data race in logging when several Centrifugo apps run in one process, as in tests (#1317).

Miscellaneous

  • This release is built with Go 1.27.4.
  • Dependency updates.
  • See also the corresponding Centrifugo PRO release.

Don't miss a new centrifugo release

NewReleases is sending notifications on new releases.