full stack reactive nosql: spring data and couchbase
TRANSCRIPT
Full Stack Reactive NoSQLSpring Data and Couchbase
Matt Ingenthron @ingenthr
Subhashni Balakrishnan @subalakr
2
AGENDA01/ Reactive Programming
02/Building a Reactive Repository with Spring
Framework 5
03/ Let's do it Live
04/ Next Steps
Reactive Programming
3
1
Motivation for Reactive
4
Resilient
• System stays responsive even when failures occur
• Data is replicated, failures are contained within each
component
• Component client abstracted from failures
Message Driven
• System uses asynchronous message passing
• Applies back-pressure to degrade gracefully
• Communication is non-blocking to only consume
resources while active, lowering overhead
Responsive
• System responds quickly
• Allows for more engaging experiences for users
• Has consistent response times for consistent quality of
service
Elastic
• System stays responsive under varying workload
• System allows addition of resources without interruption
of existing workload
© Couchbase 2017. All rights reserved.
Image courtesy
reactivemanifesto.org
Async Programming Without Reactive
6© Couchbase 2017. All rights reserved.
client.asyncGet("posts::1").addListener(future -> {if (future.isDone() && !future.isCancelled()) {
String content = (String) future.get();List<String> ids =
comments_ids_from_json(content);
int i = 0;for (String id : ids) {
if (i++ == 5) {break;
}client.asyncGet(id).addListener(future1 -> {
if (future1.isDone() && !future1.isCancelled()) {System.out.println(future1.get());
}});
}}
});
Reactive Asynchronous Programming
7© Couchbase 2017. All rights reserved.
bucket.get("posts::1").map(document -> document.content().getArray("comments")).flatMap(comments -> Observable.from(comments.toList())).take(5).filter(o -> o instanceof String).flatMap(o -> bucket.get((String) o)).subscribe(doc -> System.out.println(doc.content()));
Modern Reactive Internals
8© Couchbase 2017. All rights reserved.
Flux<Person> findByFirstname(St ring fi rstname);
Java SDK
Spring Data Couchbase
Java SDK (core-io)
Request/ Response Flow
Request
RingBuf erCouchbaseCore
(1)
publishes
Observable
(2) returns
(3a) maybe
fails fast
RequestHandler
(3b)
not ifies
Node
Service
Endpoint
(4)
locates
(5)
selects
(6)
selects
Net tyChannel
s
Net tyChannel
s
Net ty
Channels
Net ty
Channels
Net tyChannel
s
Net tyChannel
s
Net tyChannel
s
(7)
w rit es int o
(8)
sends t o CB
(9)
reads from CB
(10 a)
complet es
(10 b)
fails
Application
Method Calls
Couchbase is End to End Reactive
9© Couchbase 2017. All rights reserved.
Couchbase Server
Couchbase IO EngineJava SDK (core-io)
Request/ Response Flow
Request
RingBuf erCouchbaseCore
(1)
publishes
Observable
(2) returns
(3a) maybe
fails fast
RequestHandler
(3b)
not ifies
Node
Service
Endpoint
(4)
locates
(5)
selects
(6)
selects
Net tyChannel
s
Net tyChannel
s
Net tyChannel
s
Net ty
Channels
Net tyChannel
s
Net tyChannel
s
Net tyChannel
s
(7)
w rit es int o
(8)
pipelines
requests
(9)
sends
responses
(10 a)
completes
(10 b)
fails
couchstore
Hashtable
mc:w orker <N>mc:w orker <N>mc:worker <N>mc:w orker <N>
(1)
request it em
from engine
memory cache
(7)
not ifies complet ion
(4a)
not ifies complet ion
(maybe)
ep:io
ep:nio
(4b)
requests
backfi ll
(5)
async IO request
(6)
IO complet ion
(2)
looks up it em
(3)
returns
metadata
Poll
© Couchbase 2017. All rights reserved.
Building a Reactive Repository with Spring Framework 5
11
2
Reactive Programming Model
12© Couchbase 2017. All rights reserved.
Publisher Subscriber
Reactive Composeable API
13
Reactor
Mono [0 | 1]
Flux [N]
RxJava
Observable [N]
© Couchbase 2017. All rights reserved.
Reactive Couchbase Client
14© Couchbase 2017. All rights reserved.
Reactive Template
15© Couchbase 2017. All rights reserved.
Reactive Repository
16© Couchbase 2017. All rights reserved.
Full Stack Reactive Web Application - Components
17
• http layer using Spring web flux
• Data access using Spring Data Couchbase
© Couchbase 2017. All rights reserved.
Reactive Flow
18© Couchbase 2017. All rights reserved.
Let's Do It Live
19
3
Activity Tracker Application
20© Couchbase 2017. All rights reserved.
Couchbase Cluster
Couchbase Node
Browser
Spring Boot App
Spring Boot
Spring Framework 5
Spring Data 2.0 (Kay)
Spring Data Couchbase 3.0
act ivityTracker
Bucket
KV
Se
rvic
e
Qu
ery
Se
rvic
e
Oth
er
Se
rvic
es
Couchbase Node
act ivityTracker
Bucket
KV
Se
rvic
e
Qu
ery
Se
rvic
e
Oth
er
Se
rvic
es
Sp
rin
g D
ata
Co
uc
hb
ase
/ leader
/ leaderSt ream
Spring
@GetMapping()
Summary, Next Steps
21
4
Summary
22
Today's Demo
https://github.com/subalakr/springdatareactive
Spring Data Couchbase
http://projects.spring.io/spring-data-couchbase/
Try Spring Data Kay
http://projects.spring.io/spring-data/#quick-start
(select Kay-M4 from the pulldown)
Spring Data on GitHub
https://github.com/spring-projects/spring-data-
couchbase/
• Awesomeness with reactive programming in
Spring Framework 5 using the Couchbase
reactive core
• Reactive template and repository introduced in
Spring data Couchbase Kay
• End-to-end reactive web application for activity
tracking
• Spring data Couchbase is • Fully Java 8
• Supports other cool features:• DTO projections
• Java 8 time support
• Auto-generation of IDs
• Kotlin support is in the works
© Couchbase 2017. All rights reserved.
Now is the
Time for Your
Feedback
Try the Spring Data
Milestone Build with
Spring Data Couchbase
http://projects.spring.io/spring-
data/#quick-start (Select Kay-M4)
Attend Couchbase Connect!
http://connect.couchbase.com
Europe– Paris
26 September 2017
Silicon Valley
Fall 2017
23Couchbase 2017. All rights reserved.
Thank you
©2017 Couchbase. All rights reserved. 24