English3000

English3000

When to switch from a stateful server to a database

I’m currently reading through Functional Web Development in Elixir. It walks the reader through how to build an app using a stateful OTP server in place of a database.

When one first begins a project, a main benefit of using a stateful server, the author @lance posits, is it decouples and separates one’s design from one’s database tables. Because of Elixir’s concurrency, Phoenix’s bandwidth and Presence feature, and OTP’s fault tolerance and high-scale distributability, the stateful server is now a viable alternative to a database, reducing code complexity and app latency.

What I’m wondering:

  1. What are the bounds of a stateful server? I.e. What level of usage requires the switch to a database?

  2. What does that transition look like? For example, could one still keep pieces of the stateful server for speed (where throughput isn’t as much an issue)?

  3. In production, wouldn’t best practice be for the stateful server to offload state to the database, and for it to fetch the most recent state from maybe Redis, were there a system-wide reboot? I.e. Is OTP truly reliable enough for a production app not to require some sort of fallback for the state?

Most Liked

wmnnd

wmnnd

Using a database (whether it’s a traditional SQL database or an OTP database such as Mnesia) makes sense when you want to persist data beyond application restarts. This might be true for parts of your data or all of your data, depending on your application.

I personally tend to just use an SQL database right away because it usually fits my application’s need anyways.
But if that’s not the case, you can always add a persistence layer to your stateful application that takes care of storing data when required and loading it when it gets restarted. With this approach you can leave most of your code unchanged and simply add code for storing/retrieving in/from the DB to your own state management functions.

dom

dom

A database is useful to store dynamic state that can’t be reconstructed, for instance user input. Things that can be rebuilt, or queried from another service, often don’t need to go in DB. See:

blatyo

blatyo

Conduit Core Team

Another consideration is deployment. If you have persistent state in your application, you’ve essentially made your application a database. The way you database is very different from the way you deploy an application with no persistent state. There’s a concept called Pets vs Cattle, where Pets are special applications that need a lot of criteria to be true. For example, if you’re using disk based persistence, then if you roll that application, you’ll want it to have access to the same disk on reboot. Cattle, applications without persistent state, can be killed and booted anywhere.

Last Post!

dimitarvp

dimitarvp

See Phoenix Contexts for reference. You can make your own modules and functions that do reading, querying, writing and deleting and the consumer would be none the wiser on how is the data stored. So you can code according to your requirements from day one really.

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement