DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Your Application Architecture Has Evolved
  • Mastering Advanced Aggregations in Spark SQL
  • Micro-Frontends in a Microservice Architecture
  • Designing Scalable and Secure Cloud-Native Architectures: Technical Strategies and Best Practices

Trending

  • Grafana Loki Fundamentals and Architecture
  • How to Format Articles for DZone
  • Ensuring Configuration Consistency Across Global Data Centers
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  1. DZone
  2. Data Engineering
  3. Data
  4. A Small Microservice Developed in Scala Using Hexagonal Architecture

A Small Microservice Developed in Scala Using Hexagonal Architecture

A software developer explores how to create HTTP REST end-points for use in a microservice application using the Scala language and Hexagonal Architecture.

By 
Ederson Corbari user avatar
Ederson Corbari
·
Updated Jul. 10, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.8K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

Scala is a language that I have been using extensively in my work, but with the focus on Big Data using Spark for data processing.

Scala was in my playlist of languages that I wanted to delve into a bit more and not only experiment with in the area of Big Data using Spark, but to write a microservice using HTTP and REST.

The idea is to apply the concept of Hexagonal Architecture (ports and adapters). My microservice is called a Sparrow Account, and the source code is available in GitHub. I also did a similar experiment using the Clojure language that you can see on GitHub.

  • https://github.com/edersoncorbari/sparrow-account
  • https://github.com/edersoncorbari/small-bank

Let’s go to the requirements.

1. Microservice Requirements

Create a microservice that checks and creates a hypothetical bank account.

It must comprise an HTTP Server with two endpoints:

  • One to insert a new monetary transaction, money in or out, for a given user.
  • One to return a user’s current balance.

Requirements:

  • It must not be possible to withdraw money for a given user when they don’t have enough balance.
  • You should take concurrency issues into consideration.
  • Store data in memory.
  • Use designer partners and good coding practices, like:
    • Immutability.
    • Separation of concerns.
    • Unit and integration tests.
    • API design.
    • Error handling.
    • Language idiomatic use.
    • Use the functional programming paradigm.

Proposed Solution

The architecture of the proposed solution follows the Hexagonal Architecture concept. The design is based on two books:

  • Functional Programming Patterns in Scala and Clojure
  • Scala Design Patterns

Below is an example diagram of a Hexagonal Architecture:

Hexagonal Architecture

Below is an diagram laying out the Organization Application Package:

Organization Application Package

1.1 HTTP REST Server

To build a request and response HTTP REST Server, Finagle-Finch was used:

  • https://twitter.github.io/finagle/
  • https://finagle.github.io/finch/

Here's the piece of code where the server is used: src/main/scala/sparrow/account/ServerApp.scala

def runServer(): Unit = {
  val app = Http
    .server
    .withLabel(serverConf.name)
    .withAdmissionControl.concurrencyLimit(
    maxConcurrentRequests = serverConf.maxConcurrentRequests,
    maxWaiters = serverConf.maxWaiters
  ).serve(s"${serverConf.host}:${serverConf.port}",
  (Routes.balanceAccount :+: Routes.fillAccount).toService)
  onExit {
    app.close()
  }
  Await.ready(app)
}

The end-points available on the server are:

Method EndPoint Example Parameter
POST /account {“uuid”:”1”, “amount”:100.50}
GET /balance not required

Here's the piece of code of the routes with the end-points: src/main/scala/sparrow/account/ServerApp.scala

final val fillAccount: Endpoint[Account] =
  post("account" :: jsonBody[AccountFillRequest]) {req: AccountFillRequest =>
    for {
      r <- accountService.fillAccount(req.uuid, req.amount)
    } yield r match {
      case Right(a) => Ok(a)
      case Left(m) => BadRequest(m)
    }
  }

There are two end-points: fillAccount that can create an account and deposit a value, as well as can withdraw using the negative value; and the  balanceAccount end-piont to see the balance available to the user.

1.2 Transactional Memory and Concurrency Control

When we are talking about microservices, we have to guarantee the atomicity of the code, so that no undue competition occurs. To control concurrency the ScalaSTM was used.

  • https://nbronson.github.io/scala-stm/

Here's the piece of code where atomicity is used: src/main/scala/sparrow/account/controller/AccountController.scala

override def fillAccount(uuid: String, amount: Double): Future[Either[AccountFillException, AccountTransaction]] = Future {
  if (accounts.get(uuid).isEmpty) createAccount(uuid, 0)

  accounts.get(uuid) match {
    case Some(transact) => {
      atomic {implicit tx =>
        transact() = AccountTransaction(transact().uuid, transact().amount + amount)

        displayOperationType(transact().uuid, transact().amount)

        if (amountIsNegative(transact().amount))
          transact() = AccountTransaction(transact().uuid, transact().amount - amount)

        Right(transact())
      }
    }
    case _ => Left(AccountFillException("Fill account not found."))
  }
}

1.3 Other Tools Used

Other tools used in the project are in the order below:

  • https://github.com/lightbend/config
  • http://www.scalatest.org/

1.4 Building the Project

To build the project, you can follow the instructions in the GitHub repository located here:

  • https://github.com/edersoncorbari/sparrow-account

Conclusion

We saw in this short article how easy and simple it is to create a microservice application using the Scala language. It could also have used other libraries such as the AKKA that I predict will soon make an article about it.

Thanks!

microservice Scala (programming language) Architecture Big data

Opinions expressed by DZone contributors are their own.

Related

  • How Your Application Architecture Has Evolved
  • Mastering Advanced Aggregations in Spark SQL
  • Micro-Frontends in a Microservice Architecture
  • Designing Scalable and Secure Cloud-Native Architectures: Technical Strategies and Best Practices

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!