Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of Process in memory database table that supports queries for high speed caching

I have a SQL table that is accessed continually but changes very rarely.

The Table is partitioned by UserID and each user has many records in the table.

I want to save database resources and move this table closer to the application in some kind of memory cache.

In process caching is too memory intensive so it needs to be external to the application.

Key Value stores like Redis are proving inefficient due to the overhead of serializing and deserializing the table to and from Redis.

I am looking for something that can store this table (or partitions of data) in memory, but let me query only the information I need without serializing and deserializing large blocks of data for each read.

Is there anything that would provide Out of Process in memory database table that supports queries for high speed caching?

Searching has shown that Apache Ignite might be a possible option, but I am looking for more informed suggestions.

like image 607
SetiSeeker Avatar asked Sep 01 '17 05:09

SetiSeeker


4 Answers

Since it's out-of-process, it has to do serialization and deserialization. The problem you concern is how to reduce the serialization/deserizliation work. If you use Redis' STRING type, you CANNOT reduce these work.

However, You can use HASH to solve the problem: mapping your SQL table to a HASH.

Suppose you have the following table: person: id(varchar), name(varchar), age(int), you can take person id as key, and take name and age as fields. When you want to search someone's name, you only need to get the name field (HGET person-id name), other fields won't be deserialzed.

like image 200
for_stack Avatar answered Nov 19 '22 11:11

for_stack


Ignite is indeed a possible solution for you since you may optimize serialization/deserialization overhead by using internal binary representation for accessing objects' fields. You may refer to this documentation page for more information: https://apacheignite.readme.io/docs/binary-marshaller

Also access overhead may be optimized by disabling copy-on-read option https://apacheignite.readme.io/docs/performance-tips#section-do-not-copy-value-on-read

Data collocation by user id is also possible with Ignite: https://apacheignite.readme.io/docs/affinity-collocation

like image 34
Denis Avatar answered Nov 19 '22 11:11

Denis


As the @for_stack said, Hash will be very suitable for your case.

you said that Each user has many rows in db indexed by the user_id and tag_id . So It is that (user_id, tag_id) uniquely specify one row. Every row is functional depends on this tuple, you could use the tuple as the HASH KEY.

For example, if you want save the row (user_id, tag_id, username, age) which values are ("123456", "FDSA", "gsz", 20) into redis, You could do this:

HMSET 123456:FDSA username "gsz" age 30

When you want to query the username with the user_id and tag_id, you could do like this:

HGET 123456:FDSA username

So Every Hash Key will be a combination of user_id and tag_id, if you want the key to be more human readable, you could add a prefix string such as "USERINFO". e.g. : USERINFO:123456:FDSA .

BUT If you want to query with only a user_id and get all rows with this user_id, this method above will be not enough.

And you could build the secondary indexes in redis for you HASH.

as the above said, we use the user_id:tag_id as the HASH key. Because it can unique points to one row. If we want to query all the rows about one user_id.

We could use sorted set to build a secondary indexing to index which Hashes store the info about this user_id.

We could add this in SortedSet:

ZADD user_index 0 123456:FDSA

As above, we set the member to the string of HASH key, and set the score to 0. And the rule is that we should set all score in this zset to 0 and then we could use the lexicographical order to do range query. refer zrangebylex.

E.g. We want to get the all rows about user_id 123456,

ZRANGEBYLEX user_index [123456 (123457

It will return all the HASH key whose prefix are 123456, and then we use this string as HASH key and hget or hmget to retrieve infomation what we want.

[ means inclusive, and ( means exclusive. and why we use 123457? it is obvious. So when we want to get all rows with a user_id, we shoud specify the upper bound to make the user_id string's leftmost char's ascii value plus 1.

More about lex index you could refer the article I mentioned above.

like image 2
GuangshengZuo Avatar answered Nov 19 '22 10:11

GuangshengZuo


You can try apache mnemonic started by intel. Link -http://incubator.apache.org/projects/mnemonic.html. It supports serdeless features

like image 1
Srini Sydney Avatar answered Nov 19 '22 12:11

Srini Sydney