The document discusses using Redis to optimize queries in polyglot persistence applications by creating materialized views that denormalize and index data from a relational database into Redis for faster access. It provides an example of using Redis sorted sets to index available restaurant data from a MySQL database in a way that allows fast retrieval of open restaurants for a given zip code and time. The approach simplifies queries by eliminating joins and reducing data returned through concatenation and indexing techniques.
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
Developing polyglot persistence applications (SpringOne China 2012)
1. Developing polyglot persistence
applications
Chris Richardson,
Author of POJOs in Action, Founder of the original CloudFoundry.com
@crichardson chris.richardson@springsource.com http://plainoldobjects.com
2. Presentation goal
The benefits and drawbacks of polyglot
persistence
and
How to design applications that use this
approach
9. Agenda
• Why polyglot persistence?
• Using Redis as a cache
• Optimizing queries using Redis materialized views
• Synchronizing MySQL and Redis
• Tracking changes to entities
• Using a modular asynchronous architecture
10. Food to Go
• Take-out food delivery service
• “Launched” in 2006
11. Food To Go Architecture
RESTAURANT
CONSUMER
OWNER
Restaurant
Order taking
Management
MySQL
Database
12. Success Growth challenges
• Increasing traffic
• Increasing data volume
• Distribute across a few data centers
• Increasing domain model complexity
13. Limitations of relational databases
• Scalability
• Distribution
• Schema updates
• O/R impedance mismatch
• Handling semi-structured data
16. Example NoSQL Databases
Database Key features
Cassandra Extensible column store, very scalable, distributed
Neo4j Graph database
Document-oriented, fast, scalable
MongoDB
Redis Key-value store, very fast
http://nosql-database.org/ lists 122+ NoSQL databases
17. Redis
K1 V1
• Advanced key-value store
K2 V2
• Very fast, e.g. 100K reqs/sec
• Optional persistence ... ...
• Transactions with optimistic locking
• Master-slave replication
• Sharding using client-side consistent hashing
18. Sorted sets
Value
Key
a b
myset
5.0 10.0
Members are sorted Score
by score
19. Adding members to a sorted set
Redis Server
Key Score Value
a
zadd myset 5.0 a myset
5.0
20. Adding members to a sorted set
Redis Server
a b
zadd myset 10.0 b myset
5.0 10.0
21. Adding members to a sorted set
Redis Server
c a b
zadd myset 1.0 c myset
1.0 5.0 10.0
22. Retrieving members by index range
Start End
Key
Index Index Redis Server
zrange myset 0 1
c a b
myset
1.0 5.0 10.0
c a
23. Retrieving members by score
Min Max
Key
value value Redis Server
zrangebyscore myset 1 6
c a b
myset
1.0 5.0 10.0
c a
24. Redis use cases
• Replacement for Memcached • Handling tasks that overload an RDBMS
• Session state • Hit counts - INCR
• Cache of data retrieved from • Most recent N items - LPUSH and LTRIM
system of record (SOR)
• Randomly selecting an item –
• Replica of SOR for queries needing SRANDMEMBER
high-performance
• Queuing – Lists with LPOP, RPUSH, ….
• High score tables – Sorted sets and ZINCRBY
• …
25. Redis is great but there are tradeoffs
• Low-level query language: PK-based access only
• Limited transaction model:
• Read first and then execute updates as batch
• Difficult to compose code
• Data must fit in memory
• Single-threaded server: run multiple with client-side sharding
• Missing features such as access control, ...
27. The future is polyglot
e.g. Netflix
• RDBMS
• SimpleDB
• Cassandra
• Hadoop/Hbase
IEEE Software Sept/October 2010 - Debasish Ghosh / Twitter @debasishg
28. Agenda
• Why polyglot persistence?
• Using Redis as a cache
• Optimizing queries using Redis materialized views
• Synchronizing MySQL and Redis
• Tracking changes to entities
• Using a modular asynchronous architecture
29. Increase scalability by caching
RESTAURANT
CONSUMER
OWNER
Restaurant
Order taking
Management
MySQL
Cache
Database
30. Caching Options
• Where:
• Hibernate 2nd level cache
• Explicit calls from application code
• Caching aspect
• Cache technologies: Ehcache, Memcached, Infinispan, ...
Redis is also an option
31. Using Redis as a cache
• Spring 3.1 cache abstraction
• Annotations specify which methods to cache
• CacheManager - pluggable back-end cache
• Spring Data for Redis
• Simplifies the development of Redis applications
• Provides RedisTemplate (analogous to JdbcTemplate)
• Provides RedisCacheManager
32. Using Spring 3.1 Caching
@Service
public class RestaurantManagementServiceImpl implements RestaurantManagementService {
private final RestaurantRepository restaurantRepository;
@Autowired
public RestaurantManagementServiceImpl(RestaurantRepository restaurantRepository) {
this.restaurantRepository = restaurantRepository;
}
@Override
public void add(Restaurant restaurant) {
Cache result
restaurantRepository.add(restaurant);
}
@Override
@Cacheable(value = "Restaurant")
public Restaurant findById(int id) {
return restaurantRepository.findRestaurant(id);
Evict from
} cache
@Override
@CacheEvict(value = "Restaurant", key="#restaurant.id")
public void update(Restaurant restaurant) {
restaurantRepository.update(restaurant);
}
33. Configuring the Redis Cache Manager
Enables caching
<cache:annotation-driven />
<bean id="cacheManager"
class="org.springframework.data.redis.cache.RedisCacheManager" >
<constructor-arg ref="restaurantTemplate"/>
</bean>
Specifies CacheManager The RedisTemplate used to access
implementation Redis
37. Serializing a Restaurant as JSON
@Configuration
public class RestaurantManagementRedisConfiguration {
@Autowired
private RestaurantObjectMapperFactory restaurantObjectMapperFactory;
private JacksonJsonRedisSerializer<Restaurant> makeRestaurantJsonSerializer() {
JacksonJsonRedisSerializer<Restaurant> serializer =
new JacksonJsonRedisSerializer<Restaurant>(Restaurant.class);
...
return serializer;
}
@Bean
@Qualifier("Restaurant")
public RedisTemplate<String, Restaurant> restaurantTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Restaurant> template = new RedisTemplate<String, Restaurant>();
template.setConnectionFactory(factory);
JacksonJsonRedisSerializer<Restaurant> jsonSerializer = makeRestaurantJsonSerializer();
template.setValueSerializer(jsonSerializer);
return template;
}
} Serialize restaurants using Jackson JSON
38. Caching with Redis
CONSUMER RESTAURANT
OWNER
Restaurant
Order taking
Management
Redis MySQL
First Second
Cache Database
39. Agenda
• Why polyglot persistence?
• Using Redis as a cache
• Optimizing queries using Redis materialized views
• Synchronizing MySQL and Redis
• Tracking changes to entities
• Using a modular asynchronous architecture
40. Finding available restaurants
Available restaurants =
Serve the zip code of the delivery address
AND
Are open at the delivery time
public interface AvailableRestaurantRepository {
List<AvailableRestaurant>
findAvailableRestaurants(Address deliveryAddress, Date deliveryTime);
...
}
41. Food to Go – Domain model (partial)
class Restaurant { class TimeRange {
long id; long id;
String name; int dayOfWeek;
Set<String> serviceArea; int openTime;
Set<TimeRange> openingHours; int closeTime;
List<MenuItem> menuItems;
}
}
class MenuItem {
String name;
double price;
}
43. Finding available restaurants on Monday, 6.15pm for 94619 zipcode
Straightforward three-way join
select r.*
from restaurant r
inner join restaurant_time_range tr
on r.id =tr.restaurant_id
inner join restaurant_zipcode sa
on r.id = sa.restaurant_id
where ’94619’ = sa.zip_code
and tr.day_of_week=’monday’
and tr.openingtime <= 1815
and 1815 <= tr.closingtime
45. Option #1: Query caching
• [ZipCode, DeliveryTime] ⇨ list of available restaurants
BUT
• Long tail queries
• Update restaurant ⇨ Flush entire cache
Ineffective
46. Option #2: Master/Slave replication
Writes Consistent reads
Queries
(Inconsistent reads)
MySQL
Master
MySQL MySQL MySQL
Slave 1 Slave 2 Slave N
47. Master/Slave replication
• Mostly straightforward
BUT
• Assumes that SQL query is efficient
• Complexity of administration of slaves
• Doesn’t scale writes
48. Option #3: Redis materialized views
RESTAURANT
CONSUMER
OWNER
Restaurant
Order taking
Management
System
Copy update() of Record
findAvailable()
MySQL
Redis Cache
Database
49. BUT how to implement findAvailableRestaurants() with Redis?!
?
select r.*
from restaurant r K1 V1
inner join restaurant_time_range tr
on r.id =tr.restaurant_id
inner join restaurant_zipcode sa
on r.id = sa.restaurant_id
K2 V2
where ’94619’ = sa.zip_code
and tr.day_of_week=’monday’
and tr.openingtime <= 1815 ... ...
and 1815 <= tr.closingtime
50. Where we need to be
ZRANGEBYSCORE myset 1 6
=
sorted_set
select value,score key value score
from sorted_set
where key = ‘myset’
and score >= 1
and score <= 6
51. We need to denormalize
Think materialized view
52. Simplification #1: Denormalization
Restaurant_id Day_of_week Open_time Close_time Zip_code
1 Monday 1130 1430 94707
1 Monday 1130 1430 94619
1 Monday 1730 2130 94707
1 Monday 1730 2130 94619
2 Monday 0700 1430 94619
…
SELECT restaurant_id
FROM time_range_zip_code
Simpler query:
WHERE day_of_week = ‘Monday’
§ No joins
AND zip_code = 94619 § Two = and two <
AND 1815 < close_time
AND open_time < 1815
53. Simplification #2: Application filtering
SELECT restaurant_id, open_time
FROM time_range_zip_code
Even simpler query
WHERE day_of_week = ‘Monday’
• No joins
AND zip_code = 94619 • Two = and one <
AND 1815 < close_time
AND open_time < 1815
54. Simplification #3: Eliminate multiple =’s with concatenation
Restaurant_id Zip_dow Open_time Close_time
1 94707:Monday 1130 1430
1 94619:Monday 1130 1430
1 94707:Monday 1730 2130
1 94619:Monday 1730 2130
2 94619:Monday 0700 1430
…
SELECT restaurant_id, open_time
FROM time_range_zip_code
WHERE zip_code_day_of_week = ‘94619:Monday’
AND 1815 < close_time
key
range
56. Using a Redis sorted set as an index
zip_dow open_time_restaurant_id close_time
94707:Monday 1130_1 1430
94619:Monday 1130_1 1430
94707:Monday 1730_1 2130
94619:Monday 1730_1 2130
94619:Monday
94619:Monday 0700_2
0700_2 1430
1430
...
Key Sorted Set [ Entry:Score, …]
94619:Monday [0700_2:1430, 1130_1:1430, 1730_1:2130]
94707:Monday [1130_1:1430, 1730_1:2130]
57. Querying with ZRANGEBYSCORE
Key Sorted Set [ Entry:Score, …]
94619:Monday [0700_2:1430, 1130_1:1430, 1730_1:2130]
94707:Monday [1130_1:1430, 1730_1:2130]
Delivery zip and day Delivery time
ZRANGEBYSCORE 94619:Monday 1815 2359
è
{1730_1}
1730 is before 1815 è Ajanta is open
58. Adding a Restaurant
@Component
public class AvailableRestaurantRepositoryImpl implements AvailableRestaurantRepository {
@Override
public void add(Restaurant restaurant) { Store as
addRestaurantDetails(restaurant);
addAvailabilityIndexEntries(restaurant); JSON
}
private void addRestaurantDetails(Restaurant restaurant) { Text
restaurantTemplate.opsForValue().set(keyFormatter.key(restaurant.getId()), restaurant);
}
private void addAvailabilityIndexEntries(Restaurant restaurant) {
for (TimeRange tr : restaurant.getOpeningHours()) {
String indexValue = formatTrId(restaurant, tr); key member
int dayOfWeek = tr.getDayOfWeek();
int closingTime = tr.getClosingTime();
for (String zipCode : restaurant.getServiceArea()) {
redisTemplate.opsForZSet().add(closingTimesKey(zipCode, dayOfWeek), indexValue,
closingTime);
}
}
}
score
59. Finding available Restaurants
@Component
public class AvailableRestaurantRepositoryImpl implements AvailableRestaurantRepository {
@Override
public List<AvailableRestaurant>
findAvailableRestaurants(Address deliveryAddress, Date deliveryTime) { Find those that close
String zipCode = deliveryAddress.getZip(); after
int dayOfWeek = DateTimeUtil.dayOfWeek(deliveryTime);
int timeOfDay = DateTimeUtil.timeOfDay(deliveryTime);
String closingTimesKey = closingTimesKey(zipCode, dayOfWeek);
Set<String> trsClosingAfter =
redisTemplate.opsForZSet().rangeByScore(closingTimesKey, timeOfDay, 2359);
Set<String> restaurantIds = new HashSet<String>();
for (String tr : trsClosingAfter) { Filter out those that
String[] values = tr.split("_"); open after
if (Integer.parseInt(values[0]) <= timeOfDay)
restaurantIds.add(values[1]);
}
Collection<String> keys = keyFormatter.keys(restaurantIds);
return availableRestaurantTemplate.opsForValue().multiGet(keys); Retrieve open
} restaurants
61. Agenda
• Why polyglot persistence?
• Using Redis as a cache
• Optimizing queries using Redis materialized views
• Synchronizing MySQL and Redis
• Tracking changes to entities
• Using a modular asynchronous architecture
63. Two-Phase commit is not an option
• Redis does not support it
• Even if it did, 2PC is best avoided
http://www.infoq.com/articles/ebay-scalability-best-practices
64. Atomic
Consistent Basically Available
Isolated Soft state
Durable Eventually consistent
BASE: An Acid Alternative http://queue.acm.org/detail.cfm?id=1394128
65. Updating Redis #FAIL
begin MySQL transaction
update MySQL Redis has update
update Redis MySQL does not
rollback MySQL transaction
begin MySQL transaction
update MySQL
MySQL has update
commit MySQL transaction
Redis does not
<<system crashes>>
update Redis
66. Updating Redis reliably
Step 1 of 2
begin MySQL transaction
update MySQL
ACID
queue CRUD event in MySQL
commit transaction
Event Id
Operation: Create, Update, Delete
New entity state, e.g. JSON
67. Updating Redis reliably
Step 2 of 2
for each CRUD event in MySQL queue
get next CRUD event from MySQL queue
If CRUD event is not duplicate then
Update Redis (incl. eventId)
end if
begin MySQL transaction
mark CRUD event as processed
commit transaction
68. Step 1 Step 2
Timer
EntityCrudEvent EntityCrudEvent apply(event) Redis
Repository Processor Updater
INSERT INTO ... SELECT ... FROM ...
ENTITY_CRUD_EVENT
ID JSON processed?
Redis
69. Optimistic
locking Updating Redis
WATCH restaurant:lastSeenEventId:≪restaurantId≫
lastSeenEventId = GET restaurant:lastSeenEventId:≪restaurantId≫
Duplicate
if (lastSeenEventId >= eventId) return; detection
MULTI
SET restaurant:lastSeenEventId:≪restaurantId≫ eventId
Transaction
... update the restaurant data...
EXEC
70. Agenda
• Why polyglot persistence?
• Using Redis as a cache
• Optimizing queries using Redis materialized views
• Synchronizing MySQL and Redis
• Tracking changes to entities
• Using a modular asynchronous architecture
75. Agenda
• Why polyglot persistence?
• Using Redis as a cache
• Optimizing queries using Redis materialized views
• Synchronizing MySQL and Redis
• Tracking changes to entities
• Using a modular asynchronous architecture
77. Drawbacks of this monolithic architecture
WAR
Restaurant • Obstacle to frequent deployments
Management
• Overloads IDE and web container
... • Obstacle to scaling development
• Technology lock-in
79. Using a message broker
Asynchronous is preferred
JSON is fashionable but binary format is
more efficient
80. Modular architecture
CONSUMER Timer RESTAURANT
OWNER
Order taking Event Publisher Restaurant
Management
MySQL Redis
Redis RabbitMQ
Database Cache
81. Benefits of a modular asynchronous
architecture
• Scales development: develop, deploy and scale each service independently
• Redeploy UI frequently/independently
• Improves fault isolation
• Eliminates long-term commitment to a single technology stack
• Message broker decouples producers and consumers
82. Step 2 of 2
for each CRUD event in MySQL queue
get next CRUD event from MySQL queue
Publish persistent message to RabbitMQ
begin MySQL transaction
mark CRUD event as processed
commit transaction
83. Message flow
EntityCrudEvent
Processor
AvailableRestaurantManagement
Service
Redis
Updater
Spring Integration glue code
RABBITMQ REDIS
86. Summary
• Each SQL/NoSQL database = set of tradeoffs
• Polyglot persistence: leverage the strengths of SQL and NoSQL databases
• Use Redis as a distributed cache
• Store denormalized data in Redis for fast querying
• Reliable database synchronization required