Easy scaling with
distributed data
structures
Talip Ozturk
@oztalip
Keywords
In-memory data grid
Distributed(Elastic) Cache
NoSQL
Clustering, Scalability, Partitioning
Cloud Computing
Map
import java.util.Map;
import java.util.HashMap;
Map map = new HashMap();
map.put(“1”, “value”);
map.get(“1”);
Concurrent Map
import java.util.Map;
import java.util.concurrent.*;
Map map = new ConcurrentHashMap();
map.put(“1”, “value...
Distributed Map
import java.util.Map;
import com.hazelcast.core.Hazelcast;
Map map = Hazelcast.getMap(“mymap”);
map.put(“1...
HAZELCAST
Art of Data Distribution
open source, in-memory data grid
Talip Ozturk
@oztalip
Who uses Hazelcast?
Financials
Telco
Gaming
e-commerce
Every sec. one Hazelcast node starts around the
globe
9th Fastest Growing Skill
Why Hazelcast?
To build highly
available and
scalable applications
Alternatives
Oracle Coherence
IBM Extreme Scale
VMware Gemfire
Gigaspaces
Redhat Infinispan
Gridgain
Terracotta
Difference
License/Cost
Feature-set
API
Ease of use
Main focus (distributed map, tuple space,
cache, processing vs. data)
...
HAZELCAST
Apache License
1.7 MB jar
Lightweight without any dependency
Introducing Hazelcast
Map, queue, set, list, lock, semaphore, topic and
executor service
Native Java, C#, REST and Memcach...
Use-cases
Scale your application
Share data across cluster
Partition your data
Send/receive messages
Balance the load
Proc...
Where is the Data?
Data Partitioning in a Cluster
Fixed number of partitions (default 271)
Each key falls into a partition
partitionId = hash...
New Node Added
DA B C
Migration
DA B C
Migration
DA B C
Migration
DA B C
Migration
DA B C
Migration
DA B C
Migration
DA B C
Migration Complete
DA B C
Crash
Node Crashes
DA B C
Crash
Restoring Backups
DA B C
Crash
Restoring Backups
DA B C
Crash
Restoring Backups
DA B C
Crash
Restoring Backups
DA B C
Crash
Restoring Data from Backup
DA B C
Crash
Data is Recovered from Backup
DA B C
Crash
Backup for Recovered Data
DA B C
Crash
Backup for Recovered Data
DA B C
Crash
Backup for Recovered Data
DA B C
Crash
All Safe
DA C
Node Types
Topology
Native Client:
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
Lite Member:
-Dhazelc...
Hazelcast
Enterprise
Community vs Enterprise
Enterprise =
Community +
Elastic Memory + Security + Man. Center
Elastic Memory is OFF-HEAP storage
<hazelcast>
...
<map name="default">
...
<storage-type>OFFHEAP</storage-type>
</map>
</...
JAAS based Security
Credentials
Cluster Login Module
Cluster Member Security
Native Client Security
Authentication
Authori...
Code Samples
Hazelcast
Hazelcast is thread safe
Many instances on the same JVM
Config config = new Config();
HazelcastInstance h1 = Haz...
Cluster Interface
import com.hazelcast.core.*;
import java.util.Set;
Cluster cluster = Hazelcast.getCluster();
cluster.add...
Distributed Map
import com.hazelcast.core.*;
import java.util.ConcurrentMap;
Map<String, Customer> map = Hazelcast.getMap(...
Distributed Queue
import com.hazelcast.core.Hazelcast;
import java.util.concurrent.BlockingQueue;
import java.util.concurr...
Distributed Lock
import com.hazelcast.core.Hazelcast;
import java.util.concurrent.locks.Lock;
Lock mylock = Hazelcast.getL...
Distributed Topic
import com.hazelcast.core.*;
public class Sample implements MessageListener {
public static void main(St...
Distributed Events
import com.hazelcast.core.*;
public class Sample implements EntryListener {
public static void main(Str...
Executor Service
Hello Task
A simple task
Execute on any member
ExecutorService ex = Hazelcast.getExecutorService();
Future<String> future ...
Hazelcast can execute a task ...
ExecutorService executor = Hazelcast.getExecutorService();
FutureTask<String> task1, task...
Executor Service Scenario
public int removeOrder(long customerId, long orderId){
IMap<Long, Customer> map = Hazelcast.getM...
Add Bonus Task
public class OrderDeletionTask implements Callable<Integer>, PartitionAware, Serializable {
private long cu...
Send computation over data
public static int removeOrder(long customerId, long orderId){
ExecutorService es = Hazelcast.ge...
Query
Code Samples – Query
public class Customer {
private boolean active;
private String name;
private int age;
// getters
// s...
Code Samples – Query
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.IMap;
import com.hazelcast.query.SqlPr...
Persistence
import com.hazelcast.core.MapStore,
import com.hazelcast.core.MapLoader,
public class MyMapStore implements MapStore, MapL...
Write-Behind : asynchronously storing entries
Write-Through : synchronous
Read-Through : if get(key) is null, load it
Pers...
Recap
• Distributed implementation of
• Map
• Queue
• Set
• List
• MultiMap
• Lock
• Executor Service
• Topic
• Semaphore
...
Q & A
Thank you!!!
@oztalip
Upcoming SlideShare
Loading in …5
×

Easy Scaling with Open Source Data Structures, by Talip Ozturk

2,003
-1

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,003
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
46
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Easy Scaling with Open Source Data Structures, by Talip Ozturk

  1. 1. Easy scaling with distributed data structures Talip Ozturk @oztalip
  2. 2. Keywords In-memory data grid Distributed(Elastic) Cache NoSQL Clustering, Scalability, Partitioning Cloud Computing
  3. 3. Map import java.util.Map; import java.util.HashMap; Map map = new HashMap(); map.put(“1”, “value”); map.get(“1”);
  4. 4. Concurrent Map import java.util.Map; import java.util.concurrent.*; Map map = new ConcurrentHashMap(); map.put(“1”, “value”); map.get(“1”);
  5. 5. Distributed Map import java.util.Map; import com.hazelcast.core.Hazelcast; Map map = Hazelcast.getMap(“mymap”); map.put(“1”, “value”); map.get(“1”);
  6. 6. HAZELCAST Art of Data Distribution open source, in-memory data grid Talip Ozturk @oztalip
  7. 7. Who uses Hazelcast? Financials Telco Gaming e-commerce Every sec. one Hazelcast node starts around the globe
  8. 8. 9th Fastest Growing Skill
  9. 9. Why Hazelcast? To build highly available and scalable applications
  10. 10. Alternatives Oracle Coherence IBM Extreme Scale VMware Gemfire Gigaspaces Redhat Infinispan Gridgain Terracotta
  11. 11. Difference License/Cost Feature-set API Ease of use Main focus (distributed map, tuple space, cache, processing vs. data) Light/Heavy weight
  12. 12. HAZELCAST
  13. 13. Apache License
  14. 14. 1.7 MB jar Lightweight without any dependency
  15. 15. Introducing Hazelcast Map, queue, set, list, lock, semaphore, topic and executor service Native Java, C#, REST and Memcache Interfaces Cluster info and membership events Dynamic clustering, backup, fail-over Transactional and secure
  16. 16. Use-cases Scale your application Share data across cluster Partition your data Send/receive messages Balance the load Process in parallel on many JVM
  17. 17. Where is the Data?
  18. 18. Data Partitioning in a Cluster Fixed number of partitions (default 271) Each key falls into a partition partitionId = hash(keyData)%PARTITION_COUNT Partition ownerships are reassigned upon membership change A B C
  19. 19. New Node Added DA B C
  20. 20. Migration DA B C
  21. 21. Migration DA B C
  22. 22. Migration DA B C
  23. 23. Migration DA B C
  24. 24. Migration DA B C
  25. 25. Migration DA B C
  26. 26. Migration Complete DA B C Crash
  27. 27. Node Crashes DA B C Crash
  28. 28. Restoring Backups DA B C Crash
  29. 29. Restoring Backups DA B C Crash
  30. 30. Restoring Backups DA B C Crash
  31. 31. Restoring Backups DA B C Crash
  32. 32. Restoring Data from Backup DA B C Crash
  33. 33. Data is Recovered from Backup DA B C Crash
  34. 34. Backup for Recovered Data DA B C Crash
  35. 35. Backup for Recovered Data DA B C Crash
  36. 36. Backup for Recovered Data DA B C Crash
  37. 37. All Safe DA C
  38. 38. Node Types
  39. 39. Topology Native Client: HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); Lite Member: -Dhazelcast.lite.member=true
  40. 40. Hazelcast Enterprise
  41. 41. Community vs Enterprise Enterprise = Community + Elastic Memory + Security + Man. Center
  42. 42. Elastic Memory is OFF-HEAP storage <hazelcast> ... <map name="default"> ... <storage-type>OFFHEAP</storage-type> </map> </hazelcast>
  43. 43. JAAS based Security Credentials Cluster Login Module Cluster Member Security Native Client Security Authentication Authorization Permission
  44. 44. Code Samples
  45. 45. Hazelcast Hazelcast is thread safe Many instances on the same JVM Config config = new Config(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config) HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config) All objects must be serializable class Employee implements java.io.Serializable //better class Employee implements com.hazelcast.nio.DataSerializable Map<String, Employee> = Hazelcast.getMap(“employees”); List<Users> = Hazelcast.getList(“users”);
  46. 46. Cluster Interface import com.hazelcast.core.*; import java.util.Set; Cluster cluster = Hazelcast.getCluster(); cluster.addMembershipListener(listener); Member localMember = cluster.getLocalMember(); System.out.println (localMember.getInetAddress()); Set<Member> setMembers = cluster.getMembers();
  47. 47. Distributed Map import com.hazelcast.core.*; import java.util.ConcurrentMap; Map<String, Customer> map = Hazelcast.getMap("customers"); map.put ("1", customer); Customer c = map.get("1"); //ConcurrentMap methods map.putIfAbsent ("2", new Customer(“Chuck Norris”)); map.replace ("3", new Customer(“Bruce Lee”));
  48. 48. Distributed Queue import com.hazelcast.core.Hazelcast; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; BlockingQueue<Task> queue = Hazelcast.getQueue(“tasks"); queue.offer(task); Task t = queue.poll(); //Timed blocking Operations queue.offer(task, 500, TimeUnit.MILLISECONDS) Task t = queue.poll(5, TimeUnit.SECONDS); //Indefinitely blocking Operations queue.put(task) Task t = queue.take();
  49. 49. Distributed Lock import com.hazelcast.core.Hazelcast; import java.util.concurrent.locks.Lock; Lock mylock = Hazelcast.getLock(mylockobject); mylock.lock(); try { // do something } finally { mylock.unlock(); } //Lock on Map IMap<String, Customer> map = Hazelcast.getMap("customers"); map.lock("1"); try { // do something } finally { map.unlock("1"); }
  50. 50. Distributed Topic import com.hazelcast.core.*; public class Sample implements MessageListener { public static void main(String[] args) { Sample sample = new Sample(); ITopic<String> topic = Hazelcast.getTopic ("default"); topic.addMessageListener(sample); topic.publish ("my-message-object"); } public void onMessage(Object msg) { System.out.println("Got msg :" + msg); } }
  51. 51. Distributed Events import com.hazelcast.core.*; public class Sample implements EntryListener { public static void main(String[] args) { Sample sample = new Sample(); IMap map = Hazelcast.getMap ("default"); map.addEntryListener (sample, true); map.addEntryListener (sample, "key"); } public void entryAdded(EntryEvent event) { System.out.println("Added " + event.getKey() + ":" + event.getValue()); } public void entryRemoved(EntryEvent event) { System.out.println("Removed " + event.getKey() + ":" + event.getValue()); } public void entryUpdated(EntryEvent event) { System.out.println("Updated " + event.getKey() + ":" + event.getValue()); } }
  52. 52. Executor Service
  53. 53. Hello Task A simple task Execute on any member ExecutorService ex = Hazelcast.getExecutorService(); Future<String> future = executor.submit(new HelloTask()); // ... String result = future.get(); Attach a callback DistributedTask task = ... task.setExecutionCallback(new ExecutionCallback() { public void done(Future f) { // Get notified when the task is done! } }); public class HelloTask implements Callable<String>, Serializable{ @Override public String call(){ Cluster cluster = Hazelcast.getCluster(); return “Hello from ” + cluster.getLocalMember(); }
  54. 54. Hazelcast can execute a task ... ExecutorService executor = Hazelcast.getExecutorService(); FutureTask<String> task1, task2; // CASE 1: Run task on a specific member. Member member = ... task1 = new DistributedTask<String>(new HelloTask(), member); executor.execute(task1); // CASE 2: Run task on a member owning the key. Member member = ... task1 = new DistributedTask<String>(new HelloTask(), “key”); executor.execute(task1); // CASE 3: Run task on group of members. Set<Member> members = ... task = new MultiTask<String>(new HelloTask(), members); executor.execute(task2); 1. On a specific node 2. On any available node 3. On a collection of defined nodes 4. On a node owning a key
  55. 55. Executor Service Scenario public int removeOrder(long customerId, long orderId){ IMap<Long, Customer> map = Hazelcast.getMap("customers"); map.lock(customerId); Customer customer = map.get(customerId); customer.removeOrder (orderId); map.put(customerId, customer); map.unlock(customerId); return customer.getOrderCount(); }
  56. 56. Add Bonus Task public class OrderDeletionTask implements Callable<Integer>, PartitionAware, Serializable { private long customerId; private long orderId; public OrderDeletionTask(long customerId, long orderId) { super(); this.customerId = customerId; this.orderId = orderId; } public Integer call () { IMap<Long, Customer> map = Hazelcast.getMap("customers"); map.lock(customerId); customer customer = map. get(customerId); customer.removeOrder (orderId); map.put(customerId, customer); map.unlock(customerId); return customer.getOrderCount(); } public Object getPartitionKey() { return customerId; } }
  57. 57. Send computation over data public static int removeOrder(long customerId, long orderId){ ExecutorService es = Hazelcast.getExecutorService(); OrderDeletionTask task = new OrderDeletionTask(customerId, orderId); Future future = es.submit(task); int remainingOrders = future.get(); return remainingOrders; }
  58. 58. Query
  59. 59. Code Samples – Query public class Customer { private boolean active; private String name; private int age; // getters // setters }
  60. 60. Code Samples – Query import com.hazelcast.core.Hazelcast; import com.hazelcast.core.IMap; import com.hazelcast.query.SqlPredicate; import java.util.Collection; IMap map = Hazelcast.getMap(“customers”); map.addIndex(“active” ,false); map.addIndex(“name” ,false); map.addIndex(“age” ,true); Collection<Customer> customers = map.values(new SqlPredicate(“active AND age <= 30”));
  61. 61. Persistence
  62. 62. import com.hazelcast.core.MapStore, import com.hazelcast.core.MapLoader, public class MyMapStore implements MapStore, MapLoader { public Set loadAllKeys () { return readKeys(); } public Object load (Object key) { return readFromDatabase(key); } public void store (Object key, Object value) { saveIntoDatabase(key, value); } public void remove(Object key) { removeFromDatabase(key); } } Persistence
  63. 63. Write-Behind : asynchronously storing entries Write-Through : synchronous Read-Through : if get(key) is null, load it Persistence
  64. 64. Recap • Distributed implementation of • Map • Queue • Set • List • MultiMap • Lock • Executor Service • Topic • Semaphore • AtomicLong • CountDownLatch • Embedded, but accessible through • Native Java & C# Client • REST • Memcache • Dynamic • Cluster • Add/ remove nodes • Backup • Fail-over
  65. 65. Q & A Thank you!!! @oztalip

×