suggestions to batch this call, pipepline, lua or?

52 views
Skip to first unread message

S Ahmed

unread,
Nov 10, 2012, 11:45:44 AM11/10/12
to redi...@googlegroups.com
I have a web service endpoint that I am looking at, and each requests currently makes a series of redis calls.  Sometimes if a particular result returns null, I don't need to make further calls.  
Anyhow, here is the pseudo code of what my method does now, hoping someone can help me refactor this as it is currently making to many requests and I know there is a better way!

Integer userId = hget("users", userGuid)

if userId > 0 {
  Integer departmentId = hget("departments", userId)

  Integer companyId = hget("companies", userId)

  Integer isUnique = hsetnx("some", "thing", "0")

  if(isUnique == 0) {
    String key1 = hset("asdf" + userId, "123")
  }
  
}

I am open to any version of redis, and I am using the java jedis client.

Dvir Volk

unread,
Nov 10, 2012, 12:50:44 PM11/10/12
to redi...@googlegroups.com
Is the schema set in stone?
the lowest hanging fruits are:
1. use one user id. hash it if you don't want sequential ids to be
visible, but that already saves a lot of complexity and one call.
2. save the departmentId and companyId inside a HASH for the user
itself, not as separate HASH objects. that way you can use HMGET to
get all the user's properties.

apart from the guid->id query at the beginning, and the uniqueness
dependent HSET, the rest can be pipelined. and the whole thing can be
written as one lua function of course.
provided you implement 1 and 2, the code can look like:


pipeline.hmget(userId, "department", "company")
pipeline.hsetnx("some", "thing", "0")

properties, isUnique = pipeline.execute()
> --
> You received this message because you are subscribed to the Google Groups
> "Redis DB" group.
> To post to this group, send email to redi...@googlegroups.com.
> To unsubscribe from this group, send email to
> redis-db+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/redis-db?hl=en.



--
Dvir Volk
Chief Architect, Everything.me
http://everything.me

S Ahmed

unread,
Nov 10, 2012, 4:45:57 PM11/10/12
to redi...@googlegroups.com
No the schema is not set in stone, its my app so I own it :) plus it isn't live yet.

Ok I guess I didn't understand hashes, but you can store multiple values per hash key?

So comparing pipeline to a lua script, is the following correct:

With pipelining, you are saving time it takes to return the responses only.  So when you pipeline 3 calls, there is just the time it takes to send the 3 calls, and wait for the final response (as oppose to the time waiting for all 3 responses to come back).  So pipeline is 3 sends, 1 response.

With lua, it is a single send and response.

pipeline.hmget(userId, "department", "company")
pipeline.hsetnx("some", "thing", "0")

properties, isUnique = pipeline.execute()
if(isUnique == 0) {
    String key1 = hset("asdf" + userId, "123")
 }

So what would that look like in lua? :)

Man I'm glad I asked that question, that's a huge savings thanks! (I love over optimizing hehe...plus it is actually making things clearer and more concise.).
Reply all
Reply to author
Forward
0 new messages