Monday, October 21, 2013

MEMCACHE : FEW THINGS UNKNOWN

MEMCACHE ARCHITECTURE ISSUES


If you start memcache by default it will take some memory and will grow up to the set limit. Lets say you set memory limit 200MB. Memcache will be allocating new memory blocks (slabs) for new items but once it reaches 200Mb it wont be able to allocate any more. Then every new add or set command will evict some older entry. Its all fine as long as your memory limit is high and you are storing relatively small values. If you store values of 100KB
you will run out of space soon and therefore life time of your elements will be shorter.
Memcache never releases memory. So once allocated will remain as memcached property.
Memcache allocates memory in 1MB slabs. Then divides slab in 'chunks' and depending on this division 1MB can be storing items of particular size. For example:
● you store value of 700KB it will create 1MB slab and mark it as slab storing values of size 0.51MB.
● Then if you add value of size 5KB it will not fill out the empty 300KB you have left in previous slab.

The new
item will have to be stored in correct slab. So if there are no free chunks it will allocate 1MB and mark it as
a slab storing items of size 4KB8KB
(just an example)
● you want to set another item 6KB, memcache takes one of the empty chunks in previously allocated slab
(48KB)
Problem is that if you run out of memory now and only have 3 slabs storing items 0.51MB
it meas you can only store 3 such items at once. Obviously if your application needs to store 6 they will be evicting each other and your cache will become very inefficient.



REMOVING OLD ITEMS
Memcached does not have garbage collection so it can not be guaranteed that memcache evicts expired
items. It also does not free memory so you can easily have slabs allocated to some big values that expired days
ago. If there is no need for big items anymore they will sit there forever as memcache expires ite only when you ask
for it.
When you request item with key XYZ it will find it and check its timestamp. If too old, item will be discarded.


DEBUGGING
If you are using memcahced for caching it will be is sometimes necessary to check the
state of the cache. There is no way to dump all keys stored in a memcached server but
using cache dump we can retrieve about a megabyte of data which is often sufficient for
debugging.
Use the stats command to get stats about the different slabs of keys in your server. The
number after "items:" is a slab id and memecached will store your stats in several slabs.
stats items

STAT items:1:number 1
STAT items:1:age 3430476
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 113
STAT items:2:number 4
STAT items:2:age 555952
STAT items:2:evicted 0
STAT items:2:evicted_nonzero 0
STAT items:2:evicted_time 0
STAT items:2:outofmemory 0
STAT items:2:tailrepairs 0
STAT items:2:reclaimed 12
STAT items:3:number 4
STAT items:3:age 2894457
STAT items:3:evicted 0
STAT items:3:evicted_nonzero 0
STAT items:3:evicted_time 0
STAT items:3:outofmemory 0
STAT items:3:tailrepairs 0
STAT items:3:reclaimed 4
STAT items:4:number 2
STAT items:4:age 3411747
STAT items:4:evicted 0
STAT items:4:evicted_nonzero 0
STAT items:4:evicted_time 0
STAT items:4:outofmemory 0
STAT items:4:tailrepairs 0
STAT items:4:reclaimed 9
STAT items:8:number 18
STAT items:8:age 1330321
STAT items:8:evicted 0
STAT items:8:evicted_nonzero 0
STAT items:8:evicted_time 0
STAT items:8:outofmemory 0
STAT items:8:tailrepairs 0
STAT items:8:reclaimed 1
STAT items:10:number 11
STAT items:10:age 3238392
STAT items:10:evicted 0
STAT items:10:evicted_nonzero 0
STAT items:10:evicted_time 0
STAT items:10:outofmemory 0
STAT items:10:tailrepairs 0
STAT items:10:reclaimed 0
END


To get the keys stored in each slab use the cachedump command. In the command
shown below we are retrieving a maximum of hundred keys from the 4th slab.

stats cachedump 4 100


Wednesday, April 17, 2013

Python - impressed me alot

Recently i tried out python.
i found is extremely easy and beautiful.
I now strongly feel python should be the first language everybody should learn.
Python may not be as fast,  small as c,  but its fun to use.
It helps to concentrate on algorithms rather than syntax and data types.
if people start with python,  more of them will love programming.

Its super crisp.

Hello world program is just

print('hello world ')

Cat command would be

Fin=open('file.txt')

for line in Fin:
    print(line)

Dats it.
Its so easy.

Also the gui programming in python is fast and simple.
I prefer pyqt,  but pygtk,  wxpython would also be useful.

So if u wanna teach programming to kids make sure u start with python.