Post on 11-May-2015
description
transcript
© 2014 Hazelcast Inc.
How to speed up your Database.
DAVID BRIMLEYSENIOR SOLUTIONS ARCHITECT
© 2014 Hazelcast Inc.
About me
2
"Senior Solutions Architect for Hazelcast.
Oracle Coherence @ HSBC 2007-2010 Gemfire @ Nomura 2010 - 2013
Worked for Major Investment Banks in the City of London.
Java Programmer since 1998.
David Brimley @dbrimley
© 2014 Hazelcast Inc.
Agenda - 40 Minutes + Q&A time.
• Introduction to Hazelcast.
• The Slow Database problem.
• Adding Hazelcast for faster queries.
• And doing the same for inserts and updates.
• Q&A
3
© 2014 Hazelcast Inc.
Introduction to Hazelcast
• Hazelcast the Open Source Projectwww.hazelcast.org""
• Hazelcast the Companywww.hazelcast.com
4
© 2014 Hazelcast Inc.
The Slow Database Problem
• Traditional Databases are hard to scale.
• They cost a lot in time and effort.
• Common solution is a Cache.""
• IMDG like Hazelcast give horizontal scalability.
5
© 2014 Hazelcast Inc.
The Hazelcast Approach
• Cluster of JVM that all contribute memory.
• Client API can work against Maps.
• Query using Predicates or SQL like strings.""
• Faster Reads using the Cache.
• Faster Update/Inserts using write-behind.
6
© 2014 Hazelcast Inc.
The Test Suite
• Postgres 9.3.4 installed on a Macbook Pro Dual Core with 16gb RAM and 256gb SSD.
• Dataset : Stackoverflow Public Dump (47gb)
• Hazelcast 3.2
7
© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.
The Simple Postgres Java DAO
8
© 2014 Hazelcast Inc.
Step 1 : Configure the Hazelcast Distributed Map."A) MapStore for connecting Hazelcast to the Database."B) Configure Indexes for faster In Memory Query of Map."
"
Integrating Hazelcast in 4 steps
9
" <map name="user"> <map-store enabled="true"> <class-name>com.craftedbytes.hazelcast.dao.UserMapStore</class-name> <write-delay-seconds>30</write-delay-seconds> </map-store> <indexes> <index ordered="true">location</index> </indexes> </map>
"
© 2014 Hazelcast Inc.
Integrating Hazelcast in 4 steps
Step 2 : Code your MapStore implementation"
10
"public class UserMapStore implements MapStore<Integer, User> { " private UserDAO userDao = new UserDAO(); " private UserMapStore() {} " @Override public void store(Integer userKey, User user) { userDao.insertUser(user); } @Override public User load(Integer userKey) { return userDao.getUser(userKey); } " @Override public Map<Integer, User> loadAll(Collection<Integer> userKeys) { return userDao.loadAll(userKeys); } "
© 2014 Hazelcast Inc.
Integrating Hazelcast in 4 steps
Step 3 : Query against a Java Map !"
11
" @Override public Collection<User> getUserByLocation(String location){ return userMap.values(Predicates.equal("location", location)); } " @Override public User getUser(Integer userKey) { return userMap.get(userKey); } " @Override public void removeUser(Integer userKey) { userMap.evict(userKey); } " @Override public void addUsers(Map<Integer,User> users) { userMap.putAll(users); } ""
© 2014 Hazelcast Inc.
Integrating Hazelcast in 4 steps
Step 4 : Start your cluster !
Hazelcast will prime from Database using the loadAllKeys method from your MapStore.""No changes were required to the Database.
12
© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.
Hazelcast for faster reads
13
© 2014 Hazelcast Inc.
Hazelcast will read through to Database
• If you only load a subset of data from your whole set.
• Hazelcast will use the MapStore to read to the Database to cache a Map Entry on a Cache Miss.
14
© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.
Hazelcast read-through to DB
15
© 2014 Hazelcast Inc.
Hazelcast can make updates faster as well
• A Map put can be async to the Database.
• Write behind stores records and writes to DB after delay.
• The User doesn’t have to wait for the DB to complete, once entry hits cache the transaction is complete.
16
© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.© 2014 Hazelcast Inc.
Hazelcast write-behind to DB
17
© 2014 Hazelcast Inc.
Q&A
ANY QUESTIONS?
18
© 2014 Hazelcast Inc.