State

Think of state as a person's memory. Usually, a service has state, which means it has memory of actions and information it's serving. The idea is that our service will run indefinitely, but sometimes we're forced to restart it or even stop it for some time because of maintenance or an upgrade.

Ideally, a service should resume without losing state, giving its users the perception that it never stopped. This is achieved by doing one of two things:

  • Having state stored in a persistent storage
  • Saving state in a persistent storage before stopping and loading that state after restarting

The first option will make your service a bit slower (nothing is faster than state in system memory) but should give you a more consistent state across restarts.

The second option is trickier, because sometimes our service might stop abruptly and be unable to save that state, but for those use cases, you may not care about the state. It's up to you.

There are a lot of options to store state; it will depend on what you want to store. For a microservice, you should avoid the filesystem so as to make your service more compatible with multiple operating systems.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset