How to Build Scalable, Stateful Java Services in Under 15 Minutes

DZone 's Guide to

How to Build Scalable, Stateful Java Services in Under 15 Minutes

Learn more about building scalable Java services in 15 minutes or less.

· Java Zone ·
Free Resource

Five years ago, when I started tracking media buzz around stateful architectures, I’d see a few articles every month about running stateful containers. That's about when Caitie McCaffrey first shared this awesome presentation about building scalable stateful architectures. Since then, the dominant software paradigm has become functional application design. The actor model and other object-oriented paradigms are still in use, but database-centric RESTful architectures are standard means of building web applications today.

However, the tides are beginning to shift. Due to innovations like the blockchain, growing demand for real-time applications, the digitization of OT assets, and the proliferation of cheap compute resources at the network edge; there’s renewed interest in decentralized application architectures. As such, there’s also been increased focus on stateful applications. For example, at least five Apache Foundation projects (Beam, Flink, Spark, Samza, and TomEE) are touting statefulness as a benefit today. Modern applications communicate across multiple application silos and must span real-world machines, devices, and distributed data centers around the world. Stateful application architectures provide a way to abstract away the logistical effort of state management, thereby reducing development and management effort necessary to operate massive-scale distributed applications.

Build a Scalable, Stateful To-Do List in 15 Minutes or Less

For the rest of this post, I want to disprove the notion that building scalable, stateful applications is a task too complex for everyday Java developers. In order to illustrate how easily a stateful application can be set up, we’ll walk through a tutorial for building a simple to-do list using the Swim platform. You can find all the source code for the to-do list tutorial here on GitHub.

Getting Started: Setting Up Your Development Environment

Before diving into the to-do list app itself, it’s important to ensure your development environment is properly set up to run Swim applications.

To run the to-do list example application, only Java 9 and the Gradle build tool (which is included in the wrapper script) are required. This video tutorial goes through the process of setting up your local Swim development environment:

Tutorial: How to Build a Simple, Stateful “To-Do List” App Using Swim

Once your development environment is set up, you’re ready to build a stateful to-do list. The to-do list example is quite basic, but the same patterns can be applied to Swim applications of any complexity. As I mentioned earlier in the post, all the code for the to-do list example app is available on GitHub.

For a better idea of what's included in this tutorial, take a look at the following lines of server code. This code block defines a Web Agent that can create and observe a to-do list. Every server-side to-do list continuously and statefully runs the logic below:

 * This is a simple WebAgent to manage the list of items in the To-do list
public class ListAgent extends AbstractAgent {

   * This MapLane holds each list item and uses a timestamp for a unique key
  MapLane<Long, String> todoList = this.<Long, String>mapLane();

   * This is a command lane used to add new items to the to-do list
  public CommandLane<String> addListItem = this.<String>commandLane()
    .onCommand((String newListItem) -> {
      // create a new timestamp as our UUID
      final long newUuid = System.currentTimeMillis();
      // cap item length to 255 characters
      newListItem = newListItem.substring(0, Math.min(newListItem.length(), 255));

      // add the new item to the todoList MapLane
      todoList.put(newUuid, newListItem);

   * This is a command lane used to remove items from the 
   * to-do list by its key (or uuid)
  public CommandLane<String> removeListItem = this.<String>commandLane()
    .onCommand((String uuidString) -> {
      // parse the value sent by the UI from a String to Long
      long uuidToRemove = Long.parseLong(uuidString);

      // remove the list item from the todoList MapLane

Finally, if you’re ready to get started building your own stateful to-do list application, check out this tutorial video:

Learn More

Let us know your thoughts about this tutorial and what you're building on the open-source Swim platform. You can get started with Swim here and make sure to STAR us on GitHub.

actor model, distributed architecture, example, java, microservices, object oriented programming, serverless, stateful applications, streaming api, tutorial

Published at DZone with permission of Bradley Johnson . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}