Scaling Spring Applications in 4 Easy Steps: Using Gigaspaces Xap and Openspaces
Scaling Spring Applications in 4 Easy Steps: Using Gigaspaces Xap and Openspaces
Scaling Spring Applications in 4 Easy Steps: Using Gigaspaces Xap and Openspaces
in 4 Easy Steps
Using GigaSpaces XAP and OpenSpaces
Data⎯the central database is in the critical Asynchronous persistence⎯I/O delay does not affect the application
path of the business transaction. I/O delays
Data stored reliably in an in-memory cluster until it is persisted
slow the application down, and the database
must be able to respond to increasing loads in Writing to database in batches, from a collocated service, for highly
real-time. optimized data access
Messaging—the central messaging provider Data and messaging in the same place, with one clustering model,
is in the critical path of the transaction and guaranteeing affinity with no overhead
might become overloaded. It might also
No network hops between messaging and database, less moving parts
impose an I/O delay, if the messaging provider
uses persistence for high availability, as is High availability without disk access, using in-memory clustering
often the case.
Messaging can be done over a WAN
Business Logic—business logic must be Data and messaging collocated with business logic services
tightly coupled to the data tier and messaging
Application becomes a self-sufficient resource that can be deployed
provider, to ensure data affinity and
anywhere and scaled linearly
consistency.
Minimal possible end-to-end latency
1
To learn more about OpenSpaces, refer to GigaSpaces Online Help, OpenSpaces,
http://www.gigaspaces.com/wiki/x/FIBI. The open source community website is at http://www.openspaces.org.
2
To learn more about SBA, a holistic architectural model which powers these virtualization steps, refer to the GigaSpaces
white paper, "The Scalability Revolution: From Dead End to Open Road".
Transition: Virtualize the data tier using the In-Memory Data Grid (IMDG).
Figure 7 – Application interacts with GigaSpaces IMDG, with database in the background
In most cases the IMDG will be partitioned, with a backup node for each partition to provide high-
availability. But many other topologies are also available, including replicated and master-local. 4
Because OpenSpaces uses the Persistence as a Service model, you can tailor the persistence to
suit your needs:
The GigaSpaces Mirror Service can perform transparent, asynchronous
persistency of all IMDG data to the database. 5
The IMDG can also persist some or all data to a log file or any other persistent
resource.
It is possible to write-through certain types of data to a database of your choice,
or read-through from a database when certain operations are performed on the
IMDG. 6
Some types of data won’t need persistence at all, given that they are kept highly
available by means of replication between IMDG nodes.
3
See GigaSpaces Online Help, Enterprise Data Grid Tutorial B - Aggregate Queries,
http://www.gigaspaces.com/wiki/display/GS6/Enterprise+Data+Grid+Tutorial+B+-+Aggregate+Queries.
4
See GigaSpaces Online Help, Data Grid Topologies, http://www.gigaspaces.com/wiki/x/c4GY.
5
See GigaSpaces Online Help, Mirror Service, http://www.gigaspaces.com/wiki/x/7IGY.
6
See GigaSpaces Online Help, Read-Through and Write-Through, http://www.gigaspaces.com/wiki/x/K4KY.
The IMDG, which uses the PaaS paradigm to persist data to disk in the background, is not only
more efficient but also more reliable than a database:
If the database crashes, the system continues to work and recovers automatically
as soon as the database is up and running.
It provides true hot fail-over within a few miliseconds, or a maximum of 2-3
seconds in a worst-case scenario such as a network cable coming unplugged.
Fail-over happens on the client side, which enables seamless transtion between
primary and backup.
It monitors memory usage and will block operations before running out of
memory, thus enabling graceful recovery and tracing of the cause of the event.
return orders;
}
}
To move the application to Step One, you first need to add the following beans to the Spring
configuration, so you can wire the DAO to the IMDG instead of to the database:
Now all that's left is to switch the existing DAO implementation with the following. Note we are still
using Spring declarative transactions management, which is supported by GigaSpaces.
As you recall, persistence is completely decoupled from the application. You can add any of the
persistence options discussed above via the UI of the IMDG, and change your persistence strategy
at any time. The same goes for the underlying topology of the IMDG nodes, which may be
partitioned, replicated, and so on.
7
As discussed in the following chapter, the IMDG can also perform the messaging function, which removes the need for a
distributed transaction.
Transition: Create a Data Bus that consolidates your messaging and your data.
Result: Messaging provider can be scaled without threatening affinity, and can be made highly
available without writing to disk.
The reference application's services use Spring's jmsTemplate to send JMS messages. This
stays exactly the same. Behind the scenes, you wire the jmsTemplate bean to a Connection
Factory and Destination provided by OpenSpaces:
The last bean is a message-to-object convertor, which will convert the JMS messages created by
your services to objects that can be accepted by the GigaSpaces middleware components. This
bean is wired into the Connection Factory, so message conversion is transparent to your services.
Transition: Use the Spring Application Context to package your business logic together with the
middleware, creating a Processing Unit.
Result: Application can be scaled linearly, by deploying the Processing Unit on as many machines as
needed.
8
For more details, refer to GigaSpaces Online Help, OpenSpaces Processing Unit,
http://www.gigaspaces.com/wiki/x/moCE.
9
For more details, refer to GigaSpaces Online Help, OpenSpaces Event Containers,
http://www.gigaspaces.com/wiki/x/bICE.
As you can see, the Polling Container declares the object to listen on (the domain model, which
hasn't changed in the transition), and the business logic services (the Validator and Matcher, in this
case), which receive events from the OpenSpaces middleware component.
The Polling Container is defined in the broader context of a Processing Unit. The Processing Unit is
just the full Spring Application Context, defining all the business logic services and other required
beans. This is exactly the same as in a regular Spring application, except that all the services
happen to be collocated on the same machine.
Figure 8 - SLA-driven Containers are Java processes that provide a hosting environment for running a Processing Unit.
Aggregate monitoring⎯one view of the entire deployment scope, with the ability
to view the full path of a business transaction.