10 Persistent state management with databases

 

This chapter covers

  • Storing data and authenticating users with MongoDB
  • Using PostgreSQL from Vert.x
  • Testing strategies for integration testing of event-driven services that interact with databases

Reactive applications favor stateless designs, but state has to be managed somewhere.

Databases are essential in most applications, because data needs to be stored, retrieved, and queried. Databases can store all kinds of data, such as application state, facts, or user credentials. There are different types of databases on the market: some are generalist and others are specialized for certain types of use cases, access patterns, and data.

In this chapter we’ll explore database and state management with Vert.x by diving into the implementation of the user and activity services. These services will allow us to use a document-oriented database (MongoDB) and a relational database (PostgreSQL). You will also see how you can use MongoDB for authenticating users, and how to write integration tests for data-driven services.

10.1 Databases and Vert.x

Vert.x offers a wide range of clients for connecting to data sources. These clients contain drivers that talk to servers, and that may offer efficient connection management, like connection pooling. This is useful for building all kinds of services, from APIs backed by a data source to integration services that mix data sources, messaging, and APIs.

10.1.1 What the Eclipse Vert.x stack provides

10.1.2 A note on data/object mapping, and why you may not always need it

10.2 User profile service with MongoDB

10.2.1 Data model

10.2.2 User profile API verticle and initialization

10.2.3 Validating user input

10.2.4 Adding users in MongoDB

10.2.5 Authenticating a user

10.2.6 Fetching a user’s data

10.2.7 Updating a user’s data

10.3 Activity service with PostgreSQL

10.3.1 Data model

10.3.2 Opening a connection pool