Top Ten Tips for Using Redis with Phoenix LiveView



This content originally appeared on DEV Community and was authored by HexShift

Phoenix LiveView allows for building rich, real-time user interfaces with server-rendered HTML, but its default state management is tightly coupled to each LiveView process. While this works well for isolated sessions, there are many situations where LiveView processes need to share or coordinate state. Redis can serve as a fast, reliable bridge between LiveView sessions, enabling shared counters, collaborative editing, global feature toggles, and more. Here are ten tips for effectively integrating Redis into your LiveView applications.

Use Redis for global counters across sessions

One of the simplest and most effective ways to use Redis is for global counters. Whether you are tracking active users, page views, or real-time votes, Redis allows for atomic increment and decrement operations. Using Redix.command(conn, ["INCR", "liveview:counter"]) from your LiveView lets multiple users interact with the same state without race conditions. Pair this with PubSub broadcasts to push the updated count to all connected LiveViews in real time.

Back shared form state with Redis in collaborative UIs

In collaborative apps where users fill out forms or work on shared documents together, Redis can act as a central store for intermediate state. Each keystroke or selection can be pushed to Redis with SET and read by all other sessions working on the same resource. This makes it easier to support real-time collaboration features like co-editing or watching someone else’s cursor move across a form.

Set expiration times to manage temporary state

Redis keys support expiration out of the box. Use SETEX to store temporary values like verification codes, onboarding progress, or one-time tokens that should disappear after a short time. This keeps your data layer clean and allows LiveViews to reference ephemeral state without needing to create dedicated database tables or cleanup jobs.

Leverage Redis PubSub for lightweight broadcasting

While Phoenix has excellent built-in PubSub, Redis also provides a cross-platform publish and subscribe system. You can use Redis PubSub to push updates to worker nodes, non-Phoenix services, or third-party integrations. This is especially useful in polyglot systems where Elixir is just one part of the backend stack.

Use JSON encoding for structured state

When storing more complex state in Redis, always serialize it as JSON. This makes the data human-readable and portable. You can use Jason.encode!/decode! in Elixir to safely round-trip maps and structs through Redis. This approach helps when syncing structured UI state across multiple LiveViews, such as configuration panels or dashboards.

Avoid bottlenecks by namespacing keys

When storing data in Redis, choose your key names carefully. Use consistent namespacing like liveview:form:session123 or dashboard:project456:settings. This prevents conflicts and helps you selectively delete or update related groups of keys. It also makes debugging and monitoring easier, as you can filter keys by namespace patterns.

Use Redis as a feature toggle backend

For applications with live feature rollout needs, Redis can be used to manage toggles in real time. Set boolean flags like features:new_dashboard or flags:user_search in Redis and check them inside your LiveView mount functions. Because Redis updates are fast and in-memory, changes to toggles take effect across all users almost instantly without needing a redeploy.

Handle Redis disconnects with backoff and retry

Redis is fast but not immune to failure. Always wrap your Redis interactions in retry logic, especially for writes that must succeed. Use exponential backoff and structured error logging to handle timeouts or connection issues gracefully. This ensures your LiveViews continue to function even when Redis becomes temporarily unavailable.

Watch Redis keys for change detection

Although Redis does not natively support watching keys for change notifications in the same way a database might, you can simulate this using Redis PubSub. When one LiveView updates a key, it can publish a message on a related channel. Other LiveViews subscribed to that channel can then fetch the updated value from Redis and refresh their UI accordingly.

Use Redis to share presence-like data outside Phoenix Presence

Phoenix Presence is a great tool, but it can be overkill in some scenarios. If you need lightweight tracking of which users are viewing a page or working on a document, consider using a Redis set. Each LiveView can add its user ID to a set on mount and remove it on terminate. This pattern works well for presence outside of traditional channels, especially in systems that mix Phoenix and non-Phoenix clients.

By introducing Redis into your Phoenix LiveView stack, you gain access to a shared, fast state layer that is well-suited for real-time features, cross-session coordination, and integration with other services. It complements the LiveView process model and allows your app to scale horizontally while keeping UIs consistent and synchronized.

To explore more of these real-world architectural patterns and build better Elixir applications, get your copy of Mastering Phoenix: A Practical Guide to Scalable, Secure, and Maintainable Applications. This 29-page PDF guide is packed with insights and available for just $10 on Gumroad.


This content originally appeared on DEV Community and was authored by HexShift