Building a Stock Trading App:...
October 21, 2024
Hazelcast – an in-memory data grid – fits well with Spring Boot to provide a solid solution for developing distributed applications. In the ever-expanding landscape of modern computing, distributed systems have become the foundation for developing applications that are scalable, fault-tolerant, and high-performance.
A distributed system can be described as a network of communicating processes, or more accurately, computers that cooperate to provide a predefined service. These nodes further divide work into tasks that they collectively perform with the help of a network over which they share information.
The main properties of distributed systems include scalability, fault tolerance, concurrency and performance, and decentralization.
With the help of Hazelcast when used with Spring Boot, the developers and other professionals working on the projects can have full control over the dissemination of the data, its caching, and processing across the multiple nodes so that it can achieve the highest level of efficiency and scalability with the provision of enhanced fault tolerance.
This also promotes concurrency, that is the ability to perform tasks at the same time, and is also implemented in a distributed fashion for higher availability and scalability.
IMDG stands for in-memory data grid which is a networked or clustered set of computers designed to pool its RAM to allow applications to share data with other applications residing in the cluster.
IMDGs are actually designed for high-speed data processing and intended for use in constructing and implementing expansive applications that are apt for installation in massive computer server systems with more RAM than what is ordinarily ascertainable in a typical server.
IMDGs are therefore a critical factor for use in applications requiring superior performance, generation, and expedited data processing.
Hazelcast is an open-source, distributed, in-memory data grid (IMDG) and caching platform that speeds up, scales up, and scales out applications. It enables the distribution of computing in that you can store and process data in memory across several nodes of a cluster. This architecture provides very high availability, built-in redundancy, and equally elegant scalability.
Distributed Data Structures: The distributed data structures include maps queues lists and sets that are provided by Hazelcast. Such structures help you to implement efficient storage and processing of data among specific segments of the cluster.
In-Memory Storage: Caching in Hazelcast puts data into the memory across different nodes and ensures fast access to the frequently used data. This positively impacts the rate at which data is pulled off and processed relative to disk-based systems.
Distributed Computing: In terms of scalability Hazelcast enables distributed computing through deployment of tasks and computations over the cluster. This makes use of the parallel processing capabilities of the number of nodes to accomplish its task in the most optimized manner.
Scalability: The management of Hazelcast can be done easily by scaling out, the number of nodes in the cluster is increased. This capability gives the ability to scale up the numbers of data and traffic which is crucial when your application grows in popularity.
Caching: Hazelcast also includes a feature that provides a distributed caching that is capable of storing frequently used data in the cluster. This minimizes the load of having to pull data from other applications that are normally slower than the target application thus enhancing the application’s performance.
Cloud-Native Support: Hazelcast says that they can be deployed and run on clouds from AWS, Azure, Google Cloud Platform, etc. It works with Kubernetes and Docker for containerization, which helps in easily managing Hazelcast clusters in cloud environments.
Enterprise Features: Hazelcast offers enterprise-grade features such as advanced security, monitoring, management, and clustering capabilities. These features cater to the needs of enterprise deployments requiring robust security, detailed monitoring, and comprehensive management capabilities.
Hazelcast’s robust feature set makes it a powerful tool for building high-performance, scalable, and reliable distributed applications.
Read More: Jaeger Integration with Spring Cloud
https://hazelcast.com/open-source-projects/downloads/
Now go to the bin directory in hazelcast-management-center and run this command to start hazelcast-management-center on port 8202
sh start.sh 8202
Now go to this URL http://localhost:8202/ on which hazelcast-management-center will be started and create an admin user. After creating an admin user and logging in, you will get to see this dashboard of hazelcast-management-center.
Now go to Spring Initializr and create a sample spring boot project.
Make sure to add the following dependency for hazelcast in pom.xml.
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>4.1.1</version>
</dependency>
Create Student Entity class, StudentService Interface, StudentServiceImpl class, and application.properties as follows:
Create one hazelcast.yaml file in /src/main/resources/ in which we will specify the url of hazelcast-management-center and cluster-name to which this spring boot app should be connected.
Now, create a controller class to perform CRUD operations.
Here, we have used a few of the annotations for caching our data:
@CacheConfig(cacheNames = “students”)
Here the name of the cache that will be used for caching the data is students.
@Cacheable(key=”#id”)
As the name implies, we can use @Cacheable to demarcate methods that are cacheable — that is, methods for which the result is stored in the cache so that, on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually invoke the method.
Here, we are caching the getStudentById() method based on its id field.
@CachePut(key= ‘#id’)
It is generally used with update methods if we want our cache to be updated with the result of the method execution.
@CacheEvict(key=’#id’)
The @CacheEvict annotation is used to remove one or more entries from a cache. When a method annotated with @CacheEvict is called, Spring will remove the cached data associated with the specified cache name and key (or keys) along with the method execution.
Now, run the Spring Boot app
Read More: Spring Security with Auth0 Integration
We can test that caching using Hazelcast is working by using various endpoints of the controller class using postman.
In the first call to the method in which we are using caching, the operation time will be very high since it will interact with the database. After that all subsequent calls to that method will fetch data from cache so it will be very quick.
Github Link to the demo Project:
https://github.com/AnkitKJSInexture/Hazelcast-With-Spring-Boot/tree/main