What is an In Memory Database?


In this article, we understand what the term “In-Memory Database” actually means. We see what scenarios they can be used, and why they are important.

You will Learn

  • What Is An In-Memory Database?
  • Why should you use an In-Memory Database?
  • When should you use An In-Memory Database?
  • How do in-memory databases help with unit testing?

Java Programmer Essentials

As we go towards microservices architectures, what should a Java Programmer learn?

This is the last article in series of six articles on Java Programmer Essentials :

What Is An In-Memory Database?

Imagine a real world database such as MySQL Server or Oracle. The first step in using one is to install it. The second step would be to create the schemas, and the third involves creating the tables and other definitions.

With an in-memory database:

  • The step of installing a separate database is done away with.
  • The database now sits inside the running application’s memory.
  • The database schema can be automatically created at application startup using a simple script or based on entities configured in case of Spring Data JPA.

In memory databases allow you to focus on building a great prototype and validate business ideas.

Once you have a prototype ready, you can make a switch to a real world database.

When to use An In-Memory Database?

An in-memory database is of great value when a developer tries to learn a new language or framework. You would prefer to learn the features of the language or a framework, rather than be caught up with configuring a database, or learning how to create a schema.

Switching over to a real database is relatively easy, with a few simple configuration changes.

Another major advantage of an in-memory database is in unit testing or system testing. You don’t want to depend on external resources such as a database, because the information stored in them can change any time. This can lead to tests failing, even with code remaining the same!

It is much better to launch a database in memory, populate the data, run the tests and verify the results.

An in memory database also provides a great option when you are building prototypes or initial designs.

Features of in memory databases

An in-memory database typically supports a subset of the SQL language standard. Make sure that it supports all features you want to use. Such a database also offers a browser-based console, such as the following for H2:

image info

When you launch your application, you could also visit the in memory DB console and connect to the application. That would enable you to view the data stored there:

image info

You can see data from various tables.

Examples Of In-Memory Databases

Popular examples of in-memory databases are

  • H2 : This is the more popular one among others
  • HSQL

Using An In-Memory Database with Spring Boot

If you are developing an application using the Spring Boot framework, then the Spring Initializr web utility makes this very easy for you. You just need to add your in-memory database as a dependency to your configuration:

image info

Notice how we added H2 and JPA as dependencies in our list.

Do check out our video on this:

image info

Summary

In this article, we talked about the need for an in memory database, and how it is good for quick prototyping and learning. We focused on what such a database is, and how it is different from a real world database. We then took the example of H2 and described how to use an in-memory database.Finally, we showed you how easy it is to use an in-memory database, such as H2.

10 Step Reference Courses

Image Image Image Image Image

in28Minutes is helping 300,000 Learners across the world reach their learning goals. Click here for the complete catalogue of 30 Courses.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Software Design - Separation Of Concerns - with examples

Software architects and programmers love having Seperation of Concerns. What is it? Why is it important? Let's get started.

Object Oriented Software Design - Solid Principles - with examples

Software design is typically complex. Object oriented design takes it to the next level. There are a number of design patterns and other stuff to be aware of. Can we make things simple? What are the goals to aim for when you are doing object oriented design? SOLID Principles is a great starting point for Object Oriented Design.

Software Design - Open Closed Principle - with examples

Open Closed Principle is one of the SOLID Principles. You want your code to be easily extended. How do you achieve it with minimum fuss? Let's get started.

Software Design - What is Dependency Inversion Principle?

Dependency Inversion Principle is one of the important SOLID Principles. Dependency Inversion Principle is implemented by one of the most popular Java frameworks - Spring. What is it all about? How does it help you design good applications?

Introduction to Four Principles Of Simple Design

With agile and extreme programming, the focus is on keeping your design simple. How do you keep your design simple? How do you decide whether your code is good enough?

Software Design - Single Responsibility Principle - with examples

For me, Single Responsibility Principle is the most important design principle. What is Single Responsibility Principle? How do you use it? How does it help with making your software better? Let's get started.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Designing REST API - What is Code First Approach?

Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?