SlideShare a Scribd company logo
A memcached
implementation in Java


Bela Ban
JBoss
2340
2


AGENDA

>   Introduction
>   memcached
>   memcached in Java
>   Improving memcached
>   Infinispan
>   Demo
3


Introduction

> We want to store all of our data in memory
  – “Memory is the new disk, disk is the new tape” (Tim Bray)
  – Reducing DB access speeds up the application
               A network round trip is at least an order of magnitude
                   faster than accessing the disk
               The DB becomes the bottleneck
> However, we'll likely exceed the memory of a single host
> Therefore, we spread our data across multiple hosts
> The aggregated memories of all hosts serves as our (large virtual) disk
  – Given enough data redundancy, we can temporarily afford to lose
     hosts due to crashes, updates etc
4


What is memcached ?

> Server (daemon) process written in C, providing an in-memory hashmap
> Clients can be written in any language
  – They talk to memcached servers via the memcached protocol
> Simple API: put(key,val), get(key), remove(key)
> A key always maps to the same memcached server (consistent hashing)
> Clients pick the right memcached server based on consistentHash(K)
> Given a good consistent hash function, keys are evenly distributed
  across memcached servers
> Typical use cases
  – PUT(K, V): write K,V to DB and put K,V into the cache
  – GET(K): get from cache, return V if found. Else fetch from DB, insert
      into cache and then return V
5


memcached        JBoss
                 Servlet                         memcached A



        Apache   JBoss
 HTTP            Servlet                         memcached B
        mod_jk              memcached protocol


                 JBoss
                 Servlet                         memcached C




                           DB
6


Issues with memcached

> Client have to use the memcached protocol to talk to a server
  – Protocol is ASCII over HTTP, inefficient, parsing of headers
> No L1 cache (some memcached clients cache locally though)
> memcached servers don't know about each other
> When a memcached server leaves, or a new server is started, some
  keys may now map to a different server ... which doesn't have them →
  DB lookup
> Keys are not redundantly stored on multiple servers
  – Server crash (or graceful shutdown) causes keys not to be found →
      DB lookup
  – On shutdown of a server, keys are not relocated to a different server
7


memcached in Java

> L1 caches
  – Clients run in the same address space as ReplCache
  – Access to data is fast when in L1 cache, no marshalling overhead
> All ReplCaches know about each other (in the same cluster)
  – Data can be migrated when shutting down
  – On rehashing, data is rebalanced automatically
                Avoids a DB access
> Fast binary protocol (JGroups)
  – Can be adapted (compression, encryption, batching) via XML config
> Fewer network round trips
8


 ReplCache: memcached improved
> memcached has no redundancy
  – When a server crashes, the keys on that server need to be re-read
     from the DB
> With ReplCache, every key has a replication count
  – Defines how many times the key should be stored in the cluster
  – -1: store key on every server
  – N: store key on N servers
                Example: put(key,val, 3) stores {key,val} on 3 servers
                When one of the 3 servers crashes, {key,val} will be
                  copied to another server
> Advantage:
  – We can define redundancy per data element !
  – This allows us to use more of the available aggregated memory
9


ReplCache
                  JBoss          ReplCache

                  Servlet


         Apache   JBoss          ReplCache
                                             Cluster
  HTTP            Servlet
         mod_jk

                  JBoss          ReplCache

                  Servlet




                            DB
10


ReplCache: memcached improved
>   Data that can be easily re-read from the DB might use repl-count=1
>   Data that isn't available in the DB needs to have a repl-count of -1
>   Data where we can tolerate 3 concurrent crashes has repl-count=3
>   Think of ReplCache as Dynamic RAID
11




Demo
12


 Infinispan
> Open Source Data Grid Platform
> Simple JSR-107 interface
  – Cache extends Map
> Content distribution based on Consistent Hashing
  – Ability to address massive heap
  – 100 nodes with 2GB each, DIST configured with 1 copy => 100GB
     addressable space from anywhere!
> JTA compliant, support for persistence to cache stores, eviction to
  prevent OOMs
> JMX reporting, (upcoming) mgmt console, migration tools
> Extremely high performance core container to support high concurrency
  – State of the art algorithms with minimal use of mutual exclusion
13


 Infinispan
> memcached server module
  – Coming soon in Infinispan
> Speaks both memcached text protocol + custom binary protocol
> Able to use any memcached client
  – Not just Java! C#, C++, Python, PHP
> Custom Java client with ability to load-balance/fail-over
  – Makes use of binary protocol
  – We expect more clients like this for other systems (C++, etc) to be
     contributed
> Drop-in replacement for memcached farms
  – With all the added benefits of Infinispan
14


 Infinispan
> Cool new AsyncAPI
  – Future<V> putAsync(K key, V value)
  – Allows you to fire off a number of such operations which operate in
     parallel
               Operations can be synchronous
  – And wait for responses using Future.get()
> Other upcoming features include
  – Querying API
  – JPA-style interface for “POJO Caching”
  – Distributed executors
               Runnables moved to the data, not the other way around
               Map/reduce on your data!
15


 Outlook
> Implementation of file system (java.io) interfaces to use the grid as a
  large in-memory file system
> JDBC: use the grid as data store rather than the disk ?
> Hibernate on a grid
16


 Links
> memcached: http://www.danga.com/memcached
> ReplCache: http://www.jgroups.org/memcached.html and
  http://www.jgroups.org/replcache.html
> ReplCache WebStart demo: http://www.jgroups.org/jnlp/replcache.jnlp
> ReplCache Flash demo: http://www.jgroups.org/movies/ReplCache.swf
> Infinispan: http://www.infinispan.org
Bela Ban          http://www.jboss.com
JBoss / Red Hat   bela@jboss.com

More Related Content

PDF
plProxy, pgBouncer, pgBalancer
PDF
Connection Pooling in PostgreSQL using pgbouncer
KEY
Introduction to memcached
PDF
Memcached Study
PDF
GOTO 2013: Why Zalando trusts in PostgreSQL
PDF
Streaming replication in practice
PDF
Give Your Site a Boost with Memcache
ODP
Shootout at the PAAS Corral
plProxy, pgBouncer, pgBalancer
Connection Pooling in PostgreSQL using pgbouncer
Introduction to memcached
Memcached Study
GOTO 2013: Why Zalando trusts in PostgreSQL
Streaming replication in practice
Give Your Site a Boost with Memcache
Shootout at the PAAS Corral

What's hot (20)

PPTX
Apache Performance Tuning: Scaling Up
PPTX
Memcached B box presentation
PDF
Administration and Management with UltraESB
PPTX
PDF
Introduction to Redis
PDF
NginX - good practices, tips and advanced techniques
PDF
Haproxy - zastosowania
PDF
HBase 2.0 cluster topology
PDF
MySQL Oslayer performace optimization
PDF
UltraESB - Installation and Configuration
PDF
Using advanced options in MariaDB Connector/J
PDF
Webinar Slides : Migrating to MySQL, MariaDB Galera and/or Percona XtraDB Clu...
PDF
Clug 2012 March web server optimisation
PDF
Content Caching with NGINX and NGINX Plus
PDF
ReplacingSquidWithATS
PDF
Building low latency java applications with ehcache
PDF
Implementing High Availability Caching with Memcached
PPTX
MySQL Replication Overview -- PHPTek 2016
PDF
Elephant Roads: a tour of Postgres forks
PDF
7 Ways To Crash Postgres
Apache Performance Tuning: Scaling Up
Memcached B box presentation
Administration and Management with UltraESB
Introduction to Redis
NginX - good practices, tips and advanced techniques
Haproxy - zastosowania
HBase 2.0 cluster topology
MySQL Oslayer performace optimization
UltraESB - Installation and Configuration
Using advanced options in MariaDB Connector/J
Webinar Slides : Migrating to MySQL, MariaDB Galera and/or Percona XtraDB Clu...
Clug 2012 March web server optimisation
Content Caching with NGINX and NGINX Plus
ReplacingSquidWithATS
Building low latency java applications with ehcache
Implementing High Availability Caching with Memcached
MySQL Replication Overview -- PHPTek 2016
Elephant Roads: a tour of Postgres forks
7 Ways To Crash Postgres
Ad

Similar to A memcached implementation in Java (20)

PPTX
Jug Lugano - Scale over the limits
PDF
Introduction to Galera Cluster
PPTX
openSUSE storage workshop 2016
PDF
vSphere APIs for performance monitoring
PDF
EVCache & Moneta (GoSF)
PDF
Caching methodology and strategies
PDF
Caching Methodology & Strategies
PDF
Membase Meetup Chicago - january 2011
PDF
Kfs presentation
PDF
OpenDS_Jazoon2010
PPTX
Designing enterprise drupal
ODP
Caching and tuning fun for high scalability
ODP
Experience In Building Scalable Web Sites Through Infrastructure's View
ODP
MySQL 5.7 clustering: The developer perspective
ODP
phptek13 - Caching and tuning fun tutorial
PDF
High Performance Drupal Sites
ODP
Caching and tuning fun for high scalability @ FrOSCon 2011
PDF
Learning from ZFS to Scale Storage on and under Containers
PPTX
Redis Clustering Advanced___31Mar2025.pptx
ODP
Clug 2011 March web server optimisation
Jug Lugano - Scale over the limits
Introduction to Galera Cluster
openSUSE storage workshop 2016
vSphere APIs for performance monitoring
EVCache & Moneta (GoSF)
Caching methodology and strategies
Caching Methodology & Strategies
Membase Meetup Chicago - january 2011
Kfs presentation
OpenDS_Jazoon2010
Designing enterprise drupal
Caching and tuning fun for high scalability
Experience In Building Scalable Web Sites Through Infrastructure's View
MySQL 5.7 clustering: The developer perspective
phptek13 - Caching and tuning fun tutorial
High Performance Drupal Sites
Caching and tuning fun for high scalability @ FrOSCon 2011
Learning from ZFS to Scale Storage on and under Containers
Redis Clustering Advanced___31Mar2025.pptx
Clug 2011 March web server optimisation
Ad

More from elliando dias (20)

PDF
Clojurescript slides
PDF
Why you should be excited about ClojureScript
PDF
Functional Programming with Immutable Data Structures
PPT
Nomenclatura e peças de container
PDF
Geometria Projetiva
PDF
Polyglot and Poly-paradigm Programming for Better Agility
PDF
Javascript Libraries
PDF
How to Make an Eight Bit Computer and Save the World!
PDF
Ragel talk
PDF
A Practical Guide to Connecting Hardware to the Web
PDF
Introdução ao Arduino
PDF
Minicurso arduino
PDF
Incanter Data Sorcery
PDF
PDF
Fab.in.a.box - Fab Academy: Machine Design
PDF
The Digital Revolution: Machines that makes
PDF
Hadoop + Clojure
PDF
Hadoop - Simple. Scalable.
PDF
Hadoop and Hive Development at Facebook
PDF
Multi-core Parallelization in Clojure - a Case Study
Clojurescript slides
Why you should be excited about ClojureScript
Functional Programming with Immutable Data Structures
Nomenclatura e peças de container
Geometria Projetiva
Polyglot and Poly-paradigm Programming for Better Agility
Javascript Libraries
How to Make an Eight Bit Computer and Save the World!
Ragel talk
A Practical Guide to Connecting Hardware to the Web
Introdução ao Arduino
Minicurso arduino
Incanter Data Sorcery
Fab.in.a.box - Fab Academy: Machine Design
The Digital Revolution: Machines that makes
Hadoop + Clojure
Hadoop - Simple. Scalable.
Hadoop and Hive Development at Facebook
Multi-core Parallelization in Clojure - a Case Study

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
KodekX | Application Modernization Development
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
Teaching material agriculture food technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Weekly Chronicles - August'25 Week I
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Dropbox Q2 2025 Financial Results & Investor Presentation
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KodekX | Application Modernization Development
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Teaching material agriculture food technology
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
A Presentation on Artificial Intelligence
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf

A memcached implementation in Java

  • 1. A memcached implementation in Java Bela Ban JBoss 2340
  • 2. 2 AGENDA > Introduction > memcached > memcached in Java > Improving memcached > Infinispan > Demo
  • 3. 3 Introduction > We want to store all of our data in memory – “Memory is the new disk, disk is the new tape” (Tim Bray) – Reducing DB access speeds up the application  A network round trip is at least an order of magnitude faster than accessing the disk  The DB becomes the bottleneck > However, we'll likely exceed the memory of a single host > Therefore, we spread our data across multiple hosts > The aggregated memories of all hosts serves as our (large virtual) disk – Given enough data redundancy, we can temporarily afford to lose hosts due to crashes, updates etc
  • 4. 4 What is memcached ? > Server (daemon) process written in C, providing an in-memory hashmap > Clients can be written in any language – They talk to memcached servers via the memcached protocol > Simple API: put(key,val), get(key), remove(key) > A key always maps to the same memcached server (consistent hashing) > Clients pick the right memcached server based on consistentHash(K) > Given a good consistent hash function, keys are evenly distributed across memcached servers > Typical use cases – PUT(K, V): write K,V to DB and put K,V into the cache – GET(K): get from cache, return V if found. Else fetch from DB, insert into cache and then return V
  • 5. 5 memcached JBoss Servlet memcached A Apache JBoss HTTP Servlet memcached B mod_jk memcached protocol JBoss Servlet memcached C DB
  • 6. 6 Issues with memcached > Client have to use the memcached protocol to talk to a server – Protocol is ASCII over HTTP, inefficient, parsing of headers > No L1 cache (some memcached clients cache locally though) > memcached servers don't know about each other > When a memcached server leaves, or a new server is started, some keys may now map to a different server ... which doesn't have them → DB lookup > Keys are not redundantly stored on multiple servers – Server crash (or graceful shutdown) causes keys not to be found → DB lookup – On shutdown of a server, keys are not relocated to a different server
  • 7. 7 memcached in Java > L1 caches – Clients run in the same address space as ReplCache – Access to data is fast when in L1 cache, no marshalling overhead > All ReplCaches know about each other (in the same cluster) – Data can be migrated when shutting down – On rehashing, data is rebalanced automatically  Avoids a DB access > Fast binary protocol (JGroups) – Can be adapted (compression, encryption, batching) via XML config > Fewer network round trips
  • 8. 8 ReplCache: memcached improved > memcached has no redundancy – When a server crashes, the keys on that server need to be re-read from the DB > With ReplCache, every key has a replication count – Defines how many times the key should be stored in the cluster – -1: store key on every server – N: store key on N servers  Example: put(key,val, 3) stores {key,val} on 3 servers  When one of the 3 servers crashes, {key,val} will be copied to another server > Advantage: – We can define redundancy per data element ! – This allows us to use more of the available aggregated memory
  • 9. 9 ReplCache JBoss ReplCache Servlet Apache JBoss ReplCache Cluster HTTP Servlet mod_jk JBoss ReplCache Servlet DB
  • 10. 10 ReplCache: memcached improved > Data that can be easily re-read from the DB might use repl-count=1 > Data that isn't available in the DB needs to have a repl-count of -1 > Data where we can tolerate 3 concurrent crashes has repl-count=3 > Think of ReplCache as Dynamic RAID
  • 12. 12 Infinispan > Open Source Data Grid Platform > Simple JSR-107 interface – Cache extends Map > Content distribution based on Consistent Hashing – Ability to address massive heap – 100 nodes with 2GB each, DIST configured with 1 copy => 100GB addressable space from anywhere! > JTA compliant, support for persistence to cache stores, eviction to prevent OOMs > JMX reporting, (upcoming) mgmt console, migration tools > Extremely high performance core container to support high concurrency – State of the art algorithms with minimal use of mutual exclusion
  • 13. 13 Infinispan > memcached server module – Coming soon in Infinispan > Speaks both memcached text protocol + custom binary protocol > Able to use any memcached client – Not just Java! C#, C++, Python, PHP > Custom Java client with ability to load-balance/fail-over – Makes use of binary protocol – We expect more clients like this for other systems (C++, etc) to be contributed > Drop-in replacement for memcached farms – With all the added benefits of Infinispan
  • 14. 14 Infinispan > Cool new AsyncAPI – Future<V> putAsync(K key, V value) – Allows you to fire off a number of such operations which operate in parallel  Operations can be synchronous – And wait for responses using Future.get() > Other upcoming features include – Querying API – JPA-style interface for “POJO Caching” – Distributed executors  Runnables moved to the data, not the other way around  Map/reduce on your data!
  • 15. 15 Outlook > Implementation of file system (java.io) interfaces to use the grid as a large in-memory file system > JDBC: use the grid as data store rather than the disk ? > Hibernate on a grid
  • 16. 16 Links > memcached: http://www.danga.com/memcached > ReplCache: http://www.jgroups.org/memcached.html and http://www.jgroups.org/replcache.html > ReplCache WebStart demo: http://www.jgroups.org/jnlp/replcache.jnlp > ReplCache Flash demo: http://www.jgroups.org/movies/ReplCache.swf > Infinispan: http://www.infinispan.org
  • 17. Bela Ban http://www.jboss.com JBoss / Red Hat bela@jboss.com