TechBuzzPro

Redis Alternatives for Real-Time Player Presence in Go

· 6 min read · Apps & Browsers

In this article
  1. Why Redis Is Common but Not Mandatory
  2. In-Process Presence with Go Channels
  3. Pub/Sub Brokers: NATS and MQTT
  4. Distributed Caches with TTL: Hazelcast and Aerospike
  5. SQL with a Heartbeat Table
  6. How to Choose the Right Alternative
  7. Common Pitfalls and How to Avoid Them
  8. Cost and Operational Comparison
  9. Frequently Asked Questions

If you're building a Go service that needs real-time player presence and you want to avoid Redis, you can use an in-process library like centrifuge, a pub/sub broker like NATS or MQTT, or a distributed cache like Hazelcast or Aerospike — each with different trade-offs in latency, consistency, and operational complexity. This article compares those options and explains how to implement presence tracking in Go without a central Redis dependency.

Why Redis Is Common but Not Mandatory

Redis is popular for presence because it offers fast key-value lookups, built-in expiry (TTL), pub/sub, and sorted sets — all useful for tracking who is online and broadcasting events. But it also introduces a separate service to deploy, monitor, and scale. For small teams or single-region games, the operational overhead may not be worth it. Alternatives can reduce moving parts, improve latency by keeping data local, or better fit an existing infrastructure (e.g., you already run NATS or a SQL database).

In-Process Presence with Go Channels

The simplest alternative is to manage presence inside your Go process using maps and mutexes, combined with WebSocket or gRPC connections. For a single instance, this is extremely fast (microsecond reads) and requires no extra service. You'd store a map of player ID to last-seen timestamp, with a background goroutine that periodically removes stale entries. For broadcasts, use Go channels or a simple subscriber list. This works well for a small game server (e.g., under 10,000 concurrent players). The downside: if you run multiple instances behind a load balancer, a player connected to instance A won't be visible to instance B. You'd need sticky sessions or a shared store.

Pub/Sub Brokers: NATS and MQTT

When you need cross-instance presence, a lightweight pub/sub broker is a natural fit. NATS is written in Go, has a tiny footprint, and offers at-most-once or at-least-once delivery. You can publish presence updates (player online/offline) to a subject like presence.<player_id>, and each instance subscribes to all subjects to maintain a local cache. MQTT (e.g., using the Eclipse Paho client) is another option, especially if you already have a broker like Mosquitto or EMQX. MQTT has a built-in Last Will and Testament (LWT) feature that automatically publishes an offline message when a client disconnects unexpectedly — great for presence. Both brokers are simpler than Redis and scale horizontally, but you still need to handle stale entries if a client crashes without a clean disconnect (MQTT's LWT helps, NATS requires you to implement heartbeats).

Distributed Caches with TTL: Hazelcast and Aerospike

If you need a shared key-value store with automatic expiry, Hazelcast (an in-memory data grid) and Aerospike (a NoSQL database) are viable alternatives. Hazelcast offers a Go client and supports distributed maps with TTL, so you can store player_id → last_seen and let the cache expire entries automatically. Aerospike also supports TTL and is known for high throughput with predictable latency. These are heavier than Redis but can be justified if you already use them for other game data. They provide strong consistency options, which Redis does not by default. However, they are more complex to operate and may be overkill for simple presence.

SQL with a Heartbeat Table

A relational database like PostgreSQL can serve presence if you're willing to poll or use LISTEN/NOTIFY. Create a table presence(player_id, last_seen) and update it every 30 seconds from each client. A background query can clean up rows older than a threshold. For real-time broadcasts, use PostgreSQL's LISTEN/NOTIFY to push events to other instances. This works for low-frequency updates (e.g., 1 Hz) and small player counts, but it won't scale to thousands of writes per second without careful indexing and connection pooling. It's a good choice if you already have a database and want to avoid adding a new service.

How to Choose the Right Alternative

Your choice depends on three factors: scale, consistency, and infrastructure.

  • Single instance — Use in-process maps. Fastest, zero extra services.
  • Multiple instances, small scale — NATS or MQTT. Simple and reliable.
  • Multiple instances, high scale — Hazelcast or Aerospike for TTL-based expiry and horizontal scaling.
  • Already using SQL — PostgreSQL with LISTEN/NOTIFY is acceptable for modest loads.

Also consider whether you need presence history (e.g., who was online in the last hour). Redis with sorted sets is convenient, but you can replicate that with a time-series table in SQL or a ring buffer in memory.

Common Pitfalls and How to Avoid Them

  • Stale entries — Always implement a heartbeat with a timeout. For in-process maps, use a ticker to evict old entries. For MQTT, use LWT. For NATS, publish a periodic heartbeat and subscribe to it.
  • Race conditions — When multiple instances update the same player's presence, use atomic operations or a last-write-wins strategy. In Go, use sync/atomic for counters or a mutex for maps.
  • Network partitions — If an instance loses connectivity, it may not see offline events. Design your system to tolerate temporary inconsistency; presence is usually forgiving.
  • Scaling the broker — NATS can cluster, but you need to configure routes. MQTT brokers like Mosquitto support clustering but with more setup.

Cost and Operational Comparison

Redis itself is free and open-source, but running it costs memory and CPU. In-process maps cost nothing extra. NATS and MQTT brokers are also open-source but require a separate process. Hazelcast and Aerospike have commercial versions with costs, though open-source editions exist. The main cost difference is operational: fewer services means less monitoring, fewer failure modes, and simpler backups. For a small game, the in-process approach is cheapest; for a large one, a dedicated broker may be worth the overhead.

Frequently Asked Questions

Can I use WebSockets alone for presence?

Yes, if you track connections in a map on each instance. But for cross-instance visibility, you need a shared store or broker. WebSockets are the transport, not the presence store.

Is NATS fast enough for real-time games?

NATS can handle millions of messages per second with sub-millisecond latency in a single cluster. For presence updates (a few per second per player), it's far more than enough.

How do I handle a player disconnecting unexpectedly?

Use heartbeats and timeouts. MQTT's LWT is automatic; for NATS, you need to publish a last-will message or have the client send a final message before closing. In-process, you can use a goroutine that watches the connection and cleans up on error.

Can I use MongoDB or Cassandra for presence?

Yes, but they are heavier and slower for high-frequency updates. They work if you need rich queries or history, but for simple presence, a cache or broker is more efficient.

What about using etcd or Consul?

Those are strongly consistent key-value stores designed for configuration, not high-frequency writes. They can work for presence but may become a bottleneck under load.

Is it worth dropping Redis just to simplify ops?

If Redis is your only extra service and you're comfortable running it, the operational cost is low. But if you already have NATS or a database, reusing them can reduce complexity. For a single-instance game, in-process presence is the simplest of all.