Reputation: 597
I have a table in my database which has like 40000 rows.Ids of these rows lies between 1 to 40000. Now i want to fetch data for ids say [1,2,800,5006,12006,25001,35000,35018].
For this scenario i can follow one of these approaches:
1.Store each row through a single memcached key. Like key_1 would store the data of row1 , key_2 would store the data of row2 and so on. So there would be 40000 keys in memcached. For the scenario given above i have to make 8 calls to memcached(one for each id).
2.I can directly query to database using 'in' operator.This would take only one query.
Which approach should i follow? Does flooding keys in memcached impact performance?
Upvotes: 1
Views: 133
Reputation: 73306
You have two points to consider:
With memcached, the cost of a roundtrip is very low, and generally quite deterministic (about half a ms on a correct network). With mysql, the cost of a roundtrip depends on many more factors (like the complexity of the query, the latency of the eventual I/Os, etc ...). You should consider than the cost of a roundtrip to mysql is more expensive than for memcached.
Now what about the number of roundtrips? With mysql, the application will generate only one (provided you fetch all rows in one shot). With memcached, you can also generate a single roundtrip, because:
If you use a distributed deployment of memcached (several memcached instances), then your keys will be distributed on several nodes. Again, a good memcached client will parallelize the roundtrips to the various nodes. The resulting cost will be the one of the slowest roundtrip.
Example of multiple get (from telnet):
set didier1 0 0 1
x
STORED
set didier2 0 0 1
y
STORED
get didier1 didier2
VALUE didier1 0 1
x
VALUE didier2 0 1
y
END
You should not do 8 calls to memcached. You should either do one call to fetch the 8 keys, or send a single batch of 8 commands to fetch the keys.
In your example, I would say a proper implementation with memcached will be slightly more efficient, and a lot more scalable. The less you stress the database, the more scalable your system.
Upvotes: 1