Server Side Development in Swift with Vapor Tutorial Series Part 1: Hello, World

Since last couple of years I have not learnt any new programming language so this week I decided to change that. I decided to learn Swift programming language.

Swift is a modern, multi-paradigm, compiled, statically typed, safe, open-source programming language by Apple. Most developers use it to build apps for Apple platforms – macOS, iOS, watchOS, tvOS and beyond.

I don’t build Apple apps for living. For most of software engineering life I have built web applications and backend services. So,the best way for me to learn a new language is to build a web application using it. In this tutorial series I will be building a web application from scratch using Swift. We will be using Vapor web framework. It is the most used web framework for Swift. Vapor is non-blocking and event-driven built on top of Apple’s SwiftNIO.

Continue reading “Server Side Development in Swift with Vapor Tutorial Series Part 1: Hello, World”

Collection of Best Management and Leadership Lessons

Below is the list of management and leadership lessons that I find useful. I collected them from various books and articles I read in the last few years. It is a work in progress so I will keep updating this list as I learn more on this subject.

The greatest leader is not necessarily the one who does the greatest things. He is the one that gets the people to do the greatest things.

Ronald Reagan
Continue reading “Collection of Best Management and Leadership Lessons”

How to Solve Database Deadlock on Microsoft SQL Server

Today, I was helping a team that was getting a database deadlock in their Java Spring Boot application using Microsoft SQL Server. The stacktrace of the error was as shown below.

Exception is org.springframework.dao.CannotAcquireLockException: could not execute query; SQL ; nested exception is org.hibernate.exception.LockAcquisitionException: could not execute query] with root cause Transaction (Process ID 132) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

The database deadlock was caused when user uploads a file that was being written to the database. While the file was being uploaded user navigated to another page where data from the same table was queried.

When we debugged SQL Server traces we discovered that row-level lock was escalated to table lock. This was causing SQL Server to stop other transactions from accessing the table.

To solve this issue we enabled READ_COMMITTED_SNAPSHOT option for the database so that it uses READ_COMMITTED with row versioning isolation strategy. The default database isolation level in SQL Server is READ_COMMITTED which means a query in the current transaction cannot read data modified by another transaction that has not yet committed, thus preventing dirty reads. The isolation level uses shared locking or row versioning to prevent dirty reads, depending on whether the READ_COMMITTED_SNAPSHOT database option is enabled. Since wee enabled READ_COMMITTED_SNAPSHOT SQL Server will use versioning instead of locks.

ALTER DATABASE <database name>

Useful Resources

  1. Cause of a process being a deadlock victim – Link
  2. Database deadlock on Microsoft SQL Server – Link
  3. Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim – Link

Running H2 in SQL Server Mode With Custom Schema Name on Connection

These days I am working on a multi-tenant SaaS application where we are using SQL Server as our main transactional database. Using SQL Server is fine when you run the application but for unit and integration tests you want to use an in-memory database for quick turn around, better isolation, free from any external service to be available. Last thing you want is developers to not write tests because they take too much time to run. I love the experience of running a build on a clean machine and it works without any setup or configuration.

Continue reading “Running H2 in SQL Server Mode With Custom Schema Name on Connection”

mssql-scripter: A CLI tool to generate data definition language and data manipulation language for SQL Server

Lately I have to use SQL Server for a Microservices based system that I am currently building. This is the first time in my 15 years of software development life I had to use SQL Server. I was looking for a tool that can help me generate database schema from the already created database schema. I wanted to do store schema in my version control system. The usual tool to do this kind of work is SQL Server Management Studio but it works only on Windows operating system. Since I use Mac I was looking for a tool that is cross platform. My search led me to discover mssql-scripter, a multi-platform command line experience for scripting SQL Server databases.

Continue reading “mssql-scripter: A CLI tool to generate data definition language and data manipulation language for SQL Server”

Improving Spring Data JPA/Hibernate Bulk Insert Performance by more than 100 times

This week I had to work on a performance issue. Performance issues are always fun to work with. They give an opportunity to get into the depth of the technology we are using. We learn how much we don’t know about the technology we are using everyday. These days we too quickly think about changing the database or underlying library when faced with the performance bottleneck. When what we really need to do is learn about the technology we are using in depth.

The performance issue I am talking about was related to bulk insertion of data in to the database. We were using Spring Data JPA with SQL Server. In our use case, end user was uploading an excel file that the application code first parse, then process, and finally store in the database. The NFR for this requirement was that we should be able to process and store 100,000 records in less than 5 minutes.

Before my changes we were processing 10,000 records in 47 minutes. This certainly looked bad.

After making the changes that I will discuss in this post we were able to process 10,000 records in 20 seconds. We were able to process 100,000 in less that 4 minutes which is well below our NFR.

Continue reading “Improving Spring Data JPA/Hibernate Bulk Insert Performance by more than 100 times”

4 Reasons You Might Want To Build Stateful Apps

These days we all are told to build stateless applications. Stateless apps are those that don’t store any state in the application process and fetch any state from a centralised datastore (it could be a global cache or a database). The sixth factor in 12 factor app also talk about the same principle.

Execute the app as one or more stateless processes

Twelve-factor processes are stateless and share-nothing. Any data that needs to persist must be stored in a stateful backing service, typically a database.

There are advantages in building stateless applications primary being ability to scale horizontally with ease. When we build stateless applications we push the scalability problem to the database. We expect our database to scale horizontally. This usually is solved by sticking a global cache (Redis or Memcached) in between. Scaling cache is relatively easy and solved problem. Keeping cache updated with updates is a hard problem. We will discuss it some other time.

Continue reading “4 Reasons You Might Want To Build Stateful Apps”

Using ArchUnit To Enforce Architecture Best Practices

I have worked with multiple software development teams that because of feature delivery pressure does not apply best practices. This later leads to tech debt and cost more time. In the last project team that I helped they made two small mistakes:

  1. They didn’t use pagination in the collection resources. They were fetching the list of data from the database and returning back to the user as JSON. During development when data was small they didn’t face any issues. But, later when customer started doing testing it became a monumental task to add pagination in all the collection resources.
  2. They were returning domain entities in the response. This meant they were transferring more data over the wire than it was necessary.

So, they were breaking two best practices:

  1. Using pagination for collection resources
  2. Keep your domain object different from representation object

Continue reading “Using ArchUnit To Enforce Architecture Best Practices”

Issue #41: A Handcrafted Monthly Newsletter For Software Developers

Welcome to the 41st edition of my newsletter. I will be publishing this newsletter monthly now instead of weekly.

The three most harmful addictions are heroin, carbohydrates, and a monthly salary – Nassim Nicholas Taleb

Continue reading “Issue #41: A Handcrafted Monthly Newsletter For Software Developers”

Spring Boot Angular 7/8/9 Server Side Pagination Tutorial

Recently I had a to help a team add pagination in their application. The application was built using:

  1. Spring Boot 2.x
  2. Angular 7
  3. Spring Data JPA

In this blog I will quickly show how to add pagination to a Spring Boot 2.2.6 and Angular 9.1.1 application.

To quickly build the application we will use the spring-boot-maven-angular-starter project I wrote a couple of years. It is updated to the latest version of Spring Boot and Angular.

Continue reading “Spring Boot Angular 7/8/9 Server Side Pagination Tutorial”